/
main_sdl.cpp
2287 lines (2025 loc) · 71.5 KB
/
main_sdl.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Warzone 2100.
Copyright (C) 2013-2017 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file main_sdl.cpp
*
* SDL backend code
*/
// Get platform defines before checking for them!
#include "lib/framework/wzapp.h"
#include <QtWidgets/QApplication>
// This is for the cross-compiler, for static QT 5 builds to avoid the 'plugins' crap on windows
#if defined(QT_STATICPLUGIN)
#include <QtCore/QtPlugin>
#endif
#include "lib/framework/input.h"
#include "lib/framework/utf.h"
#include "lib/framework/opengl.h"
#include "lib/ivis_opengl/pieclip.h"
#include "lib/ivis_opengl/piemode.h"
#include "lib/ivis_opengl/screen.h"
#include "lib/gamelib/gtime.h"
#include "src/warzoneconfig.h"
#include "src/game.h"
#include <SDL.h>
#include <SDL_thread.h>
#include <SDL_clipboard.h>
#include "wz2100icon.h"
#include "cursors_sdl.h"
#include <algorithm>
#include <map>
#include <locale.h>
#include <atomic>
// This is for the cross-compiler, for static QT 5 builds to avoid the 'plugins' crap on windows
#if defined(QT_STATICPLUGIN)
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
#endif
void mainLoop();
// used in crash reports & version info
const char *BACKEND = "SDL";
std::map<KEY_CODE, SDL_Keycode> KEY_CODE_to_SDLKey;
std::map<SDL_Keycode, KEY_CODE > SDLKey_to_KEY_CODE;
int realmain(int argc, char *argv[]);
// the main stub which calls realmain() aka, WZ's main startup routines
int main(int argc, char *argv[])
{
return realmain(argc, argv);
}
// At this time, we only have 1 window and 1 GL context.
static SDL_Window *WZwindow = nullptr;
static SDL_GLContext WZglcontext = nullptr;
// The screen that the game window is on.
int screenIndex = 0;
// The logical resolution of the game in the game's coordinate system (points).
unsigned int screenWidth = 0;
unsigned int screenHeight = 0;
// The logical resolution of the SDL window in the window's coordinate system (points) - i.e. not accounting for the Game Display Scale setting.
unsigned int windowWidth = 0;
unsigned int windowHeight = 0;
// The current display scale factor.
unsigned int current_displayScale = 100;
float current_displayScaleFactor = 1.f;
static std::vector<screeninfo> displaylist; // holds all our possible display lists
QCoreApplication *appPtr; // Needed for qtscript
#if defined(WZ_OS_MAC)
// on macOS, SDL_WINDOW_FULLSCREEN_DESKTOP *must* be used (or high-DPI fullscreen toggling breaks)
const SDL_WindowFlags WZ_SDL_FULLSCREEN_MODE = SDL_WINDOW_FULLSCREEN_DESKTOP;
#else
const SDL_WindowFlags WZ_SDL_FULLSCREEN_MODE = SDL_WINDOW_FULLSCREEN;
#endif
std::atomic<Uint32> wzSDLAppEvent((Uint32)-1);
enum wzSDLAppEventCodes
{
MAINTHREADEXEC
};
/* The possible states for keys */
enum KEY_STATE
{
KEY_UP,
KEY_PRESSED,
KEY_DOWN,
KEY_RELEASED,
KEY_PRESSRELEASE, // When a key goes up and down in a frame
KEY_DOUBLECLICK, // Only used by mouse keys
KEY_DRAG // Only used by mouse keys
};
struct INPUT_STATE
{
KEY_STATE state; /// Last key/mouse state
UDWORD lastdown; /// last key/mouse button down timestamp
Vector2i pressPos; ///< Location of last mouse press event.
Vector2i releasePos; ///< Location of last mouse release event.
};
// Clipboard routines
bool has_scrap(void);
bool put_scrap(char *src);
bool get_scrap(char **dst);
/// constant for the interval between 2 singleclicks for doubleclick event in ms
#define DOUBLE_CLICK_INTERVAL 250
/* The current state of the keyboard */
static INPUT_STATE aKeyState[KEY_MAXSCAN]; // NOTE: SDL_NUM_SCANCODES is the max, but KEY_MAXSCAN is our limit
/* The current location of the mouse */
static Uint16 mouseXPos = 0;
static Uint16 mouseYPos = 0;
static bool mouseInWindow = true;
/* How far the mouse has to move to start a drag */
#define DRAG_THRESHOLD 5
/* Which button is being used for a drag */
static MOUSE_KEY_CODE dragKey;
/* The start of a possible drag by the mouse */
static int dragX = 0;
static int dragY = 0;
/* The current mouse button state */
static INPUT_STATE aMouseState[MOUSE_END];
static MousePresses mousePresses;
/* The current screen resizing state for this iteration through the game loop, in the game coordinate system */
struct ScreenSizeChange
{
ScreenSizeChange(unsigned int oldWidth, unsigned int oldHeight, unsigned int newWidth, unsigned int newHeight)
: oldWidth(oldWidth)
, oldHeight(oldHeight)
, newWidth(newWidth)
, newHeight(newHeight)
{ }
unsigned int oldWidth;
unsigned int oldHeight;
unsigned int newWidth;
unsigned int newHeight;
};
static ScreenSizeChange* currentScreenResizingStatus = nullptr;
/* The size of the input buffer */
#define INPUT_MAXSTR 256
/* The input string buffer */
struct InputKey
{
UDWORD key;
utf_32_char unicode;
};
static InputKey pInputBuffer[INPUT_MAXSTR];
static InputKey *pStartBuffer, *pEndBuffer;
static utf_32_char *utf8Buf; // is like the old 'unicode' from SDL 1.x
static char text[INPUT_MAXSTR] = {'\0'}; // string where are text is located (only for debugging)
static unsigned int CurrentKey = 0; // Our Current keypress
bool GetTextEvents = false;
/**************************/
/*** Misc support ***/
/**************************/
// See if we have TEXT in the clipboard
bool has_scrap(void)
{
return SDL_HasClipboardText();
}
// When (if?) we decide to put text into the clipboard...
bool put_scrap(char *src)
{
if (SDL_SetClipboardText(src))
{
debug(LOG_ERROR, "Could not put clipboard text because : %s", SDL_GetError());
return false;
}
return true;
}
// Get text from the clipboard
bool get_scrap(char **dst)
{
if (has_scrap())
{
char *cliptext = SDL_GetClipboardText();
if (!cliptext)
{
debug(LOG_ERROR, "Could not get clipboard text because : %s", SDL_GetError());
return false;
}
*dst = cliptext;
return true;
}
else
{
// wasn't text or no text in the clipboard
return false;
}
}
void StartTextInput()
{
if (!GetTextEvents)
{
SDL_StartTextInput(); // enable text events
memset(text, 0x0, sizeof(text));
CurrentKey = 0;
GetTextEvents = true;
debug(LOG_INPUT, "SDL text events started");
}
}
void StopTextInput()
{
SDL_StopTextInput(); // disable text events
CurrentKey = 0;
memset(text, 0x0, sizeof(text));
GetTextEvents = false;
debug(LOG_INPUT, "SDL text events stopped");
}
WzString wzGetCurrentText()
{
return WzString::fromUtf8(text);
}
unsigned int wzGetCurrentKey(void)
{
return CurrentKey;
}
/* Put a character into a text buffer overwriting any text under the cursor */
WzString wzGetSelection()
{
WzString retval;
static char *scrap = nullptr;
if (get_scrap(&scrap))
{
retval = WzString::fromUtf8(scrap);
strlcpy(text, scrap, strlen(scrap));
}
return retval;
}
// Here we handle VSYNC enable/disabling
#if defined(WZ_WS_X11)
#include <GL/glx.h> // GLXDrawable
// X11 polution
#ifdef Status
#undef Status
#endif // Status
#ifdef CursorShape
#undef CursorShape
#endif // CursorShape
#ifdef Bool
#undef Bool
#endif // Bool
#ifndef GLX_SWAP_INTERVAL_EXT
#define GLX_SWAP_INTERVAL_EXT 0x20F1
#endif // GLX_SWAP_INTERVAL_EXT
// Need this global for use case of only having glXSwapIntervalSGI
static int swapInterval = -1;
void wzSetSwapInterval(int interval)
{
typedef void (* PFNGLXQUERYDRAWABLEPROC)(Display *, GLXDrawable, int, unsigned int *);
typedef void (* PFNGLXSWAPINTERVALEXTPROC)(Display *, GLXDrawable, int);
typedef int (* PFNGLXGETSWAPINTERVALMESAPROC)(void);
typedef int (* PFNGLXSWAPINTERVALMESAPROC)(unsigned);
typedef int (* PFNGLXSWAPINTERVALSGIPROC)(int);
PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT;
PFNGLXQUERYDRAWABLEPROC glXQueryDrawable;
PFNGLXGETSWAPINTERVALMESAPROC glXGetSwapIntervalMESA;
PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA;
PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
if (interval < 0)
{
interval = 0;
}
#if GLX_VERSION_1_2
// Hack-ish, but better than not supporting GLX_SWAP_INTERVAL_EXT?
GLXDrawable drawable = glXGetCurrentDrawable();
Display *display = glXGetCurrentDisplay();
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC) SDL_GL_GetProcAddress("glXSwapIntervalEXT");
glXQueryDrawable = (PFNGLXQUERYDRAWABLEPROC) SDL_GL_GetProcAddress("glXQueryDrawable");
if (glXSwapIntervalEXT && glXQueryDrawable && drawable)
{
unsigned clampedInterval;
glXSwapIntervalEXT(display, drawable, interval);
glXQueryDrawable(display, drawable, GLX_SWAP_INTERVAL_EXT, &clampedInterval);
swapInterval = clampedInterval;
return;
}
#endif
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) SDL_GL_GetProcAddress("glXSwapIntervalMESA");
glXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC) SDL_GL_GetProcAddress("glXGetSwapIntervalMESA");
if (glXSwapIntervalMESA && glXGetSwapIntervalMESA)
{
glXSwapIntervalMESA(interval);
swapInterval = glXGetSwapIntervalMESA();
return;
}
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC) SDL_GL_GetProcAddress("glXSwapIntervalSGI");
if (glXSwapIntervalSGI)
{
if (interval < 1)
{
interval = 1;
}
if (glXSwapIntervalSGI(interval))
{
// Error, revert to default
swapInterval = 1;
glXSwapIntervalSGI(1);
}
else
{
swapInterval = interval;
}
return;
}
swapInterval = 0;
}
int wzGetSwapInterval()
{
if (swapInterval >= 0)
{
return swapInterval;
}
typedef void (* PFNGLXQUERYDRAWABLEPROC)(Display *, GLXDrawable, int, unsigned int *);
typedef int (* PFNGLXGETSWAPINTERVALMESAPROC)(void);
typedef int (* PFNGLXSWAPINTERVALSGIPROC)(int);
PFNGLXQUERYDRAWABLEPROC glXQueryDrawable;
PFNGLXGETSWAPINTERVALMESAPROC glXGetSwapIntervalMESA;
PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
#if GLX_VERSION_1_2
// Hack-ish, but better than not supporting GLX_SWAP_INTERVAL_EXT?
GLXDrawable drawable = glXGetCurrentDrawable();
Display *display = glXGetCurrentDisplay();
glXQueryDrawable = (PFNGLXQUERYDRAWABLEPROC) SDL_GL_GetProcAddress("glXQueryDrawable");
if (glXQueryDrawable && drawable)
{
unsigned interval;
glXQueryDrawable(display, drawable, GLX_SWAP_INTERVAL_EXT, &interval);
swapInterval = interval;
return swapInterval;
}
#endif
glXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC) SDL_GL_GetProcAddress("glXGetSwapIntervalMESA");
if (glXGetSwapIntervalMESA)
{
swapInterval = glXGetSwapIntervalMESA();
return swapInterval;
}
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC) SDL_GL_GetProcAddress("glXSwapIntervalSGI");
if (glXSwapIntervalSGI)
{
swapInterval = 1;
}
else
{
swapInterval = 0;
}
return swapInterval;
}
#elif defined(WZ_WS_WIN)
void wzSetSwapInterval(int interval)
{
typedef BOOL (WINAPI * PFNWGLSWAPINTERVALEXTPROC)(int);
PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
if (interval < 0)
{
interval = 0;
}
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) SDL_GL_GetProcAddress("wglSwapIntervalEXT");
if (wglSwapIntervalEXT)
{
wglSwapIntervalEXT(interval);
}
}
int wzGetSwapInterval()
{
typedef int (WINAPI * PFNWGLGETSWAPINTERVALEXTPROC)(void);
PFNWGLGETSWAPINTERVALEXTPROC wglGetSwapIntervalEXT;
wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) SDL_GL_GetProcAddress("wglGetSwapIntervalEXT");
if (wglGetSwapIntervalEXT)
{
return wglGetSwapIntervalEXT();
}
return 0;
}
#elif !defined(WZ_OS_MAC)
// FIXME: This can't be right?
void wzSetSwapInterval(int)
{
return;
}
int wzGetSwapInterval()
{
return 0;
}
#endif
std::vector<screeninfo> wzAvailableResolutions()
{
return displaylist;
}
std::vector<unsigned int> wzAvailableDisplayScales()
{
static const unsigned int wzDisplayScales[] = { 100, 125, 150, 200, 250, 300, 400, 500 };
return std::vector<unsigned int>(wzDisplayScales, wzDisplayScales + (sizeof(wzDisplayScales) / sizeof(wzDisplayScales[0])));
}
void setDisplayScale(unsigned int displayScale)
{
current_displayScale = displayScale;
current_displayScaleFactor = (float)displayScale / 100.f;
}
unsigned int wzGetCurrentDisplayScale()
{
return current_displayScale;
}
void wzShowMouse(bool visible)
{
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
}
int wzGetTicks()
{
return SDL_GetTicks();
}
void wzFatalDialog(const char *msg)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "We have a problem!", msg, nullptr);
}
void wzScreenFlip()
{
SDL_GL_SwapWindow(WZwindow);
}
void wzToggleFullscreen()
{
Uint32 flags = SDL_GetWindowFlags(WZwindow);
if (flags & WZ_SDL_FULLSCREEN_MODE)
{
SDL_SetWindowFullscreen(WZwindow, 0);
wzSetWindowIsResizable(true);
}
else
{
SDL_SetWindowFullscreen(WZwindow, WZ_SDL_FULLSCREEN_MODE);
wzSetWindowIsResizable(false);
}
}
bool wzIsFullscreen()
{
assert(WZwindow != nullptr);
Uint32 flags = SDL_GetWindowFlags(WZwindow);
if ((flags & SDL_WINDOW_FULLSCREEN) || (flags & SDL_WINDOW_FULLSCREEN_DESKTOP))
{
return true;
}
return false;
}
#if defined(WZ_OS_MAC)
#include "cocoa_sdl_helpers.h"
#endif
bool wzIsMaximized()
{
assert(WZwindow != nullptr);
Uint32 flags = SDL_GetWindowFlags(WZwindow);
if (flags & SDL_WINDOW_MAXIMIZED)
{
return true;
}
return false;
}
void wzQuit()
{
// Create a quit event to halt game loop.
SDL_Event quitEvent;
quitEvent.type = SDL_QUIT;
SDL_PushEvent(&quitEvent);
}
void wzGrabMouse()
{
SDL_SetWindowGrab(WZwindow, SDL_TRUE);
}
void wzReleaseMouse()
{
SDL_SetWindowGrab(WZwindow, SDL_FALSE);
}
void wzDelay(unsigned int delay)
{
SDL_Delay(delay);
}
/**************************/
/*** Thread support ***/
/**************************/
WZ_THREAD *wzThreadCreate(int (*threadFunc)(void *), void *data)
{
return (WZ_THREAD *)SDL_CreateThread(threadFunc, "wzThread", data);
}
int wzThreadJoin(WZ_THREAD *thread)
{
int result;
SDL_WaitThread((SDL_Thread *)thread, &result);
return result;
}
void wzThreadDetach(WZ_THREAD *thread)
{
SDL_DetachThread((SDL_Thread *)thread);
}
void wzThreadStart(WZ_THREAD *thread)
{
(void)thread; // no-op
}
void wzYieldCurrentThread()
{
SDL_Delay(40);
}
WZ_MUTEX *wzMutexCreate()
{
return (WZ_MUTEX *)SDL_CreateMutex();
}
void wzMutexDestroy(WZ_MUTEX *mutex)
{
SDL_DestroyMutex((SDL_mutex *)mutex);
}
void wzMutexLock(WZ_MUTEX *mutex)
{
SDL_LockMutex((SDL_mutex *)mutex);
}
void wzMutexUnlock(WZ_MUTEX *mutex)
{
SDL_UnlockMutex((SDL_mutex *)mutex);
}
WZ_SEMAPHORE *wzSemaphoreCreate(int startValue)
{
return (WZ_SEMAPHORE *)SDL_CreateSemaphore(startValue);
}
void wzSemaphoreDestroy(WZ_SEMAPHORE *semaphore)
{
SDL_DestroySemaphore((SDL_sem *)semaphore);
}
void wzSemaphoreWait(WZ_SEMAPHORE *semaphore)
{
SDL_SemWait((SDL_sem *)semaphore);
}
void wzSemaphorePost(WZ_SEMAPHORE *semaphore)
{
SDL_SemPost((SDL_sem *)semaphore);
}
// Asynchronously executes exec->doExecOnMainThread() on the main thread
// `exec` should be a subclass of `WZ_MAINTHREADEXEC`
//
// `exec` must be allocated on the heap since the main event loop takes ownership of it
// and will handle deleting it once it has been processed.
// It is not safe to access `exec` after calling wzAsyncExecOnMainThread.
//
// No guarantees are made about when execFunc() will be called relative to the
// calling of this function - this function may return before, during, or after
// execFunc()'s execution on the main thread.
void wzAsyncExecOnMainThread(WZ_MAINTHREADEXEC *exec)
{
assert(exec != nullptr);
Uint32 _wzSDLAppEvent = wzSDLAppEvent.load();
assert(_wzSDLAppEvent != ((Uint32)-1));
if (_wzSDLAppEvent == ((Uint32)-1)) {
// The app-defined event has not yet been registered with SDL
return;
}
SDL_Event event;
SDL_memset(&event, 0, sizeof(event));
event.type = _wzSDLAppEvent;
event.user.code = wzSDLAppEventCodes::MAINTHREADEXEC;
event.user.data1 = exec;
event.user.data2 = 0;
SDL_PushEvent(&event);
// receiver handles deleting `exec` on the main thread after doExecOnMainThread() has been called
}
/*!
** The keycodes we care about
**/
static inline void initKeycodes()
{
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_ESC, SDLK_ESCAPE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_1, SDLK_1));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_2, SDLK_2));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_3, SDLK_3));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_4, SDLK_4));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_5, SDLK_5));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_6, SDLK_6));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_7, SDLK_7));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_8, SDLK_8));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_9, SDLK_9));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_0, SDLK_0));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_MINUS, SDLK_MINUS));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_EQUALS, SDLK_EQUALS));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_BACKSPACE, SDLK_BACKSPACE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_TAB, SDLK_TAB));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_Q, SDLK_q));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_W, SDLK_w));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_E, SDLK_e));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_R, SDLK_r));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_T, SDLK_t));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_Y, SDLK_y));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_U, SDLK_u));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_I, SDLK_i));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_O, SDLK_o));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_P, SDLK_p));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LBRACE, SDLK_LEFTBRACKET));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RBRACE, SDLK_RIGHTBRACKET));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RETURN, SDLK_RETURN));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LCTRL, SDLK_LCTRL));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_A, SDLK_a));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_S, SDLK_s));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_D, SDLK_d));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F, SDLK_f));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_G, SDLK_g));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_H, SDLK_h));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_J, SDLK_j));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_K, SDLK_k));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_L, SDLK_l));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_SEMICOLON, SDLK_SEMICOLON));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_QUOTE, SDLK_QUOTE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_BACKQUOTE, SDLK_BACKQUOTE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LSHIFT, SDLK_LSHIFT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LMETA, SDLK_LGUI));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LSUPER, SDLK_LGUI));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_BACKSLASH, SDLK_BACKSLASH));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_Z, SDLK_z));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_X, SDLK_x));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_C, SDLK_c));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_V, SDLK_v));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_B, SDLK_b));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_N, SDLK_n));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_M, SDLK_m));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_COMMA, SDLK_COMMA));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_FULLSTOP, SDLK_PERIOD));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_FORWARDSLASH, SDLK_SLASH));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RSHIFT, SDLK_RSHIFT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RMETA, SDLK_RGUI));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RSUPER, SDLK_RGUI));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_STAR, SDLK_KP_MULTIPLY));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LALT, SDLK_LALT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_SPACE, SDLK_SPACE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_CAPSLOCK, SDLK_CAPSLOCK));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F1, SDLK_F1));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F2, SDLK_F2));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F3, SDLK_F3));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F4, SDLK_F4));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F5, SDLK_F5));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F6, SDLK_F6));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F7, SDLK_F7));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F8, SDLK_F8));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F9, SDLK_F9));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F10, SDLK_F10));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_NUMLOCK, SDLK_NUMLOCKCLEAR));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_SCROLLLOCK, SDLK_SCROLLLOCK));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_7, SDLK_KP_7));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_8, SDLK_KP_8));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_9, SDLK_KP_9));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_MINUS, SDLK_KP_MINUS));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_4, SDLK_KP_4));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_5, SDLK_KP_5));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_6, SDLK_KP_6));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_PLUS, SDLK_KP_PLUS));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_1, SDLK_KP_1));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_2, SDLK_KP_2));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_3, SDLK_KP_3));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_0, SDLK_KP_0));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_FULLSTOP, SDLK_KP_PERIOD));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F11, SDLK_F11));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_F12, SDLK_F12));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RCTRL, SDLK_RCTRL));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KP_BACKSLASH, SDLK_KP_DIVIDE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RALT, SDLK_RALT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_HOME, SDLK_HOME));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_UPARROW, SDLK_UP));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_PAGEUP, SDLK_PAGEUP));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_LEFTARROW, SDLK_LEFT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_RIGHTARROW, SDLK_RIGHT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_END, SDLK_END));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_DOWNARROW, SDLK_DOWN));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_PAGEDOWN, SDLK_PAGEDOWN));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_INSERT, SDLK_INSERT));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_DELETE, SDLK_DELETE));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_KPENTER, SDLK_KP_ENTER));
KEY_CODE_to_SDLKey.insert(std::pair<KEY_CODE, SDL_Keycode>(KEY_IGNORE, SDL_Keycode(5190)));
std::map<KEY_CODE, SDL_Keycode>::iterator it;
for (it = KEY_CODE_to_SDLKey.begin(); it != KEY_CODE_to_SDLKey.end(); it++)
{
SDLKey_to_KEY_CODE.insert(std::pair<SDL_Keycode, KEY_CODE>(it->second, it->first));
}
}
static inline KEY_CODE sdlKeyToKeyCode(SDL_Keycode key)
{
std::map<SDL_Keycode, KEY_CODE >::iterator it;
it = SDLKey_to_KEY_CODE.find(key);
if (it != SDLKey_to_KEY_CODE.end())
{
return it->second;
}
return (KEY_CODE)key;
}
static inline SDL_Keycode keyCodeToSDLKey(KEY_CODE code)
{
std::map<KEY_CODE, SDL_Keycode>::iterator it;
it = KEY_CODE_to_SDLKey.find(code);
if (it != KEY_CODE_to_SDLKey.end())
{
return it->second;
}
return (SDL_Keycode)code;
}
// Cyclic increment.
static InputKey *inputPointerNext(InputKey *p)
{
return p + 1 == pInputBuffer + INPUT_MAXSTR ? pInputBuffer : p + 1;
}
/* add count copies of the characater code to the input buffer */
static void inputAddBuffer(UDWORD key, utf_32_char unicode)
{
/* Calculate what pEndBuffer will be set to next */
InputKey *pNext = inputPointerNext(pEndBuffer);
if (pNext == pStartBuffer)
{
return; // Buffer full.
}
// Add key to buffer.
pEndBuffer->key = key;
pEndBuffer->unicode = unicode;
pEndBuffer = pNext;
}
void keyScanToString(KEY_CODE code, char *ascii, UDWORD maxStringSize)
{
if (code == KEY_LCTRL)
{
// shortcuts with modifier keys work with either key.
strcpy(ascii, "Ctrl");
return;
}
else if (code == KEY_LSHIFT)
{
// shortcuts with modifier keys work with either key.
strcpy(ascii, "Shift");
return;
}
else if (code == KEY_LALT)
{
// shortcuts with modifier keys work with either key.
strcpy(ascii, "Alt");
return;
}
else if (code == KEY_LMETA)
{
// shortcuts with modifier keys work with either key.
#ifdef WZ_OS_MAC
strcpy(ascii, "Cmd");
#else
strcpy(ascii, "Meta");
#endif
return;
}
if (code < KEY_MAXSCAN)
{
snprintf(ascii, maxStringSize, "%s", SDL_GetKeyName(keyCodeToSDLKey(code)));
if (ascii[0] >= 'a' && ascii[0] <= 'z' && ascii[1] != 0)
{
// capitalize
ascii[0] += 'A' - 'a';
return;
}
}
else
{
strcpy(ascii, "???");
}
}
/* Initialise the input module */
void inputInitialise(void)
{
for (unsigned int i = 0; i < KEY_MAXSCAN; i++)
{
aKeyState[i].state = KEY_UP;
}
for (unsigned int i = 0; i < MOUSE_END; i++)
{
aMouseState[i].state = KEY_UP;
}
pStartBuffer = pInputBuffer;
pEndBuffer = pInputBuffer;
dragX = mouseXPos = screenWidth / 2;
dragY = mouseYPos = screenHeight / 2;
dragKey = MOUSE_LMB;
}
/* Clear the input buffer */
void inputClearBuffer(void)
{
pStartBuffer = pInputBuffer;
pEndBuffer = pInputBuffer;
}
/* Return the next key press or 0 if no key in the buffer.
* The key returned will have been remapped to the correct ascii code for the
* windows key map.
* All key presses are buffered up (including windows auto repeat).
*/
UDWORD inputGetKey(utf_32_char *unicode)
{
UDWORD retVal;
if (pStartBuffer == pEndBuffer)
{
return 0; // Buffer empty.
}
retVal = pStartBuffer->key;
if (unicode)
{
*unicode = pStartBuffer->unicode;
}
if (!retVal)
{
retVal = ' '; // Don't return 0 if we got a virtual key, since that's interpreted as no input.
}
pStartBuffer = inputPointerNext(pStartBuffer);
return retVal;
}
MousePresses const &inputGetClicks()
{
return mousePresses;
}
/*!
* This is called once a frame so that the system can tell
* whether a key was pressed this turn or held down from the last frame.
*/
void inputNewFrame(void)
{
// handle the keyboard
for (unsigned int i = 0; i < KEY_MAXSCAN; i++)
{
if (aKeyState[i].state == KEY_PRESSED)
{
aKeyState[i].state = KEY_DOWN;
debug(LOG_NEVER, "This key is DOWN! %x, %d [%s]", i, i, SDL_GetKeyName(keyCodeToSDLKey((KEY_CODE)i)));
}
else if (aKeyState[i].state == KEY_RELEASED ||
aKeyState[i].state == KEY_PRESSRELEASE)
{
aKeyState[i].state = KEY_UP;
debug(LOG_NEVER, "This key is UP! %x, %d [%s]", i, i, SDL_GetKeyName(keyCodeToSDLKey((KEY_CODE)i)));
}
}
// handle the mouse
for (unsigned int i = 0; i < MOUSE_END; i++)
{
if (aMouseState[i].state == KEY_PRESSED)
{
aMouseState[i].state = KEY_DOWN;
}
else if (aMouseState[i].state == KEY_RELEASED
|| aMouseState[i].state == KEY_DOUBLECLICK
|| aMouseState[i].state == KEY_PRESSRELEASE)
{
aMouseState[i].state = KEY_UP;
}
}
mousePresses.clear();
}
/*!
* Release all keys (and buttons) when we lose focus
*/
void inputLoseFocus(void)
{
/* Lost the window focus, have to take this as a global key up */
for (unsigned int i = 0; i < KEY_MAXSCAN; i++)
{
aKeyState[i].state = KEY_UP;
}
for (unsigned int i = 0; i < MOUSE_END; i++)
{
aMouseState[i].state = KEY_UP;
}
}
/* This returns true if the key is currently depressed */
bool keyDown(KEY_CODE code)
{
ASSERT_OR_RETURN(false, code < KEY_MAXSCAN, "Invalid keycode of %d!", (int)code);
return (aKeyState[code].state != KEY_UP);
}
/* This returns true if the key went from being up to being down this frame */
bool keyPressed(KEY_CODE code)
{
ASSERT_OR_RETURN(false, code < KEY_MAXSCAN, "Invalid keycode of %d!", (int)code);