2009-01-08 18:54:48 +00:00
|
|
|
/*
|
|
|
|
* Pretty cairo flower hack.
|
|
|
|
*/
|
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
2009-03-31 12:44:46 +00:00
|
|
|
#ifndef _MSC_VER
|
2009-01-08 18:54:48 +00:00
|
|
|
#include <unistd.h> /* for sleep(), used for screenshots */
|
2009-03-31 12:44:46 +00:00
|
|
|
#endif
|
2009-01-08 18:54:48 +00:00
|
|
|
#include <stdlib.h>
|
2009-03-31 12:44:46 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
#include <math.h>
|
2009-01-08 18:54:48 +00:00
|
|
|
|
|
|
|
#define PETAL_MIN 20
|
|
|
|
#define PETAL_VAR 40
|
|
|
|
#define N_FLOWERS 40 /* reduce if you have a small card */
|
|
|
|
|
|
|
|
typedef struct Flower
|
|
|
|
{
|
|
|
|
ClutterActor *ctex;
|
|
|
|
gint x,y,rot,v,rv;
|
|
|
|
}
|
|
|
|
Flower;
|
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
static ClutterActor *stage = NULL;
|
|
|
|
|
2015-12-05 23:15:17 +00:00
|
|
|
static gboolean
|
|
|
|
draw_flower (ClutterCanvas *canvas,
|
|
|
|
cairo_t *cr,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gpointer user_data)
|
2009-01-08 18:54:48 +00:00
|
|
|
{
|
|
|
|
/* No science here, just a hack from toying */
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
double colors[] = {
|
|
|
|
0.71, 0.81, 0.83,
|
|
|
|
1.0, 0.78, 0.57,
|
|
|
|
0.64, 0.30, 0.35,
|
|
|
|
0.73, 0.40, 0.39,
|
|
|
|
0.91, 0.56, 0.64,
|
|
|
|
0.70, 0.47, 0.45,
|
|
|
|
0.92, 0.75, 0.60,
|
|
|
|
0.82, 0.86, 0.85,
|
|
|
|
0.51, 0.56, 0.67,
|
|
|
|
1.0, 0.79, 0.58,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
gint size;
|
|
|
|
gint petal_size;
|
|
|
|
gint n_groups; /* Num groups of petals 1-3 */
|
|
|
|
gint n_petals; /* num of petals 4 - 8 */
|
|
|
|
gint pm1, pm2;
|
|
|
|
|
|
|
|
gint idx, last_idx = -1;
|
|
|
|
|
2015-12-05 23:15:17 +00:00
|
|
|
petal_size = GPOINTER_TO_INT (user_data);
|
2009-01-08 18:54:48 +00:00
|
|
|
size = petal_size * 8;
|
|
|
|
|
|
|
|
n_groups = rand() % 3 + 1;
|
|
|
|
|
|
|
|
cairo_set_tolerance (cr, 0.1);
|
|
|
|
|
|
|
|
/* Clear */
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
|
|
|
|
|
|
|
|
cairo_translate(cr, size/2, size/2);
|
|
|
|
|
|
|
|
for (i=0; i<n_groups; i++)
|
|
|
|
{
|
|
|
|
n_petals = rand() % 5 + 4;
|
|
|
|
cairo_save (cr);
|
|
|
|
|
|
|
|
cairo_rotate (cr, rand() % 6);
|
|
|
|
|
|
|
|
do {
|
|
|
|
idx = (rand() % (sizeof (colors) / sizeof (double) / 3)) * 3;
|
|
|
|
} while (idx == last_idx);
|
|
|
|
|
|
|
|
cairo_set_source_rgba (cr, colors[idx], colors[idx+1],
|
|
|
|
colors[idx+2], 0.5);
|
|
|
|
|
|
|
|
last_idx = idx;
|
|
|
|
|
|
|
|
/* some bezier randomness */
|
|
|
|
pm1 = rand() % 20;
|
|
|
|
pm2 = rand() % 4;
|
|
|
|
|
|
|
|
for (j=1; j<n_petals+1; j++)
|
|
|
|
{
|
|
|
|
cairo_save (cr);
|
|
|
|
cairo_rotate (cr, ((2*M_PI)/n_petals)*j);
|
|
|
|
|
|
|
|
/* Petals are made up beziers */
|
|
|
|
cairo_new_path (cr);
|
|
|
|
cairo_move_to (cr, 0, 0);
|
|
|
|
cairo_rel_curve_to (cr,
|
|
|
|
petal_size, petal_size,
|
|
|
|
(pm2+2)*petal_size, petal_size,
|
|
|
|
(2*petal_size) + pm1, 0);
|
|
|
|
cairo_rel_curve_to (cr,
|
|
|
|
0 + (pm2*petal_size), -petal_size,
|
|
|
|
-petal_size, -petal_size,
|
|
|
|
-((2*petal_size) + pm1), 0);
|
|
|
|
cairo_close_path (cr);
|
|
|
|
cairo_fill (cr);
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
petal_size -= rand() % (size/8);
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally draw flower center */
|
|
|
|
do {
|
|
|
|
idx = (rand() % (sizeof (colors) / sizeof (double) / 3)) * 3;
|
|
|
|
} while (idx == last_idx);
|
|
|
|
|
|
|
|
if (petal_size < 0)
|
|
|
|
petal_size = rand() % 10;
|
|
|
|
|
|
|
|
cairo_set_source_rgba (cr, colors[idx], colors[idx+1], colors[idx+2], 0.5);
|
|
|
|
|
|
|
|
cairo_arc(cr, 0, 0, petal_size, 0, M_PI * 2);
|
|
|
|
cairo_fill(cr);
|
|
|
|
|
2015-12-05 23:15:17 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClutterActor *
|
|
|
|
make_flower_actor (void)
|
|
|
|
{
|
|
|
|
gint petal_size = PETAL_MIN + rand() % PETAL_VAR;
|
|
|
|
gint size = petal_size * 8;
|
|
|
|
ClutterActor *ctex;
|
|
|
|
ClutterContent *canvas;
|
|
|
|
|
|
|
|
canvas = clutter_canvas_new ();
|
|
|
|
g_signal_connect (canvas, "draw",
|
|
|
|
G_CALLBACK (draw_flower), GINT_TO_POINTER (petal_size));
|
|
|
|
|
|
|
|
clutter_canvas_set_size (CLUTTER_CANVAS (canvas), size, size);
|
|
|
|
ctex = g_object_new (CLUTTER_TYPE_ACTOR,
|
|
|
|
"content", canvas,
|
|
|
|
"width", (gfloat) size,
|
|
|
|
"height", (gfloat) size,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_unref (canvas);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
|
|
|
return ctex;
|
|
|
|
}
|
|
|
|
|
2010-01-27 17:55:18 +00:00
|
|
|
static void
|
|
|
|
tick (ClutterTimeline *timeline,
|
|
|
|
gint msecs,
|
|
|
|
gpointer data)
|
2009-01-08 18:54:48 +00:00
|
|
|
{
|
2011-11-09 13:41:15 +00:00
|
|
|
Flower **flowers = data;
|
2009-01-08 18:54:48 +00:00
|
|
|
gint i = 0;
|
|
|
|
|
2010-02-03 15:05:37 +00:00
|
|
|
for (i = 0; i < N_FLOWERS; i++)
|
2009-01-08 18:54:48 +00:00
|
|
|
{
|
|
|
|
flowers[i]->y += flowers[i]->v;
|
|
|
|
flowers[i]->rot += flowers[i]->rv;
|
|
|
|
|
2010-02-03 15:05:37 +00:00
|
|
|
if (flowers[i]->y > (gint) clutter_actor_get_height (stage))
|
|
|
|
flowers[i]->y = -clutter_actor_get_height (flowers[i]->ctex);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
|
|
|
clutter_actor_set_position (flowers[i]->ctex,
|
|
|
|
flowers[i]->x, flowers[i]->y);
|
|
|
|
|
|
|
|
clutter_actor_set_rotation (flowers[i]->ctex,
|
|
|
|
CLUTTER_Z_AXIS,
|
|
|
|
flowers[i]->rot,
|
|
|
|
clutter_actor_get_width (flowers[i]->ctex)/2,
|
|
|
|
clutter_actor_get_height (flowers[i]->ctex)/2,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
static void
|
|
|
|
stop_and_quit (ClutterActor *actor,
|
|
|
|
ClutterTimeline *timeline)
|
|
|
|
{
|
|
|
|
clutter_timeline_stop (timeline);
|
|
|
|
clutter_main_quit ();
|
|
|
|
}
|
|
|
|
|
|
|
|
G_MODULE_EXPORT int
|
2010-10-15 16:35:41 +00:00
|
|
|
test_cairo_flowers_main (int argc, char **argv)
|
2009-01-08 18:54:48 +00:00
|
|
|
{
|
2011-11-09 13:41:15 +00:00
|
|
|
Flower *flowers[N_FLOWERS];
|
2010-01-27 17:55:18 +00:00
|
|
|
ClutterTimeline *timeline;
|
2011-11-09 13:41:15 +00:00
|
|
|
int i;
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2009-05-31 14:15:46 +00:00
|
|
|
srand (time (NULL));
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2011-02-22 00:19:35 +00:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
|
|
return 1;
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
/* Create a timeline to manage animation */
|
|
|
|
timeline = clutter_timeline_new (6000);
|
2012-02-13 16:47:17 +00:00
|
|
|
clutter_timeline_set_repeat_count (timeline, -1);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
stage = clutter_stage_new ();
|
|
|
|
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cairo Flowers");
|
|
|
|
g_signal_connect (stage, "destroy", G_CALLBACK (stop_and_quit), timeline);
|
|
|
|
|
2012-02-13 16:47:17 +00:00
|
|
|
clutter_actor_set_background_color (stage, CLUTTER_COLOR_Black);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
|
|
|
for (i=0; i< N_FLOWERS; i++)
|
|
|
|
{
|
|
|
|
flowers[i] = g_new0(Flower, 1);
|
|
|
|
flowers[i]->ctex = make_flower_actor();
|
Remove Units from the public API
With the recent change to internal floating point values, ClutterUnit
has become a redundant type, defined to be a float. All integer entry
points are being internally converted to floating point values to be
passed to the GL pipeline with the least amount of conversion.
ClutterUnit is thus exposed as just a "pixel with fractionary bits",
and not -- as users might think -- as generic, resolution and device
independent units. not that it was the case, but a definitive amount
of people was convinced it did provide this "feature", and was flummoxed
about the mere existence of this type.
So, having ClutterUnit exposed in the public API doubles the entry
points and has the following disadvantages:
- we have to maintain twice the amount of entry points in ClutterActor
- we still do an integer-to-float implicit conversion
- we introduce a weird impedance between pixels and "pixels with
fractionary bits"
- language bindings will have to choose what to bind, and resort
to manually overriding the API
+ *except* for language bindings based on GObject-Introspection, as
they cannot do manual overrides, thus will replicate the entire
set of entry points
For these reason, we should coalesces every Actor entry point for
pixels and for ClutterUnit into a single entry point taking a float,
like:
void clutter_actor_set_x (ClutterActor *self,
gfloat x);
void clutter_actor_get_size (ClutterActor *self,
gfloat *width,
gfloat *height);
gfloat clutter_actor_get_height (ClutterActor *self);
etc.
The issues I have identified are:
- we'll have a two cases of compiler warnings:
- printf() format of the return values from %d to %f
- clutter_actor_get_size() taking floats instead of unsigned ints
- we'll have a problem with varargs when passing an integer instead
of a floating point value, except on 64bit platforms where the
size of a float is the same as the size of an int
To be clear: the *intent* of the API should not change -- we still use
pixels everywhere -- but:
- we remove ambiguity in the API with regard to pixels and units
- we remove entry points we get to maintain for the whole 1.0
version of the API
- we make things simpler to bind for both manual language bindings
and automatic (gobject-introspection based) ones
- we have the simplest API possible while still exposing the
capabilities of the underlying GL implementation
2009-05-06 15:44:47 +00:00
|
|
|
flowers[i]->x = rand() % (int) clutter_actor_get_width (stage)
|
|
|
|
- (PETAL_MIN + PETAL_VAR) * 2;
|
|
|
|
flowers[i]->y = rand() % (int) clutter_actor_get_height (stage);
|
|
|
|
flowers[i]->rv = rand() % 5 + 1;
|
|
|
|
flowers[i]->v = rand() % 10 + 2;
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2009-05-31 14:15:46 +00:00
|
|
|
clutter_container_add_actor (CLUTTER_CONTAINER (stage),
|
|
|
|
flowers[i]->ctex);
|
2009-01-08 18:54:48 +00:00
|
|
|
clutter_actor_set_position (flowers[i]->ctex,
|
|
|
|
flowers[i]->x, flowers[i]->y);
|
|
|
|
}
|
|
|
|
|
2010-01-27 17:55:18 +00:00
|
|
|
/* fire a callback for frame change */
|
|
|
|
g_signal_connect (timeline, "new-frame", G_CALLBACK (tick), flowers);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2009-05-31 14:15:46 +00:00
|
|
|
clutter_actor_show (stage);
|
2009-01-08 18:54:48 +00:00
|
|
|
|
2010-01-27 17:55:18 +00:00
|
|
|
clutter_timeline_start (timeline);
|
|
|
|
|
2009-01-08 18:54:48 +00:00
|
|
|
g_signal_connect (stage, "key-press-event",
|
|
|
|
G_CALLBACK (clutter_main_quit),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
clutter_main();
|
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
g_object_unref (timeline);
|
|
|
|
|
2009-05-31 14:15:46 +00:00
|
|
|
return EXIT_SUCCESS;
|
2009-01-08 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 13:41:15 +00:00
|
|
|
G_MODULE_EXPORT const char *
|
|
|
|
test_cairo_flowers_describe (void)
|
|
|
|
{
|
|
|
|
return "Drawing pretty flowers with Cairo";
|
|
|
|
}
|