## 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 `clutter_threads_add_timeout()`. 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 we reached the target angle, stop */
      if (clos->current_angle == clos->final_angle)
        return G_SOURCE_REMOVE;

      return G_SOURCE_CONTINUE;
    }

    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;

      clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT,
                                        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 `clutter_threads_add_timeout_full()`) as not to intefere with Clutter's scheduling of repaints and input event handling.