class BgUtils::TiledImage

Attributes

height[R]
scale_x[R]
scale_y[R]
width[R]

Public Class Methods

new(gosu_image, width, height, scale_x = 1.0, scale_y = 1.0, color = 0xff_ffffff, mode = :default, options = {:retro => true}) click to toggle source
# File lib/bg_utils.rb, line 132
def initialize(gosu_image, width, height, scale_x = 1.0, scale_y = 1.0, color = 0xff_ffffff, mode = :default, options = {:retro => true})
        options[:tileable] = true
        if gosu_image.is_a?(Gosu::Image) or gosu_image.is_a?(String)
                @image = (gosu_image.is_a?(Gosu::Image)) ? gosu_image : Gosu::Image.new(gosu_image, options)
        else
                raise("Wrong parameter 0 for 'Image' : \ngiven : #{gosu_image}\nshould be either a Gosu::Image instance or a string that contains path and filename")
        end

        @width, @height, @scale_x, @scale_y = width, height, scale_x, scale_y
        @color, @mode = color, mode
end

Public Instance Methods

draw(x, y, z, scale_x = 1, scale_y = 1) click to toggle source
# File lib/bg_utils.rb, line 144
def draw(x, y, z, scale_x = 1, scale_y = 1)
        @drawing_macro ||= create_macro
        @drawing_macro.draw(x, y, z, scale_x, scale_y)
end
draw_as_quad(x1, y1, x2, y2, x3, y3, x4, y4, z) click to toggle source
# File lib/bg_utils.rb, line 154
def draw_as_quad(x1, y1, x2, y2, x3, y3, x4, y4, z)
        @drawing_macro ||= create_macro
        c = Gosu::Color::WHITE
        @drawing_macro.draw_as_quad(x1, y1, c, x2, y2, c, x3, y3, c, x4, y4, c, z)
end
draw_rot(x, y, z, angle, center_x = 0.5, center_y = 0.5, scale_x = 1, scale_y = 1) click to toggle source
# File lib/bg_utils.rb, line 149
def draw_rot(x, y, z, angle, center_x = 0.5, center_y = 0.5, scale_x = 1, scale_y = 1)
        @drawing_macro ||= create_macro
        @drawing_macro.draw_rot(x, y, z, angle, center_x, center_y, scale_x, scale_y)
end

Private Instance Methods

create_macro() click to toggle source
# File lib/bg_utils.rb, line 161
def create_macro
        Gosu::record(@width, @height) do
                tile_width = @image.width * @scale_x
                tile_height = @image.height * @scale_y
                c = @color

                # filling with as many full tiles as possible
                for tile_x in 0...(@width / tile_width).floor
                        for tile_y in 0...(@height / tile_height).floor
                                @image.draw(tile_x * tile_width, tile_y * tile_height, 0, @scale_x, @scale_y, c, @mode)
                        end
                end                 

                # filling extra width with part of the tile if needed
                if (extra_width = @width % tile_width) > 0
                        subimage_width = (extra_width / @scale_x).floor
                        subimage = @image.subimage(0, 0, subimage_width, @image.height)
                        for tile_y in 0...(@height / tile_height).floor
                                subimage.draw_as_quad(
                                        @width - extra_width, tile_y * tile_height, c, 
                                        @width, tile_y * tile_height, c,
                                        @width - extra_width, (tile_y + 1) * tile_height, c,
                                        @width, (tile_y + 1) * tile_height, c, 
                                        0, @mode
                                )
                        end
                end         

                # filling extra height with part of the tile if needed
                if (extra_height = @height % tile_height) > 0
                        subimage_height = (extra_height / @scale_y).floor
                        subimage = @image.subimage(0, 0, @image.width, subimage_height)
                        for tile_x in 0...(@width / tile_width).floor
                                subimage.draw_as_quad(
                                        tile_x * tile_width, @height - extra_height, c, 
                                        (tile_x + 1) * tile_width, @height - extra_height, c,
                                        tile_x * tile_width, @height, c,
                                        (tile_x + 1) * tile_width, @height, c, 
                                        0, @mode
                                )
                        end
                end 

                # if there were extra pixels in both axis, we have to draw a bottom right corner
                if (extra_width > 0 and extra_height > 0)
                        subimage_width = (extra_width / @scale_x).floor
                        subimage_height = (extra_height / @scale_y).floor
                        subimage = @image.subimage(0, 0, subimage_width, subimage_height)

                        subimage.draw_as_quad(
                                @width - extra_width, @height - extra_height, c,
                                @width, @height - extra_height, c,
                                @width - extra_width, @height, c,
                                @width, @height, c,
                                0, @mode
                        )
                end
        end
end