Debian Codegraph @

tests/interactive/test-cairo-flowers.c

raw
1 /*
2 * Pretty cairo flower hack.
3 */
4 #include <clutter/clutter.h>
5
6 #ifndef _MSC_VER
7 #include <unistd.h> /* for sleep(), used for screenshots */
8 #endif
9 #include <stdlib.h>
10 #ifdef _MSC_VER
11 #define _USE_MATH_DEFINES
12 #endif
13 #include <math.h>
14
15 #define PETAL_MIN 20
16 #define PETAL_VAR 40
17 #define N_FLOWERS 40 /* reduce if you have a small card */
18
19 typedef struct Flower
20 {
21 ClutterActor *ctex;
22 gint x,y,rot,v,rv;
23 }
24 Flower;
25
26 static ClutterActor *stage = NULL;
27
28 static gboolean
29 draw_flower (ClutterCanvas *canvas,
30 cairo_t *cr,
31 gint width,
32 gint height,
33 gpointer user_data)
34 {
35 /* No science here, just a hack from toying */
36 gint i, j;
37
38 double colors[] = {
39 0.71, 0.81, 0.83,
40 1.0, 0.78, 0.57,
41 0.64, 0.30, 0.35,
42 0.73, 0.40, 0.39,
43 0.91, 0.56, 0.64,
44 0.70, 0.47, 0.45,
45 0.92, 0.75, 0.60,
46 0.82, 0.86, 0.85,
47 0.51, 0.56, 0.67,
48 1.0, 0.79, 0.58,
49
50 };
51
52 gint size;
53 gint petal_size;
54 gint n_groups; /* Num groups of petals 1-3 */
55 gint n_petals; /* num of petals 4 - 8 */
56 gint pm1, pm2;
57
58 gint idx, last_idx = -1;
59
60 petal_size = GPOINTER_TO_INT (user_data);
61 size = petal_size * 8;
62
63 n_groups = rand() % 3 + 1;
64
65 cairo_set_tolerance (cr, 0.1);
66
67 /* Clear */
68 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
69 cairo_paint(cr);
70 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
71
72 cairo_translate(cr, size/2, size/2);
73
74 for (i=0; i<n_groups; i++)
75 {
76 n_petals = rand() % 5 + 4;
77 cairo_save (cr);
78
79 cairo_rotate (cr, rand() % 6);
80
81 do {
82 idx = (rand() % (sizeof (colors) / sizeof (double) / 3)) * 3;
83 } while (idx == last_idx);
84
85 cairo_set_source_rgba (cr, colors[idx], colors[idx+1],
86 colors[idx+2], 0.5);
87
88 last_idx = idx;
89
90 /* some bezier randomness */
91 pm1 = rand() % 20;
92 pm2 = rand() % 4;
93
94 for (j=1; j<n_petals+1; j++)
95 {
96 cairo_save (cr);
97 cairo_rotate (cr, ((2*M_PI)/n_petals)*j);
98
99 /* Petals are made up beziers */
100 cairo_new_path (cr);
101 cairo_move_to (cr, 0, 0);
102 cairo_rel_curve_to (cr,
103 petal_size, petal_size,
104 (pm2+2)*petal_size, petal_size,
105 (2*petal_size) + pm1, 0);
106 cairo_rel_curve_to (cr,
107 0 + (pm2*petal_size), -petal_size,
108 -petal_size, -petal_size,
109 -((2*petal_size) + pm1), 0);
110 cairo_close_path (cr);
111 cairo_fill (cr);
112 cairo_restore (cr);
113 }
114
115 petal_size -= rand() % (size/8);
116
117 cairo_restore (cr);
118 }
119
120 /* Finally draw flower center */
121 do {
122 idx = (rand() % (sizeof (colors) / sizeof (double) / 3)) * 3;
123 } while (idx == last_idx);
124
125 if (petal_size < 0)
126 petal_size = rand() % 10;
127
128 cairo_set_source_rgba (cr, colors[idx], colors[idx+1], colors[idx+2], 0.5);
129
130 cairo_arc(cr, 0, 0, petal_size, 0, M_PI * 2);
131 cairo_fill(cr);
132
133 return TRUE;
134 }
135
136 static ClutterActor *
137 make_flower_actor (void)
138 {
139 gint petal_size = PETAL_MIN + rand() % PETAL_VAR;
140 gint size = petal_size * 8;
141 ClutterActor *ctex;
142 ClutterContent *canvas;
143
144 canvas = clutter_canvas_new ();
145 g_signal_connect (canvas, "draw",
146 G_CALLBACK (draw_flower), GINT_TO_POINTER (petal_size));
147
148 clutter_canvas_set_size (CLUTTER_CANVAS (canvas), size, size);
149 ctex = g_object_new (CLUTTER_TYPE_ACTOR,
150 "content", canvas,
151 "width", (gfloat) size,
152 "height", (gfloat) size,
153 NULL);
154
155 g_object_unref (canvas);
156
157 return ctex;
158 }
159
160 static void
161 tick (ClutterTimeline *timeline,
162 gint msecs,
163 gpointer data)
164 {
165 Flower **flowers = data;
166 gint i = 0;
167
168 for (i = 0; i < N_FLOWERS; i++)
169 {
170 flowers[i]->y += flowers[i]->v;
171 flowers[i]->rot += flowers[i]->rv;
172
173 if (flowers[i]->y > (gint) clutter_actor_get_height (stage))
174 flowers[i]->y = -clutter_actor_get_height (flowers[i]->ctex);
175
176 clutter_actor_set_position (flowers[i]->ctex,
177 flowers[i]->x, flowers[i]->y);
178
179 clutter_actor_set_rotation (flowers[i]->ctex,
180 CLUTTER_Z_AXIS,
181 flowers[i]->rot,
182 clutter_actor_get_width (flowers[i]->ctex)/2,
183 clutter_actor_get_height (flowers[i]->ctex)/2,
184 0);
185 }
186 }
187
188 static void
189 stop_and_quit (ClutterActor *actor,
190 ClutterTimeline *timeline)
191 {
192 clutter_timeline_stop (timeline);
193 clutter_main_quit ();
194 }
195
196 G_MODULE_EXPORT int
197 test_cairo_flowers_main (int argc, char **argv)
198 {
199 Flower *flowers[N_FLOWERS];
200 ClutterTimeline *timeline;
201 int i;
202
203 srand (time (NULL));
204
205 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
206 return 1;
207
208 /* Create a timeline to manage animation */
209 timeline = clutter_timeline_new (6000);
210 clutter_timeline_set_repeat_count (timeline, -1);
211
212 stage = clutter_stage_new ();
213 clutter_stage_set_title (CLUTTER_STAGE (stage), "Cairo Flowers");
214 g_signal_connect (stage, "destroy", G_CALLBACK (stop_and_quit), timeline);
215
216 clutter_actor_set_background_color (stage, CLUTTER_COLOR_Black);
217
218 for (i=0; i< N_FLOWERS; i++)
219 {
220 flowers[i] = g_new0(Flower, 1);
221 flowers[i]->ctex = make_flower_actor();
222 flowers[i]->x = rand() % (int) clutter_actor_get_width (stage)
223 - (PETAL_MIN + PETAL_VAR) * 2;
224 flowers[i]->y = rand() % (int) clutter_actor_get_height (stage);
225 flowers[i]->rv = rand() % 5 + 1;
226 flowers[i]->v = rand() % 10 + 2;
227
228 clutter_container_add_actor (CLUTTER_CONTAINER (stage),
229 flowers[i]->ctex);
230 clutter_actor_set_position (flowers[i]->ctex,
231 flowers[i]->x, flowers[i]->y);
232 }
233
234 /* fire a callback for frame change */
235 g_signal_connect (timeline, "new-frame", G_CALLBACK (tick), flowers);
236
237 clutter_actor_show (stage);
238
239 clutter_timeline_start (timeline);
240
241 g_signal_connect (stage, "key-press-event",
242 G_CALLBACK (clutter_main_quit),
243 NULL);
244
245 clutter_main();
246
247 g_object_unref (timeline);
248
249 return EXIT_SUCCESS;
250 }
251
252 G_MODULE_EXPORT const char *
253 test_cairo_flowers_describe (void)
254 {
255 return "Drawing pretty flowers with Cairo";
256 }