mkxp/src/eventthread.cpp

497 lines
9.8 KiB
C++
Raw Normal View History

2013-09-01 14:27:21 +00:00
/*
** eventthread.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "eventthread.h"
#include <SDL_events.h>
#include <SDL_joystick.h>
#include <SDL_messagebox.h>
#include <SDL_timer.h>
#include <SDL_thread.h>
2013-09-01 14:27:21 +00:00
#include "sharedstate.h"
2013-09-01 14:27:21 +00:00
#include "graphics.h"
#include "settingsmenu.h"
#include "debugwriter.h"
2013-09-01 14:27:21 +00:00
#include <string.h>
2013-09-01 14:27:21 +00:00
uint8_t EventThread::keyStates[] = { false };
2013-09-01 14:27:21 +00:00
EventThread::JoyState EventThread::joyState =
{
2014-12-31 15:39:28 +00:00
{ 0 }, { 0 }, { false }
2013-09-01 14:27:21 +00:00
};
EventThread::MouseState EventThread::mouseState =
{
0, 0, false, { false }
2013-09-01 14:27:21 +00:00
};
/* User event codes */
2013-09-09 19:22:31 +00:00
enum
{
REQUEST_SETFULLSCREEN = 0,
2013-09-09 19:22:31 +00:00
REQUEST_WINRESIZE,
REQUEST_MESSAGEBOX,
REQUEST_SETCURSORVISIBLE,
UPDATE_FPS,
EVENT_COUNT
2013-09-09 19:22:31 +00:00
};
2013-09-01 14:27:21 +00:00
static uint32_t usrIdStart;
bool EventThread::allocUserEvents()
{
usrIdStart = SDL_RegisterEvents(EVENT_COUNT);
if (usrIdStart == (uint32_t) -1)
return false;
return true;
}
2013-09-01 14:27:21 +00:00
EventThread::EventThread()
: fullscreen(false),
showCursor(false)
2013-09-01 14:27:21 +00:00
{}
void EventThread::process(RGSSThreadData &rtData)
{
SDL_Event event;
SDL_Window *win = rtData.window;
WindowSizeNotify &windowSizeMsg = rtData.windowSizeMsg;
fullscreen = rtData.config.fullscreen;
int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
2013-09-01 14:27:21 +00:00
fps.lastFrame = SDL_GetPerformanceCounter();
fps.displayCounter = 0;
fps.displaying = false;
fps.immInitFlag = false;
fps.immFiniFlag = false;
fps.acc = 0;
fps.accDiv = 0;
bool cursorInWindow = false;
bool windowFocused = false;
2013-09-01 14:27:21 +00:00
bool terminate = false;
SDL_Joystick *js = 0;
if (SDL_NumJoysticks() > 0)
js = SDL_JoystickOpen(0);
char buffer[128];
char pendingTitle[128];
bool havePendingTitle = false;
bool resetting = false;
SettingsMenu *sMenu = 0;
2013-09-01 14:27:21 +00:00
while (true)
{
if (!SDL_WaitEvent(&event))
{
Debug() << "EventThread: Event error";
2013-09-01 14:27:21 +00:00
break;
}
if (sMenu && sMenu->onEvent(event))
{
if (sMenu->destroyReq())
{
delete sMenu;
sMenu = 0;
updateCursorState(cursorInWindow && windowFocused);
}
continue;
}
2013-09-01 14:27:21 +00:00
switch (event.type)
{
case SDL_WINDOWEVENT :
switch (event.window.event)
{
2014-08-22 21:54:26 +00:00
case SDL_WINDOWEVENT_SIZE_CHANGED :
2013-09-01 14:27:21 +00:00
windowSizeMsg.notifyChange(event.window.data1,
event.window.data2);
break;
case SDL_WINDOWEVENT_ENTER :
cursorInWindow = true;
mouseState.inWindow = true;
updateCursorState(cursorInWindow && windowFocused && !sMenu);
2014-01-01 11:56:45 +00:00
break;
case SDL_WINDOWEVENT_LEAVE :
cursorInWindow = false;
mouseState.inWindow = false;
updateCursorState(cursorInWindow && windowFocused && !sMenu);
2014-01-01 11:56:45 +00:00
break;
2013-09-01 14:27:21 +00:00
case SDL_WINDOWEVENT_CLOSE :
terminate = true;
2014-01-01 11:56:45 +00:00
2013-09-01 14:27:21 +00:00
break;
case SDL_WINDOWEVENT_FOCUS_GAINED :
windowFocused = true;
updateCursorState(cursorInWindow && windowFocused && !sMenu);
2014-01-01 11:56:45 +00:00
break;
2013-09-01 14:27:21 +00:00
case SDL_WINDOWEVENT_FOCUS_LOST :
windowFocused = false;
updateCursorState(cursorInWindow && windowFocused && !sMenu);
resetInputStates();
2014-01-01 11:56:45 +00:00
2013-09-01 14:27:21 +00:00
break;
}
break;
case SDL_QUIT :
terminate = true;
Debug() << "EventThread termination requested";
2014-01-01 11:56:45 +00:00
2013-09-01 14:27:21 +00:00
break;
case SDL_KEYDOWN :
if (event.key.keysym.scancode == SDL_SCANCODE_RETURN &&
(event.key.keysym.mod & toggleFSMod))
2013-09-01 14:27:21 +00:00
{
setFullscreen(win, !fullscreen);
if (!fullscreen && havePendingTitle)
{
SDL_SetWindowTitle(win, pendingTitle);
pendingTitle[0] = '\0';
havePendingTitle = false;
}
break;
}
if (event.key.keysym.scancode == SDL_SCANCODE_F1)
{
if (!sMenu)
{
sMenu = new SettingsMenu(rtData);
updateCursorState(false);
}
sMenu->raise();
}
if (event.key.keysym.scancode == SDL_SCANCODE_F2)
{
if (!fps.displaying)
{
fps.immInitFlag = true;
fps.displaying = true;
}
else
{
fps.displaying = false;
if (fullscreen)
{
/* Prevent fullscreen flicker */
strncpy(pendingTitle, rtData.config.game.title.c_str(),
sizeof(pendingTitle));
havePendingTitle = true;
2014-01-01 11:56:45 +00:00
break;
}
SDL_SetWindowTitle(win, rtData.config.game.title.c_str());
}
2013-09-01 14:27:21 +00:00
break;
}
if (event.key.keysym.scancode == SDL_SCANCODE_F12)
{
if (!rtData.config.enableReset)
break;
if (resetting)
break;
resetting = true;
rtData.rqResetFinish.clear();
rtData.rqReset.set();
break;
}
2013-09-01 14:27:21 +00:00
keyStates[event.key.keysym.scancode] = true;
break;
case SDL_KEYUP :
if (event.key.keysym.scancode == SDL_SCANCODE_F12)
{
if (!rtData.config.enableReset)
break;
resetting = false;
rtData.rqResetFinish.set();
break;
}
2013-09-01 14:27:21 +00:00
keyStates[event.key.keysym.scancode] = false;
break;
case SDL_JOYBUTTONDOWN :
joyState.buttons[event.jbutton.button] = true;
break;
case SDL_JOYBUTTONUP :
joyState.buttons[event.jbutton.button] = false;
break;
2014-12-31 15:39:28 +00:00
case SDL_JOYHATMOTION :
joyState.hats[event.jhat.hat] = event.jhat.value;
break;
2013-09-01 14:27:21 +00:00
case SDL_JOYAXISMOTION :
2014-12-31 15:39:28 +00:00
joyState.axes[event.jaxis.axis] = event.jaxis.value;
2013-09-01 14:27:21 +00:00
break;
case SDL_JOYDEVICEADDED :
if (event.jdevice.which > 0)
break;
js = SDL_JoystickOpen(0);
break;
case SDL_JOYDEVICEREMOVED :
resetInputStates();
2013-09-01 14:27:21 +00:00
break;
case SDL_MOUSEBUTTONDOWN :
mouseState.buttons[event.button.button] = true;
break;
case SDL_MOUSEBUTTONUP :
mouseState.buttons[event.button.button] = false;
break;
case SDL_MOUSEMOTION :
mouseState.x = event.motion.x;
mouseState.y = event.motion.y;
2014-01-01 11:56:45 +00:00
2013-09-01 14:27:21 +00:00
break;
default :
/* Handle user events */
switch(event.type - usrIdStart)
{
case REQUEST_SETFULLSCREEN :
setFullscreen(win, static_cast<bool>(event.user.code));
break;
case REQUEST_WINRESIZE :
SDL_SetWindowSize(win, event.window.data1, event.window.data2);
break;
case REQUEST_MESSAGEBOX :
SDL_ShowSimpleMessageBox(event.user.code,
rtData.config.game.title.c_str(),
(const char*) event.user.data1, win);
free(event.user.data1);
msgBoxDone.set();
break;
case REQUEST_SETCURSORVISIBLE :
showCursor = event.user.code;
updateCursorState(cursorInWindow);
break;
case UPDATE_FPS :
if (!fps.displaying)
break;
snprintf(buffer, sizeof(buffer), "%s - %d FPS",
rtData.config.game.title.c_str(), event.user.code);
/* Updating the window title in fullscreen
* mode seems to cause flickering */
if (fullscreen)
{
strncpy(pendingTitle, buffer, sizeof(pendingTitle));
havePendingTitle = true;
break;
}
SDL_SetWindowTitle(win, buffer);
break;
}
2013-09-01 14:27:21 +00:00
}
if (terminate)
break;
}
if (SDL_JoystickGetAttached(js))
SDL_JoystickClose(js);
delete sMenu;
2013-09-01 14:27:21 +00:00
}
void EventThread::cleanup()
{
SDL_Event event;
2014-01-04 12:49:08 +00:00
2013-09-01 14:27:21 +00:00
while (SDL_PollEvent(&event))
if ((event.type - usrIdStart) == REQUEST_MESSAGEBOX)
2013-09-01 14:27:21 +00:00
free(event.user.data1);
}
void EventThread::resetInputStates()
2013-09-01 14:27:21 +00:00
{
memset(&keyStates, 0, sizeof(keyStates));
memset(&joyState, 0, sizeof(joyState));
memset(&mouseState.buttons, 0, sizeof(mouseState.buttons));
}
void EventThread::setFullscreen(SDL_Window *win, bool mode)
{
SDL_SetWindowFullscreen
(win, mode ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
fullscreen = mode;
}
void EventThread::updateCursorState(bool inWindow)
{
if (inWindow)
SDL_ShowCursor(showCursor ? SDL_TRUE : SDL_FALSE);
else
SDL_ShowCursor(SDL_TRUE);
}
2013-09-01 14:27:21 +00:00
void EventThread::requestTerminate()
{
SDL_Event event;
event.type = SDL_QUIT;
2013-09-01 14:27:21 +00:00
SDL_PushEvent(&event);
}
void EventThread::requestFullscreenMode(bool mode)
{
if (mode == fullscreen)
return;
SDL_Event event;
event.type = usrIdStart + REQUEST_SETFULLSCREEN;
2013-09-01 14:27:21 +00:00
event.user.code = static_cast<Sint32>(mode);
SDL_PushEvent(&event);
}
void EventThread::requestWindowResize(int width, int height)
{
SDL_Event event;
event.type = usrIdStart + REQUEST_WINRESIZE;
2013-09-01 14:27:21 +00:00
event.window.data1 = width;
event.window.data2 = height;
SDL_PushEvent(&event);
}
void EventThread::requestShowCursor(bool mode)
{
SDL_Event event;
event.type = usrIdStart + REQUEST_SETCURSORVISIBLE;
event.user.code = mode;
SDL_PushEvent(&event);
}
2013-09-01 14:27:21 +00:00
void EventThread::showMessageBox(const char *body, int flags)
{
msgBoxDone.clear();
2013-09-01 14:27:21 +00:00
SDL_Event event;
event.user.code = flags;
event.user.data1 = strdup(body);
event.type = usrIdStart + REQUEST_MESSAGEBOX;
2013-09-01 14:27:21 +00:00
SDL_PushEvent(&event);
/* Keep repainting screen while box is open */
shState->graphics().repaintWait(msgBoxDone);
2013-09-01 14:27:21 +00:00
/* Prevent endless loops */
resetInputStates();
2013-09-01 14:27:21 +00:00
}
bool EventThread::getFullscreen() const
2013-09-01 14:27:21 +00:00
{
return fullscreen;
}
bool EventThread::getShowCursor() const
{
return showCursor;
}
void EventThread::notifyFrame()
{
if (!fps.displaying)
return;
uint64_t current = SDL_GetPerformanceCounter();
uint64_t diff = current - fps.lastFrame;
fps.lastFrame = current;
if (fps.immInitFlag)
{
fps.immInitFlag = false;
fps.immFiniFlag = true;
2014-01-04 12:49:08 +00:00
return;
}
static uint64_t freq = SDL_GetPerformanceFrequency();
double currFPS = (double) freq / diff;
fps.acc += currFPS;
++fps.accDiv;
fps.displayCounter += diff;
if (fps.displayCounter < freq && !fps.immFiniFlag)
return;
fps.displayCounter = 0;
fps.immFiniFlag = false;
int32_t avgFPS = fps.acc / fps.accDiv;
fps.acc = fps.accDiv = 0;
SDL_Event event;
event.user.code = avgFPS;
event.user.type = usrIdStart + UPDATE_FPS;
SDL_PushEvent(&event);
}