如何用Ruby Gosu 库画子图?

How to draw subimage with Ruby Gosu library?

我正在尝试使用 this subimage method 在屏幕上绘制 Image 实例的子图像。这样做似乎很简单,但是当我调用函数时 returns a NilClass 而不是新的 Image.

主图定义为:

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

当我 运行 @bg_img.draw(0,0,0) 它正常绘制图像,但是当我尝试将其转换为新图像时:

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

变量 test 被赋值为 null,因此不能 .draw。我在这里做错了什么吗?或者,还有另一种绘制子图像的方法吗?提前致谢。

如果您的图片为 1024 x 1024 或更小,您只能使用 .subimage()。我不确定是否有解决方法,但由于此限制是受内存限制的启发,因此最好将图像拆分为大小为 512 x 512 或 1024 x 1024 的多个图像。

来自 gosu 文档:

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).

来自源代码:

//! 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;

好消息,Image#subimage 在 Gosu 0.12.0 及更高版本中无论图像大小如何都有效。