[SDL] processing events in a thread

Jon Willeke willeke at users.sourceforge.net
Sat Feb 28 14:12:00 PST 2004


According to the documentation, "You can only call [SDL_PumpEvents()] in 
the thread that set the video mode."  In fact, SDL_PumpEvents() seems to be 
dependent on SDL_Init(), rather than SDL_SetVideoMode().

The following program (shortened as much as possible) displays a window for 
five seconds:

#include "SDL.h"
#include "SDL_thread.h"
#undef main

int
event_loop( void *init )
{
   Uint32 start;

   SDL_Init( SDL_INIT_VIDEO );
   SDL_CondSignal( (SDL_cond *)init );

   start = SDL_GetTicks();
   while( SDL_GetTicks() - start < 5000 ) {
     SDL_PumpEvents();
     SDL_Delay( 1 );
   }

   return 0;
}

int
main()
{
   SDL_Thread *thread;
   SDL_cond *initialized;
   SDL_mutex *init_mutex;
   SDL_Surface *screen;

   initialized = SDL_CreateCond();
   thread = SDL_CreateThread( event_loop, initialized );

   init_mutex = SDL_CreateMutex();
   SDL_mutexP( init_mutex );
   SDL_CondWait( initialized, init_mutex );

   screen = SDL_SetVideoMode( 800, 600, 32, SDL_SWSURFACE );

   SDL_WaitThread( thread, NULL );
   SDL_Quit();

   return 0;
}





More information about the SDL mailing list