require 'rubygems'
require 'gosu'
require 'RMagick'
# Extension to the image class so we can easily convert to rmagick
class Gosu::Image
def to_rmagick
this = self
Magick::Image.from_blob(to_blob) do
self.format = 'RGBA'
self.depth = 8
self.size = "#{this.width}x#{this.height}"
end.first
end
end
# A class that holds a single background image and handles cropping it
class Scene
# rmbg - rmagick image representing the background of the scene
def initialize(rmbg)
@rmbg = rmbg
end
# Pass in a Gosu::Image for a canvas and describe the section of the background to copy into it
def crop(canvas, x, y, w, h)
canvas.insert(@rmbg.excerpt(x, y, w, h), 0, 0)
end
end
# A class which views an image at some position without using up the entire screen
class View
# Define the geometry of the view within the main window. It must be given a Scene object to show
def initialize(win, scene, x, y, w, h)
@scene = scene
@canvas = Gosu::Image.new(win, Magick::Image.new(640, 480), false)
@x = x
@y = y
@w = w
@h = h
@camera_x = 0
@camera_y = 0
end
# Draws the camera position of the scene into the view space
def draw
@scene.crop(@canvas, @camera_x, @camera_y, @w, @h)
@canvas.draw(@x, @y, 1.0, 1.0)
end
# Use these to set the camera position
def cameraX(x)
@camera_x = x
end
def cameraY(y)
@camera_y = y
end
end
class GameWindow < Gosu::Window
def initialize
super 640, 480, false
self.caption = "Multiview test"
@i = 0
@scene = Scene.new(Gosu::Image.new(self, 'panorama.jpg', false).to_rmagick)
@viewa = View.new(self, @scene, 0, 0, 319, 480)
@viewb = View.new(self, @scene, 321, 0, 319, 480)
end
# Move the two views on the scene
def update
@i = @i + 1
@viewa.cameraX(@i)
@viewa.cameraY(@i)
@viewb.cameraY(@i)
end
# Just draw the views
def draw
@viewa.draw
@viewb.draw
end
end
window = GameWindow.new
window.show
Window#clip_to
and it'll be a gazillion times faster :)
Gosu
module as a free function. Would that have helped? It doesn't fit into Image
because it also affects Font
and draw_quad
etc...
Font#draw
. The implementation of <b>
and other codes is really inefficient for long strings. That's the only thing a Ruby coder might have to be careful about.
clip_to
does not work inside of record
. It would be easy to add Image#sub_image(x, y, w, h)
that returns an Image
by itself. My main worry about it is that it messes with the whole tileability
thing. Gosu usually adds a pixel of padding around images, duplicating pixels for tileable things, adding transparent pixels for everything else. This prevents bleeding when rotating or stretching, and sub_image
couldn't do this (or would have to make a copy).
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill