Events in a multi-threaded game

Hi,
I’ve started programming a 2D game using SDL. I decided to use a separate
thread to read events from SDL, using SDL_WaitEvent in a while loop. This
worked great under all Unix based box, but under Win32 the window isn’t
respoding and I can’t get the mouse events, for example.
After searching on google I found some interesting things:
- First I’ve read that I had to catch events in the same thread that the one
who set up the video mode. The which one who call SDL_SetVideoMode() or
SDL_Init() ? It’s not very clear in the documentation, and some person said
that it’s in fact the thread which call SDL_Init().
- I read also that I can catch events in another thread but I had to run
SDL_PumpEvents() in the main thread. I tried this way but I got some Xlib async
errors :frowning:

Well I would like to know what’s the correct way to do this. I will add this to
the Wiki documentation of SDL, it will be helpful for newbis like me :wink:
Thanks. :-)–
Thomas Lecomte
@Thomas_Lecomte

under linux or some platform, there is a flag called SDL_INITEVETNTHREAD that
makes the SDL event stuff work in another thread. this implies to me
that some control has been done to make the event functions same in
linux. this, i understand, is not so with windows.

somewhere in the documentation is the warning to not call any library
or SDL functions that have not been declared thread safe in a thread.

to do what you are attempting try:

http://www.gameprogrammer.com/fastevents/fastevents1.html

the Fastevents functions replace most of the SDL_*Event functions with
thread safe ones, allowing you to use SDL_USEREVENTS to signal between threads.
use this to communicate with your “main thread”( which is the one
which you should be
calling SDL_Init && SDL_SetVideoMode AFAIK ) to make it do any none
thread safe stuff

Well I would like to know what’s the correct way to do this. I will add
this to the Wiki documentation of SDL, it will be helpful for newbis
like me :wink:

I believe the correct documentation for adding threads to most games
should be:

"Don't."

They add complexity, bugs, and, in most cases, no significant
performance gain, and in other cases, a performance loss. Think twice
(no, three times) before adding threads to your game.

–ryan.