Appendix A. Implementing Actors

Table of Contents

Implementing Simple Actors

If the standard Clutter actors don't meet all your needs then you may create your own custom actor objects. Implementing a custom actor is much like implementing any new GObject type. You may use the G_DEFINE_TYPE macro to specify that the type is derived from ClutterActor. For instance:

G_DEFINE_TYPE (ClutterTriangle, clutter_triangle, CLUTTER_TYPE_ACTOR);

You should then specify your object's implementation of the ClutterActor::paint() virtual function in your class_init function:

static void
clutter_triangle_class_init (ClutterTriangleClass *klass)
{
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);

  actor_class->paint = clutter_triangle_paint;

  ...
}

Your ClutterActor::paint() implementation should use the OpenGL API to actually paint something. You will probably need some information from your object's generic ClutterActor base class, for instance by calling clutter_actor_get_geometry() and clutter_actor_get_opacity(), and by using your object's specific property values.

To make your code work with both OpenGL ES and regular OpenGL (and maybe even future Clutter backends), you may wish to use Clutter's cogl abstraction API which provides functions such as cogl_rectangle() and cogl_push_matrix(). You can also detect whether the platform has support for either the OpenGL or OpenGL ES API by ifdefing for CLUTTER_COGL_HAS_GL or CLUTTER_COGL_HAS_GLES.

You should also implement the ClutterActor::pick() virtual function, painting a silhouette of your actor in the provided color. Clutter uses this to draw each actor's silhouette offscreen in a unique color, using the color to quickly identify the actor under the cursor. If your actor is simple then you can probably reuse the code from your paint() implementation.

Most of the rest of ClutterActor's virtual functions don't need to be reimplemented, because a suitable default implemention exists in ClutterActor. For instance, show(), show_all(), hide(), hide_all(), request_coord().