Factor out some thread communication code (window size, bindings)

This commit is contained in:
Jonas Kulla 2015-01-02 01:41:23 +01:00
parent c63a8947ce
commit 3411435138
4 changed files with 25 additions and 69 deletions

View File

@ -80,7 +80,7 @@ void EventThread::process(RGSSThreadData &rtData)
{ {
SDL_Event event; SDL_Event event;
SDL_Window *win = rtData.window; SDL_Window *win = rtData.window;
WindowSizeNotify &windowSizeMsg = rtData.windowSizeMsg; UnidirMessage<Vec2i> &windowSizeMsg = rtData.windowSizeMsg;
fullscreen = rtData.config.fullscreen; fullscreen = rtData.config.fullscreen;
int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT; int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
@ -140,8 +140,7 @@ void EventThread::process(RGSSThreadData &rtData)
switch (event.window.event) switch (event.window.event)
{ {
case SDL_WINDOWEVENT_SIZE_CHANGED : case SDL_WINDOWEVENT_SIZE_CHANGED :
windowSizeMsg.notifyChange(event.window.data1, windowSizeMsg.post(Vec2i(event.window.data1, event.window.data2));
event.window.data2);
break; break;
case SDL_WINDOWEVENT_ENTER : case SDL_WINDOWEVENT_ENTER :

View File

@ -107,102 +107,59 @@ private:
}; };
/* Used to asynchronously inform the RGSS thread /* Used to asynchronously inform the RGSS thread
* about window size changes */ * about certain value changes */
struct WindowSizeNotify template<typename T>
struct UnidirMessage
{ {
SDL_mutex *mutex; UnidirMessage()
: mutex(SDL_CreateMutex()),
current(T())
{}
AtomicFlag changed; ~UnidirMessage()
int w, h;
WindowSizeNotify()
{
mutex = SDL_CreateMutex();
w = h = 0;
}
~WindowSizeNotify()
{ {
SDL_DestroyMutex(mutex); SDL_DestroyMutex(mutex);
} }
/* Done from the sending side */ /* Done from the sending side */
void notifyChange(int w, int h) void post(const T &value)
{ {
SDL_LockMutex(mutex); SDL_LockMutex(mutex);
this->w = w;
this->h = h;
changed.set(); changed.set();
current = value;
SDL_UnlockMutex(mutex); SDL_UnlockMutex(mutex);
} }
/* Done from the receiving side */ /* Done from the receiving side */
bool pollChange(int *w, int *h) bool poll(T &out) const
{ {
if (!changed) if (!changed)
return false; return false;
SDL_LockMutex(mutex); SDL_LockMutex(mutex);
*w = this->w; out = current;
*h = this->h;
changed.clear(); changed.clear();
SDL_UnlockMutex(mutex); SDL_UnlockMutex(mutex);
return true; return true;
} }
};
struct BindingNotify /* Done from either */
void get(T &out) const
{ {
BindingNotify() SDL_LockMutex(mutex);
{ out = current;
mut = SDL_CreateMutex(); SDL_UnlockMutex(mutex);
}
~BindingNotify()
{
SDL_DestroyMutex(mut);
}
bool poll(BDescVec &out) const
{
if (!changed)
return false;
SDL_LockMutex(mut);
out = data;
changed.clear();
SDL_UnlockMutex(mut);
return true;
}
void get(BDescVec &out) const
{
SDL_LockMutex(mut);
out = data;
SDL_UnlockMutex(mut);
}
void post(const BDescVec &d)
{
SDL_LockMutex(mut);
changed.set();
data = d;
SDL_UnlockMutex(mut);
} }
private: private:
SDL_mutex *mut; SDL_mutex *mutex;
BDescVec data;
mutable AtomicFlag changed; mutable AtomicFlag changed;
T current;
}; };
struct RGSSThreadData struct RGSSThreadData
@ -220,8 +177,8 @@ struct RGSSThreadData
AtomicFlag rqResetFinish; AtomicFlag rqResetFinish;
EventThread *ethread; EventThread *ethread;
WindowSizeNotify windowSizeMsg; UnidirMessage<Vec2i> windowSizeMsg;
BindingNotify bindingUpdateMsg; UnidirMessage<BDescVec> bindingUpdateMsg;
const char *argv0; const char *argv0;

View File

@ -558,7 +558,7 @@ struct GraphicsPrivate
void checkResize() void checkResize()
{ {
if (threadData->windowSizeMsg.pollChange(&winSize.x, &winSize.y)) if (threadData->windowSizeMsg.poll(winSize))
{ {
/* some GL drivers change the viewport on window resize */ /* some GL drivers change the viewport on window resize */
glState.viewport.refresh(); glState.viewport.refresh();

View File

@ -289,7 +289,7 @@ int main(int argc, char *argv[])
int winW, winH; int winW, winH;
SDL_GetWindowSize(win, &winW, &winH); SDL_GetWindowSize(win, &winW, &winH);
rtData.windowSizeMsg.notifyChange(winW, winH); rtData.windowSizeMsg.post(Vec2i(winW, winH));
/* Load and post key bindings */ /* Load and post key bindings */
rtData.bindingUpdateMsg.post(loadBindings(conf)); rtData.bindingUpdateMsg.post(loadBindings(conf));