Processing events in a thread

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;
}

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:

[snipped]
Is this a question or a statement?
Your program will run just fine, but it won’t actually get any events.
SDL doesn’t crash if you call SDL_PumpEvents() in another thread, it
just won’t do anything.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

At 10:22 AM 2/29/2004 -0800, Sam Lantinga wrote:

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:

[snipped]
Is this a question or a statement?

Yes.

Your program will run just fine, but it won’t actually get any events.
SDL doesn’t crash if you call SDL_PumpEvents() in another thread, it
just won’t do anything.

I changed the PumpEvents() call to PollEvent() and it handles an SDL_QUIT
event fine. Is there, in fact, any dependency on SetVideoMode(), or can I
safely leave the event loop in its own thread?

snip

I changed the PumpEvents() call to PollEvent() and it handles an SDL_QUIT
event fine. Is there, in fact, any dependency on SetVideoMode(), or can I
safely leave the event loop in its own thread?

Aye, you can run it in its own thread, but you will need to call
SDL_PumpEvents() from the main thread (I’m not sure if it needs to be the
initial thread, or the thread that called SDL_SetVideoMode(); Sam?) for that
thread to actually get any events. I might be wrong, but I believe thats
the only actual use for SDL_PumpEvents
().

Hope this helps.


=cut JAPH

$=“aa4e0a827b14150960910ca0305691”;$=
unpack(“b*”,pack(“H*”,$));s/([01]{5})/
pack(“b*”,$1)/gex;$
=join(’’,map{chr(
ord($_)+96)}split’’);s/`/ /gx;print;

=head1 AUTHOR

Alex Lyman (“Silicon”) is the Lead Programmer,
among other things, for LAO Gaming.

Alex also is a Web Designer/Programmer in his
spare time. If you need a website designed,
or just need programming done for one, contact
him at AlexLyman at Earthlink.net

=cut


Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.594 / Virus Database: 377 - Release Date: 2/24/2004

----- Original Message -----
From: willeke@users.sourceforge.net (Jon Willeke)
To:
Sent: Sunday, February 29, 2004 6:59 PM
Subject: Re: [SDL] processing events in a thread

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:

Were you asking a question or trying to make a point? If so, please be
more specific because I can’t figure out what you are trying to say.

BTW, the listed code will wait for 5 seconds, but it will not properly
handle input events because you are calling pollevent in the wrong
thread. If you take 10 minutes to look at the SDL code you will find
that what you are doing is not thread safe.

	Bob PendletonOn Sat, 2004-02-28 at 16:13, Jon Willeke wrote:

#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;
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±--------------------------------------+

At 3/1/2004, Bob Pendleton wrote:>On Sat, 2004-02-28 at 16:13, Jon Willeke wrote:

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().

Were you asking a question or trying to make a point? If so, please be
more specific because I can’t figure out what you are trying to say.

BTW, the listed code will wait for 5 seconds, but it will not properly
handle input events because you are calling pollevent in the wrong
thread. If you take 10 minutes to look at the SDL code you will find
that what you are doing is not thread safe.

I was asking whether the stated dependency was correct, because my testing
appeared to contradict it. PumpEvents() works fine in a thread separate
from SetVideoMode() and PollEvent() can handle a QUIT event. On further
testing I discovered, as you point out, that it could not handle a KEYDOWN
event unless I moved SetVideoMode() into the event loop thread (or vice versa).

Hi only want to expres what i have done…
Months ago with out knoing nothing about side effects between threads
and event catchin i made a little demo of arcanoid

In my game code i was using SDL_WaitEvent() in a second thread and the
program runs as i’d espected, chatching all the keboard events.

This is an snip of my code

class Teclado : public Colector {
friend int LeerTeclado(void *);
public:
Teclado ();
~Teclado ();
void update();
private:
SDL_Thread *teclado;
int mover;
int disparar;
int salir;
};

Teclado::Teclado ()
{
mover = 0;
disparar = false;
salir = false;
teclado = SDL_CreateThread(LeerTeclado, this);
}

int LeerTeclado (void *m)
{
SDL_Event event;
Teclado *jugador = (Teclado *) m;

while(SDL_WaitEvent(&event) != 0){
switch(event.type){
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE)
jugador->disparar = true;

The only thing i know is tha it works
By the way it is in debian unstable, SDL 1.2

Losky

The only thing i know is tha it works

Yes, but it won’t work on all platforms. It just happens to work on Linux. :slight_smile:

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment