mutter/examples/super-oh.c
Emmanuele Bassi 212c4a0ee8 2006-07-06 Emmanuele Bassi <ebassi@openedhand.com>
Big rework of the actor management semantics: now ClutterActor
	objects behave like GtkObjects - that is they have an initial
	"floating" reference that gets "sunk" when they are added to
	a ClutterGroup.  This makes a group responsible of de-allocating
	each actor inside it, so you just have to destroy the group to
	get every child actor destroyed.  Also, now you can do:

	  clutter_group_add (group, clutter_video_texture_new ());
	
	without having to care about reference counting and explicit
	unreffing.

	* clutter/clutter-private.h: Add private flags setter and
	getter macros.

	* clutter/clutter-actor.h:
	* clutter/clutter-actor.c: Clean up; inherit from GInitiallyUnowned;
	add a "visible" property; add the "destroy", "show" and "hide"
	signals to ClutterActorClass.

	(clutter_actor_show), (clutter_actor_hide): Refactor a bit; emit
	the "show" and "hide" signals.

	(clutter_actor_set_property), (clutter_actor_get_property),
	(clutter_actor_class_init): Implement the "visible" property; add
	signals.

	(clutter_actor_finalize): Do not leak the actor's name, if it is
	set.
	
	(clutter_actor_dispose): Emit the "destroy" signal here.

	(clutter_actor_init): Sink the initial floating flag if needed.

	(clutter_actor_destroy): Add a function to explicitely destroy
	a ClutterActor.

	(clutter_actor_set_parent), (clutter_actor_get_parent),
	(clutter_actor_unparent): Make set_parent require a valid parent;
	add unparent; check on get_parent; ref_sink the actor when
	setting its parent and unref it when unsetting it.  Probably we'll
	need a function that does reparenting as unparent+set_parent in
	a single shot.

	* clutter/clutter-group.h:
	* clutter/clutter-group.c (clutter_group_dispose),
	(clutter_group_finalize), (clutter_group_add),
	(clutter_group_remove): Make the group destroy its children when
	disposing it; clean up, and use the newly-available
	clutter_actor_unparent().

	* clutter/clutter-stage.h:
	* clutter/clutter-stage.c (clutter_stage_init): ClutterStage is
	a top-level actor; clean up.

	* clutter/clutter-video-texture.h:
	* clutter/clutter-video-texture.c: Clean up.

	* examples/super-oh.c:
	* examples/test.c:
	* examples/video-player.c:
	* examples/test-text.c:
	* examples/video-cube.c: Remove the g_object_unref() call, as the
	ClutterStage object is destroyed on clutter_main_quit().
2006-07-06 17:52:57 +00:00

217 lines
5.4 KiB
C

