Ruby-sdl: How do you poll events?

The code of my raw main loop is below. I’m sure there are better ways
to poll the event queue. Do you have better ideas?
Also I’m not sure about the garbage collect thing at the bottom…

loop {
offset_x = 0
offset_y = 0
if event.poll != 0
case event.type
when SDL::Event::QUIT
break
when SDL::Event::KEYDOWN
case event.keySym
when SDL::key::ESCAPE
break
when SDL::key::LEFT
offset_x = -5
when SDL::key::RIGHT
offset_x = +5
when SDL::key::DOWN
offset_y = +5
when SDL::key::UP
offset_y = -5
end
end
end

before = now
now = SDL::getTicks
dt = now-before

here comes the rendering finally

player.draw(screen)
enemies.each{|i| i.draw(screen)}
screen.fillRect(0,0,640,480,0)
w.to_sdl(screen, offset_x, offset_y)
screen.fillRect(0,0,640,480,0)
ObjectSpace.garbage_collect
screen.flip

}

ChrisKaelin wrote:

  break
        when SDL::Key::UP
enemies.each{|i| i.draw(screen)}
screen.fillRect(0,0,640,480,0)
w.to_sdl(screen, offset_x, offset_y)
screen.fillRect(0,0,640,480,0)
ObjectSpace.garbage_collect
screen.flip

}

Here’s the main loop from my game engine. You should poll the event
queue in a loop or you’ll only handle one event every iteration of the
main loop.
I handle the input by passing the important info from the event on to
the top element of a stack of input handlers. This means you can easily
switch control
contexts by pushing/popping handlers. This isn’t really perfect though.
Ideally it wouldn’t be method calls directly on the top element, ideally
the event poller shouldn’t
know about the stack and the input handler should allow for allowing
events to continue down the stack. Errr, this may not really have been
what you were asking for.
Also I think there’s a helper function in sdl that builds a hash of
keysyms to booleans that stores the keyboard state.
Anyway, I hope this helps you.

def main_loop
    @running = true
    @last_time = SDL.get_ticks
    while @running
    while event = SDL::Event2.poll
        case event
        when SDL::Event2::Quit
        quit
        when SDL::Event2::KeyDown
        @input.last.key_down( event.sym ) unless @input.empty?
        when SDL::Event2::KeyUp
        @input.last.key_up( event.sym ) unless @input.empty?
        when SDL::Event2::MouseButtonDown
        @input.last.mouse_down( event.button, P[event.x,event.y] )
        when SDL::Event2::MouseButtonUp
        @input.last.mouse_up( event.button, P[event.x,event.y] )
        end
    end
    current_time = SDL.get_ticks
    if current_time - @last_time > GAME_SPEED
        @last_time = current_time
        Core.objects.update current_time
    end
    Core.display.draw
    end
end

Ok, thanks a lot I’ll give that one a try. It is remarkable, that
there is not quite good tutorial stuff around. Some part of the C-SDL
tutorial can be transponded to ruby, but not all…

btw, your game engine is not open-source by any chance? :wink:

Indeed, the ruby-sdl binding has very poor documentation.

I’ve used it twice before.

Would you like to cooperate on creating some tutorial for ruby-sdl? I
coudn’t do it alone (time) but I’d be glad to help.

Also, check http://rubymentor.rubyforge.org

I’m sure there are mentors with SDL experience there (at least if you
consider me a potential mentor, which you should - in other words, I’m
offering mentorship :slight_smile: )

Aur

SonOfLilit wrote:

I’m sure there are mentors with SDL experience there (at least if you
consider me a potential mentor, which you should - in other words, I’m
offering mentorship :slight_smile: )

Aur

I like that “the documentation is poor - let’s change it” attitude :wink:
Thanks for your offering. But be warned, I’m not a programmer, just a
lazy system administrator (that’s why I love ruby, it supports my
lazyness) with general interest in ruby OO programming.
The most easy thing would be a wiki-place, where we could start a
nice, ever-growing tutorial/howto. Advantage would be, that also
experienced SDL programmers could give input then… any idea where we
could start that thing?

Greetings

Chris

We can create a RubyForge project for it (with permission from the
author, which I think is japanese) and use the wiki there.

But I was thinking more of a tutorial.

Something that reads and streams.

In the process, of course, reference documentation would also be
improved.

Aur

Have you guys try Rubygame? It have good documentation. I think is a
fork of
ruby-SDL but I don’t know.

Here is one of the “polling” code(I don’t know much game development
terminology) that I used for keyboard stuff. Notes that this is a code
snippet from one of my game.

q = Rubygame::EventQueue.new
while (1)
if @pet.data[‘status’] == false
@screen.background_cleanup(800,600)
Death.new(@screen)
@pet.save()
return
end
execute()
q.each do |event|
case event
when Rubygame::KeyDownEvent
case event.key
when Rubygame::K_S
@pet.save()
when Rubygame::K_ESCAPE
@pet.save()
return
end
when Rubygame::MouseDownEvent
case event.button
when Rubygame::MOUSE_LEFT
if @itemmenu.act == true
@itemmenu.test(event)
else
if @item.check(@screen.background,event,@
pet.sprite.return) == true
@display.itnext.count()
end
@display.draw()
@screen.screen_clear()
end
end
when Rubygame::QuitEvent
@pet.save()
exit
when Rubygame::MouseMotionEvent
@map.points = event.pos()
end
end
end

On 19 Mrz., 16:00, SonOfLilit [email protected] wrote:

coudn’t do it alone (time) but I’d be glad to help.
Thanks for your offering. But be warned, I’m not a programmer, just a
lazy system administrator (that’s why I love ruby, it supports my
lazyness) with general interest in ruby OO programming.
The most easy thing would be a wiki-place, where we could start a
nice, ever-growing tutorial/howto. Advantage would be, that also
experienced SDL programmers could give input then… any idea where we
could start that thing?

Greetings

Chris

Yeah, but still I think a wiki is the best place to start easy
tutorials, that can hyperlink into detailed parts or even the
documentation inside the same wiki. Also my point is, I’m very poor on
spare time atm. and don’t know, what progress I’d make on my own…