Search code examples
rubylibgosu

How to draw subimage with Ruby Gosu library?


I'm trying to draw on the screen the subimage of an Image instance using this subimage method. It seems to be pretty straight forward to do it but when I call the function it returns a NilClass instead of a new Image.

The main image is defined as:

@bg_img = Gosu::Image.new("res/space1.jpg") # dimensions 1080 x 1920

when I run @bg_img.draw(0,0,0) it draws the image normally, but when I I try to cast it into a new image:

test = @bg_img.subimage(0,0, 100, 100) # from (0,0), get rectangle of 100x100 dimension

The variable test is assigned with a null and therefore can't .draw it. Am I doing something wrong here? Or, is there another way to draw the subimage? Thanks in advance.


Solution

  • You can only use .subimage() if your image is 1024 x 1024 or smaller. I'm not sure if there's a work-around, but because this limit is inspired by memory constraints, you would probably be better off just splitting your image into multiple images of size 512 x 512 or 1024 x 1024.

    From the gosu docs:

    Caveats:

    • subimage only works if the image lives on a single texture. If the image was too large and had to be split up into several OpenGL textures, subimage will return nil (same as #gl_tex_info).

    From the source code:

    //! Returns the maximum size of an texture that will be allocated
    //! internally by Gosu.
    //! Useful when extending Gosu using OpenGL.
    const unsigned MAX_TEXTURE_SIZE = 1024;