# Ruby-sdl: How do you poll events?

**URL:** <https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203>\
**Category:** Ruby\
**Created:** [March 18, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203 "2007-03-18T17:00:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 18, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/1 "2007-03-18T17:00:39Z")

</div>

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:🔑:ESCAPE  
break  
when SDL:🔑:LEFT  
offset\_x = -5  
when SDL:🔑:RIGHT  
offset\_x = +5  
when SDL:🔑:DOWN  
offset\_y = +5  
when SDL:🔑: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

```

}

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 18, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/2 "2007-03-18T21:59:17Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 9:26am UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/3 "2007-03-19T09:26:12Z")

</div>

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? 😉

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 9:53am UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/4 "2007-03-19T09:53:14Z")

</div>

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](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 🙂 )

Aur

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/5 "2007-03-19T13:40:36Z")

</div>

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 🙂 )
> 
> Aur

I like that “the documentation is poor - let’s change it” attitude 😉  
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

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/6 "2007-03-19T16:01:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/7 "2007-03-19T14:45:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ChrisKaelin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@ChrisKaelin](https://www.ruby-forum.com/u/ChrisKaelin)\
**Post date:** [March 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/ruby-sdl-how-do-you-poll-events/89203/8 "2007-03-19T16:31:23Z")

</div>

On 19 Mrz., 16:00, SonOfLilit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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…
