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"
|
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_events.h>
|
|
|
|
#include <SDL_joystick.h>
|
|
|
|
#include <SDL_messagebox.h>
|
|
|
|
#include <SDL_timer.h>
|
|
|
|
#include <SDL_thread.h>
|
2015-01-03 18:18:52 +00:00
|
|
|
#include <SDL_touch.h>
|
2017-04-08 18:06:12 +00:00
|
|
|
#include <SDL_rect.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2015-05-25 23:40:53 +00:00
|
|
|
#include <al.h>
|
|
|
|
#include <alc.h>
|
2015-01-15 07:07:22 +00:00
|
|
|
#include <alext.h>
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "graphics.h"
|
2014-01-25 08:24:55 +00:00
|
|
|
#include "settingsmenu.h"
|
2015-01-15 07:07:22 +00:00
|
|
|
#include "al-util.h"
|
2013-12-11 19:46:54 +00:00
|
|
|
#include "debugwriter.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <string.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2015-01-15 07:07:22 +00:00
|
|
|
typedef void (ALC_APIENTRY *LPALCDEVICEPAUSESOFT) (ALCdevice *device);
|
|
|
|
typedef void (ALC_APIENTRY *LPALCDEVICERESUMESOFT) (ALCdevice *device);
|
|
|
|
|
|
|
|
#define AL_DEVICE_PAUSE_FUN \
|
|
|
|
AL_FUN(DevicePause, LPALCDEVICEPAUSESOFT) \
|
|
|
|
AL_FUN(DeviceResume, LPALCDEVICERESUMESOFT)
|
|
|
|
|
|
|
|
struct ALCFunctions
|
|
|
|
{
|
|
|
|
#define AL_FUN(name, type) type name;
|
|
|
|
AL_DEVICE_PAUSE_FUN
|
|
|
|
#undef AL_FUN
|
|
|
|
} static alc;
|
|
|
|
|
|
|
|
static void
|
|
|
|
initALCFunctions(ALCdevice *alcDev)
|
|
|
|
{
|
|
|
|
if (!strstr(alcGetString(alcDev, ALC_EXTENSIONS), "ALC_SOFT_pause_device"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Debug() << "ALC_SOFT_pause_device present";
|
|
|
|
|
|
|
|
#define AL_FUN(name, type) alc. name = (type) alcGetProcAddress(alcDev, "alc" #name "SOFT");
|
|
|
|
AL_DEVICE_PAUSE_FUN;
|
|
|
|
#undef AL_FUN
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HAVE_ALC_DEVICE_PAUSE alc.DevicePause
|
|
|
|
|
2015-01-03 18:18:52 +00:00
|
|
|
uint8_t EventThread::keyStates[];
|
|
|
|
EventThread::JoyState EventThread::joyState;
|
|
|
|
EventThread::MouseState EventThread::mouseState;
|
|
|
|
EventThread::TouchState EventThread::touchState;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-01-02 01:22:10 +00:00
|
|
|
/* User event codes */
|
2013-09-09 19:22:31 +00:00
|
|
|
enum
|
|
|
|
{
|
2014-08-06 13:54:44 +00:00
|
|
|
REQUEST_SETFULLSCREEN = 0,
|
2013-09-09 19:22:31 +00:00
|
|
|
REQUEST_WINRESIZE,
|
2013-09-24 20:52:42 +00:00
|
|
|
REQUEST_MESSAGEBOX,
|
2013-10-06 05:05:01 +00:00
|
|
|
REQUEST_SETCURSORVISIBLE,
|
|
|
|
|
2014-01-02 01:22:10 +00:00
|
|
|
UPDATE_FPS,
|
2017-04-08 14:16:36 +00:00
|
|
|
UPDATE_SCREEN_RECT,
|
2014-01-02 01:22:10 +00:00
|
|
|
|
|
|
|
EVENT_COUNT
|
2013-09-09 19:22:31 +00:00
|
|
|
};
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-06 13:54:44 +00:00
|
|
|
static uint32_t usrIdStart;
|
2014-01-02 01:22:10 +00:00
|
|
|
|
|
|
|
bool EventThread::allocUserEvents()
|
|
|
|
{
|
2014-08-06 13:54:44 +00:00
|
|
|
usrIdStart = SDL_RegisterEvents(EVENT_COUNT);
|
2014-01-02 01:22:10 +00:00
|
|
|
|
2014-08-06 13:54:44 +00:00
|
|
|
if (usrIdStart == (uint32_t) -1)
|
2014-01-02 01:22:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
EventThread::EventThread()
|
2013-09-24 20:52:42 +00:00
|
|
|
: fullscreen(false),
|
|
|
|
showCursor(false)
|
2013-09-01 14:27:21 +00:00
|
|
|
{}
|
|
|
|
|
2020-04-14 11:55:00 +00:00
|
|
|
SDL_DisplayMode dm = {0};
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void EventThread::process(RGSSThreadData &rtData)
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
SDL_Window *win = rtData.window;
|
2015-01-02 00:41:23 +00:00
|
|
|
UnidirMessage<Vec2i> &windowSizeMsg = rtData.windowSizeMsg;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2015-01-15 07:07:22 +00:00
|
|
|
initALCFunctions(rtData.alcDev);
|
2016-02-24 16:55:28 +00:00
|
|
|
|
|
|
|
// XXX this function breaks input focus on OSX
|
|
|
|
#ifndef __MACOSX__
|
2015-01-15 07:02:21 +00:00
|
|
|
SDL_SetEventFilter(eventFilter, &rtData);
|
2016-02-24 16:55:28 +00:00
|
|
|
#endif
|
2015-01-15 07:02:21 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
fullscreen = rtData.config.fullscreen;
|
2020-04-26 23:44:40 +00:00
|
|
|
showCursor = rtData.config.showCursor;
|
|
|
|
int defScreenW, defScreenH;
|
|
|
|
defScreenW = rtData.config.defScreenW;
|
|
|
|
defScreenH = rtData.config.defScreenH;
|
2014-01-01 20:56:38 +00:00
|
|
|
int toggleFSMod = rtData.config.anyAltToggleFS ? KMOD_ALT : KMOD_LALT;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
fps.lastFrame = SDL_GetPerformanceCounter();
|
2014-08-16 02:02:44 +00:00
|
|
|
fps.displayCounter = 0;
|
2013-10-06 05:05:01 +00:00
|
|
|
fps.acc = 0;
|
|
|
|
fps.accDiv = 0;
|
|
|
|
|
2014-12-24 05:23:28 +00:00
|
|
|
if (rtData.config.printFPS)
|
|
|
|
fps.sendUpdates.set();
|
|
|
|
|
|
|
|
bool displayingFPS = false;
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
bool cursorInWindow = false;
|
2017-04-08 14:16:36 +00:00
|
|
|
/* Will be updated eventually */
|
|
|
|
SDL_Rect gameScreen = { 0, 0, 0, 0 };
|
2016-02-24 16:44:42 +00:00
|
|
|
|
|
|
|
/* SDL doesn't send an initial FOCUS_GAINED event */
|
|
|
|
bool windowFocused = true;
|
2013-09-24 20:52:42 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
bool terminate = false;
|
|
|
|
|
|
|
|
SDL_Joystick *js = 0;
|
|
|
|
if (SDL_NumJoysticks() > 0)
|
|
|
|
js = SDL_JoystickOpen(0);
|
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
char buffer[128];
|
|
|
|
|
|
|
|
char pendingTitle[128];
|
|
|
|
bool havePendingTitle = false;
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
bool resetting = false;
|
|
|
|
|
2015-01-03 18:18:52 +00:00
|
|
|
int winW, winH;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
SDL_GetWindowSize(win, &winW, &winH);
|
|
|
|
|
2014-01-25 08:24:55 +00:00
|
|
|
SettingsMenu *sMenu = 0;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!SDL_WaitEvent(&event))
|
|
|
|
{
|
2013-12-11 19:46:54 +00:00
|
|
|
Debug() << "EventThread: Event error";
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:24:55 +00:00
|
|
|
if (sMenu && sMenu->onEvent(event))
|
|
|
|
{
|
|
|
|
if (sMenu->destroyReq())
|
|
|
|
{
|
|
|
|
delete sMenu;
|
|
|
|
sMenu = 0;
|
|
|
|
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow && windowFocused, gameScreen);
|
2014-01-25 08:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-03 19:48:31 +00:00
|
|
|
/* Preselect and discard unwanted events here */
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case SDL_MOUSEBUTTONDOWN :
|
|
|
|
case SDL_MOUSEBUTTONUP :
|
|
|
|
case SDL_MOUSEMOTION :
|
|
|
|
if (event.button.which == SDL_TOUCH_MOUSEID)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_FINGERDOWN :
|
|
|
|
case SDL_FINGERUP :
|
|
|
|
case SDL_FINGERMOTION :
|
|
|
|
if (event.tfinger.fingerId >= MAX_FINGERS)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now process the rest */
|
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 :
|
2015-01-03 18:18:52 +00:00
|
|
|
winW = event.window.data1;
|
|
|
|
winH = event.window.data2;
|
|
|
|
|
|
|
|
windowSizeMsg.post(Vec2i(winW, winH));
|
|
|
|
resetInputStates();
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
case SDL_WINDOWEVENT_ENTER :
|
|
|
|
cursorInWindow = true;
|
2014-01-05 00:15:33 +00:00
|
|
|
mouseState.inWindow = true;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow && windowFocused && !sMenu, gameScreen);
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_LEAVE :
|
|
|
|
cursorInWindow = false;
|
2014-01-05 00:15:33 +00:00
|
|
|
mouseState.inWindow = false;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow && windowFocused && !sMenu, gameScreen);
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-24 20:52:42 +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;
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED :
|
2013-09-25 18:35:00 +00:00
|
|
|
windowFocused = true;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow && windowFocused && !sMenu, gameScreen);
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
break;
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST :
|
2013-09-25 18:35:00 +00:00
|
|
|
windowFocused = false;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow && windowFocused && !sMenu, gameScreen);
|
2013-09-04 10:10:21 +00:00
|
|
|
resetInputStates();
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT :
|
|
|
|
terminate = true;
|
2013-12-11 19:46:54 +00:00
|
|
|
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 &&
|
2014-01-01 20:56:38 +00:00
|
|
|
(event.key.keysym.mod & toggleFSMod))
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
setFullscreen(win, !fullscreen);
|
2013-10-06 05:05:01 +00:00
|
|
|
if (!fullscreen && havePendingTitle)
|
|
|
|
{
|
|
|
|
SDL_SetWindowTitle(win, pendingTitle);
|
|
|
|
pendingTitle[0] = '\0';
|
|
|
|
havePendingTitle = false;
|
|
|
|
}
|
2020-04-26 23:44:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
SDL_SetWindowSize(win, defScreenW, defScreenH);
|
|
|
|
SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
|
|
}
|
2013-10-06 05:05:01 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-25 08:24:55 +00:00
|
|
|
if (event.key.keysym.scancode == SDL_SCANCODE_F1)
|
|
|
|
{
|
|
|
|
if (!sMenu)
|
|
|
|
{
|
|
|
|
sMenu = new SettingsMenu(rtData);
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(false, gameScreen);
|
2014-01-25 08:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sMenu->raise();
|
|
|
|
}
|
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
if (event.key.keysym.scancode == SDL_SCANCODE_F2)
|
|
|
|
{
|
2014-12-24 05:23:28 +00:00
|
|
|
if (!displayingFPS)
|
2013-10-06 05:05:01 +00:00
|
|
|
{
|
2014-12-24 05:23:28 +00:00
|
|
|
fps.immInitFlag.set();
|
|
|
|
fps.sendUpdates.set();
|
|
|
|
displayingFPS = true;
|
2013-10-06 05:05:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-24 05:23:28 +00:00
|
|
|
displayingFPS = false;
|
|
|
|
|
|
|
|
if (!rtData.config.printFPS)
|
|
|
|
fps.sendUpdates.clear();
|
2013-10-06 05:05:01 +00:00
|
|
|
|
|
|
|
if (fullscreen)
|
|
|
|
{
|
|
|
|
/* Prevent fullscreen flicker */
|
2017-12-12 16:57:02 +00:00
|
|
|
strncpy(pendingTitle, rtData.config.windowTitle.c_str(),
|
2013-10-06 05:05:01 +00:00
|
|
|
sizeof(pendingTitle));
|
|
|
|
havePendingTitle = true;
|
2014-01-01 11:56:45 +00:00
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:57:02 +00:00
|
|
|
SDL_SetWindowTitle(win, rtData.config.windowTitle.c_str());
|
2013-10-06 05:05:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-24 05:36:19 +00:00
|
|
|
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 :
|
2014-08-24 05:36:19 +00:00
|
|
|
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 :
|
2013-09-04 10:10:21 +00:00
|
|
|
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;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow, gameScreen);
|
2015-01-03 18:18:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_FINGERDOWN :
|
|
|
|
i = event.tfinger.fingerId;
|
|
|
|
touchState.fingers[i].down = true;
|
|
|
|
|
|
|
|
case SDL_FINGERMOTION :
|
|
|
|
i = event.tfinger.fingerId;
|
|
|
|
touchState.fingers[i].x = event.tfinger.x * winW;
|
|
|
|
touchState.fingers[i].y = event.tfinger.y * winH;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_FINGERUP :
|
|
|
|
i = event.tfinger.fingerId;
|
|
|
|
memset(&touchState.fingers[i], 0, sizeof(touchState.fingers[0]));
|
2013-09-01 14:27:21 +00:00
|
|
|
break;
|
2014-01-02 01:22:10 +00:00
|
|
|
|
|
|
|
default :
|
|
|
|
/* Handle user events */
|
2014-08-06 13:54:44 +00:00
|
|
|
switch(event.type - usrIdStart)
|
2014-01-02 01:22:10 +00:00
|
|
|
{
|
2014-08-06 13:54:44 +00:00
|
|
|
case REQUEST_SETFULLSCREEN :
|
2014-01-02 01:22:10 +00:00
|
|
|
setFullscreen(win, static_cast<bool>(event.user.code));
|
2014-08-06 13:54:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REQUEST_WINRESIZE :
|
2014-01-02 01:22:10 +00:00
|
|
|
SDL_SetWindowSize(win, event.window.data1, event.window.data2);
|
2014-08-06 13:54:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REQUEST_MESSAGEBOX :
|
2014-01-02 01:22:10 +00:00
|
|
|
SDL_ShowSimpleMessageBox(event.user.code,
|
2017-12-12 16:57:02 +00:00
|
|
|
rtData.config.windowTitle.c_str(),
|
2014-01-02 01:22:10 +00:00
|
|
|
(const char*) event.user.data1, win);
|
|
|
|
free(event.user.data1);
|
2014-08-24 05:36:19 +00:00
|
|
|
msgBoxDone.set();
|
2014-08-06 13:54:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REQUEST_SETCURSORVISIBLE :
|
2014-01-02 01:22:10 +00:00
|
|
|
showCursor = event.user.code;
|
2017-04-08 14:16:36 +00:00
|
|
|
updateCursorState(cursorInWindow, gameScreen);
|
2014-08-06 13:54:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UPDATE_FPS :
|
2014-12-24 05:23:28 +00:00
|
|
|
if (rtData.config.printFPS)
|
|
|
|
Debug() << "FPS:" << event.user.code;
|
|
|
|
|
|
|
|
if (!fps.sendUpdates)
|
2014-01-02 01:22:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s - %d FPS",
|
2017-12-12 16:57:02 +00:00
|
|
|
rtData.config.windowTitle.c_str(), event.user.code);
|
2014-01-02 01:22:10 +00:00
|
|
|
|
|
|
|
/* 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);
|
2014-08-06 13:54:44 +00:00
|
|
|
break;
|
2017-04-08 14:16:36 +00:00
|
|
|
|
|
|
|
case UPDATE_SCREEN_RECT :
|
|
|
|
gameScreen.x = event.user.windowID;
|
|
|
|
gameScreen.y = event.user.code;
|
|
|
|
gameScreen.w = reinterpret_cast<intptr_t>(event.user.data1);
|
|
|
|
gameScreen.h = reinterpret_cast<intptr_t>(event.user.data2);
|
|
|
|
updateCursorState(cursorInWindow, gameScreen);
|
|
|
|
|
|
|
|
break;
|
2014-01-02 01:22:10 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (terminate)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
/* Just in case */
|
|
|
|
rtData.syncPoint.resumeThreads();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (SDL_JoystickGetAttached(js))
|
|
|
|
SDL_JoystickClose(js);
|
2014-01-25 08:24:55 +00:00
|
|
|
|
|
|
|
delete sMenu;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
int EventThread::eventFilter(void *data, SDL_Event *event)
|
|
|
|
{
|
|
|
|
RGSSThreadData &rtData = *static_cast<RGSSThreadData*>(data);
|
|
|
|
|
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case SDL_APP_WILLENTERBACKGROUND :
|
|
|
|
Debug() << "SDL_APP_WILLENTERBACKGROUND";
|
|
|
|
|
2015-01-15 07:07:22 +00:00
|
|
|
if (HAVE_ALC_DEVICE_PAUSE)
|
|
|
|
alc.DevicePause(rtData.alcDev);
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
rtData.syncPoint.haltThreads();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDL_APP_DIDENTERBACKGROUND :
|
|
|
|
Debug() << "SDL_APP_DIDENTERBACKGROUND";
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDL_APP_WILLENTERFOREGROUND :
|
|
|
|
Debug() << "SDL_APP_WILLENTERFOREGROUND";
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDL_APP_DIDENTERFOREGROUND :
|
|
|
|
Debug() << "SDL_APP_DIDENTERFOREGROUND";
|
|
|
|
|
2015-01-15 07:07:22 +00:00
|
|
|
if (HAVE_ALC_DEVICE_PAUSE)
|
|
|
|
alc.DeviceResume(rtData.alcDev);
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
rtData.syncPoint.resumeThreads();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDL_APP_TERMINATING :
|
|
|
|
Debug() << "SDL_APP_TERMINATING";
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SDL_APP_LOWMEMORY :
|
|
|
|
Debug() << "SDL_APP_LOWMEMORY";
|
|
|
|
return 0;
|
|
|
|
|
2015-07-26 21:56:50 +00:00
|
|
|
// case SDL_RENDER_TARGETS_RESET :
|
|
|
|
// Debug() << "****** SDL_RENDER_TARGETS_RESET";
|
|
|
|
// return 0;
|
2015-01-15 07:02:21 +00:00
|
|
|
|
2015-07-26 21:56:50 +00:00
|
|
|
// case SDL_RENDER_DEVICE_RESET :
|
|
|
|
// Debug() << "****** SDL_RENDER_DEVICE_RESET";
|
|
|
|
// return 0;
|
2015-01-15 07:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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))
|
2014-08-06 13:54:44 +00:00
|
|
|
if ((event.type - usrIdStart) == REQUEST_MESSAGEBOX)
|
2013-09-01 14:27:21 +00:00
|
|
|
free(event.user.data1);
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:10:21 +00:00
|
|
|
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));
|
2015-01-03 18:18:52 +00:00
|
|
|
memset(&touchState, 0, sizeof(touchState));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::setFullscreen(SDL_Window *win, bool mode)
|
|
|
|
{
|
2020-04-14 11:55:00 +00:00
|
|
|
SDL_GetDesktopDisplayMode(0, &dm);
|
|
|
|
SDL_SetWindowSize(win, dm.w, dm.h);
|
|
|
|
SDL_WINDOWEVENT_SIZE_CHANGED;
|
2013-09-01 14:27:21 +00:00
|
|
|
SDL_SetWindowFullscreen
|
|
|
|
(win, mode ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
|
|
|
|
fullscreen = mode;
|
|
|
|
}
|
|
|
|
|
2017-04-08 14:16:36 +00:00
|
|
|
void EventThread::updateCursorState(bool inWindow,
|
|
|
|
const SDL_Rect &screen)
|
2013-09-24 20:52:42 +00:00
|
|
|
{
|
2017-04-08 14:16:36 +00:00
|
|
|
SDL_Point pos = { mouseState.x, mouseState.y };
|
|
|
|
bool inScreen = inWindow && SDL_PointInRect(&pos, &screen);
|
|
|
|
|
|
|
|
if (inScreen)
|
2013-09-24 20:52:42 +00:00
|
|
|
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;
|
2014-08-06 13:54:44 +00:00
|
|
|
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;
|
2014-08-06 13:54:44 +00:00
|
|
|
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;
|
2014-08-06 13:54:44 +00:00
|
|
|
event.type = usrIdStart + REQUEST_WINRESIZE;
|
2013-09-01 14:27:21 +00:00
|
|
|
event.window.data1 = width;
|
|
|
|
event.window.data2 = height;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
void EventThread::requestShowCursor(bool mode)
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
2014-08-06 13:54:44 +00:00
|
|
|
event.type = usrIdStart + REQUEST_SETCURSORVISIBLE;
|
2013-09-24 20:52:42 +00:00
|
|
|
event.user.code = mode;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void EventThread::showMessageBox(const char *body, int flags)
|
|
|
|
{
|
2014-08-24 05:36:19 +00:00
|
|
|
msgBoxDone.clear();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
event.user.code = flags;
|
|
|
|
event.user.data1 = strdup(body);
|
2014-08-06 13:54:44 +00:00
|
|
|
event.type = usrIdStart + REQUEST_MESSAGEBOX;
|
2013-09-01 14:27:21 +00:00
|
|
|
SDL_PushEvent(&event);
|
|
|
|
|
|
|
|
/* Keep repainting screen while box is open */
|
2014-08-24 05:36:19 +00:00
|
|
|
shState->graphics().repaintWait(msgBoxDone);
|
2013-09-01 14:27:21 +00:00
|
|
|
/* Prevent endless loops */
|
2013-09-04 10:10:21 +00:00
|
|
|
resetInputStates();
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 20:52:42 +00:00
|
|
|
bool EventThread::getFullscreen() const
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
return fullscreen;
|
|
|
|
}
|
2013-09-24 20:52:42 +00:00
|
|
|
|
|
|
|
bool EventThread::getShowCursor() const
|
|
|
|
{
|
|
|
|
return showCursor;
|
|
|
|
}
|
2013-10-06 05:05:01 +00:00
|
|
|
|
|
|
|
void EventThread::notifyFrame()
|
|
|
|
{
|
2014-12-24 05:23:28 +00:00
|
|
|
if (!fps.sendUpdates)
|
2013-10-06 05:05:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
uint64_t current = SDL_GetPerformanceCounter();
|
|
|
|
uint64_t diff = current - fps.lastFrame;
|
|
|
|
fps.lastFrame = current;
|
|
|
|
|
|
|
|
if (fps.immInitFlag)
|
|
|
|
{
|
2014-12-24 05:23:28 +00:00
|
|
|
fps.immInitFlag.clear();
|
|
|
|
fps.immFiniFlag.set();
|
2014-01-04 12:49:08 +00:00
|
|
|
|
2013-10-06 05:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t freq = SDL_GetPerformanceFrequency();
|
|
|
|
|
2013-10-06 12:11:50 +00:00
|
|
|
double currFPS = (double) freq / diff;
|
2013-10-06 05:05:01 +00:00
|
|
|
fps.acc += currFPS;
|
|
|
|
++fps.accDiv;
|
|
|
|
|
|
|
|
fps.displayCounter += diff;
|
|
|
|
if (fps.displayCounter < freq && !fps.immFiniFlag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fps.displayCounter = 0;
|
2014-12-24 05:23:28 +00:00
|
|
|
fps.immFiniFlag.clear();
|
2013-10-06 05:05:01 +00:00
|
|
|
|
|
|
|
int32_t avgFPS = fps.acc / fps.accDiv;
|
|
|
|
fps.acc = fps.accDiv = 0;
|
|
|
|
|
|
|
|
SDL_Event event;
|
|
|
|
event.user.code = avgFPS;
|
2014-08-06 13:54:44 +00:00
|
|
|
event.user.type = usrIdStart + UPDATE_FPS;
|
2013-10-06 05:05:01 +00:00
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
2015-01-15 07:02:21 +00:00
|
|
|
|
2017-04-08 14:16:36 +00:00
|
|
|
void EventThread::notifyGameScreenChange(const SDL_Rect &screen)
|
|
|
|
{
|
|
|
|
/* We have to get a bit hacky here to fit the rectangle
|
|
|
|
* data into the user event struct */
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = usrIdStart + UPDATE_SCREEN_RECT;
|
|
|
|
event.user.windowID = screen.x;
|
|
|
|
event.user.code = screen.y;
|
|
|
|
event.user.data1 = reinterpret_cast<void*>(screen.w);
|
|
|
|
event.user.data2 = reinterpret_cast<void*>(screen.h);
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2015-01-15 07:02:21 +00:00
|
|
|
void SyncPoint::haltThreads()
|
|
|
|
{
|
|
|
|
if (mainSync.locked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Lock the reply sync first to avoid races */
|
|
|
|
reply.lock();
|
|
|
|
|
|
|
|
/* Lock main sync and sleep until RGSS thread
|
|
|
|
* reports back */
|
|
|
|
mainSync.lock();
|
|
|
|
reply.waitForUnlock();
|
|
|
|
|
|
|
|
/* Now that the RGSS thread is asleep, we can
|
|
|
|
* safely put the other threads to sleep as well
|
|
|
|
* without causing deadlocks */
|
|
|
|
secondSync.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::resumeThreads()
|
|
|
|
{
|
|
|
|
if (!mainSync.locked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mainSync.unlock(false);
|
|
|
|
secondSync.unlock(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SyncPoint::mainSyncLocked()
|
|
|
|
{
|
|
|
|
return mainSync.locked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::waitMainSync()
|
|
|
|
{
|
|
|
|
reply.unlock(false);
|
|
|
|
mainSync.waitForUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::passSecondarySync()
|
|
|
|
{
|
|
|
|
if (!secondSync.locked)
|
|
|
|
return;
|
|
|
|
|
|
|
|
secondSync.waitForUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncPoint::Util::Util()
|
|
|
|
{
|
|
|
|
mut = SDL_CreateMutex();
|
|
|
|
cond = SDL_CreateCond();
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncPoint::Util::~Util()
|
|
|
|
{
|
|
|
|
SDL_DestroyCond(cond);
|
|
|
|
SDL_DestroyMutex(mut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::Util::lock()
|
|
|
|
{
|
|
|
|
locked.set();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::Util::unlock(bool multi)
|
|
|
|
{
|
|
|
|
locked.clear();
|
|
|
|
|
|
|
|
if (multi)
|
|
|
|
SDL_CondBroadcast(cond);
|
|
|
|
else
|
|
|
|
SDL_CondSignal(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncPoint::Util::waitForUnlock()
|
|
|
|
{
|
|
|
|
SDL_LockMutex(mut);
|
|
|
|
|
|
|
|
while (locked)
|
|
|
|
SDL_CondWait(cond, mut);
|
|
|
|
|
|
|
|
SDL_UnlockMutex(mut);
|
|
|
|
}
|