rubydrawinglibgosu

Gosu: How to resize an image to fit window?


Using Ruby Gosu, I'm drawing an image this way:

@background_image.draw(0, 0, 0)

The image is larger than the window, is there a way to magically resize it to fit the window? I'm trying to avoid hardcoding factor_x and factor_y.


Solution

  • You can calculate factor_x\factor_y with something like this (assuming your window is stored in @window)

    fx = @window.width/@background_image.width
    fy = @window.height/@background_image.height
    @background_image.draw(0, 0, 0, fx, fy)
    

    Or use a draw_as_quad method, but it's slightly more verbose (http://www.libgosu.org/rdoc/Gosu/Image.html#draw_as_quad-instance_method)

    @background_image.draw_as_quad(0, 0, 0xffffffff, @window.width, 0, 0xffffffff, @window.width, @window.height, 0xffffffff, 0, @window.height, 0xffffffff, 0)