Archive for the 'gosu' Category

15
Sep
09

adding a mouse pointer to your gosu window through monkey-patching


In a previous post I mentioned how I was displaying a mouse pointer in a Gosu window. You can read about it here. What if you don’t wanna do that each time you need to show a mouse pointer?

Monkey patching is a technique some people don’t wanna use. I think there are cases when it can be very useful. This is one of those cases. Here’s the code that makes it possible:


#require "gosu"

# this monkey patches a gosu window so that the draw will
# also draw a mouse pointer :)
module Gosu
	class Window
		# add this method to set the mouse pointer
		def pointer=(pointer_image)
			# see if we already defined the mouse_pointer accessor
			if self.methods.include? "mouse_pointer="
				# if it's a string, it means it's the path to the image
				# to be used as the pointer
				if pointer_image.class.to_s == "String"
					# create a gosu image from it
					self.mouse_pointer = Gosu::Image.new(self,pointer_image,false)
				else
					# we assume it's an already build gosu image
					self.mouse_pointer = pointer_image
				end
			else
				# the mouse_pointer accessor isn't defined. let's add it
				# transform the parameter into an image ( if it's a string )
				pointer_image = Gosu::Image.new(self,pointer_image,false) if pointer_image.class.to_s == "String"
				# modify ! :)
				class << self
					# add the accessor
					attr_accessor :mouse_pointer
					# save the draw method by the name old_draw
					alias :old_draw :draw

					def draw
						# invoke old draw to get stuff painted on the screen
						old_draw
						# draw the mouse pointer
						@mouse_pointer.draw(mouse_x,mouse_y,0) if !@mouse_pointer.nil?
					end
				end
				# finally, assign it so that it will actually paint something
				self.mouse_pointer = pointer_image
			end
		end
	end
end


This adds the method pointer= to the Gosu::Window class. Therefore, every class that inherits from Gosu::Window will have the method too. Here’s an example of how to use that:


require "gosu"
require "mouse_hack" # nice name right?

class MyWindow < Gosu::Window
   # do whatever you want here
end

my_w = MyWindow.new
my_w.pointer = "pointer.png"
my_w.show

or


my_w.pointer = Gosu::Image.new(my_w,"pointer.png",false)

11
Sep
09

how to add a mouse pointer to your gosu window


I think the current version of the gosu framework doesn’t have an option to show the mouse pointer. At least, I haven’t found one ( and when I asked on gosu’s wiki, Julian said it was on the todo list ). So, I thought about drawing a mouse pointer. The only thing you’d want to be careful about is that when you draw a mouse pointer in your game ( or whatever you’re using gosu for ) is to make sure that the pointer image is the last one you’re drawing. Seems logic, right ? We’re doing this because the mouse pointer should always the top-most image.

Here’s a screenshot of how it looks like :

gosu_pointer

And the code behind it :


require "gosu"

class GameWindow < Gosu::Window
	
	def initialize
		super(640,480,false)
		self.caption = "Gosu Window"
		@pointer = Gosu::Image.new(self,"pointer.png",true) # we're loading a picture of the mouse
		@px = @py = 0
	end
	
	def update
		@px = mouse_x # this method returns the x coordinate of the mouse
		@py = mouse_y # this method returns the y coordinate of the mouse
	end
	
	def draw
		@pointer.draw(@px,@py,0) # we're drawing the mouse at the current position
	end
	
end

if __FILE__ == $0
	a = GameWindow.new
	a.show
end

09
Sep
09

gosu rox


I managed to create a working button implementation using the Ruby version of the Gosu framework. It was very fun to work with. If I’ll have the time and mood for it, maybe I’ll create a tutorial on how one would go about doing this.
Anyway, check it out here.




Blog Stats

  • 218,505 hits