ruby-sdl: How do you poll events?

C

ChrisKaelin

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
}
 
K

Ken Allen

ChrisKaelin said:
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
}
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
 
C

ChrisKaelin

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? ;-)
 
S

SonOfLilit

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 :) )


Aur
 
C

ChrisKaelin

SonOfLilit said:
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 :) )


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
 
S

SonOfLilit

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
 
C

ChrisKaelin

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,238
Messages
2,571,192
Members
47,829
Latest member
wyattmoon

Latest Threads

Top