forked from od-contrib/commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.cpp
executable file
·366 lines (338 loc) · 11.9 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "window.h"
#include <cstdint>
#include <iostream>
#include "axis_direction.h"
#include "def.h"
#include "resourceManager.h"
#include "screen.h"
#include "sdlutils.h"
#define KEYHOLD_TIMER_FIRST 12
#define KEYHOLD_TIMER 3
CWindow::CWindow(void)
: m_keyHoldCountdown(0)
, m_controllerButtonCountdown(0)
, m_lastPressed(SDLK_0)
, m_retVal(0)
{
// Add window to the lists for render
Globals::g_windows.push_back(this);
}
CWindow::~CWindow(void)
{
// Remove last window
Globals::g_windows.pop_back();
}
namespace
{
std::uint32_t frameDeadline = 0;
// Limit FPS to avoid high CPU load, use when v-sync isn't available
void LimitFrameRate()
{
const int refreshDelay = 1000000 / screen.refreshRate;
std::uint32_t tc = SDL_GetTicks() * 1000;
std::uint32_t v = 0;
if (frameDeadline > tc)
{
v = tc % refreshDelay;
SDL_Delay(v / 1000 + 1); // ceil
}
frameDeadline = tc + v + refreshDelay;
}
void ResetFrameDeadline() {
frameDeadline = 0;
}
} // namespace
int CWindow::execute()
{
#ifdef USE_SDL2
const bool text_input_was_active = SDL_IsTextInputActive();
SDL_StopTextInput();
if (handlesTextInput()) SDL_StartTextInput();
#endif
m_retVal = 0;
SDL_Event event;
bool l_loop(true);
bool l_render(true);
static AxisDirectionRepeater axisDirectionRepeater;
static AxisDirection axisDirection;
// Main loop
while (l_loop)
{
// Handle key press
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_MOUSEMOTION:
SDL_utils::setMouseCursorEnabled(true);
break;
case SDL_MOUSEBUTTONDOWN:
SDL_utils::setMouseCursorEnabled(true);
switch (event.button.button) {
#ifndef USE_SDL2
case SDL_BUTTON_WHEELUP:
l_render = mouseWheel(0, 1) || l_render;
break;
case SDL_BUTTON_WHEELDOWN:
l_render = mouseWheel(0, -1) || l_render;
break;
#endif
default:
l_render = this->mouseDown(event.button.button,
event.button.x, event.button.y)
|| l_render;
}
if (m_retVal) l_loop = false;
break;
case SDL_KEYDOWN: {
SDL_utils::setMouseCursorEnabled(false);
if (handleZoomTrigger(event)) {
l_render = true;
break;
}
l_render = this->keyPress(event, event.key.keysym.sym,
ControllerButton::NONE)
|| l_render;
if (m_retVal) l_loop = false;
break;
}
case SDL_QUIT: return m_retVal;
#ifdef USE_SDL2
case SDL_CONTROLLERDEVICEADDED:
SDL_GameControllerOpen(event.cdevice.which);
break;
case SDL_CONTROLLERAXISMOTION:
case SDL_CONTROLLERBUTTONDOWN: {
bool thumbStickEvent = false;
if (event.type == SDL_CONTROLLERAXISMOTION) {
constexpr int AxisDeadzoneX = 16000;
constexpr int AxisDeadzoneY = 4000;
switch (event.caxis.axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
case SDL_CONTROLLER_AXIS_RIGHTX: {
thumbStickEvent = true;
if (event.caxis.value < -AxisDeadzoneX
|| event.caxis.value > AxisDeadzoneX) {
axisDirection.x = event.caxis.value > 0
? AxisDirectionX::RIGHT
: AxisDirectionX::LEFT;
} else {
axisDirection.x = AxisDirectionX::NONE;
}
} break;
case SDL_CONTROLLER_AXIS_LEFTY:
case SDL_CONTROLLER_AXIS_RIGHTY: {
thumbStickEvent = true;
if (event.caxis.value < -AxisDeadzoneY
|| event.caxis.value > AxisDeadzoneY) {
axisDirection.y = event.caxis.value > 0
? AxisDirectionY::DOWN
: AxisDirectionY::UP;
} else {
axisDirection.y = AxisDirectionY::NONE;
}
} break;
default: break;
}
}
if (!thumbStickEvent) {
const ControllerButton button
= ControllerButtonFromSdlEvent(event);
SDL_utils::setMouseCursorEnabled(false);
l_render = this->keyPress(event, SDLK_UNKNOWN, button)
|| l_render;
if (m_retVal) l_loop = false;
}
break;
}
case SDL_TEXTINPUT:
case SDL_TEXTEDITING:
l_render = textInput(event) || l_render;
break;
case SDL_MOUSEWHEEL:
SDL_utils::setMouseCursorEnabled(true);
l_render
= mouseWheel(event.wheel.x, event.wheel.y) || l_render;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_EXPOSED:
l_render = true;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
l_render = true;
ResetFrameDeadline();
screen.onResize(
event.window.data1, event.window.data2);
triggerOnResize();
break;
}
break;
#else
case SDL_VIDEORESIZE:
l_render = true;
ResetFrameDeadline();
screen.onResize(event.resize.w, event.resize.h);
triggerOnResize();
break;
#endif
}
}
// Handle key hold
if (!l_loop) break;
#if SDL_VERSION_ATLEAST(2, 0, 0)
const int num_joysticks = SDL_NumJoysticks();
for (int i = 0; i < num_joysticks; ++i) {
if (!SDL_IsGameController(i)) continue;
SDL_GameController *controller = SDL_GameControllerFromInstanceID(
SDL_JoystickGetDeviceInstanceID(i));
if (controller == nullptr) continue;
l_render = this->gamepadHold(controller) || l_render;
}
// Thumb stick movement.
AxisDirection dir = axisDirectionRepeater.Get(axisDirection);
if (dir.x != AxisDirectionX::NONE) {
l_render
= this->keyPress(event, SDLK_UNKNOWN,
dir.x == AxisDirectionX::LEFT ? ControllerButton::LEFT
: ControllerButton::RIGHT)
|| l_render;
}
if (dir.y != AxisDirectionY::NONE) {
l_render = this->keyPress(event, SDLK_UNKNOWN,
dir.y == AxisDirectionY::UP ? ControllerButton::UP
: ControllerButton::DOWN)
|| l_render;
}
#endif
l_render = this->keyHold() || l_render;
// Render if necessary
if (l_render)
{
SDL_utils::renderAll();
screen.flip();
l_render = false;
}
LimitFrameRate();
}
#ifdef USE_SDL2
SDL_StopTextInput();
if (text_input_was_active) SDL_StartTextInput();
#endif
// -1 is used to signal cancellation but we must return 0 in that case.
if (m_retVal == -1) m_retVal = 0;
return m_retVal;
}
bool CWindow::handleZoomTrigger(const SDL_Event &event)
{
if (event.type != SDL_KEYDOWN) return false;
const auto sym = event.key.keysym.sym;
// Zoom on CTRL +/-
if ((SDL_GetModState() & KMOD_CTRL) == 0) return false;
float factor;
switch (sym) {
case SDLK_PLUS:
case SDLK_KP_PLUS: factor = 1.1f; break;
case SDLK_MINUS:
case SDLK_KP_MINUS: factor = 1 / 1.1f; break;
default: return false;
}
screen.zoom(factor);
triggerOnResize();
return true;
}
void CWindow::triggerOnResize() {
CResourceManager::instance().onResize();
for (auto *window : Globals::g_windows) window->onResize();
}
bool CWindow::keyPress(
const SDL_Event &event, SDLC_Keycode key, ControllerButton button)
{
// Reset timer if running
if (m_keyHoldCountdown) m_keyHoldCountdown = 0;
if (key != SDLK_UNKNOWN) m_lastPressed = key;
if (button != ControllerButton::NONE) {
m_controllerButtonCountdown = 0;
m_lastPressedButton = button;
}
return false;
}
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool CWindow::keyHold() { return false; }
#if SDL_VERSION_ATLEAST(2, 0, 0)
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool CWindow::gamepadHold([[maybe_unused]] SDL_GameController *controller)
{
return false;
}
#endif
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool CWindow::textInput([[maybe_unused]] const SDL_Event &event)
{
return false;
}
void CWindow::onResize() { }
bool CWindow::tick(SDLC_Keycode keycode)
{
if (m_lastPressed != keycode) return false;
#if SDL_VERSION_ATLEAST(2, 0, 0)
const bool held
= SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(keycode)];
#else
const bool held = SDL_GetKeyState(NULL)[keycode];
#endif
if (held) {
if (m_keyHoldCountdown != 0) {
--m_keyHoldCountdown;
if (m_keyHoldCountdown == 0) {
// Timer continues
m_keyHoldCountdown = KEYHOLD_TIMER;
// Trigger!
return true;
}
} else {
// Start timer
m_keyHoldCountdown = KEYHOLD_TIMER_FIRST;
}
} else {
// Stop timer if running
if (m_keyHoldCountdown != 0) m_keyHoldCountdown = 0;
}
return false;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
bool CWindow::tick(SDL_GameController *controller, ControllerButton button)
{
if (controller == nullptr || m_lastPressedButton != button) return false;
if (IsControllerButtonDown(controller, button)) {
if (m_controllerButtonCountdown != 0) {
--m_controllerButtonCountdown;
if (m_controllerButtonCountdown == 0) {
// Timer continues
m_controllerButtonCountdown = KEYHOLD_TIMER;
// Trigger!
return true;
}
} else {
// Start timer
m_controllerButtonCountdown = KEYHOLD_TIMER_FIRST;
}
} else {
// Stop timer if running
if (m_controllerButtonCountdown != 0) m_controllerButtonCountdown = 0;
}
return false;
}
#endif
bool CWindow::mouseDown(int button, int x, int y) { return false; }
bool CWindow::mouseWheel(int dx, int dy) { return false; }
bool CWindow::isFullScreen(void) const
{
// Default behavior
return false;
}
bool CWindow::handlesTextInput() const
{
// Default behavior
return false;
}