-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
rcore_desktop_sdl.c
1978 lines (1723 loc) · 73.7 KB
/
rcore_desktop_sdl.c
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
/**********************************************************************************************
*
* rcore_desktop_sdl - Functions to manage window, graphics device and inputs
*
* PLATFORM: DESKTOP: SDL
* - Windows (Win32, Win64)
* - Linux (X11/Wayland desktop mode)
* - Others (not tested)
*
* LIMITATIONS:
* - Limitation 01
* - Limitation 02
*
* POSSIBLE IMPROVEMENTS:
* - Improvement 01
* - Improvement 02
*
* ADDITIONAL NOTES:
* - TRACELOG() function is located in raylib [utils] module
*
* CONFIGURATION:
* #define RCORE_PLATFORM_CUSTOM_FLAG
* Custom flag for rcore on target platform -not used-
*
* DEPENDENCIES:
* - SDL 2 or SLD 3 (main library): Windowing and inputs management
* - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
*
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2013-2024 Ramon Santamaria (@raysan5) and contributors
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef SDL_ENABLE_OLD_NAMES
#define SDL_ENABLE_OLD_NAMES // Just in case we're on SDL3, we need some in-between compatibily
#endif
#include "SDL.h" // SDL base library (window/rendered, input, timing... functionality)
#if defined(GRAPHICS_API_OPENGL_ES2)
// It seems it does not need to be included to work
//#include "SDL_opengles2.h"
#else
#include "SDL_opengl.h" // SDL OpenGL functionality (if required, instead of internal renderer)
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#ifndef MAX_CLIPBOARD_BUFFER_LENGTH
#define MAX_CLIPBOARD_BUFFER_LENGTH 1024 // Size of the clipboard buffer used on GetClipboardText()
#endif
#if ((defined(SDL_MAJOR_VERSION) && SDL_MAJOR_VERSION == 3) && (defined(SDL_MINOR_VERSION) && SDL_MINOR_VERSION >= 1))
#ifndef PLATFORM_DESKTOP_SDL3
#define PLATFORM_DESKTOP_SDL3
#endif
#endif
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct {
SDL_Window *window;
SDL_GLContext glContext;
SDL_GameController *gamepad[MAX_GAMEPADS];
SDL_Cursor *cursor;
bool cursorRelative;
} PlatformData;
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
extern CoreData CORE; // Global CORE state context
static PlatformData platform = { 0 }; // Platform specific data
//----------------------------------------------------------------------------------
// Local Variables Definition
//----------------------------------------------------------------------------------
#define SCANCODE_MAPPED_NUM 232
static const KeyboardKey mapScancodeToKey[SCANCODE_MAPPED_NUM] = {
KEY_NULL, // SDL_SCANCODE_UNKNOWN
0,
0,
0,
KEY_A, // SDL_SCANCODE_A
KEY_B, // SDL_SCANCODE_B
KEY_C, // SDL_SCANCODE_C
KEY_D, // SDL_SCANCODE_D
KEY_E, // SDL_SCANCODE_E
KEY_F, // SDL_SCANCODE_F
KEY_G, // SDL_SCANCODE_G
KEY_H, // SDL_SCANCODE_H
KEY_I, // SDL_SCANCODE_I
KEY_J, // SDL_SCANCODE_J
KEY_K, // SDL_SCANCODE_K
KEY_L, // SDL_SCANCODE_L
KEY_M, // SDL_SCANCODE_M
KEY_N, // SDL_SCANCODE_N
KEY_O, // SDL_SCANCODE_O
KEY_P, // SDL_SCANCODE_P
KEY_Q, // SDL_SCANCODE_Q
KEY_R, // SDL_SCANCODE_R
KEY_S, // SDL_SCANCODE_S
KEY_T, // SDL_SCANCODE_T
KEY_U, // SDL_SCANCODE_U
KEY_V, // SDL_SCANCODE_V
KEY_W, // SDL_SCANCODE_W
KEY_X, // SDL_SCANCODE_X
KEY_Y, // SDL_SCANCODE_Y
KEY_Z, // SDL_SCANCODE_Z
KEY_ONE, // SDL_SCANCODE_1
KEY_TWO, // SDL_SCANCODE_2
KEY_THREE, // SDL_SCANCODE_3
KEY_FOUR, // SDL_SCANCODE_4
KEY_FIVE, // SDL_SCANCODE_5
KEY_SIX, // SDL_SCANCODE_6
KEY_SEVEN, // SDL_SCANCODE_7
KEY_EIGHT, // SDL_SCANCODE_8
KEY_NINE, // SDL_SCANCODE_9
KEY_ZERO, // SDL_SCANCODE_0
KEY_ENTER, // SDL_SCANCODE_RETURN
KEY_ESCAPE, // SDL_SCANCODE_ESCAPE
KEY_BACKSPACE, // SDL_SCANCODE_BACKSPACE
KEY_TAB, // SDL_SCANCODE_TAB
KEY_SPACE, // SDL_SCANCODE_SPACE
KEY_MINUS, // SDL_SCANCODE_MINUS
KEY_EQUAL, // SDL_SCANCODE_EQUALS
KEY_LEFT_BRACKET, // SDL_SCANCODE_LEFTBRACKET
KEY_RIGHT_BRACKET, // SDL_SCANCODE_RIGHTBRACKET
KEY_BACKSLASH, // SDL_SCANCODE_BACKSLASH
0, // SDL_SCANCODE_NONUSHASH
KEY_SEMICOLON, // SDL_SCANCODE_SEMICOLON
KEY_APOSTROPHE, // SDL_SCANCODE_APOSTROPHE
KEY_GRAVE, // SDL_SCANCODE_GRAVE
KEY_COMMA, // SDL_SCANCODE_COMMA
KEY_PERIOD, // SDL_SCANCODE_PERIOD
KEY_SLASH, // SDL_SCANCODE_SLASH
KEY_CAPS_LOCK, // SDL_SCANCODE_CAPSLOCK
KEY_F1, // SDL_SCANCODE_F1
KEY_F2, // SDL_SCANCODE_F2
KEY_F3, // SDL_SCANCODE_F3
KEY_F4, // SDL_SCANCODE_F4
KEY_F5, // SDL_SCANCODE_F5
KEY_F6, // SDL_SCANCODE_F6
KEY_F7, // SDL_SCANCODE_F7
KEY_F8, // SDL_SCANCODE_F8
KEY_F9, // SDL_SCANCODE_F9
KEY_F10, // SDL_SCANCODE_F10
KEY_F11, // SDL_SCANCODE_F11
KEY_F12, // SDL_SCANCODE_F12
KEY_PRINT_SCREEN, // SDL_SCANCODE_PRINTSCREEN
KEY_SCROLL_LOCK, // SDL_SCANCODE_SCROLLLOCK
KEY_PAUSE, // SDL_SCANCODE_PAUSE
KEY_INSERT, // SDL_SCANCODE_INSERT
KEY_HOME, // SDL_SCANCODE_HOME
KEY_PAGE_UP, // SDL_SCANCODE_PAGEUP
KEY_DELETE, // SDL_SCANCODE_DELETE
KEY_END, // SDL_SCANCODE_END
KEY_PAGE_DOWN, // SDL_SCANCODE_PAGEDOWN
KEY_RIGHT, // SDL_SCANCODE_RIGHT
KEY_LEFT, // SDL_SCANCODE_LEFT
KEY_DOWN, // SDL_SCANCODE_DOWN
KEY_UP, // SDL_SCANCODE_UP
KEY_NUM_LOCK, // SDL_SCANCODE_NUMLOCKCLEAR
KEY_KP_DIVIDE, // SDL_SCANCODE_KP_DIVIDE
KEY_KP_MULTIPLY, // SDL_SCANCODE_KP_MULTIPLY
KEY_KP_SUBTRACT, // SDL_SCANCODE_KP_MINUS
KEY_KP_ADD, // SDL_SCANCODE_KP_PLUS
KEY_KP_ENTER, // SDL_SCANCODE_KP_ENTER
KEY_KP_1, // SDL_SCANCODE_KP_1
KEY_KP_2, // SDL_SCANCODE_KP_2
KEY_KP_3, // SDL_SCANCODE_KP_3
KEY_KP_4, // SDL_SCANCODE_KP_4
KEY_KP_5, // SDL_SCANCODE_KP_5
KEY_KP_6, // SDL_SCANCODE_KP_6
KEY_KP_7, // SDL_SCANCODE_KP_7
KEY_KP_8, // SDL_SCANCODE_KP_8
KEY_KP_9, // SDL_SCANCODE_KP_9
KEY_KP_0, // SDL_SCANCODE_KP_0
KEY_KP_DECIMAL, // SDL_SCANCODE_KP_PERIOD
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
KEY_LEFT_CONTROL, //SDL_SCANCODE_LCTRL
KEY_LEFT_SHIFT, //SDL_SCANCODE_LSHIFT
KEY_LEFT_ALT, //SDL_SCANCODE_LALT
KEY_LEFT_SUPER, //SDL_SCANCODE_LGUI
KEY_RIGHT_CONTROL, //SDL_SCANCODE_RCTRL
KEY_RIGHT_SHIFT, //SDL_SCANCODE_RSHIFT
KEY_RIGHT_ALT, //SDL_SCANCODE_RALT
KEY_RIGHT_SUPER //SDL_SCANCODE_RGUI
};
static const int CursorsLUT[] = {
SDL_SYSTEM_CURSOR_ARROW, // 0 MOUSE_CURSOR_DEFAULT
SDL_SYSTEM_CURSOR_ARROW, // 1 MOUSE_CURSOR_ARROW
SDL_SYSTEM_CURSOR_IBEAM, // 2 MOUSE_CURSOR_IBEAM
SDL_SYSTEM_CURSOR_CROSSHAIR, // 3 MOUSE_CURSOR_CROSSHAIR
SDL_SYSTEM_CURSOR_HAND, // 4 MOUSE_CURSOR_POINTING_HAND
SDL_SYSTEM_CURSOR_SIZEWE, // 5 MOUSE_CURSOR_RESIZE_EW
SDL_SYSTEM_CURSOR_SIZENS, // 6 MOUSE_CURSOR_RESIZE_NS
SDL_SYSTEM_CURSOR_SIZENWSE, // 7 MOUSE_CURSOR_RESIZE_NWSE
SDL_SYSTEM_CURSOR_SIZENESW, // 8 MOUSE_CURSOR_RESIZE_NESW
SDL_SYSTEM_CURSOR_SIZEALL, // 9 MOUSE_CURSOR_RESIZE_ALL
SDL_SYSTEM_CURSOR_NO // 10 MOUSE_CURSOR_NOT_ALLOWED
//SDL_SYSTEM_CURSOR_WAIT, // No equivalent implemented on MouseCursor enum on raylib.h
//SDL_SYSTEM_CURSOR_WAITARROW, // No equivalent implemented on MouseCursor enum on raylib.h
};
// SDL3 Migration Layer made to avoid `ifdefs` inside functions when we can.
#ifdef PLATFORM_DESKTOP_SDL3
// SDL3 Migration:
// SDL_WINDOW_FULLSCREEN_DESKTOP has been removed,
// and you can call SDL_GetWindowFullscreenMode()
// to see whether an exclusive fullscreen mode will be used
// or the borderless fullscreen desktop mode will be used
#define SDL_WINDOW_FULLSCREEN_DESKTOP SDL_WINDOW_FULLSCREEN
#define SDL_IGNORE false
#define SDL_DISABLE false
#define SDL_ENABLE true
// SDL3 Migration: SDL_INIT_TIMER - no longer needed before calling SDL_AddTimer()
#define SDL_INIT_TIMER 0x0 // It's a flag, so no problem in setting it to zero if we use in a bitor (|)
// SDL3 Migration: The SDL_WINDOW_SHOWN flag has been removed. Windows are shown by default and can be created hidden by using the SDL_WINDOW_HIDDEN flag.
#define SDL_WINDOW_SHOWN 0x0 // It's a flag, so no problem in setting it to zero if we use in a bitor (|)
//
// SDL3 Migration: Renamed
// IMPORTANT:
// Might need to call SDL_CleanupEvent somewhere see :https://github.com/libsdl-org/SDL/issues/3540#issuecomment-1793449852
//
#define SDL_DROPFILE SDL_EVENT_DROP_FILE
const char* SDL_GameControllerNameForIndex(int joystickIndex)
{
// NOTE: SDL3 uses the IDs itself (SDL_JoystickID) instead of SDL2 joystick_index
const char* name = NULL;
int numJoysticks = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
if (joystickIndex < numJoysticks) {
SDL_JoystickID instance_id = joysticks[joystickIndex];
name = SDL_GetGamepadNameForID(instance_id);
}
SDL_free(joysticks);
}
return name;
}
int SDL_GetNumVideoDisplays(void)
{
int monitorCount = 0;
SDL_DisplayID *displays = SDL_GetDisplays(&monitorCount);
// Safe because If `mem` is NULL, SDL_free does nothing.
SDL_free(displays);
return monitorCount;
}
// SLD3 Migration:
// To emulate SDL2 this function should return `SDL_DISABLE` or `SDL_ENABLE`
// representing the *processing state* of the event before this function makes any changes to it.
Uint8 SDL_EventState(Uint32 type, int state) {
Uint8 stateBefore = SDL_EventEnabled(type);
switch (state)
{
case SDL_DISABLE: SDL_SetEventEnabled(type, false); break;
case SDL_ENABLE: SDL_SetEventEnabled(type, true); break;
default: TRACELOG(LOG_WARNING, "Event sate: unknow type");
}
return stateBefore;
}
void SDL_GetCurrentDisplayMode_Adapter(SDL_DisplayID displayID, SDL_DisplayMode* mode)
{
const SDL_DisplayMode* currMode = SDL_GetCurrentDisplayMode(displayID);
if (currMode == NULL)
{
TRACELOG(LOG_WARNING, "No current display mode");
}
else
{
*mode = *currMode;
}
}
// SDL3 Migration: Renamed
#define SDL_GetCurrentDisplayMode SDL_GetCurrentDisplayMode_Adapter
SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
return SDL_CreateSurface(width, height, SDL_GetPixelFormatForMasks(depth, Rmask, Gmask, Bmask, Amask));
}
// SDL3 Migration:
// SDL_GetDisplayDPI() -
// not reliable across platforms, approximately replaced by multiplying
// SDL_GetWindowDisplayScale() times 160 on iPhone and Android, and 96 on other platforms.
// returns 0 on success or a negative error code on failure
int SDL_GetDisplayDPI(int displayIndex, float * ddpi, float * hdpi, float * vdpi) {
float dpi = SDL_GetWindowDisplayScale(platform.window) * 96.0;
if (ddpi != NULL) *ddpi = dpi;
if (hdpi != NULL) *hdpi = dpi;
if (vdpi != NULL) *vdpi = dpi;
return 0;
}
SDL_Surface *SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
{
return SDL_CreateSurface(width, height, format);
}
SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
return SDL_CreateSurfaceFrom(width, height, SDL_GetPixelFormatForMasks(depth, Rmask, Gmask, Bmask, Amask), pixels, pitch);
}
SDL_Surface *SDL_CreateRGBSurfaceWithFormatFrom(void *pixels, int width, int height, int depth, int pitch, Uint32 format)
{
return SDL_CreateSurfaceFrom(width, height, format, pixels, pitch);
}
int SDL_NumJoysticks(void)
{
int numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
SDL_free(joysticks);
return numJoysticks;
}
// SDL_SetRelativeMouseMode
// returns 0 on success or a negative error code on failure
// If relative mode is not supported, this returns -1.
int SDL_SetRelativeMouseMode_Adapter(SDL_bool enabled)
{
//
// SDL_SetWindowRelativeMouseMode(SDL_Window *window, bool enabled)
// \returns true on success or false on failure; call SDL_GetError() for more
//
if (SDL_SetWindowRelativeMouseMode(platform.window, enabled))
{
return 0; // success
}
else
{
return -1; // failure
}
}
#define SDL_SetRelativeMouseMode SDL_SetRelativeMouseMode_Adapter
bool SDL_GetRelativeMouseMode_Adapter(void)
{
return SDL_GetWindowRelativeMouseMode(platform.window);
}
#define SDL_GetRelativeMouseMode SDL_GetRelativeMouseMode_Adapter
int SDL_GetNumTouchFingers(SDL_TouchID touchID)
{
// SDL_Finger **SDL_GetTouchFingers(SDL_TouchID touchID, int *count)
int count = 0;
SDL_Finger **fingers = SDL_GetTouchFingers(touchID, &count);
SDL_free(fingers);
return count;
}
#else // We're on SDL2
// Since SDL2 doesn't have this function we leave a stub
// SDL_GetClipboardData function is available since SDL 3.1.3. (e.g. SDL3)
void* SDL_GetClipboardData(const char *mime_type, size_t *size) {
TRACELOG(LOG_WARNING, "Getting clipboard data that is not text is only available in SDL3");
// We could possibly implement it ourselves in this case for some easier platforms
return NULL;
}
#endif // PLATFORM_DESKTOP_SDL3
//----------------------------------------------------------------------------------
// Module Internal Functions Declaration
//----------------------------------------------------------------------------------
int InitPlatform(void); // Initialize platform (graphics, inputs and more)
void ClosePlatform(void); // Close platform
static KeyboardKey ConvertScancodeToKey(SDL_Scancode sdlScancode); // Help convert SDL scancodes to raylib key
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
// NOTE: Functions declaration is provided by raylib.h
//----------------------------------------------------------------------------------
// Module Functions Definition: Window and Graphics Device
//----------------------------------------------------------------------------------
// Check if application should close
bool WindowShouldClose(void)
{
if (CORE.Window.ready) return CORE.Window.shouldClose;
else return true;
}
// Toggle fullscreen mode
void ToggleFullscreen(void)
{
const int monitor = SDL_GetWindowDisplayIndex(platform.window);
const int monitorCount = SDL_GetNumVideoDisplays();
#ifdef PLATFORM_DESKTOP_SDL3 // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount))
#endif
{
if ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0)
{
SDL_SetWindowFullscreen(platform.window, 0);
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
CORE.Window.fullscreen = false;
}
else
{
SDL_SetWindowFullscreen(platform.window, SDL_WINDOW_FULLSCREEN);
CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
CORE.Window.fullscreen = true;
}
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
}
// Toggle borderless windowed mode
void ToggleBorderlessWindowed(void)
{
const int monitor = SDL_GetWindowDisplayIndex(platform.window);
const int monitorCount = SDL_GetNumVideoDisplays();
#ifdef PLATFORM_DESKTOP_SDL3 // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount))
#endif
{
if ((CORE.Window.flags & FLAG_BORDERLESS_WINDOWED_MODE) > 0)
{
SDL_SetWindowFullscreen(platform.window, 0);
CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
}
else
{
SDL_SetWindowFullscreen(platform.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
CORE.Window.flags |= FLAG_BORDERLESS_WINDOWED_MODE;
}
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
}
// Set window state: maximized, if resizable
void MaximizeWindow(void)
{
SDL_MaximizeWindow(platform.window);
CORE.Window.flags |= FLAG_WINDOW_MAXIMIZED;
}
// Set window state: minimized
void MinimizeWindow(void)
{
SDL_MinimizeWindow(platform.window);
CORE.Window.flags |= FLAG_WINDOW_MINIMIZED;
}
// Set window state: not minimized/maximized
void RestoreWindow(void)
{
SDL_ShowWindow(platform.window);
}
// Set window configuration state using flags
void SetWindowState(unsigned int flags)
{
CORE.Window.flags |= flags;
if (flags & FLAG_VSYNC_HINT)
{
SDL_GL_SetSwapInterval(1);
}
if (flags & FLAG_FULLSCREEN_MODE)
{
const int monitor = SDL_GetWindowDisplayIndex(platform.window);
const int monitorCount = SDL_GetNumVideoDisplays();
#ifdef PLATFORM_DESKTOP_SDL3 // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount))
#endif
{
SDL_SetWindowFullscreen(platform.window, SDL_WINDOW_FULLSCREEN);
CORE.Window.fullscreen = true;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
}
if (flags & FLAG_WINDOW_RESIZABLE)
{
SDL_SetWindowResizable(platform.window, SDL_TRUE);
}
if (flags & FLAG_WINDOW_UNDECORATED)
{
SDL_SetWindowBordered(platform.window, SDL_FALSE);
}
if (flags & FLAG_WINDOW_HIDDEN)
{
SDL_HideWindow(platform.window);
}
if (flags & FLAG_WINDOW_MINIMIZED)
{
SDL_MinimizeWindow(platform.window);
}
if (flags & FLAG_WINDOW_MAXIMIZED)
{
SDL_MaximizeWindow(platform.window);
}
if (flags & FLAG_WINDOW_UNFOCUSED)
{
// NOTE: To be able to implement this part it seems that we should
// do it ourselves, via `Windows.h`, `X11/Xlib.h` or even `Cocoa.h`
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_WINDOW_UNFOCUSED is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_TOPMOST)
{
SDL_SetWindowAlwaysOnTop(platform.window, SDL_FALSE);
}
if (flags & FLAG_WINDOW_ALWAYS_RUN)
{
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_WINDOW_ALWAYS_RUN is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_TRANSPARENT)
{
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_WINDOW_TRANSPARENT is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_HIGHDPI)
{
// NOTE: Such a function does not seem to exist
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_WINDOW_HIGHDPI is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_MOUSE_PASSTHROUGH)
{
//SDL_SetWindowGrab(platform.window, SDL_FALSE);
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_WINDOW_MOUSE_PASSTHROUGH is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_BORDERLESS_WINDOWED_MODE)
{
const int monitor = SDL_GetWindowDisplayIndex(platform.window);
const int monitorCount = SDL_GetNumVideoDisplays();
#ifdef PLATFORM_DESKTOP_SDL3 // SDL3 Migration: Monitor is an id instead of index now, returns 0 on failure
if ((monitor > 0) && (monitor <= monitorCount))
#else
if ((monitor >= 0) && (monitor < monitorCount))
#endif
{
SDL_SetWindowFullscreen(platform.window, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
}
if (flags & FLAG_MSAA_4X_HINT)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); // Enable multisampling buffers
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); // Enable multisampling
}
if (flags & FLAG_INTERLACED_HINT)
{
TRACELOG(LOG_WARNING, "SetWindowState() - FLAG_INTERLACED_HINT is not supported on PLATFORM_DESKTOP_SDL");
}
}
// Clear window configuration state flags
void ClearWindowState(unsigned int flags)
{
CORE.Window.flags &= ~flags;
if (flags & FLAG_VSYNC_HINT)
{
SDL_GL_SetSwapInterval(0);
}
if (flags & FLAG_FULLSCREEN_MODE)
{
SDL_SetWindowFullscreen(platform.window, 0);
CORE.Window.fullscreen = false;
}
if (flags & FLAG_WINDOW_RESIZABLE)
{
SDL_SetWindowResizable(platform.window, SDL_FALSE);
}
if (flags & FLAG_WINDOW_UNDECORATED)
{
SDL_SetWindowBordered(platform.window, SDL_TRUE);
}
if (flags & FLAG_WINDOW_HIDDEN)
{
SDL_ShowWindow(platform.window);
}
if (flags & FLAG_WINDOW_MINIMIZED)
{
SDL_RestoreWindow(platform.window);
}
if (flags & FLAG_WINDOW_MAXIMIZED)
{
SDL_RestoreWindow(platform.window);
}
if (flags & FLAG_WINDOW_UNFOCUSED)
{
//SDL_RaiseWindow(platform.window);
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_WINDOW_UNFOCUSED is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_TOPMOST)
{
SDL_SetWindowAlwaysOnTop(platform.window, SDL_FALSE);
}
if (flags & FLAG_WINDOW_ALWAYS_RUN)
{
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_WINDOW_ALWAYS_RUN is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_TRANSPARENT)
{
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_WINDOW_TRANSPARENT is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_HIGHDPI)
{
// NOTE: There also doesn't seem to be a feature to disable high DPI once enabled
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_WINDOW_HIGHDPI is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_WINDOW_MOUSE_PASSTHROUGH)
{
//SDL_SetWindowGrab(platform.window, SDL_TRUE);
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_WINDOW_MOUSE_PASSTHROUGH is not supported on PLATFORM_DESKTOP_SDL");
}
if (flags & FLAG_BORDERLESS_WINDOWED_MODE)
{
SDL_SetWindowFullscreen(platform.window, 0);
}
if (flags & FLAG_MSAA_4X_HINT)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); // Disable multisampling buffers
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); // Disable multisampling
}
if (flags & FLAG_INTERLACED_HINT)
{
TRACELOG(LOG_WARNING, "ClearWindowState() - FLAG_INTERLACED_HINT is not supported on PLATFORM_DESKTOP_SDL");
}
}
// Set icon for window
void SetWindowIcon(Image image)
{
SDL_Surface *iconSurface = NULL;
unsigned int rmask = 0, gmask = 0, bmask = 0, amask = 0;
int depth = 0; // Depth in bits
int pitch = 0; // Pixel spacing (pitch) in bytes
switch (image.format)
{
case PIXELFORMAT_UNCOMPRESSED_GRAYSCALE:
rmask = 0xFF, gmask = 0;
bmask = 0, amask = 0;
depth = 8, pitch = image.width;
break;
case PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA:
rmask = 0xFF, gmask = 0xFF00;
bmask = 0, amask = 0;
depth = 16, pitch = image.width*2;
break;
case PIXELFORMAT_UNCOMPRESSED_R5G6B5:
rmask = 0xF800, gmask = 0x07E0;
bmask = 0x001F, amask = 0;
depth = 16, pitch = image.width*2;
break;
case PIXELFORMAT_UNCOMPRESSED_R8G8B8: // Uses BGR for 24-bit
rmask = 0x0000FF, gmask = 0x00FF00;
bmask = 0xFF0000, amask = 0;
depth = 24, pitch = image.width*3;
break;
case PIXELFORMAT_UNCOMPRESSED_R5G5B5A1:
rmask = 0xF800, gmask = 0x07C0;
bmask = 0x003E, amask = 0x0001;
depth = 16, pitch = image.width*2;
break;
case PIXELFORMAT_UNCOMPRESSED_R4G4B4A4:
rmask = 0xF000, gmask = 0x0F00;
bmask = 0x00F0, amask = 0x000F;
depth = 16, pitch = image.width*2;
break;
case PIXELFORMAT_UNCOMPRESSED_R8G8B8A8:
rmask = 0xFF000000, gmask = 0x00FF0000;
bmask = 0x0000FF00, amask = 0x000000FF;
depth = 32, pitch = image.width*4;
break;
case PIXELFORMAT_UNCOMPRESSED_R32:
rmask = 0xFFFFFFFF, gmask = 0;
bmask = 0, amask = 0;
depth = 32, pitch = image.width*4;
break;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32:
rmask = 0xFFFFFFFF, gmask = 0xFFFFFFFF;
bmask = 0xFFFFFFFF, amask = 0;
depth = 96, pitch = image.width*12;
break;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32A32:
rmask = 0xFFFFFFFF, gmask = 0xFFFFFFFF;
bmask = 0xFFFFFFFF, amask = 0xFFFFFFFF;
depth = 128, pitch = image.width*16;
break;
case PIXELFORMAT_UNCOMPRESSED_R16:
rmask = 0xFFFF, gmask = 0;
bmask = 0, amask = 0;
depth = 16, pitch = image.width*2;
break;
case PIXELFORMAT_UNCOMPRESSED_R16G16B16:
rmask = 0xFFFF, gmask = 0xFFFF;
bmask = 0xFFFF, amask = 0;
depth = 48, pitch = image.width*6;
break;
case PIXELFORMAT_UNCOMPRESSED_R16G16B16A16:
rmask = 0xFFFF, gmask = 0xFFFF;
bmask = 0xFFFF, amask = 0xFFFF;
depth = 64, pitch = image.width*8;
break;
default: return; // Compressed formats are not supported
}
iconSurface = SDL_CreateRGBSurfaceFrom( image.data, image.width, image.height, depth, pitch, rmask, gmask, bmask, amask );
if (iconSurface)
{
SDL_SetWindowIcon(platform.window, iconSurface);
SDL_FreeSurface(iconSurface);
}
}
// Set icon for window
void SetWindowIcons(Image *images, int count)
{
TRACELOG(LOG_WARNING, "SetWindowIcons() not available on target platform");
}
// Set title for window
void SetWindowTitle(const char *title)
{
SDL_SetWindowTitle(platform.window, title);
CORE.Window.title = title;
}
// Set window position on screen (windowed mode)
void SetWindowPosition(int x, int y)
{
SDL_SetWindowPosition(platform.window, x, y);
CORE.Window.position.x = x;
CORE.Window.position.y = y;
}
// Set monitor for the current window
void SetWindowMonitor(int monitor)
{
const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount))
{
// NOTE:
// 1. SDL started supporting moving exclusive fullscreen windows between displays on SDL3,
// see commit https://github.com/libsdl-org/SDL/commit/3f5ef7dd422057edbcf3e736107e34be4b75d9ba
// 2. A workaround for SDL2 is leaving fullscreen, moving the window, then entering full screen again.
const bool wasFullscreen = ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0)? true : false;
const int screenWidth = CORE.Window.screen.width;
const int screenHeight = CORE.Window.screen.height;
SDL_Rect usableBounds;
#ifdef PLATFORM_DESKTOP_SDL3 // Different style for success checking
if (SDL_GetDisplayUsableBounds(monitor, &usableBounds))
#else
if (SDL_GetDisplayUsableBounds(monitor, &usableBounds) == 0)
#endif
{
if (wasFullscreen == 1) ToggleFullscreen(); // Leave fullscreen.
// If the screen size is larger than the monitor usable area, anchor it on the top left corner, otherwise, center it
if ((screenWidth >= usableBounds.w) || (screenHeight >= usableBounds.h))
{
// NOTE:
// 1. There's a known issue where if the window larger than the target display bounds,
// when moving the windows to that display, the window could be clipped back
// ending up positioned partly outside the target display.
// 2. The workaround for that is, previously to moving the window,
// setting the window size to the target display size, so they match.
// 3. It wasn't done here because we can't assume changing the window size automatically
// is acceptable behavior by the user.
SDL_SetWindowPosition(platform.window, usableBounds.x, usableBounds.y);
CORE.Window.position.x = usableBounds.x;
CORE.Window.position.y = usableBounds.y;
}
else
{
const int x = usableBounds.x + (usableBounds.w/2) - (screenWidth/2);
const int y = usableBounds.y + (usableBounds.h/2) - (screenHeight/2);
SDL_SetWindowPosition(platform.window, x, y);
CORE.Window.position.x = x;
CORE.Window.position.y = y;
}
if (wasFullscreen == 1) ToggleFullscreen(); // Re-enter fullscreen
}
else TRACELOG(LOG_WARNING, "SDL: Failed to get selected display usable bounds");
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
}
// Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
void SetWindowMinSize(int width, int height)
{
SDL_SetWindowMinimumSize(platform.window, width, height);
CORE.Window.screenMin.width = width;
CORE.Window.screenMin.height = height;
}
// Set window maximum dimensions (FLAG_WINDOW_RESIZABLE)
void SetWindowMaxSize(int width, int height)
{
SDL_SetWindowMaximumSize(platform.window, width, height);
CORE.Window.screenMax.width = width;
CORE.Window.screenMax.height = height;
}
// Set window dimensions
void SetWindowSize(int width, int height)
{
SDL_SetWindowSize(platform.window, width, height);
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
}
// Set window opacity, value opacity is between 0.0 and 1.0
void SetWindowOpacity(float opacity)
{
if (opacity >= 1.0f) opacity = 1.0f;
else if (opacity <= 0.0f) opacity = 0.0f;
SDL_SetWindowOpacity(platform.window, opacity);
}
// Set window focused
void SetWindowFocused(void)
{
SDL_RaiseWindow(platform.window);
}
// Get native window handle
void *GetWindowHandle(void)
{
return (void *)platform.window;
}
// Get number of monitors
int GetMonitorCount(void)
{
int monitorCount = 0;
monitorCount = SDL_GetNumVideoDisplays();
return monitorCount;
}
// Get number of monitors
int GetCurrentMonitor(void)
{
int currentMonitor = 0;
// Be aware that this returns an ID in SDL3 and a Index in SDL2
currentMonitor = SDL_GetWindowDisplayIndex(platform.window);
return currentMonitor;
}
// Get selected monitor position
Vector2 GetMonitorPosition(int monitor)
{
const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount))
{
SDL_Rect displayBounds;
#ifdef PLATFORM_DESKTOP_SDL3
if (SDL_GetDisplayUsableBounds(monitor, &displayBounds))
#else
if (SDL_GetDisplayUsableBounds(monitor, &displayBounds) == 0)
#endif
{
return (Vector2){ (float)displayBounds.x, (float)displayBounds.y };
}
else TRACELOG(LOG_WARNING, "SDL: Failed to get selected display usable bounds");
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
return (Vector2){ 0.0f, 0.0f };
}
// Get selected monitor width (currently used by monitor)
int GetMonitorWidth(int monitor)
{
int width = 0;
const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount))
{
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode);
width = mode.w;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
return width;
}
// Get selected monitor height (currently used by monitor)
int GetMonitorHeight(int monitor)
{
int height = 0;
const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount))
{
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode);
height = mode.h;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");
return height;
}
// Get selected monitor physical width in millimetres
int GetMonitorPhysicalWidth(int monitor)
{
int width = 0;
const int monitorCount = SDL_GetNumVideoDisplays();
if ((monitor >= 0) && (monitor < monitorCount))
{
float ddpi = 0.0f;
SDL_GetDisplayDPI(monitor, &ddpi, NULL, NULL);
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(monitor, &mode);
// Calculate size on inches, then convert to millimeter
if (ddpi > 0.0f) width = (mode.w/ddpi)*25.4f;
}
else TRACELOG(LOG_WARNING, "SDL: Failed to find selected monitor");