Use PNG images in cairo GJS

I am writing the game with GJS, and I am using sprites. Common way to use images is from resource but I need to load images to cairo with Cairo.ImageSurface.createFromPNG. I achieved this by something like this:

// Upload sprites from resources to the temporary folder and prepare for drawing
pixbuf_image  = GdkPixbuf.Pixbuf.new_from_resource("/org/gnome/gitlab/bazylevnik0/Convolution/sprites/image.png");
pixbuf_image.savev("/tmp/image.png","png",[],[]); // save in temporary folder
surface_image = Cairo.ImageSurface.createFromPNG("/tmp/image.png"); // surface ready for drawing

Maybe exist easiest ways to achieve it? Because Cairo.ImageSurface.createFromPNG needs an absolute path.

*also i found Gdk.cairo_set_source_pixbuf in DevDocs

it seems no, gjs doesn’t have the necessary bindings.
have you tried using the snapshot api instead? saving each file to /tmp could be very slow if /tmp is on an hdd. DevDocs
something like this:

const texture = Gdk.Texture.new_for_pixbuf(pixbuf_image);
const GameView = GObject.registerClass({
  GTypeName: 'GameView'
}, class GameView : Gtk.Widget {
  vfunc_snapshot() {
    let rect = new Graphene.Rect();
    rect.init(x, y, texture.width, texture.height);
    snapshot.append_texture(texture, rect);
  }
});

if you have things other than sprites drawn with cairo, you can also use snapshot.append_cairo

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.