Hello knowledgeable clutter people,

I created an actor to handle drawing sprite based animations, and I am
wondering if it is the correct way to do it.

The code is written in Vala, but it won't compile with the stock clutter
bindings since I am using cogl_texture_new_from_sub_texture  which isn't
bound.  There's a patch attached to fix it.

Here's my actor:

using Cogl;

public class SpriteActor : Clutter.Actor
{
  private Texture _tex;
  private Texture[] _frames;
  
  public int frame_width { get; construct set; }
  public int frame_height { get; construct set; }
  public int n_frames { get; construct set; }
  
  public int frame { get; set; }
  
  private SpriteActor()
  {
    Object();
  }
  
  public SpriteActor.from_file(int width, int height,
                               int frames, string path)
                                throws GLib.Error
  {
    Object(frame_width: width, frame_height: height, n_frames: frames);
    
    _tex = new Texture.from_file(path,
                                 TextureFlags.NO_SLICING,
                                 PixelFormat.ANY);
    
    init();
  }
  
  public SpriteActor.from_bitmap(int width, int height,
                                 int frames, Bitmap bit)
  {
    Object(frame_width: width, frame_height: height, n_frames: frames);
    
    _tex = new Texture.from_bitmap(bit,
                                   TextureFlags.NO_SLICING,
                                   PixelFormat.ANY);
    
    init();
  }
  
  private void init()
  {
    //this.notify["frame"].connect(() => {queue_redraw();});
  
    _frames = new Texture[n_frames];
    
    for (int i = 0; i < n_frames; i++)
    {
      _frames[i] = new Texture.from_sub_texture(_tex, i * frame_width,
                                                0, frame_width,
                                                frame_height);
    }
  }
  
  protected override void paint()
  {  
    Cogl.set_source_texture(_frames[frame]);
    Cogl.path_rectangle(0, 0, width, height);
    Cogl.path_fill();
  }
  
  protected override void pick(Clutter.Color c)
  {
    Cogl.set_source_color4ub(c.red, c.green, c.blue, c.alpha);
    Cogl.path_rectangle(0, 0, width, height);
    Cogl.path_fill();
  }
}


diff --git a/vapi/cogl-1.0.vapi b/vapi/cogl-1.0.vapi
index 9f356d3..9c2c807 100644
--- a/vapi/cogl-1.0.vapi
+++ b/vapi/cogl-1.0.vapi
@@ -152,6 +152,7 @@ namespace Cogl {
 		public Texture.from_bitmap (Cogl.Bitmap bmp_handle, Cogl.TextureFlags flags, Cogl.PixelFormat internal_format);
 		public Texture.from_data (uint width, uint height, Cogl.TextureFlags flags, Cogl.PixelFormat format, Cogl.PixelFormat internal_format, uint rowstride, [CCode (array_length = false)] uchar[] data);
 		public Texture.from_file (string filename, Cogl.TextureFlags flags, Cogl.PixelFormat internal_format) throws GLib.Error;
+		public Texture.from_sub_texture(Cogl.Texture full_texture, int sub_x, int sub_y, int sub_width, int sub_height);
 		public int get_data (Cogl.PixelFormat format, uint rowstride, uchar[] data);
 		public Cogl.PixelFormat get_format ();
 		public uint get_height ();

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to