Saturday, November 1, 2008

Rubygame setup

I have just completed my first mock game in rubygame (ruby's version of pygame), and I can't recommend it enough. That said, I did have a few troubles setting it up. The following describes how to setup rubygame in Ubuntu 8.04 (this is likely to work with other recent Ubuntu releases):

1. Download the rubygame gem from the rubygame official site. The gem is currently version 2.3.0.
2. Install any necessary libraries. The README from the rubygame site states that you are required to have:

1. ruby >= 1.8
2. SDL >= 1.2.7
3. rake >= 0.7.0 (for build/install system)

and that you should, but don't have to have:

1. SDL_gfx >= 2.0.10
2. SDL_image >= 1.2.3
3. SDL_mixer >= 1.2.7
4. SDL_ttf >= 2.0.6

I found that I had to have the last 4 SDL libraries to install the rubygame gem.

So, in case you don't already have ruby, rake and gems, the following ruby rails website lists the required commands to install ruby, rake and gems. Note, it will also install rails support, which is a good thing.

To get the required SDL libraries for rubygame, I installed the following packages via apt-get

1. libsdl-ruby1.8
2. libsdl1.2-all
3. libsdl1.2-dev
4. libsdl-gfx1.2-dev
5. libsdl-image1.2-dev
6. libsdl-mixer1.2-dev
7. libsdl-ttf2.0-dev

E.g. sudo apt-get install libsdl-ruby1.8

Once that was complete, I installed the gem. To install the gem, you must be in a terminal at the directory in which the gem is located. You cannot be in another directory, if you are, it won't find the gem.

To install the gem, I did the following:

1. I placed the .gem file I downloaded on my Desktop.
2. I did the following in the terminal to take me to my gem: cd ~/Desktop
3. I did the following in the terminal to install the gem: gem install gem_file.gem

If you did not receive a gem "install failed" message, you are ready to use rubygame. If you did, read the error message, I found that the error message named the libraries I was missing. An example of an error I received was, "could not find file SDL_IMAGE.h". This error meant that I was missing the libsdl-image1.2-dev package.

The following is an example ruby program you can use to test your rubygame install. Note this is the entire file contents. If your ruby install worked, the app will run, opening a window titled "Hello World".


#START FILE

require "rubygems"
require "rubygame"

Rubygame.init

screen = Rubygame::Screen.set_mode [320,240]
screen.title = 'Hello World'
screen.update

queue = Rubygame::EventQueue.new

game_over = false

until game_over do
queue.each do |event|
case event
when Rubygame::ActiveEvent
puts "Hello World. Updating"
screen.update
when Rubygame::QuitEvent
game_over = true
end
end
end

Rubygame.quit

# END FILE

2 comments:

Mike said...

Thanks for the post, Robert! I just started looking into Rubygame and had a few issues with getting up-and-running on Ubuntu. Now I'm all set!

Wanderer said...

Thanks for this. Had set Rubygame up a couple of years ago on my old laptop but hadn't until now on my new one. Remember it took a bit of figuring out back then. This was a great help!