Keeping it Small and Simple

2007.05.30

Ruby, meet SDL

Filed under: Graphics Programming, Ruby Programming — Lorenzo E. Danielsson @ 22:21

Just today somebody asked me why I am doing a pygame tutorial, since I am “a ruby guy”, whatever that’s supposed to mean. I never claimed to be “a ruby guy”. I program in Pascal as well. Go ahead and call me a “Pascal guy”. Or a “C guy”.

Anyways, such minor issues apart. There are Ruby bindings for SDL called (amazingly enough) Ruby/SDL. Now this is not “pygame in Ruby”. Ruby/SDL is a Ruby wrapper for SDL. As such, its API looks like the original SDL API. Pygame, is not a wrapper. It is a python library for developing multimedia applications that uses SDL internally. Confused? I hope not. Ruby/SDL is probably comparable to PySDL which seems to be a Python wrapper for SDL.

I started writing the pygame tutorial because a few people asked me to. Pygame is more popular than Ruby/SDL. (Note: I said more popular, not better. I have no opinion on which is better. I’m only interested in being able to use both). I’m not going to do a Ruby/SDL tutorial, at least not now, but I can at least give you something to get started. First of all, you need to get Ruby/SDL installed. On Debian that translates to:


# aptitude install libsdl-ruby1.8

at least if you are using Etch. If you are not on Debian or one of its derivates.

Once it is installed you can start off by going to /usr/share/doc/libsdl-ruby1.8/examples and studying the examples there. They show you just about everything you need to know. I will also add a small example of my own, adapted from my pygame tutorial.

 1 #! /usr/bin/ruby -w
 2 
 3 require 'sdl'
 4 
 5 SDL.init SDL::INIT_VIDEO
 6 screen = SDL::set_video_mode 640, 480, 24, SDL::SWSURFACE
 7 x = y = 0
 8 
 9 BGCOLOR = screen.format.mapRGB 0, 0, 0
10 LINECOLOR = screen.format.mapRGB 255, 255, 255
11 
12 running = true
13 
14 while running
15   while event = SDL::Event2.poll
16     case event
17     when SDL::Event2::Quit
18       running = false
19     when SDL::Event2::MouseMotion
20       x = event.x
21       y = event.y
22       end
23   end
24 
25   screen.fill_rect 0, 0, 640, 480, BGCOLOR
26   screen.draw_line x, 0, x, 479, LINECOLOR
27   screen.draw_line 0, y, 639, y, LINECOLOR
28   screen.flip
29 end

That’s it! Have fun..

Advertisements

2 Comments »

  1. hi i have this bug!
    When i comment this its work perfectly.

    maby the methode name is not good ???

    sdl.rb:26: undefined method `draw_line’ for # (NoMethodError)

    Comment by Ranska — 2008.02.18 @ 17:51

  2. @Ranska: That is weird, because it works over here. Just to be sure, I tested it again and draw_line works fine. Could it be a version issue perhaps? Could you check the version of Ruby/SDL on your system? On mine, changelog.Debian.gz says:

    libsdl-ruby (1.1.0-2) unstable;

    The API documentation for SDL::Surface is here:
    http://www.kmc.gr.jp/~ohai/rubysdl_doc.en.html#label-59

    It seems that draw_line is an alias for drawLine. Could you try changing:

    screen.draw_line x, 0, x, 479, LINECOLOR
    screen.draw_line 0, y, 639, y, LINECOLOR

    to:
    screen.drawLine x, 0, x, 479, LINECOLOR
    screen.drawLine 0, y, 639, y, LINECOLOR

    and see if that works?

    Comment by Lorenzo E. Danielsson — 2008.02.18 @ 19:03


RSS feed for comments on this post. TrackBack URI

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 at WordPress.com.

%d bloggers like this: