## Creating Animations with Clutter

### Matthew Allum

`<mallum`openedhand.com`>`

### Emmanuele Bassi

`<ebassi`linux.intel.com`>`

With Clutter using hardware accelration for graphics rendering, complex and fast animations are possible. This chapter describes basic techniques and the utilities Clutter provides in aiding animation creation.

## Basic Animations

The most basic way to create animations with Clutter is via the use of `g_timeout_add()`. This enables a callback function to be called at a defined interval. The callback function can then modify actors visual properties as to produce an animation.

Example 18. Simple timeout example

Implement a rotating actor using 360 "frames"

    struct RotationClosure {
      ClutterActor *actor;

      gdouble final_angle;
      gdouble current_angle;
    };

    static gboolean
    rotate_actor (gpointer data)
    {
      struct RotationClosure *clos = data;

      clutter_actor_set_rotation (clos->actor, clos->current_angle, 0, 0, 0);

      /* add one degree */
      clos->current_angle += 1.0

      if (clos->current_angle == clos->final_angle)
        return FALSE;

      return TRUE;
    }

    static void
    rotate_actor_cleanup (gpointer data)
    {
      struct RotationClosure *clos = data;

      g_object_unref (clos->actor);
      g_free (clos);
    }

    ...
      struct RotationClosure *clos = NULL;

      clos = g_new (struct RotationClosure, 1);
      clos->actor = g_object_ref (an_actor);
      clos->final_angle = 360.0;
      clos->current_angle = 0;

      g_timeout_add_full (1000 / 360, /* 360 updates in one second */
                          rotate_actor,
                          clos,
                          rotate_actor_cleanup);

### Priorities

`G_PRIORITY_DEFAULT` should always be used as the timeouts priority (in case of `g_timeout_add_full()`) as not to intefere with Clutter's scheduling of repaints and input event handling.