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

About these ads

5 Responses to “how to add a mouse pointer to your gosu window”


  1. September 14, 2009 at 02:04

    I found this to be really helpful. Thanks for putting this online. I’m using Gosu as a quick way to mock up game ideas and never would have figured out how to grab the mouse variables without this.

  2. 3 Thefron
    October 20, 2009 at 18:38

    Cool!
    I was thinking of quiting developing with GOSU without mouse related things..
    Great tip!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 177,931 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: