-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathrcore_desktop_rgfw.c
1364 lines (1183 loc) · 46.7 KB
/
rcore_desktop_rgfw.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_rgfw - Functions to manage window, graphics device and inputs
*
* PLATFORM: RGFW
* - Windows (Win32, Win64)
* - Linux (X11/Wayland desktop mode)
* - MacOS (Cocoa)
* - HTML5 (Emscripten)
* - Others (untested)
*
* LIMITATIONS:
* - TODO
*
* POSSIBLE IMPROVEMENTS:
* - TODO
*
* ADDITIONAL NOTES:
* - TRACELOG() function is located in raylib [utils] module
*
* CONFIGURATION:
* #define RCORE_PLATFORM_RGFW
* Custom flag for rcore on target platform RGFW
*
* DEPENDENCIES:
* - RGFW.h (main library): Windowing and inputs management
* - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
*
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2013-2025 Ramon Santamaria (@raysan5), Colleague Riley 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 RAYLIB_H /* this should never actually happen, it's only here for IDEs */
#include "raylib.h"
#include "../rcore.c"
#endif
#if defined(PLATFORM_WEB_RGFW)
#define RGFW_NO_GL_HEADER
#endif
#if defined(GRAPHICS_API_OPENGL_ES2) && !defined(PLATFORM_WEB_RGFW)
#define RGFW_OPENGL_ES2
#endif
void ShowCursor(void);
void CloseWindow(void);
#if defined(__linux__)
#define _INPUT_EVENT_CODES_H
#endif
#if defined(__unix__) || defined(__linux__)
#define _XTYPEDEF_FONT
#endif
#define RGFW_IMPLEMENTATION
#if defined(_WIN32) || defined(_WIN64)
#define WIN32_LEAN_AND_MEAN
#define Rectangle rectangle_win32
#define CloseWindow CloseWindow_win32
#define ShowCursor __imp_ShowCursor
#define _APISETSTRING_
#undef MAX_PATH
__declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char *lpMultiByteStr, int cbMultiByte, wchar_t *lpWideCharStr, int cchWideChar);
#endif
#if defined(__APPLE__)
#define Point NSPOINT
#define Size NSSIZE
#endif
#define RGFW_ALLOC RL_MALLOC
#define RGFW_FREE RL_FREE
#define RGFW_CALLOC RL_CALLOC
#include "../external/RGFW.h"
#if defined(_WIN32) || defined(_WIN64)
#undef DrawText
#undef ShowCursor
#undef CloseWindow
#undef Rectangle
#undef MAX_PATH
#define MAX_PATH 1025
#endif
#if defined(__APPLE__)
#undef Point
#undef Size
#endif
#include <stdbool.h>
#include <string.h> // Required for: strcmp()
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct {
RGFW_window *window; // Native display device (physical screen connection)
RGFW_monitor mon;
} PlatformData;
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
extern CoreData CORE; // Global CORE state context
static PlatformData platform = { NULL }; // Platform specific
static bool RGFW_disableCursor = false;
static const unsigned short keyMappingRGFW[] = {
[RGFW_keyNULL] = KEY_NULL,
[RGFW_return] = KEY_ENTER,
[RGFW_return] = KEY_ENTER,
[RGFW_apostrophe] = KEY_APOSTROPHE,
[RGFW_comma] = KEY_COMMA,
[RGFW_minus] = KEY_MINUS,
[RGFW_period] = KEY_PERIOD,
[RGFW_slash] = KEY_SLASH,
[RGFW_escape] = KEY_ESCAPE,
[RGFW_F1] = KEY_F1,
[RGFW_F2] = KEY_F2,
[RGFW_F3] = KEY_F3,
[RGFW_F4] = KEY_F4,
[RGFW_F5] = KEY_F5,
[RGFW_F6] = KEY_F6,
[RGFW_F7] = KEY_F7,
[RGFW_F8] = KEY_F8,
[RGFW_F9] = KEY_F9,
[RGFW_F10] = KEY_F10,
[RGFW_F11] = KEY_F11,
[RGFW_F12] = KEY_F12,
[RGFW_backtick] = KEY_GRAVE,
[RGFW_0] = KEY_ZERO,
[RGFW_1] = KEY_ONE,
[RGFW_2] = KEY_TWO,
[RGFW_3] = KEY_THREE,
[RGFW_4] = KEY_FOUR,
[RGFW_5] = KEY_FIVE,
[RGFW_6] = KEY_SIX,
[RGFW_7] = KEY_SEVEN,
[RGFW_8] = KEY_EIGHT,
[RGFW_9] = KEY_NINE,
[RGFW_equals] = KEY_EQUAL,
[RGFW_backSpace] = KEY_BACKSPACE,
[RGFW_tab] = KEY_TAB,
[RGFW_capsLock] = KEY_CAPS_LOCK,
[RGFW_shiftL] = KEY_LEFT_SHIFT,
[RGFW_controlL] = KEY_LEFT_CONTROL,
[RGFW_altL] = KEY_LEFT_ALT,
[RGFW_superL] = KEY_LEFT_SUPER,
#ifndef RGFW_MACOS
[RGFW_shiftR] = KEY_RIGHT_SHIFT,
[RGFW_altR] = KEY_RIGHT_ALT,
#endif
[RGFW_space] = KEY_SPACE,
[RGFW_a] = KEY_A,
[RGFW_b] = KEY_B,
[RGFW_c] = KEY_C,
[RGFW_d] = KEY_D,
[RGFW_e] = KEY_E,
[RGFW_f] = KEY_F,
[RGFW_g] = KEY_G,
[RGFW_h] = KEY_H,
[RGFW_i] = KEY_I,
[RGFW_j] = KEY_J,
[RGFW_k] = KEY_K,
[RGFW_l] = KEY_L,
[RGFW_m] = KEY_M,
[RGFW_n] = KEY_N,
[RGFW_o] = KEY_O,
[RGFW_p] = KEY_P,
[RGFW_q] = KEY_Q,
[RGFW_r] = KEY_R,
[RGFW_s] = KEY_S,
[RGFW_t] = KEY_T,
[RGFW_u] = KEY_U,
[RGFW_v] = KEY_V,
[RGFW_w] = KEY_W,
[RGFW_x] = KEY_X,
[RGFW_y] = KEY_Y,
[RGFW_z] = KEY_Z,
[RGFW_bracket] = KEY_LEFT_BRACKET,
[RGFW_backSlash] = KEY_BACKSLASH,
[RGFW_closeBracket] = KEY_RIGHT_BRACKET,
[RGFW_semicolon] = KEY_SEMICOLON,
[RGFW_insert] = KEY_INSERT,
[RGFW_home] = KEY_HOME,
[RGFW_pageUp] = KEY_PAGE_UP,
[RGFW_delete] = KEY_DELETE,
[RGFW_end] = KEY_END,
[RGFW_pageDown] = KEY_PAGE_DOWN,
[RGFW_right] = KEY_RIGHT,
[RGFW_left] = KEY_LEFT,
[RGFW_down] = KEY_DOWN,
[RGFW_up] = KEY_UP,
[RGFW_numLock] = KEY_NUM_LOCK,
[RGFW_KP_Slash] = KEY_KP_DIVIDE,
[RGFW_multiply] = KEY_KP_MULTIPLY,
[RGFW_KP_Minus] = KEY_KP_SUBTRACT,
[RGFW_KP_Return] = KEY_KP_ENTER,
[RGFW_KP_1] = KEY_KP_1,
[RGFW_KP_2] = KEY_KP_2,
[RGFW_KP_3] = KEY_KP_3,
[RGFW_KP_4] = KEY_KP_4,
[RGFW_KP_5] = KEY_KP_5,
[RGFW_KP_6] = KEY_KP_6,
[RGFW_KP_7] = KEY_KP_7,
[RGFW_KP_8] = KEY_KP_8,
[RGFW_KP_9] = KEY_KP_9,
[RGFW_KP_0] = KEY_KP_0,
[RGFW_KP_Period] = KEY_KP_DECIMAL,
[RGFW_scrollLock] = KEY_SCROLL_LOCK,
};
//----------------------------------------------------------------------------------
// Module Internal Functions Declaration
//----------------------------------------------------------------------------------
int InitPlatform(void); // Initialize platform (graphics, inputs and more)
bool InitGraphicsDevice(void); // Initialize graphics device
//----------------------------------------------------------------------------------
// 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.shouldClose == false)
CORE.Window.shouldClose = RGFW_window_shouldClose(platform.window);
if (CORE.Window.ready) return CORE.Window.shouldClose;
else return true;
}
// Toggle fullscreen mode
void ToggleFullscreen(void)
{
if (!CORE.Window.fullscreen)
{
// Store previous window position (in case we exit fullscreen)
CORE.Window.previousPosition = CORE.Window.position;
platform.mon = RGFW_window_getMonitor(platform.window);
CORE.Window.fullscreen = true;
CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
RGFW_monitor_scaleToWindow(platform.mon, platform.window);
RGFW_window_setFullscreen(platform.window, 1);
}
else
{
CORE.Window.fullscreen = false;
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
if (platform.mon.mode.area.w)
{
RGFW_monitor monitor = RGFW_window_getMonitor(platform.window);
RGFW_monitor_requestMode(monitor, platform.mon.mode, RGFW_monitorScale);
platform.mon.mode.area.w = 0;
}
// we update the window position right away
CORE.Window.position = CORE.Window.previousPosition;
RGFW_window_move(platform.window, RGFW_POINT(CORE.Window.position.x, CORE.Window.position.y));
}
// Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
// NOTE: V-Sync can be enabled by graphic driver configuration
if (CORE.Window.flags & FLAG_VSYNC_HINT) RGFW_window_swapInterval(platform.window, 1);
}
// Toggle borderless windowed mode
void ToggleBorderlessWindowed(void)
{
if (platform.window == NULL)
return;
if (CORE.Window.fullscreen)
{
CORE.Window.previousPosition = CORE.Window.position;
CORE.Window.previousScreen = CORE.Window.screen;
RGFW_window_setBorder(platform.window, 0);
RGFW_monitor mon = RGFW_window_getMonitor(platform.window);
RGFW_window_resize(platform.window, mon.mode.area);
} else {
RGFW_window_setBorder(platform.window, 1);
CORE.Window.position = CORE.Window.previousPosition;
RGFW_window_resize(platform.window, RGFW_AREA(CORE.Window.previousScreen.width, CORE.Window.previousScreen.height));
}
CORE.Window.fullscreen = !CORE.Window.fullscreen;
}
// Set window state: maximized, if resizable
void MaximizeWindow(void)
{
RGFW_window_maximize(platform.window);
}
// Set window state: minimized
void MinimizeWindow(void)
{
RGFW_window_minimize(platform.window);
}
// Set window state: not minimized/maximized
void RestoreWindow(void)
{
if (!(CORE.Window.flags & FLAG_WINDOW_UNFOCUSED))
RGFW_window_focus(platform.window);
RGFW_window_restore(platform.window);
}
// Set window configuration state using flags
void SetWindowState(unsigned int flags)
{
CORE.Window.flags |= flags;
if (flags & FLAG_VSYNC_HINT)
{
RGFW_window_swapInterval(platform.window, 1);
}
if (flags & FLAG_FULLSCREEN_MODE)
{
if (!CORE.Window.fullscreen)
ToggleFullscreen();
}
if (flags & FLAG_WINDOW_RESIZABLE)
{
RGFW_window_setMaxSize(platform.window, RGFW_AREA(0, 0));
RGFW_window_setMinSize(platform.window, RGFW_AREA(0, 0));
}
if (flags & FLAG_WINDOW_UNDECORATED)
{
RGFW_window_setBorder(platform.window, 0);
}
if (flags & FLAG_WINDOW_HIDDEN)
{
RGFW_window_hide(platform.window);
}
if (flags & FLAG_WINDOW_MINIMIZED)
{
RGFW_window_minimize(platform.window);
}
if (flags & FLAG_WINDOW_MAXIMIZED)
{
RGFW_window_maximize(platform.window);
}
if (flags & FLAG_WINDOW_UNFOCUSED)
{
CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED;
}
if (flags & FLAG_WINDOW_TOPMOST)
{
RGFW_window_setFloating(platform.window, RGFW_TRUE);
}
if (flags & FLAG_WINDOW_ALWAYS_RUN)
{
CORE.Window.flags |= FLAG_WINDOW_ALWAYS_RUN;
}
if (flags & FLAG_WINDOW_TRANSPARENT)
{
TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
}
if (flags & FLAG_WINDOW_HIGHDPI)
{
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
}
if (flags & FLAG_WINDOW_MOUSE_PASSTHROUGH)
{
RGFW_window_setMousePassthrough(platform.window, 1);
}
if (flags & FLAG_BORDERLESS_WINDOWED_MODE)
{
ToggleBorderlessWindowed();
}
if (flags & FLAG_MSAA_4X_HINT)
{
RGFW_setGLHint(RGFW_glSamples, 4);
}
if (flags & FLAG_INTERLACED_HINT)
{
TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
}
}
// Clear window configuration state flags
void ClearWindowState(unsigned int flags)
{
CORE.Window.flags &= ~flags;
if (flags & FLAG_VSYNC_HINT)
{
RGFW_window_swapInterval(platform.window, 0);
}
if (flags & FLAG_FULLSCREEN_MODE)
{
if (CORE.Window.fullscreen)
ToggleFullscreen();
}
if (flags & FLAG_WINDOW_RESIZABLE)
{
RGFW_window_setMaxSize(platform.window, RGFW_AREA(platform.window->r.w, platform.window->r.h));
RGFW_window_setMinSize(platform.window, RGFW_AREA(platform.window->r.w, platform.window->r.h));
}
if (flags & FLAG_WINDOW_UNDECORATED)
{
RGFW_window_setBorder(platform.window, 1);
}
if (flags & FLAG_WINDOW_HIDDEN)
{
if (!(CORE.Window.flags & FLAG_WINDOW_UNFOCUSED))
RGFW_window_focus(platform.window);
RGFW_window_show(platform.window);
}
if (flags & FLAG_WINDOW_MINIMIZED)
{
if (!(CORE.Window.flags & FLAG_WINDOW_UNFOCUSED))
RGFW_window_focus(platform.window);
RGFW_window_restore(platform.window);
}
if (flags & FLAG_WINDOW_MAXIMIZED)
{
if (!(CORE.Window.flags & FLAG_WINDOW_UNFOCUSED))
RGFW_window_focus(platform.window);
RGFW_window_restore(platform.window);
}
if (flags & FLAG_WINDOW_UNFOCUSED)
{
CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED;
}
if (flags & FLAG_WINDOW_TOPMOST)
{
RGFW_window_setFloating(platform.window, RGFW_FALSE);
}
if (flags & FLAG_WINDOW_ALWAYS_RUN)
{
CORE.Window.flags &= ~FLAG_WINDOW_ALWAYS_RUN;
}
if (flags & FLAG_WINDOW_TRANSPARENT)
{
TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
}
if (flags & FLAG_WINDOW_HIGHDPI)
{
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
}
if (flags & FLAG_WINDOW_MOUSE_PASSTHROUGH)
{
RGFW_window_setMousePassthrough(platform.window, 0);
}
if (flags & FLAG_BORDERLESS_WINDOWED_MODE)
{
if (CORE.Window.fullscreen)
ToggleBorderlessWindowed();
}
if (flags & FLAG_MSAA_4X_HINT)
{
RGFW_setGLHint(RGFW_glSamples, 0);
}
if (flags & FLAG_INTERLACED_HINT)
{
TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
}
}
int RGFW_formatToChannels(int format)
{
switch (format) {
case PIXELFORMAT_UNCOMPRESSED_GRAYSCALE:
case PIXELFORMAT_UNCOMPRESSED_R16: // 16 bpp (1 channel - half float)
case PIXELFORMAT_UNCOMPRESSED_R32: // 32 bpp (1 channel - float)
return 1;
case PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: // 8*2 bpp (2 channels)
case PIXELFORMAT_UNCOMPRESSED_R5G6B5: // 16 bpp
case PIXELFORMAT_UNCOMPRESSED_R8G8B8: // 24 bpp
case PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: // 16 bpp (1 bit alpha)
case PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: // 16 bpp (4 bit alpha)
case PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: // 32 bpp
return 2;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32: // 32*3 bpp (3 channels - float)
case PIXELFORMAT_UNCOMPRESSED_R16G16B16: // 16*3 bpp (3 channels - half float)
case PIXELFORMAT_COMPRESSED_DXT1_RGB: // 4 bpp (no alpha)
case PIXELFORMAT_COMPRESSED_ETC1_RGB: // 4 bpp
case PIXELFORMAT_COMPRESSED_ETC2_RGB: // 4 bpp
case PIXELFORMAT_COMPRESSED_PVRT_RGB: // 4 bpp
return 3;
case PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: // 32*4 bpp (4 channels - float)
case PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: // 16*4 bpp (4 channels - half float)
case PIXELFORMAT_COMPRESSED_DXT1_RGBA: // 4 bpp (1 bit alpha)
case PIXELFORMAT_COMPRESSED_DXT3_RGBA: // 8 bpp
case PIXELFORMAT_COMPRESSED_DXT5_RGBA: // 8 bpp
case PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: // 8 bpp
case PIXELFORMAT_COMPRESSED_PVRT_RGBA: // 4 bpp
case PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: // 8 bpp
case PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: // 2 bpp
return 4;
default: return 4;
}
}
// Set icon for window
void SetWindowIcon(Image image)
{
RGFW_window_setIcon(platform.window, image.data, RGFW_AREA(image.width, image.height), RGFW_formatToChannels(image.format));
}
// Set icon for window
void SetWindowIcons(Image *images, int count)
{
if ((images == NULL) || (count <= 0))
{
RGFW_window_setIcon(platform.window, NULL, RGFW_AREA(0, 0), 0);
} {
Image* bigIcon = NULL;
Image* smallIcon = NULL;
for (size_t i = 0; i < count; i++) {
if (bigIcon == NULL || (images[i].width > bigIcon->width && images[i].height > bigIcon->height))
bigIcon = &images[i];
if (smallIcon == NULL || (images[i].width < smallIcon->width && images[i].height > smallIcon->height))
smallIcon = &images[i];
}
if (smallIcon != NULL)
RGFW_window_setIconEx(platform.window, smallIcon->data, RGFW_AREA(smallIcon->width, smallIcon->height), RGFW_formatToChannels(smallIcon->format), RGFW_iconWindow);
if (bigIcon != NULL)
RGFW_window_setIconEx(platform.window, bigIcon->data, RGFW_AREA(bigIcon->width, bigIcon->height), RGFW_formatToChannels(bigIcon->format), RGFW_iconTaskbar);
}
}
// Set title for window
void SetWindowTitle(const char *title)
{
RGFW_window_setName(platform.window, (char *)title);
CORE.Window.title = title;
}
// Set window position on screen (windowed mode)
void SetWindowPosition(int x, int y)
{
RGFW_window_move(platform.window, RGFW_POINT(x, y));
}
// Set monitor for the current window
void SetWindowMonitor(int monitor)
{
RGFW_window_moveToMonitor(platform.window, RGFW_getMonitors()[monitor]);
}
// Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
void SetWindowMinSize(int width, int height)
{
RGFW_window_setMinSize(platform.window, RGFW_AREA(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)
{
RGFW_window_setMaxSize(platform.window, RGFW_AREA(width, height));
CORE.Window.screenMax.width = width;
CORE.Window.screenMax.height = height;
}
// Set window dimensions
void SetWindowSize(int width, int height)
{
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
RGFW_window_resize(platform.window, RGFW_AREA(width, height));
}
// Set window opacity, value opacity is between 0.0 and 1.0
void SetWindowOpacity(float opacity)
{
RGFW_window_setOpacity(platform.window, opacity);
}
// Set window focused
void SetWindowFocused(void)
{
RGFW_window_focus(platform.window);
}
// Get native window handle
void *GetWindowHandle(void)
{
#ifdef RGFW_WEBASM
return (void *)platform.window->src.ctx;
#else
return (void *)platform.window->src.window;
#endif
}
// Get number of monitors
int GetMonitorCount(void)
{
#define MAX_MONITORS_SUPPORTED 6
int count = MAX_MONITORS_SUPPORTED;
RGFW_monitor *mons = RGFW_getMonitors();
for (int i = 0; i < 6; i++)
{
if (!mons[i].x && !mons[i].y && !mons[i].mode.area.w && mons[i].mode.area.h)
{
count = i;
break;
}
}
return count;
}
// Get number of monitors
int GetCurrentMonitor(void)
{
RGFW_monitor *mons = RGFW_getMonitors();
RGFW_monitor mon = RGFW_window_getMonitor(platform.window);
for (int i = 0; i < 6; i++)
{
if ((mons[i].x == mon.x) && (mons[i].y == mon.y)) return i;
}
return 0;
}
// Get selected monitor position
Vector2 GetMonitorPosition(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return (Vector2){ (float)mons[monitor].x, (float)mons[monitor].y };
}
// Get selected monitor width (currently used by monitor)
int GetMonitorWidth(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return mons[monitor].mode.area.w;
}
// Get selected monitor height (currently used by monitor)
int GetMonitorHeight(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return mons[monitor].mode.area.h;
}
// Get selected monitor physical width in millimetres
int GetMonitorPhysicalWidth(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return mons[monitor].physW;
}
// Get selected monitor physical height in millimetres
int GetMonitorPhysicalHeight(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return (int)mons[monitor].physH;
}
// Get selected monitor refresh rate
int GetMonitorRefreshRate(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return (int)mons[monitor].mode.refreshRate;
}
// Get the human-readable, UTF-8 encoded name of the selected monitor
const char *GetMonitorName(int monitor)
{
RGFW_monitor *mons = RGFW_getMonitors();
return mons[monitor].name;
}
// Get window position XY on monitor
Vector2 GetWindowPosition(void)
{
return (Vector2){ (float)platform.window->r.x, (float)platform.window->r.y };
}
// Get window scale DPI factor for current monitor
Vector2 GetWindowScaleDPI(void)
{
RGFW_monitor monitor = RGFW_window_getMonitor(platform.window);
return (Vector2){monitor.scaleX, monitor.scaleX};
}
// Set clipboard text content
void SetClipboardText(const char *text)
{
RGFW_writeClipboard(text, strlen(text));
}
// Get clipboard text content
// NOTE: returned string is allocated and freed by RGFW
const char *GetClipboardText(void)
{
return RGFW_readClipboard(NULL);
}
#if defined(SUPPORT_CLIPBOARD_IMAGE)
#if defined(_WIN32)
#define WIN32_CLIPBOARD_IMPLEMENTATION
#define WINUSER_ALREADY_INCLUDED
#define WINBASE_ALREADY_INCLUDED
#define WINGDI_ALREADY_INCLUDED
#include "../external/win32_clipboard.h"
#endif
#endif
// Get clipboard image
Image GetClipboardImage(void)
{
Image image = { 0 };
unsigned long long int dataSize = 0;
void *fileData = NULL;
#if defined(SUPPORT_CLIPBOARD_IMAGE)
#if defined(_WIN32)
int width = 0;
int height = 0;
fileData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize);
if (fileData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data");
else image = LoadImageFromMemory(".bmp", fileData, dataSize);
#else
TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_DESKTOP_RGFW doesn't implement GetClipboardImage() for this OS");
#endif
#endif // SUPPORT_CLIPBOARD_IMAGE
return image;
}
// Show mouse cursor
void ShowCursor(void)
{
RGFW_window_showMouse(platform.window, true);
CORE.Input.Mouse.cursorHidden = false;
}
// Hides mouse cursor
void HideCursor(void)
{
RGFW_window_showMouse(platform.window, false);
CORE.Input.Mouse.cursorHidden = true;
}
// Enables cursor (unlock cursor)
void EnableCursor(void)
{
RGFW_disableCursor = false;
RGFW_window_mouseUnhold(platform.window);
// Set cursor position in the middle
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
RGFW_window_showMouse(platform.window, true);
CORE.Input.Mouse.cursorHidden = false;
}
// Disables cursor (lock cursor)
void DisableCursor(void)
{
RGFW_disableCursor = true;
RGFW_window_mouseHold(platform.window, RGFW_AREA(0, 0));
HideCursor();
}
// Swap back buffer with front buffer (screen drawing)
void SwapScreenBuffer(void)
{
RGFW_window_swapBuffers(platform.window);
}
//----------------------------------------------------------------------------------
// Module Functions Definition: Misc
//----------------------------------------------------------------------------------
// Get elapsed time measure in seconds since InitTimer()
double GetTime(void)
{
double time = 0.0;
unsigned long long int nanoSeconds = RGFW_getTimeNS();
time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
return time;
}
// Open URL with default system browser (if available)
// NOTE: This function is only safe to use if you control the URL given.
// A user could craft a malicious string performing another action.
// Only call this function yourself not with user input or make sure to check the string yourself.
// Ref: https://github.com/raysan5/raylib/issues/686
void OpenURL(const char *url)
{
// Security check to (partially) avoid malicious code on target platform
if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
else
{
// TODO: Open URL implementation
}
}
//----------------------------------------------------------------------------------
// Module Functions Definition: Inputs
//----------------------------------------------------------------------------------
// Set internal gamepad mappings
int SetGamepadMappings(const char *mappings)
{
TRACELOG(LOG_WARNING, "SetGamepadMappings() unsupported on target platform");
return 0;
}
// Set gamepad vibration
void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
{
TRACELOG(LOG_WARNING, "SetGamepadVibration() unsupported on target platform");
}
// Set mouse position XY
void SetMousePosition(int x, int y)
{
RGFW_window_moveMouse(platform.window, RGFW_POINT(x, y));
CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
}
// Set mouse cursor
void SetMouseCursor(int cursor)
{
RGFW_window_setMouseStandard(platform.window, cursor);
}
// Get physical key name.
const char *GetKeyName(int key)
{
TRACELOG(LOG_WARNING, "GetKeyName() unsupported on target platform");
return "";
}
static KeyboardKey ConvertScancodeToKey(u32 keycode);
int RGFW_gpConvTable[18] = {
[RGFW_gamepadY] = GAMEPAD_BUTTON_RIGHT_FACE_UP,
[RGFW_gamepadB] = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT,
[RGFW_gamepadA] = GAMEPAD_BUTTON_RIGHT_FACE_DOWN,
[RGFW_gamepadX] = GAMEPAD_BUTTON_RIGHT_FACE_LEFT,
[RGFW_gamepadL1] = GAMEPAD_BUTTON_LEFT_TRIGGER_1,
[RGFW_gamepadR1] = GAMEPAD_BUTTON_RIGHT_TRIGGER_1,
[RGFW_gamepadL2] = GAMEPAD_BUTTON_LEFT_TRIGGER_2,
[RGFW_gamepadR2] = GAMEPAD_BUTTON_RIGHT_TRIGGER_2,
[RGFW_gamepadSelect] = GAMEPAD_BUTTON_MIDDLE_LEFT,
[RGFW_gamepadHome] = GAMEPAD_BUTTON_MIDDLE,
[RGFW_gamepadStart] = GAMEPAD_BUTTON_MIDDLE_RIGHT,
[RGFW_gamepadUp] = GAMEPAD_BUTTON_LEFT_FACE_UP,
[RGFW_gamepadRight] = GAMEPAD_BUTTON_LEFT_FACE_RIGHT,
[RGFW_gamepadDown] = GAMEPAD_BUTTON_LEFT_FACE_DOWN,
[RGFW_gamepadLeft] = GAMEPAD_BUTTON_LEFT_FACE_LEFT,
[RGFW_gamepadL3] = GAMEPAD_BUTTON_LEFT_THUMB,
[RGFW_gamepadR3] = GAMEPAD_BUTTON_RIGHT_THUMB,
};
// Register all input events
void PollInputEvents(void)
{
#if defined(SUPPORT_GESTURES_SYSTEM)
// NOTE: Gestures update must be called every frame to reset gestures correctly
// because ProcessGestureEvent() is just called on an event, not every frame
UpdateGestures();
#endif
// Reset keys/chars pressed registered
CORE.Input.Keyboard.keyPressedQueueCount = 0;
CORE.Input.Keyboard.charPressedQueueCount = 0;
// Reset mouse wheel
CORE.Input.Mouse.currentWheelMove.x = 0;
CORE.Input.Mouse.currentWheelMove.y = 0;
// Register previous mouse position
// Reset last gamepad button/axis registered state
for (int i = 0; (i < 4) && (i < MAX_GAMEPADS); i++)
{
// Check if gamepad is available
if (CORE.Input.Gamepad.ready[i])
{
// Register previous gamepad button states
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++)
{
CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
}
}
}
// Register previous touch states
for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
// Map touch position to mouse position for convenience
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
int touchAction = -1; // 0-TOUCH_ACTION_UP, 1-TOUCH_ACTION_DOWN, 2-TOUCH_ACTION_MOVE
bool realTouch = false; // Flag to differentiate real touch gestures from mouse ones
// Register previous keys states
// NOTE: Android supports up to 260 keys
for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
{
CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
}
// Register previous mouse states
for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
// Poll input events for current platform
//-----------------------------------------------------------------------------
CORE.Window.resizedLastFrame = false;
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
if (platform.window->_flags & RGFW_HOLD_MOUSE)
{
CORE.Input.Mouse.previousPosition = (Vector2){ 0.0f, 0.0f };
CORE.Input.Mouse.currentPosition = (Vector2){ 0.0f, 0.0f };
}
else
{
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
}
while (RGFW_window_checkEvent(platform.window))
{
RGFW_event *event = &platform.window->event;
// All input events can be processed after polling
switch (event->type)
{
case RGFW_quit:
if (CORE.Window.flags & FLAG_WINDOW_ALWAYS_RUN)
event->type = 0;
else
CORE.Window.shouldClose = true;
break;
case RGFW_DND: // Dropped file
{
for (int i = 0; i < event->droppedFilesCount; i++)
{
if (CORE.Window.dropFileCount == 0)
{