Yes, but SDL translates Windows events into SDL_Events, which are
placed in a thread safe event queue. I’m not sure why you would build
another system on top of this.
@ Michael Menegakis
A small demonstration program would be great, thanks. I’m suspicious
of your PeepEvents call, why are you only requesting SDL_UserEvents?
If you don’t regularly empty the event queue your UI will hang (at
least on Windows)…
Here is a test program I literally threw together. You can test the
threading and the peeping separately. I assume it won’t be too
difficult to convert to the SDL 1.3 API (which I don’t have installed
on this machine).
#include
#include “SDL.h”
#include “SDL_thread.h”
#define THREADS 1
template<typename T, int N>
int array_size(T (&array)[N]) {
return N;
}
void handleEvent(const SDL_Event &event, volatile bool &running) {
if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
running = false;
} else {
std::cout << "Got event : " << (int)event.type << ‘\n’;
}
}
void usePeepEvents(volatile bool &running) {
SDL_Event events[100];
int result = SDL_PeepEvents(events, array_size(events),
SDL_GETEVENT, SDL_ALLEVENTS);
if(result < 0) {
std::cout << "Failed to peep events: " << SDL_GetError() << ‘\n’;
running = false;
} else {
for(int i = 0 ; i < result ; ++i) {
handleEvent(events[i], running);
}
}
}
volatile bool running = true;
int inputThread(void *) {
while(running) {
usePeepEvents(running);
SDL_Delay(10);
}
}
int main(int, char **)
{
#if defined(THREADS)
std::cout << “Events are threaded\n”;
#elif defined(PEEP)
std::cout << “Events are peeped\n”;
#else
std::cout << “Events are polled\n”;
#endif
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Failed to init video: " << SDL_GetError() << '\n';
return 1;
}
std::atexit(&SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, 0);
if(!screen) {
std::cout << "Failed to set video mode: " << SDL_GetError() << '\n';
return 1;
}
#if defined(THREADS)
SDL_Thread *thread = SDL_CreateThread(&inputThread, 0);
if(!thread) {
std::cout << "Failed to create input thread: " <<
SDL_GetError() << ‘\n’;
}
#endif
while(running) {
#if defined(THREADS)
SDL_PumpEvents();
#elif defined(PEEP)
SDL_PumpEvents();
usePeepEvents(running);
#else
SDL_Event event;
while(SDL_PollEvent(&event)) {
handleEvent(event, running);
}
#endif
SDL_Delay(10);
SDL_FillRect(screen, 0, 0);
SDL_Flip(screen);
}
}