Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / creating blank images in gosu
- By awright Date 2009-08-15 08:34
is there a way i can create blank images in gosu? i want to specify an arbitrary height and width
- By jlnr (dev) Date 2009-08-15 09:16
Yes. You can fake being an RMagick image. When you do not pass a String as the filename argument to, Gosu will send #cols and #rows to the object, and then #to_blob to get a String of cols*rows*4 bytes of data. This can actually be used to build all kinds of simple generators.
- By philomory Date 2009-08-16 18:08
You can see exactly this method used in OperationLambda:

module OperationLambda
  module ImageManager
    # EmptyImageStub is based on an idea Julian Raschke suggested in #gosu
    # on IRC. It provides empty RMagic::Image-like objects which, when
    # passed to Gosu::Image's constructor, have their to_blob method called,
    # to provide RGBA data. This allows the easy creation of new Gosu::Image
    # objects without accessing the filesystem, which can then be drawn into
    # with TexPlay.
    class EmptyImageStub
      def initialize(w,h)
        @w, @h = w, h;
      def to_blob
        "\0" * @w * @h * 4
      def rows
      def columns

Usage (from ImageManager class):
def empty(w,h)
  stub =,h)
- By banister Date 2009-08-19 03:45
this is a very cool trick...

im just wondering, are the string wrapped arrays returned by #to_blob in the same format as the arrays returned from glGetTexImage() ?

I am trying to create a new image but with data from another image (a Gosu::Image#dup).

It's not working particularly well at present which is either due to a bug in my code (quite likely) or a discrepancy in the image formats...
- By jlnr (dev) Date 2009-08-19 04:09
Hmm, I think you will have to try around a bit to find out which format this is in, but you should be able to get the right format out of glGetTexImage(). Then someone should test if it also works on a big-endian PowerPC machine :)

What I do is:

                unsigned rgba = *rgbaIter; /* next four bytes */
                result.setPixel(x, y, Gosu::Color(rgba).abgr()); // swap R and B

Not a very accurate comment I fear ;)
- By banister Date 2009-08-25 17:26

I've been using the technique discussed in the awright thread to generate new images, however this method is very slow. Is it possible to add a new method to Gosu to _quickly_ generate new Gosu Images of a given width and height ? or do you know of a faster method using the current setup?
- By jlnr (dev) Date 2009-08-26 06:40
What do you mean by slow? Too slow to do it inbetween frames, or really slow? How are you creating the strings?
- By banister Date 2009-08-26 12:19 Edited 2009-08-26 13:30
im creating the strings using memset so it should be very fast:
    int size = width * height * 4;
    char buf[size];
    memset(buf, 0, size);       
    return rb_str_new(buf, size);

So im assuming the RMagick functions that take the string and convert to an image are what's causing the delay.

I want to be able to make the creation of blank images of arbitrary size and then manipulating them with texplay in _real_time_ a feasible possibilty. I was experimenting with a damage system for a game im writing where on collision chunks of the
image are separated from the main craft and sent flying across the screen. I was doing this by creating a blank texture of a certain size and then splicing pieces from the source image into it. However it was just too slow, and identified the bottleneck was in the creation of the blank Image. :(  Would it be too hard to add a very fast Gosu::Image::create_blank_image(window, width, height) function ? :)) Or do you know another way of doing it?
- By jlnr (dev) Date 2009-08-26 22:00
Hmmm, no idea. Using the C++ API would be faster of course, but I guess you are trying stay on the Ruby side of things (Gosu::Bitmap bmp; bmp.resize(w, h); then create an Image from that). I don't think I'm doing anything completely wasteful in the progress, but updating textures, creating new textures, etc., is all something that can cause weird lags due to particular gfx drivers. (For example, changing a texture on the iPhone is almost a no-go, as far as I can see.) Does the RMagick game work smoothly for you?
- By banister Date 2009-08-27 02:42
yep the RMagick example is fine. If I use Gosu::Image::new() and load just an empty image file (a blank .png) it is very fast, of course, and I may just resort to this (though it seems a bit weird to carry around a blank .png file just for splicing things into, but not really a big deal). I guess I was hoping that creating a blank image using the to_blob() trick would be just as fast as loading a blank .png from a file.

If I was to use the c++ API i'd have to dynamically link to the file, which seems a bit weird to me...unless I've got that wrong?
- By jlnr (dev) Date 2009-08-27 12:22
To be honest, I have no idea how the C++ thing would work :(

And this is really weird. Does it get better if you cache the string? Because loading from a PNG should be WAY slower than creating an image the way you did. Is this with 1.9? Does it maybe do fancy multibyte stuff?
- By banister Date 2009-08-28 17:42
you were right, i did some benchmarking, loading from a string is alot faster than loading from a file. there was just something screwy with my code :)
Up Topic Gosu / Gosu Exchange / creating blank images in gosu

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill