Example

The following example demonstrates the use of a score containing two timelines, with one starting when the other ends, and the whole score running as a loop. The first timeline rotates the rectangle as in the previous example, and the second timeline moves the rectangle horizontally.

Figure 6.2. Score

Score

Source Code

File: main.c

#include <clutter/clutter.h>
#include <stdlib.h>

ClutterActor *rect = NULL;

gint rotation_angle = 0;
gint color_change_count = 0;

/* Rotate the rectangle and alternate its color. */
void
on_timeline_rotation_new_frame (ClutterTimeline *timeline, 
  gint frame_num, gpointer data)
{
  rotation_angle += 1;
  if(rotation_angle >= 360)
    rotation_angle = 0;

  /* Rotate the rectangle clockwise around the z axis, around it's top-left corner: */
  clutter_actor_set_rotation (rect, CLUTTER_X_AXIS, 
    rotation_angle, 0, 0, 0);

  /* Change the color
   * (This is a silly example, making the rectangle flash):
   */
  ++color_change_count;
  if(color_change_count > 100)
    color_change_count = 0;

  if(color_change_count == 0)
  {
    ClutterColor rect_color = { 0xff, 0xff, 0xff, 0x99 };
    clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), &rect_color);
  }
  else if (color_change_count == 50)
  {
    ClutterColor rect_color = { 0x10, 0x40, 0x90, 0xff };
    clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), &rect_color);
  }
}

/* Move the rectangle. */
void
on_timeline_move_new_frame (ClutterTimeline *timeline, 
  gint frame_num, gpointer data)
{
  gint x_position = clutter_actor_get_x (rect);

  x_position += 1;
  if(x_position >= 150)
    x_position = 0;

  clutter_actor_set_x (rect, x_position);
}


int main(int argc, char *argv[])
{
  ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff };
  ClutterColor rect_color = { 0xff, 0xff, 0xff, 0x99 };

  clutter_init (&argc, &argv);

  /* Get the stage and set its size and color: */
  ClutterActor *stage = clutter_stage_get_default ();
  clutter_actor_set_size (stage, 200, 200);
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);

  /* Add a rectangle to the stage: */
  rect = clutter_rectangle_new_with_color (&rect_color);
  clutter_actor_set_size (rect, 70, 70);
  clutter_actor_set_position (rect, 50, 100);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);
  clutter_actor_show (rect);

  /* Show the stage: */
  clutter_actor_show (stage);

  /* Create a score and add two timelines to it,
   * so the second timeline starts when the first one stops: */
  ClutterScore *score = clutter_score_new ();
  clutter_score_set_loop (score, TRUE);

  ClutterTimeline *timeline_rotation = clutter_timeline_new (5000 /* milliseconds */);
  g_signal_connect (timeline_rotation, "new-frame", G_CALLBACK (on_timeline_rotation_new_frame), NULL);
  clutter_score_append (score, NULL, timeline_rotation);

  ClutterTimeline *timeline_move = clutter_timeline_new (5000 /* milliseconds */);
  g_signal_connect (timeline_move, "new-frame", G_CALLBACK (on_timeline_move_new_frame), NULL);
  clutter_score_append (score,  timeline_rotation, timeline_move);

  clutter_score_start (score);

  /* Start the main loop, so we can respond to events: */
  clutter_main ();

  g_object_unref (timeline_rotation);
  g_object_unref (timeline_move);
  g_object_unref (score);

  return EXIT_SUCCESS;

}