#include <clutter/clutter.h>
#include <math.h>
#include <errno.h>
#include <stdlib.h>
#define TRAILS 0
#define NHANDS 6
#define RADIUS ((CLUTTER_STAGE_WIDTH()+CLUTTER_STAGE_HEIGHT())/6)
typedef struct SuperOH
{
ClutterActor *hand[NHANDS], *bgtex;
ClutterActor *group;
GdkPixbuf *bgpixb;
} SuperOH;
void
screensaver_setup (void)
{
Window remote_xwindow;
const char *preview_xid;
gboolean foreign_success = FALSE;
preview_xid = g_getenv ("XSCREENSAVER_WINDOW");
if (preview_xid != NULL)
{
char *end;
remote_xwindow = (Window) strtoul (preview_xid, &end, 0);
if ((remote_xwindow != 0) && (end != NULL) &&
((*end == ' ') || (*end == '\0')) &&
((remote_xwindow < G_MAXULONG) || (errno != ERANGE)))
{
foreign_success = clutter_stage_set_xwindow_foreign
(CLUTTER_STAGE(clutter_stage_get_default()), remote_xwindow);
}
}
if (!foreign_success)
clutter_actor_set_size (clutter_stage_get_default(), 800, 600);
}
/* input handler */
void
input_cb (ClutterStage *stage,
ClutterEvent *event,
gpointer data)
{
SuperOH *oh = (SuperOH *)data;
if (event->type == CLUTTER_BUTTON_PRESS)
{
ClutterButtonEvent *bev = (ClutterButtonEvent *) event;
ClutterActor *e;
g_print ("*** button press event (button:%d) ***\n",
bev->button);
e = clutter_stage_get_actor_at_pos (stage,
clutter_button_event_x (event),
clutter_button_event_y (event));
if (e)
clutter_actor_hide(e);
}
else if (event->type == CLUTTER_KEY_PRESS)
{
ClutterKeyEvent *kev = (ClutterKeyEvent *) event;
g_print ("*** key press event (key:%c) ***\n",
clutter_key_event_symbol (kev));
if (clutter_key_event_symbol (kev) == CLUTTER_q)
clutter_main_quit ();
}
}
/* Timeline handler */
void
frame_cb (ClutterTimeline *timeline,
gint frame_num,
gpointer data)
{
SuperOH *oh = (SuperOH *)data;
ClutterActor *stage = clutter_stage_get_default ();
gint i;
#if TRAILS
oh->bgpixb = clutter_stage_snapshot (CLUTTER_STAGE (stage),
0, 0,
CLUTTER_STAGE_WIDTH(),
CLUTTER_STAGE_HEIGHT());
clutter_texture_set_pixbuf (CLUTTER_TEXTURE (oh->bgtex), oh->bgpixb);
g_object_unref (G_OBJECT (oh->bgpixb));
#endif
/* Rotate everything clockwise about stage center*/
clutter_actor_rotate_z (CLUTTER_ACTOR(oh->group),
frame_num,
CLUTTER_STAGE_WIDTH()/2,
CLUTTER_STAGE_HEIGHT()/2);
for (i = 0; i < NHANDS; i++)
{
/* rotate each hand around there centers */
clutter_actor_rotate_z (oh->hand[i],
- 6.0 * frame_num,
clutter_actor_get_width (oh->hand[i])/2,
clutter_actor_get_height (oh->hand[i])/2);
}
/*
clutter_actor_rotate_x (CLUTTER_ACTOR(oh->group),
75.0,
CLUTTER_STAGE_HEIGHT()/2, 0);
*/
}
int
main (int argc, char *argv[])
{
ClutterTimeline *timeline;
ClutterActor *stage;
ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff };
GdkPixbuf *pixbuf;
SuperOH *oh;
gint i;
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
if (!pixbuf)
g_error("pixbuf load failed");
/* Set our stage (window) size */
// clutter_actor_set_size (stage, WINWIDTH, WINHEIGHT);
/* and its background color */
screensaver_setup ();
clutter_stage_set_color (CLUTTER_STAGE (stage),
&stage_color);
oh = g_new(SuperOH, 1);
#if TRAILS
oh->bgtex = clutter_texture_new();
clutter_actor_set_size (oh->bgtex,
CLUTTER_STAGE_WIDTH(), CLUTTER_STAGE_HEIGHT());
clutter_actor_set_opacity (oh->bgtex, 0x99);
clutter_group_add (CLUTTER_GROUP (stage), oh->bgtex);
#endif
/* create a new group to hold multiple actors in a group */
oh->group = clutter_group_new();
for (i = 0; i < NHANDS; i++)
{
gint x, y, w, h;
/* Create a texture from pixbuf, then clone in to same resources */
if (i == 0)
oh->hand[i] = clutter_texture_new_from_pixbuf (pixbuf);
else
oh->hand[i] = clutter_clone_texture_new (CLUTTER_TEXTURE(oh->hand[0]));
/* Place around a circle */
w = clutter_actor_get_width (oh->hand[0]);
h = clutter_actor_get_height (oh->hand[0]);
x = CLUTTER_STAGE_WIDTH() / 2
+ RADIUS * cos (i * M_PI / (NHANDS/2)) - w/2;
y = CLUTTER_STAGE_HEIGHT() / 2
+ RADIUS * sin (i * M_PI / (NHANDS/2)) - h/2;
clutter_actor_set_position (oh->hand[i], x, y);
/* Add to our group group */
clutter_group_add (CLUTTER_GROUP (oh->group), oh->hand[i]);
}
/* Add the group to the stage */
clutter_group_add (CLUTTER_GROUP (stage), CLUTTER_ACTOR(oh->group));
/* Show everying ( and map window ) */
clutter_group_show_all (CLUTTER_GROUP (oh->group));
clutter_group_show_all (CLUTTER_GROUP (stage));
g_signal_connect (stage, "button-press-event",
G_CALLBACK (input_cb),
oh);
g_signal_connect (stage, "key-release-event",
G_CALLBACK (input_cb),
oh);
/* Create a timeline to manage animation */
timeline = clutter_timeline_new (360, 90); /* num frames, fps */
g_object_set(timeline, "loop", TRUE, 0); /* have it loop */
/* fire a callback for frame change */
g_signal_connect(timeline, "new-frame", G_CALLBACK (frame_cb), oh);
/* and start it */
clutter_timeline_start (timeline);
clutter_main();
return 0;
}