[SDL] Input Callback?

David Olofson david.olofson at reologica.se
Thu Jul 4 09:35:01 PDT 2002


On 04/07/2002 11:20:38 , Jimmy <jimmy at jimmysworld.org> wrote:
[...]
>> Well, yes - but if that turns out to be a real problem, there's
>> still the option of setting up your own thread that waits events,
>> and either timpstamps them and passes them on, or handles them
>> right away. I'd think the former is easier and more robust. Just
>> use an sfifo (http://olofson.net/mixed.html) or something.
> 
>Multithreading bad, Jimmy hates and will avoid until absolutely
>necessary.  Either way, I can interlace the rendering with picking up
>the events and queueing them.

Urgh... I'd say an event thread and an sfifo is *much* cleaner than
interleaving/cooperative multitasking ever could be.

Also note that interleaving won't work with indirect rendering
systems, such as OpenGL (and thus, glSDL). If you run a reasonably
fast 2D game on a h/w accelerated retrace syncing display, your
main loop will spend most of the time in SDL_Flip() waiting for the
retrace. That is, you won't have the CPU more than a fraction of
each frame, so you'll have practically the same event timing
issues as if you didn't try to timestamp.

As for the issues with threading, the sfifo would take care of that
completely. You wouldn't see more of the threading mess than if it
had been SDL or the OS doing the timestamping. Here's about what
you'd have to do in the event thread:

while(1)
{
	Uint32 timstamp;
	SDL_Event ev;
	if(SDL_WaitEvent(&ev) < 0)
		break;	/* Or some actual error handling... */
	timestamp = SDL_GetTicks();
	while(sfifo_space(&evfifo) < sizeof(timestamp) + siveof(ev))
		SDL_Delay(10);	/* This is an error condition! */
	sfifo_write(&evfifo, &timestamp, sizeof(timestamp));
	sfifo_write(&evfifo, &ev, sizeof(ev));
}

..and in your main thread, you just do something like this:

while(1)
{
	Uint32 timstamp;
	SDL_Event ev;
	while(sfifo_used(&evfifo) >= sizeof(timestamp) + sizeof(ev))
	{
		sfifo_read(&evfifo, &timestamp, sizeof(timestamp));
		sfifo_read(&evfifo, &ev, sizeof(ev));
		advance_logic_time(timestamp);
		/*
		 * Insert the usual SDL event handling here
		 */
	}
	advance_logic_time(SDL_GetTicks());
	render_frame();
}

Note that the sfifo (AFAIK) does not function as a time gate, so we
shouldn't be getting events from the future. ;-) (Or: No, we don't
have to check the timestamps!)


//David


.---------------------------------------
| David Olofson
| Programmer
| david.olofson at reologica.se
|---------------------------------------
| Address:
| REOLOGICA Instruments AB
| Scheelevägen 30
| 223 63 LUND
| Sweden
|---------------------------------------
| Phone: 046-12 77 60
| Fax: 046-12 50 57
| Mobil: 
| E-mail: david.olofson at reologica.se
| WWW: http://www.reologica.se
| 
`-----> We Make Rheology Real





More information about the SDL mailing list