[vox-tech] fltk + SDL and questions about handling events

Mark K. Kim vox-tech@lists.lugod.org
Tue, 18 May 2004 20:02:54 -0700 (PDT)


So basically you're doing the following, right?:

   while(1) {
     // poll FLTK
     // poll SDL events

     // do something
   }

But the problem is this is a tight-loop and it's gonna eat up all of CPU
time.  So you'll need some sleeping code to yield some timeslices to other
processes, so your code becomes like this:

   while(1) {
     // poll FLTK
     // poll SDL events

     // do something

     usleep(100000);
   }

But this is problematic for SDL because SDL window freezes while usleep()
is running.  This is because SDL's event handler can't run while usleep()
is active.  So you'll need to use SDL's SDL_Delay() call instead, like
this:

   while(1) {
     // poll FLTK
     // poll SDL events

     // do something

     SDL_Delay(100);
   }

This is fine for SDL, but (without knowing FLTK too much) perhaps FLTK has
similar problems of freezing when non-FLTK sleep call is called? Looking
at the FLTK demo on the SDL website it looks like it's okay to do this
so... I guess it's good.

BTW, is this for Tuxpaint-config?  What's the SDL window for?

-Mark


On Tue, 18 May 2004, Jan W wrote:

> Hi all:
>
> I have a question about events structs with fltk and SDL:
>
> here's the guts of my main function:
>
> int main(int argc, char **argv)
> {
> // I have two functions defined to handle events:
> Fl::add_handler(my_event_handler);
> //and
> SDL_Init(SDL_INIT_VIDEO);
>
> //and I have SDL_Event *sdlevent pointer defined
> window->construct_new_window();
> window->show()
> // To allow fltk to share events with SDL, instead of then
> // going into fl::run(), I made a custom event loop:
>
> //fl::run();
>
> while(fl::check()) {
>    if (SDL_PollEvent(&sdlevent))
>        my_event_handler(0);
>    }
> return(0)
> }
>
> The only thing left out is that i copy sdlevent.key.keysym.sym to my local key
> char variable, and my other matching events (Shift, control, mouseclick) into
> the FL equivalents such that they will fall into points in the switch{} in the
> global event handler, my_event_handler.  It works nicely now, with multiple
> windows all sharing events...
>
> I know that fltk stores its event in static structures, and that SDL uses a
> completely different model to handle events (FL::event() is an integer,
> SDL_PollEvent fills the struct SDL_Event with current event data).  In fltk,
> the event structures are defined when they are created, and fl::run() passes
> the current events onto the ones defined (callbacks for button clicks, etc).
> That's why I substitute the call from run to check, because check() just the
> checks the queue once, not looping forever.  This gives me a chance to call
> PollEvent, and thus checking the two queues using the same event loop.
>
> My question is this: is this a good way to do it?  Has anyone had experience
> with handling events from multiple windows/libraries/event structs?  I just
> need my code to be cross platform portable and reliable (i haven't tried it on
> anything beside x86 linux)...
>
> Any other tips or tricks?
>
> --thanks in advance
>
> jan
>
> =====
> <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> EDUCATION, n. That which discloses to the wise and disguises from the
> foolish their lack of understanding.
> <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
>
>
>
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! - Internet access at a great low price.
> http://promo.yahoo.com/sbc/
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech
>

-- 
Mark K. Kim
AIM: markus kimius
Homepage: http://www.cbreak.org/
Xanga: http://www.xanga.com/vindaci
Friendster: http://www.friendster.com/user.jsp?id=13046
PGP key fingerprint: 7324 BACA 53AD E504 A76E  5167 6822 94F0 F298 5DCE
PGP key available on the homepage