EventThread: Simplify user event handling

This commit is contained in:
Jonas Kulla 2014-08-06 15:54:44 +02:00
parent 151a1f0d1b
commit 24759fb97b
1 changed files with 26 additions and 33 deletions

View File

@ -48,8 +48,7 @@ EventThread::MouseState EventThread::mouseState =
/* User event codes */ /* User event codes */
enum enum
{ {
REQUEST_TERMINATION, REQUEST_SETFULLSCREEN = 0,
REQUEST_SETFULLSCREEN,
REQUEST_WINRESIZE, REQUEST_WINRESIZE,
REQUEST_MESSAGEBOX, REQUEST_MESSAGEBOX,
REQUEST_SETCURSORVISIBLE, REQUEST_SETCURSORVISIBLE,
@ -59,18 +58,15 @@ enum
EVENT_COUNT EVENT_COUNT
}; };
static uint32_t usrId[EVENT_COUNT]; static uint32_t usrIdStart;
bool EventThread::allocUserEvents() bool EventThread::allocUserEvents()
{ {
uint32_t first = SDL_RegisterEvents(EVENT_COUNT); usrIdStart = SDL_RegisterEvents(EVENT_COUNT);
if (first == (uint32_t) -1) if (usrIdStart == (uint32_t) -1)
return false; return false;
for (size_t i = 0; i < EVENT_COUNT; ++i)
usrId[i] = first + i;
return true; return true;
} }
@ -259,34 +255,30 @@ void EventThread::process(RGSSThreadData &rtData)
default : default :
/* Handle user events */ /* Handle user events */
if (event.type == usrId[REQUEST_TERMINATION]) switch(event.type - usrIdStart)
{
terminate = true;
Debug() << "EventThread termination requested";
}
else if (event.type == usrId[REQUEST_SETFULLSCREEN])
{ {
case REQUEST_SETFULLSCREEN :
setFullscreen(win, static_cast<bool>(event.user.code)); setFullscreen(win, static_cast<bool>(event.user.code));
} break;
else if (event.type == usrId[REQUEST_WINRESIZE])
{ case REQUEST_WINRESIZE :
SDL_SetWindowSize(win, event.window.data1, event.window.data2); SDL_SetWindowSize(win, event.window.data1, event.window.data2);
} break;
else if (event.type == usrId[REQUEST_MESSAGEBOX])
{ case REQUEST_MESSAGEBOX :
SDL_ShowSimpleMessageBox(event.user.code, SDL_ShowSimpleMessageBox(event.user.code,
rtData.config.game.title.c_str(), rtData.config.game.title.c_str(),
(const char*) event.user.data1, win); (const char*) event.user.data1, win);
free(event.user.data1); free(event.user.data1);
msgBoxDone = true; msgBoxDone = true;
} break;
else if (event.type == usrId[REQUEST_SETCURSORVISIBLE])
{ case REQUEST_SETCURSORVISIBLE :
showCursor = event.user.code; showCursor = event.user.code;
updateCursorState(cursorInWindow); updateCursorState(cursorInWindow);
} break;
else if (event.type == usrId[UPDATE_FPS])
{ case UPDATE_FPS :
if (!fps.displaying) if (!fps.displaying)
break; break;
@ -304,6 +296,7 @@ void EventThread::process(RGSSThreadData &rtData)
} }
SDL_SetWindowTitle(win, buffer); SDL_SetWindowTitle(win, buffer);
break;
} }
} }
@ -320,7 +313,7 @@ void EventThread::cleanup()
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
if (event.type == usrId[REQUEST_MESSAGEBOX]) if ((event.type - usrIdStart) == REQUEST_MESSAGEBOX)
free(event.user.data1); free(event.user.data1);
} }
@ -349,7 +342,7 @@ void EventThread::updateCursorState(bool inWindow)
void EventThread::requestTerminate() void EventThread::requestTerminate()
{ {
SDL_Event event; SDL_Event event;
event.type = usrId[REQUEST_TERMINATION]; event.type = SDL_QUIT;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
@ -359,7 +352,7 @@ void EventThread::requestFullscreenMode(bool mode)
return; return;
SDL_Event event; SDL_Event event;
event.type = usrId[REQUEST_SETFULLSCREEN]; event.type = usrIdStart + REQUEST_SETFULLSCREEN;
event.user.code = static_cast<Sint32>(mode); event.user.code = static_cast<Sint32>(mode);
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
@ -367,7 +360,7 @@ void EventThread::requestFullscreenMode(bool mode)
void EventThread::requestWindowResize(int width, int height) void EventThread::requestWindowResize(int width, int height)
{ {
SDL_Event event; SDL_Event event;
event.type = usrId[REQUEST_WINRESIZE]; event.type = usrIdStart + REQUEST_WINRESIZE;
event.window.data1 = width; event.window.data1 = width;
event.window.data2 = height; event.window.data2 = height;
SDL_PushEvent(&event); SDL_PushEvent(&event);
@ -376,7 +369,7 @@ void EventThread::requestWindowResize(int width, int height)
void EventThread::requestShowCursor(bool mode) void EventThread::requestShowCursor(bool mode)
{ {
SDL_Event event; SDL_Event event;
event.type = usrId[REQUEST_SETCURSORVISIBLE]; event.type = usrIdStart + REQUEST_SETCURSORVISIBLE;
event.user.code = mode; event.user.code = mode;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
@ -388,7 +381,7 @@ void EventThread::showMessageBox(const char *body, int flags)
SDL_Event event; SDL_Event event;
event.user.code = flags; event.user.code = flags;
event.user.data1 = strdup(body); event.user.data1 = strdup(body);
event.type = usrId[REQUEST_MESSAGEBOX]; event.type = usrIdStart + REQUEST_MESSAGEBOX;
SDL_PushEvent(&event); SDL_PushEvent(&event);
/* Keep repainting screen while box is open */ /* Keep repainting screen while box is open */
@ -442,6 +435,6 @@ void EventThread::notifyFrame()
SDL_Event event; SDL_Event event;
event.user.code = avgFPS; event.user.code = avgFPS;
event.user.type = usrId[UPDATE_FPS]; event.user.type = usrIdStart + UPDATE_FPS;
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }