cookbook: Made code examples more consistent
Modified the code example for the Clutter API version of the cross-fade to use the same command line as the COGL version. This also simplifies the explanation in the recipe. Also made the COGL code sample more consistent with the Clutter API code sample.
This commit is contained in:
parent
83a8d0b3bb
commit
c230fd8dfd
@ -109,16 +109,8 @@ main (int argc, char *argv[])
|
||||
CoglHandle texture_1 = load_cogl_texture ("source", source);
|
||||
CoglHandle texture_2 = load_cogl_texture ("target", target);
|
||||
|
||||
/* sizes of textures */
|
||||
gfloat source_width, source_height, target_width, target_height;
|
||||
source_width = cogl_texture_get_width (texture_1);
|
||||
source_height = cogl_texture_get_height (texture_1);
|
||||
target_width = cogl_texture_get_width (texture_2);
|
||||
target_height = cogl_texture_get_height (texture_2);
|
||||
|
||||
/* Create a new Cogl material holding the two textures inside two
|
||||
* separate layers. Layer 0 is the one which will end
|
||||
* up being visible.
|
||||
* separate layers.
|
||||
*/
|
||||
CoglHandle material = cogl_material_new ();
|
||||
cogl_material_set_layer (material, 1, texture_1);
|
||||
@ -145,25 +137,22 @@ main (int argc, char *argv[])
|
||||
* assign the material we created earlier to the Texture for painting
|
||||
* it
|
||||
*/
|
||||
ClutterActor *stage = clutter_stage_new ();
|
||||
ClutterActor *stage = clutter_stage_get_default ();
|
||||
clutter_stage_set_title (CLUTTER_STAGE (stage), "cross-fade");
|
||||
clutter_actor_set_size (stage, 600, 600);
|
||||
clutter_actor_show (stage);
|
||||
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
||||
|
||||
ClutterActor *texture = clutter_texture_new ();
|
||||
clutter_actor_set_size (texture, source_width, source_height);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), texture);
|
||||
clutter_texture_set_cogl_material (CLUTTER_TEXTURE (texture), material);
|
||||
clutter_actor_add_constraint (texture, clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0.5));
|
||||
clutter_actor_add_constraint (texture, clutter_align_constraint_new (stage, CLUTTER_ALIGN_Y_AXIS, 0.5));
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), texture);
|
||||
cogl_handle_unref (material);
|
||||
|
||||
/* The timeline will drive the cross-fading */
|
||||
ClutterTimeline *timeline = clutter_timeline_new (duration);
|
||||
g_signal_connect (timeline, "new-frame", G_CALLBACK (_update_progress_cb), texture);
|
||||
|
||||
/* animate */
|
||||
clutter_timeline_start (timeline);
|
||||
|
||||
clutter_main ();
|
||||
|
@ -1,59 +1,42 @@
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
#include <clutter/clutter.h>
|
||||
|
||||
static guint stage_side = 600;
|
||||
static guint animation_duration_ms = 1500;
|
||||
|
||||
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
|
||||
|
||||
typedef struct {
|
||||
ClutterActor *front;
|
||||
ClutterActor *back;
|
||||
ClutterState *transitions;
|
||||
GSList *image_paths;
|
||||
guint next_image_index;
|
||||
} State;
|
||||
static gchar *source = NULL;
|
||||
static gchar *target = NULL;
|
||||
static guint duration = 1000;
|
||||
|
||||
static GOptionEntry entries[] = {
|
||||
{
|
||||
"source", 's',
|
||||
0,
|
||||
G_OPTION_ARG_FILENAME, &source,
|
||||
"The source image of the cross-fade", "FILE"
|
||||
},
|
||||
{
|
||||
"target", 't',
|
||||
0,
|
||||
G_OPTION_ARG_FILENAME, &target,
|
||||
"The target image of the cross-fade", "FILE"
|
||||
},
|
||||
{
|
||||
"duration", 'd',
|
||||
0,
|
||||
G_OPTION_ARG_INT, &duration,
|
||||
"The duration of the cross-fade, in milliseconds", "MSECS"
|
||||
},
|
||||
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static gboolean
|
||||
load_next_image (State *app)
|
||||
load_image (ClutterTexture *texture,
|
||||
gchar *image_path)
|
||||
{
|
||||
/* don't do anything if already animating */
|
||||
ClutterTimeline *timeline = clutter_state_get_timeline (app->transitions);
|
||||
|
||||
if (clutter_timeline_is_playing (timeline) == 1)
|
||||
{
|
||||
g_debug ("Animation is running already");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!app->next_image_index)
|
||||
app->next_image_index = 0;
|
||||
|
||||
gpointer next = g_slist_nth_data (app->image_paths, app->next_image_index);
|
||||
|
||||
if (next == NULL)
|
||||
return FALSE;
|
||||
|
||||
gchar *image_path = (gchar *)next;
|
||||
|
||||
g_debug ("Loading %s", image_path);
|
||||
|
||||
CoglHandle *cogl_texture;
|
||||
cogl_texture = clutter_texture_get_cogl_texture (CLUTTER_TEXTURE (app->front));
|
||||
|
||||
if (cogl_texture != NULL)
|
||||
{
|
||||
/* copy the current texture into the background */
|
||||
clutter_texture_set_cogl_texture (CLUTTER_TEXTURE (app->back), cogl_texture);
|
||||
|
||||
/* make the back opaque and front transparent */
|
||||
clutter_state_warp_to_state (app->transitions, "show-back");
|
||||
}
|
||||
|
||||
/* load the next image into the front */
|
||||
GError *error = NULL;
|
||||
clutter_texture_set_from_file (CLUTTER_TEXTURE (app->front),
|
||||
|
||||
gboolean success = clutter_texture_set_from_file (CLUTTER_TEXTURE (texture),
|
||||
image_path,
|
||||
&error);
|
||||
|
||||
@ -61,111 +44,88 @@ load_next_image (State *app)
|
||||
{
|
||||
g_warning ("Error loading %s\n%s", image_path, error->message);
|
||||
g_error_free (error);
|
||||
return FALSE;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* fade in the front texture and fade out the back texture */
|
||||
clutter_state_set_state (app->transitions, "show-front");
|
||||
|
||||
app->next_image_index++;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_key_pressed_cb (ClutterActor *actor,
|
||||
ClutterEvent *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
State *app = (State *)user_data;
|
||||
|
||||
load_next_image (app);
|
||||
|
||||
return TRUE;
|
||||
return success;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
clutter_init_with_args (&argc, &argv,
|
||||
" - cross-fade", entries,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
if (source == NULL || target == NULL)
|
||||
{
|
||||
g_print ("Usage: %s <image paths to load>\n", argv[0]);
|
||||
g_print ("Usage: %s -s <source> -t <target> [-d <duration>]\n", argv[0]);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
State *app = g_new0 (State, 1);
|
||||
app->image_paths = NULL;
|
||||
|
||||
/*
|
||||
* NB if your shell globs arguments to this program so argv
|
||||
* includes non-image files, they will fail to load and throw errors
|
||||
*/
|
||||
guint i;
|
||||
for (i = 1; i < argc; i++)
|
||||
app->image_paths = g_slist_append (app->image_paths, argv[i]);
|
||||
|
||||
GError *error = NULL;
|
||||
|
||||
/* UI */
|
||||
ClutterActor *stage;
|
||||
ClutterLayoutManager *layout;
|
||||
ClutterActor *box;
|
||||
ClutterActor *front, *back;
|
||||
ClutterState *transitions;
|
||||
|
||||
clutter_init (&argc, &argv);
|
||||
|
||||
stage = clutter_stage_get_default ();
|
||||
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
||||
clutter_stage_set_title (CLUTTER_STAGE (stage), "cross-fade");
|
||||
clutter_actor_set_size (stage, stage_side, stage_side);
|
||||
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
|
||||
clutter_actor_set_size (stage, 600, 600);
|
||||
clutter_actor_show (stage);
|
||||
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
||||
|
||||
layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
|
||||
CLUTTER_BIN_ALIGNMENT_CENTER);
|
||||
|
||||
box = clutter_box_new (layout);
|
||||
clutter_actor_set_size (box, stage_side, stage_side);
|
||||
clutter_actor_set_size (box, 600, 600);
|
||||
|
||||
app->back = clutter_texture_new ();
|
||||
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (app->back), TRUE);
|
||||
back = clutter_texture_new ();
|
||||
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (back), TRUE);
|
||||
|
||||
app->front = clutter_texture_new ();
|
||||
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (app->front), TRUE);
|
||||
front = clutter_texture_new ();
|
||||
clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (front), TRUE);
|
||||
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (box), app->back);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (box), app->front);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (box), back);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (box), front);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), box);
|
||||
|
||||
/* animations */
|
||||
app->transitions = clutter_state_new ();
|
||||
clutter_state_set (app->transitions, NULL, "show-front",
|
||||
app->front, "opacity", CLUTTER_EASE_IN_CUBIC, 255,
|
||||
app->back, "opacity", CLUTTER_EASE_IN_CUBIC, 0,
|
||||
transitions = clutter_state_new ();
|
||||
clutter_state_set (transitions, NULL, "show-front",
|
||||
front, "opacity", CLUTTER_EASE_IN_CUBIC, 255,
|
||||
back, "opacity", CLUTTER_EASE_IN_CUBIC, 0,
|
||||
NULL);
|
||||
clutter_state_set (app->transitions, NULL, "show-back",
|
||||
app->front, "opacity", CLUTTER_LINEAR, 0,
|
||||
app->back, "opacity", CLUTTER_LINEAR, 255,
|
||||
clutter_state_set (transitions, NULL, "show-back",
|
||||
front, "opacity", CLUTTER_LINEAR, 0,
|
||||
back, "opacity", CLUTTER_LINEAR, 255,
|
||||
NULL);
|
||||
clutter_state_set_duration (app->transitions,
|
||||
NULL,
|
||||
NULL,
|
||||
animation_duration_ms);
|
||||
clutter_state_set_duration (transitions, NULL, NULL, duration);
|
||||
|
||||
/* display the next (first) image */
|
||||
load_next_image (app);
|
||||
/* make the back opaque and front transparent */
|
||||
clutter_state_warp_to_state (transitions, "show-back");
|
||||
|
||||
/* key press displays the next image */
|
||||
g_signal_connect (stage,
|
||||
"key-press-event",
|
||||
G_CALLBACK (_key_pressed_cb),
|
||||
app);
|
||||
/* load the first image into the back */
|
||||
load_image (CLUTTER_TEXTURE (back), source);
|
||||
|
||||
/* load the second image into the front */
|
||||
load_image (CLUTTER_TEXTURE (front), target);
|
||||
|
||||
/* fade in the front texture and fade out the back texture */
|
||||
clutter_state_set_state (transitions, "show-front");
|
||||
|
||||
clutter_actor_show (stage);
|
||||
|
||||
clutter_main ();
|
||||
|
||||
g_slist_free (app->image_paths);
|
||||
g_object_unref (app->transitions);
|
||||
g_free (app);
|
||||
g_object_unref (transitions);
|
||||
|
||||
if (error != NULL)
|
||||
g_error_free (error);
|
||||
|
Loading…
x
Reference in New Issue
Block a user