Debian Codegraph @

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

raw
1 #include <glib.h>
2 #include <gmodule.h>
3 #include <stdlib.h>
4 #include <clutter/clutter.h>
5 #include <cogl/cogl.h>
6
7 /* Coglbox declaration
8 *--------------------------------------------------*/
9
10 G_BEGIN_DECLS
11
12 #define TEST_TYPE_COGLBOX test_coglbox_get_type()
13
14 #define TEST_COGLBOX(obj) \
15 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
16 TEST_TYPE_COGLBOX, TestCoglboxClass))
17
18 #define TEST_COGLBOX_CLASS(klass) \
19 (G_TYPE_CHECK_CLASS_CAST ((klass), \
20 TEST_TYPE_COGLBOX, TestCoglboxClass))
21
22 #define TEST_IS_COGLBOX(obj) \
23 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
24 TEST_TYPE_COGLBOX))
25
26 #define TEST_IS_COGLBOX_CLASS(klass) \
27 (G_TYPE_CHECK_CLASS_TYPE ((klass), \
28 TEST_TYPE_COGLBOX))
29
30 #define TEST_COGLBOX_GET_CLASS(obj) \
31 (G_TYPE_INSTANCE_GET_CLASS ((obj), \
32 TEST_TYPE_COGLBOX, TestCoglboxClass))
33
34 typedef struct _TestCoglbox TestCoglbox;
35 typedef struct _TestCoglboxClass TestCoglboxClass;
36 typedef struct _TestCoglboxPrivate TestCoglboxPrivate;
37
38 struct _TestCoglbox
39 {
40 ClutterActor parent;
41
42 /*< private >*/
43 TestCoglboxPrivate *priv;
44 };
45
46 struct _TestCoglboxClass
47 {
48 ClutterActorClass parent_class;
49
50 /* padding for future expansion */
51 void (*_test_coglbox1) (void);
52 void (*_test_coglbox2) (void);
53 void (*_test_coglbox3) (void);
54 void (*_test_coglbox4) (void);
55 };
56
57 static GType test_coglbox_get_type (void) G_GNUC_CONST;
58
59 G_END_DECLS
60
61 /* Coglbox private declaration
62 *--------------------------------------------------*/
63
64 G_DEFINE_TYPE (TestCoglbox, test_coglbox, CLUTTER_TYPE_ACTOR);
65
66 #define TEST_COGLBOX_GET_PRIVATE(obj) \
67 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TEST_TYPE_COGLBOX, TestCoglboxPrivate))
68
69 struct _TestCoglboxPrivate
70 {
71 CoglHandle sliced_tex, not_sliced_tex;
72 gint frame;
73 gboolean use_sliced;
74 gboolean use_linear_filtering;
75 };
76
77 /* Coglbox implementation
78 *--------------------------------------------------*/
79
80 static void
81 test_coglbox_fade_texture (gfloat x1,
82 gfloat y1,
83 gfloat x2,
84 gfloat y2,
85 gfloat tx1,
86 gfloat ty1,
87 gfloat tx2,
88 gfloat ty2)
89 {
90 CoglTextureVertex vertices[4];
91 int i;
92
93 vertices[0].x = x1;
94 vertices[0].y = y1;
95 vertices[0].z = 0;
96 vertices[0].tx = tx1;
97 vertices[0].ty = ty1;
98 vertices[1].x = x1;
99 vertices[1].y = y2;
100 vertices[1].z = 0;
101 vertices[1].tx = tx1;
102 vertices[1].ty = ty2;
103 vertices[2].x = x2;
104 vertices[2].y = y2;
105 vertices[2].z = 0;
106 vertices[2].tx = tx2;
107 vertices[2].ty = ty2;
108 vertices[3].x = x2;
109 vertices[3].y = y1;
110 vertices[3].z = 0;
111 vertices[3].tx = tx2;
112 vertices[3].ty = ty1;
113
114 for (i = 0; i < 4; i++)
115 {
116 cogl_color_init_from_4ub (&(vertices[i].color),
117 255,
118 255,
119 255,
120 ((i ^ (i >> 1)) & 1) ? 0 : 128);
121 cogl_color_premultiply (&(vertices[i].color));
122 }
123
124 cogl_polygon (vertices, 4, TRUE);
125 }
126
127 static void
128 test_coglbox_triangle_texture (int tex_width,
129 int tex_height,
130 gfloat x,
131 gfloat y,
132 gfloat tx1,
133 gfloat ty1,
134 gfloat tx2,
135 gfloat ty2,
136 gfloat tx3,
137 gfloat ty3)
138 {
139 CoglTextureVertex vertices[3];
140
141 vertices[0].x = x + tx1 * tex_width;
142 vertices[0].y = y + ty1 * tex_height;
143 vertices[0].z = 0;
144 vertices[0].tx = tx1;
145 vertices[0].ty = ty1;
146
147 vertices[1].x = x + tx2 * tex_width;
148 vertices[1].y = y + ty2 * tex_height;
149 vertices[1].z = 0;
150 vertices[1].tx = tx2;
151 vertices[1].ty = ty2;
152
153 vertices[2].x = x + tx3 * tex_width;
154 vertices[2].y = y + ty3 * tex_height;
155 vertices[2].z = 0;
156 vertices[2].tx = tx3;
157 vertices[2].ty = ty3;
158
159 cogl_polygon (vertices, 3, FALSE);
160 }
161
162 static void
163 test_coglbox_paint (ClutterActor *self)
164 {
165 TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (self);
166 CoglHandle tex_handle = priv->use_sliced ? priv->sliced_tex
167 : priv->not_sliced_tex;
168 int tex_width = cogl_texture_get_width (tex_handle);
169 int tex_height = cogl_texture_get_height (tex_handle);
170 CoglHandle material = cogl_material_new ();
171
172 cogl_material_set_layer (material, 0, tex_handle);
173
174 cogl_material_set_layer_filters (material, 0,
175 priv->use_linear_filtering
176 ? COGL_MATERIAL_FILTER_LINEAR :
177 COGL_MATERIAL_FILTER_NEAREST,
178 priv->use_linear_filtering
179 ? COGL_MATERIAL_FILTER_LINEAR :
180 COGL_MATERIAL_FILTER_NEAREST);
181
182 cogl_push_matrix ();
183 cogl_translate (tex_width / 2, 0, 0);
184 cogl_rotate (priv->frame, 0, 1, 0);
185 cogl_translate (-tex_width / 2, 0, 0);
186
187 /* Draw a hand and refect it */
188 cogl_set_source (material);
189 cogl_rectangle_with_texture_coords (0, 0, tex_width, tex_height,
190 0, 0, 1, 1);
191 test_coglbox_fade_texture (0, tex_height,
192 tex_width, (tex_height * 3 / 2),
193 0.0, 1.0,
194 1.0, 0.5);
195
196 cogl_pop_matrix ();
197
198 cogl_push_matrix ();
199 cogl_translate (tex_width * 3 / 2 + 60, 0, 0);
200 cogl_rotate (priv->frame, 0, 1, 0);
201 cogl_translate (-tex_width / 2 - 10, 0, 0);
202
203 /* Draw the texture split into two triangles */
204 test_coglbox_triangle_texture (tex_width, tex_height,
205 0, 0,
206 0, 0,
207 0, 1,
208 1, 1);
209 test_coglbox_triangle_texture (tex_width, tex_height,
210 20, 0,
211 0, 0,
212 1, 0,
213 1, 1);
214
215 cogl_pop_matrix ();
216
217 cogl_handle_unref (material);
218 }
219
220 static void
221 test_coglbox_finalize (GObject *object)
222 {
223 G_OBJECT_CLASS (test_coglbox_parent_class)->finalize (object);
224 }
225
226 static void
227 test_coglbox_dispose (GObject *object)
228 {
229 TestCoglboxPrivate *priv;
230
231 priv = TEST_COGLBOX_GET_PRIVATE (object);
232 cogl_handle_unref (priv->not_sliced_tex);
233 cogl_handle_unref (priv->sliced_tex);
234
235 G_OBJECT_CLASS (test_coglbox_parent_class)->dispose (object);
236 }
237
238 static void
239 test_coglbox_init (TestCoglbox *self)
240 {
241 TestCoglboxPrivate *priv;
242 GError *error = NULL;
243 gchar *file;
244
245 self->priv = priv = TEST_COGLBOX_GET_PRIVATE (self);
246
247 priv->use_linear_filtering = FALSE;
248 priv->use_sliced = FALSE;
249
250 file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
251 priv->sliced_tex =
252 cogl_texture_new_from_file (file,
253 COGL_TEXTURE_NONE,
254 COGL_PIXEL_FORMAT_ANY,
255 &error);
256 if (priv->sliced_tex == COGL_INVALID_HANDLE)
257 {
258 if (error)
259 {
260 g_warning ("Texture loading failed: %s", error->message);
261 g_error_free (error);
262 error = NULL;
263 }
264 else
265 g_warning ("Texture loading failed: <unknown>");
266 }
267
268 priv->not_sliced_tex =
269 cogl_texture_new_from_file (file,
270 COGL_TEXTURE_NO_SLICING,
271 COGL_PIXEL_FORMAT_ANY,
272 &error);
273 if (priv->not_sliced_tex == COGL_INVALID_HANDLE)
274 {
275 if (error)
276 {
277 g_warning ("Texture loading failed: %s", error->message);
278 g_error_free (error);
279 }
280 else
281 g_warning ("Texture loading failed: <unknown>");
282 }
283
284 g_free (file);
285 }
286
287 static void
288 test_coglbox_class_init (TestCoglboxClass *klass)
289 {
290 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
291 ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
292
293 gobject_class->finalize = test_coglbox_finalize;
294 gobject_class->dispose = test_coglbox_dispose;
295 actor_class->paint = test_coglbox_paint;
296
297 g_type_class_add_private (gobject_class, sizeof (TestCoglboxPrivate));
298 }
299
300 static ClutterActor*
301 test_coglbox_new (void)
302 {
303 return g_object_new (TEST_TYPE_COGLBOX, NULL);
304 }
305
306 static void
307 frame_cb (ClutterTimeline *timeline,
308 gint elapsed_msecs,
309 gpointer data)
310 {
311 TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (data);
312 gdouble progress = clutter_timeline_get_progress (timeline);
313
314 priv->frame = 360.0 * progress;
315 clutter_actor_queue_redraw (CLUTTER_ACTOR (data));
316 }
317
318 static void
319 update_toggle_text (ClutterText *button, gboolean val)
320 {
321 clutter_text_set_text (button, val ? "Enabled" : "Disabled");
322 }
323
324 static gboolean
325 on_toggle_click (ClutterActor *button, ClutterEvent *event,
326 gboolean *toggle_val)
327 {
328 update_toggle_text (CLUTTER_TEXT (button), *toggle_val = !*toggle_val);
329
330 return TRUE;
331 }
332
333 static ClutterActor *
334 make_toggle (const char *label_text, gboolean *toggle_val)
335 {
336 ClutterActor *group = clutter_group_new ();
337 ClutterActor *label = clutter_text_new_with_text ("Sans 14", label_text);
338 ClutterActor *button = clutter_text_new_with_text ("Sans 14", "");
339
340 clutter_actor_set_reactive (button, TRUE);
341
342 update_toggle_text (CLUTTER_TEXT (button), *toggle_val);
343
344 clutter_actor_set_position (button, clutter_actor_get_width (label) + 10, 0);
345 clutter_container_add (CLUTTER_CONTAINER (group), label, button, NULL);
346
347 g_signal_connect (button, "button-press-event", G_CALLBACK (on_toggle_click),
348 toggle_val);
349
350 return group;
351 }
352
353 G_MODULE_EXPORT int
354 test_cogl_tex_polygon_main (int argc, char *argv[])
355 {
356 ClutterActor *stage;
357 ClutterActor *coglbox;
358 ClutterActor *filtering_toggle;
359 ClutterActor *slicing_toggle;
360 ClutterActor *note;
361 ClutterTimeline *timeline;
362 ClutterColor blue = { 0x30, 0x30, 0xff, 0xff };
363
364 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
365 return 1;
366
367 /* Stage */
368 stage = clutter_stage_new ();
369 clutter_stage_set_color (CLUTTER_STAGE (stage), &blue);
370 clutter_actor_set_size (stage, 640, 480);
371 clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Texture Polygon");
372 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
373
374 /* Cogl Box */
375 coglbox = test_coglbox_new ();
376 clutter_container_add_actor (CLUTTER_CONTAINER (stage), coglbox);
377
378 /* Timeline for animation */
379 timeline = clutter_timeline_new (6000);
380 clutter_timeline_set_loop (timeline, TRUE);
381 g_signal_connect (timeline, "new-frame", G_CALLBACK (frame_cb), coglbox);
382 clutter_timeline_start (timeline);
383
384 /* Labels for toggling settings */
385 slicing_toggle = make_toggle ("Texture slicing: ",
386 &(TEST_COGLBOX_GET_PRIVATE (coglbox)
387 ->use_sliced));
388 clutter_actor_set_position (slicing_toggle, 0,
389 clutter_actor_get_height (stage)
390 - clutter_actor_get_height (slicing_toggle));
391 filtering_toggle = make_toggle ("Linear filtering: ",
392 &(TEST_COGLBOX_GET_PRIVATE (coglbox)
393 ->use_linear_filtering));
394 clutter_actor_set_position (filtering_toggle, 0,
395 clutter_actor_get_y (slicing_toggle)
396 - clutter_actor_get_height (filtering_toggle));
397 note = clutter_text_new_with_text ("Sans 10", "<- Click to change");
398 clutter_actor_set_position (note,
399 clutter_actor_get_width (filtering_toggle) + 10,
400 (clutter_actor_get_height (stage)
401 + clutter_actor_get_y (filtering_toggle)) / 2
402 - clutter_actor_get_height (note) / 2);
403
404 clutter_container_add (CLUTTER_CONTAINER (stage),
405 slicing_toggle,
406 filtering_toggle,
407 note,
408 NULL);
409
410 clutter_actor_show (stage);
411
412 clutter_main ();
413
414 return 0;
415 }
416
417 G_MODULE_EXPORT const char *
418 test_cogl_tex_polygon_describe (void)
419 {
420 return "Texture polygon primitive.";
421 }