in reply to SDLx::App event loop

I would like the user to be able to hold down the space bar and have things "keep happening" as long as the key is held. Preferrably, I'll be able to control the time between events.

Just poking thru the /examples/SDLx programs, the SDLx_controller_two_squares.pl seems to do that. You can control the arrow keys, but maybe your needs are more demanding? A minimal example might be helpful if you want some of us to look at it.

From my experience with Gtk2, the callbacks are designed to allow continued processing depending on whether you return a 1 or 0, TRUE or FALSE, from the callback. It looks like SDL does the same thing. I may have it slightly wrong, as it gets a bit confusing, for normal callbacks that would be stacked, returning a 1 means "I handled it, stop other callbacks"; while in a timer, returning 1 means continue the timer and a 0 means stop the timer. So if you want to play with timers within keyboard event callbacks, you may be able to return 0 or 1, depending on the state of the key you are interested in. Like set a global variable for whether a particular key is up or down, and in the timer callback, return a 0 or 1 accordingly. I hope I made sense. :-)


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: SDLx::App event loop
by Ransom (Beadle) on Jun 11, 2012 at 17:24 UTC

    The example provided follows the SDL_key_repeat solution I explained below. It is implicit when creating the app. As an aside, my exaple is essentially exactly like theirs except I've got things split up into Moose classes and it's a bit more messy. It is hard to post code of that nature in any sort of intelligible way. It is entirely safe to assume my example is the same as the two squares example.

    Following what their example is doing, the longer I hold an arrow key, the more events get pumped, the more the square moves. It is essentially a timed release of an event, by limiting how many key_repeat signals get translated into events.

    This satisfies my need for some things. Maybe my question is more a timer question then, since I would like to have a certain event only happen at specific intervals. With the above example and solution, I could only have one specific delay for many events. Imagine I wanted to shoot bullets frontwards at my key_repeat speed, but then fire one backwards every 2*key_repeat. I don't have a way to do that with the built in key_repeat functions, and am also just as clueless about how to use a timer without blocking.

    With these things being called "callbacks" is this non-blicking by default? I've only used callbacks in a Net::SNMP non-blocking context. Are these essentially the same types of things? Is SDLx already trying to make things this easy for me?

    Thanks for your quick reply

    Ransom
      With these things being called "callbacks" is this non-blicking by default?

      Well

      my $app = SDLx::App->new(); .... $app->run;
      is an eventloop system. It schedules events as you request them. You can use SDLx without the eventloop, or make your own. See Re: Tk Game Sound demo-with SDL where I use the Tk eventloop with SDL.

      Generally in eventloop systems, timers and IO watchers can run simultaneously in a non-blocking manner, but keyboard input may need to be in a separate thread. If you are using Moose, that may be having an unexpected effect.

      It's all hard to say, when we are just speculating on unseen code, but timers can run non-blocking, it depends on what the timer is doing. Possibly you could use a key_press_down to start a timer, which every 20 forward-fire cycles, will do one backward fire. Then on the keypress_up, you cancel the timer. I probably could do it in Tk or Gtk2, but I am unfamiliar with all the SDL code. SDL timers seem to address your blocking ( timeslice ) problems.

      Generally timers can be used 2 ways, one is a one-shot timer, the other will repeat itself until you tell it to stop. This is where the returning a 1 or 0 , TRUE or FALSE, comes into play.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        Updated OP with code. I'm starting to think this is a timer issue coupled with my lack of understanding of event loops. Thanks you for all your help so far.