Debian Codegraph @

tests/interactive/test-cogl-tex-tile.c

raw
1 #include <glib.h>
2 #include <gmodule.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include <clutter/clutter.h>
6 #include <cogl/cogl.h>
7
8 /* Coglbox declaration
9 *--------------------------------------------------*/
10
11 G_BEGIN_DECLS
12
13 #define TEST_TYPE_COGLBOX test_coglbox_get_type()
14
15 #define TEST_COGLBOX(obj) \
16 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
17 TEST_TYPE_COGLBOX, TestCoglboxClass))
18
19 #define TEST_COGLBOX_CLASS(klass) \
20 (G_TYPE_CHECK_CLASS_CAST ((klass), \
21 TEST_TYPE_COGLBOX, TestCoglboxClass))
22
23 #define TEST_IS_COGLBOX(obj) \
24 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
25 TEST_TYPE_COGLBOX))
26
27 #define TEST_IS_COGLBOX_CLASS(klass) \
28 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
29 TEST_TYPE_COGLBOX))
30
31 #define TEST_COGLBOX_GET_CLASS(obj) \
32 (G_TYPE_INSTANCE_GET_CLASS ((obj), \
33 TEST_TYPE_COGLBOX, TestCoglboxClass))
34
35 typedef struct _TestCoglbox TestCoglbox;
36 typedef struct _TestCoglboxClass TestCoglboxClass;
37 typedef struct _TestCoglboxPrivate TestCoglboxPrivate;
38
39 struct _TestCoglbox
40 {
41 ClutterActor parent;
42
43 /*< private >*/
44 TestCoglboxPrivate *priv;
45 };
46
47 struct _TestCoglboxClass
48 {
49 ClutterActorClass parent_class;
50
51 /* padding for future expansion */
52 void (*_test_coglbox1) (void);
53 void (*_test_coglbox2) (void);
54 void (*_test_coglbox3) (void);
55 void (*_test_coglbox4) (void);
56 };
57
58 static GType test_coglbox_get_type (void) G_GNUC_CONST;
59
60 G_END_DECLS
61
62 /* Coglbox private declaration
63 *--------------------------------------------------*/
64
65 G_DEFINE_TYPE (TestCoglbox, test_coglbox, CLUTTER_TYPE_ACTOR);
66
67 #define TEST_COGLBOX_GET_PRIVATE(obj) \
68 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TEST_TYPE_COGLBOX, TestCoglboxPrivate))
69
70 struct _TestCoglboxPrivate
71 {
72 CoglHandle cogl_tex_id;
73 gdouble animation_progress;
74 };
75
76 /* Coglbox implementation
77 *--------------------------------------------------*/
78
79 static void
80 test_coglbox_paint (ClutterActor *self)
81 {
82 TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (self);
83 gfloat texcoords[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
84 gfloat angle;
85 gfloat frac;
86 gint t;
87
88 angle = priv->animation_progress * 2 * G_PI;
89
90 frac = ((priv->animation_progress <= 0.5f
91 ? priv->animation_progress
92 : 1.0f - priv->animation_progress) + 0.5f) * 2.0f;
93
94 for (t=0; t<4; t+=2)
95 {
96 texcoords[t] += cos (angle);
97 texcoords[t+1] += sin (angle);
98
99 texcoords[t] *= frac;
100 texcoords[t+1] *= frac;
101 }
102
103 priv = TEST_COGLBOX_GET_PRIVATE (self);
104
105 cogl_push_matrix ();
106
107 cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff);
108 cogl_rectangle (0, 0, 400, 400);
109
110 cogl_translate (100, 100, 0);
111 cogl_set_source_texture (priv->cogl_tex_id);
112 cogl_rectangle_with_texture_coords (0, 0, 200, 213,
113 texcoords[0], texcoords[1],
114 texcoords[2], texcoords[3]);
115
116 cogl_pop_matrix();
117 }
118
119 static void
120 test_coglbox_finalize (GObject *object)
121 {
122 G_OBJECT_CLASS (test_coglbox_parent_class)->finalize (object);
123 }
124
125 static void
126 test_coglbox_dispose (GObject *object)
127 {
128 TestCoglboxPrivate *priv;
129
130 priv = TEST_COGLBOX_GET_PRIVATE (object);
131 cogl_handle_unref (priv->cogl_tex_id);
132
133 G_OBJECT_CLASS (test_coglbox_parent_class)->dispose (object);
134 }
135
136 static void
137 test_coglbox_init (TestCoglbox *self)
138 {
139 TestCoglboxPrivate *priv;
140 gchar *file;
141
142 self->priv = priv = TEST_COGLBOX_GET_PRIVATE(self);
143
144 file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
145 priv->cogl_tex_id = cogl_texture_new_from_file (file,
146 COGL_TEXTURE_NONE,
147 COGL_PIXEL_FORMAT_ANY,
148 NULL);
149 g_free (file);
150 }
151
152 static void
153 test_coglbox_class_init (TestCoglboxClass *klass)
154 {
155 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
156 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
157
158 gobject_class->finalize = test_coglbox_finalize;
159 gobject_class->dispose = test_coglbox_dispose;
160 actor_class->paint = test_coglbox_paint;
161
162 g_type_class_add_private (gobject_class, sizeof (TestCoglboxPrivate));
163 }
164
165 static ClutterActor*
166 test_coglbox_new (void)
167 {
168 return g_object_new (TEST_TYPE_COGLBOX, NULL);
169 }
170
171 static void
172 frame_cb (ClutterTimeline *timeline,
173 gint msecs,
174 gpointer data)
175 {
176 TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (data);
177
178 priv->animation_progress = clutter_timeline_get_progress (timeline);
179 clutter_actor_queue_redraw (CLUTTER_ACTOR (data));
180 }
181
182 G_MODULE_EXPORT int
183 test_cogl_tex_tile_main (int argc, char *argv[])
184 {
185 ClutterActor *stage;
186 ClutterActor *coglbox;
187 ClutterTimeline *timeline;
188
189 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
190 return 1;
191
192 /* Stage */
193 stage = clutter_stage_new ();
194 clutter_actor_set_size (stage, 400, 400);
195 clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Texture Tiling");
196 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
197
198 /* Cogl Box */
199 coglbox = test_coglbox_new ();
200 clutter_container_add_actor (CLUTTER_CONTAINER (stage), coglbox);
201
202 /* Timeline for animation */
203 timeline = clutter_timeline_new (6000); /* 6 second duration */
204 clutter_timeline_set_loop (timeline, TRUE);
205 g_signal_connect (timeline, "new-frame", G_CALLBACK (frame_cb), coglbox);
206 clutter_timeline_start (timeline);
207
208 clutter_actor_show_all (stage);
209
210 clutter_main ();
211
212 return 0;
213 }
214
215 G_MODULE_EXPORT const char *
216 test_cogl_tex_tile_describe (void)
217 {
218 return "Texture tiling.";
219 }