Debian Codegraph @

src/platforms/rcore_desktop_rgfw.c

raw symbols SCIP
1 /**********************************************************************************************
2 *
3 * rcore_desktop_rgfw - Functions to manage window, graphics device and inputs
4 *
5 * PLATFORM: RGFW
6 * - Windows (Win32, Win64)
7 * - Linux (X11/Wayland desktop mode)
8 * - MacOS (Cocoa)
9 * - HTML5 (Emscripten)
10 * - Others (untested)
11 *
12 * LIMITATIONS:
13 * - TODO
14 *
15 * POSSIBLE IMPROVEMENTS:
16 * - TBD
17 *
18 * CONFIGURATION:
19 * #define RCORE_PLATFORM_RGFW
20 * Custom flag for rcore on target platform RGFW
21 *
22 * DEPENDENCIES:
23 * - RGFW.h (main library): Windowing and inputs management
24 * - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
25 *
26 *
27 * LICENSE: zlib/libpng
28 *
29 * Copyright (c) 2013-2026 Ramon Santamaria (@raysan5), Colleague Riley and contributors
30 *
31 * This software is provided "as-is", without any express or implied warranty. In no event
32 * will the authors be held liable for any damages arising from the use of this software.
33 *
34 * Permission is granted to anyone to use this software for any purpose, including commercial
35 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
36 *
37 * 1. The origin of this software must not be misrepresented; you must not claim that you
38 * wrote the original software. If you use this software in a product, an acknowledgment
39 * in the product documentation would be appreciated but is not required.
40 *
41 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
42 * as being the original software.
43 *
44 * 3. This notice may not be removed or altered from any source distribution.
45 *
46 **********************************************************************************************/
47
48 #if defined(_WIN32) || defined(_WIN64)
49 #define BI_ALPHABITFIELDS 4
50 #define LoadImage LoadImageA
51
52 // Temporarily rename conflicting symbols
53 #define CloseWindow CloseWindowWin32
54 #define Rectangle RectangleWin32
55 #define ShowCursor ShowCursorWin32
56
57 #define WIN32_LEAN_AND_MEAN
58 #include <windows.h>
59
60 // Restore for raylib/RGFW
61 #undef CloseWindow
62 #undef Rectangle
63 #undef ShowCursor
64 #undef LoadImage
65
66 #include "../external/fix_win32_compatibility.h"
67 #endif
68
69 #if defined(PLATFORM_WEB_RGFW)
70 #define RGFW_NO_GL_HEADER
71 #endif
72
73 #if defined(GRAPHICS_API_OPENGL_ES2) && !defined(PLATFORM_WEB_RGFW)
74 #define RGFW_OPENGL_ES2
75 #endif
76
77 void ShowCursor(void);
78 void CloseWindow(void);
79 double get_time_seconds(void);
80
81 #if defined(__unix__) || defined(__linux__)
82 #define _XTYPEDEF_FONT
83 #endif
84
85 #define RGFW_OPENGL
86 #define RGFW_IMPLEMENTATION
87
88 #if defined(_WIN32) || defined(_WIN64)
89 #define WIN32_LEAN_AND_MEAN
90 #define Rectangle rectangle_win32
91 #define CloseWindow CloseWindow_win32
92 #define ShowCursor __imp_ShowCursor
93 #define _APISETSTRING_
94
95 #if defined(__cplusplus)
96 extern "C" {
97 #endif
98 __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char *lpMultiByteStr, int cbMultiByte, wchar_t *lpWideCharStr, int cchWideChar);
99 #if defined(__cplusplus)
100 }
101 #endif
102
103 #endif
104
105 #if defined(__APPLE__)
106 #define Point NSPOINT
107 #define Size NSSIZE
108
109 #ifdef GetColor
110 #undef GetColor
111 #endif
112 #define GetColor GetColor_osx
113 #ifdef EventType
114 #undef EventType
115 #endif
116 #define EventType EventType_osx
117 #endif
118
119 #if defined(__APPLE__)
120 // older macs (unsupported?) are missing these
121 #include <IOKit/hid/IOHIDUsageTables.h>
122 #ifndef kHIDUsage_Button_5
123 #define kHIDUsage_Button_5 0x05
124 #endif
125 #ifndef kHIDUsage_Button_6
126 #define kHIDUsage_Button_6 0x06
127 #endif
128 #ifndef kHIDUsage_Button_7
129 #define kHIDUsage_Button_7 0x07
130 #endif
131 #ifndef kHIDUsage_Button_8
132 #define kHIDUsage_Button_8 0x08
133 #endif
134 #ifndef kHIDUsage_Button_9
135 #define kHIDUsage_Button_9 0x09
136 #endif
137 #ifndef kHIDUsage_Button_10
138 #define kHIDUsage_Button_10 0x0A
139 #endif
140 #endif
141
142 // minigamepad used for gamepad support
143 #define MG_MAX_GAMEPADS MAX_GAMEPADS // copy raylibs define into minigamepad
144 #define MG_IMPLEMENTATION
145 #include "../external/RGFW/deps/minigamepad.h"
146
147 #define RGFW_ALLOC RL_MALLOC
148 #define RGFW_FREE RL_FREE
149 #define RGFW_CALLOC RL_CALLOC
150 #define RGFW_INT_DEFINED 1 // to avoid issues with minigamepad+RGFW definitions
151
152 #include "../external/RGFW/RGFW.h"
153
154 #if defined(_WIN32) || defined(_WIN64)
155 #undef DrawText
156 #undef ShowCursor
157 #undef CloseWindow
158 #undef Rectangle
159
160 #ifdef MAX_PATH
161 #undef MAX_PATH
162 #endif
163 #define MAX_PATH 1025
164 #endif
165
166 #if defined(__APPLE__)
167 #undef Point
168 #undef Size
169 #endif
170
171 #include <stdbool.h>
172
173 //----------------------------------------------------------------------------------
174 // Types and Structures Definition
175 //----------------------------------------------------------------------------------
176 typedef struct {
177 RGFW_window *window; // Native display device (physical screen connection)
178 RGFW_monitor *monitor;
179 mg_gamepads minigamepad;
180
181 #if defined(GRAPHICS_API_OPENGL_SOFTWARE)
182 RGFW_surface *surface;
183 u8 *surfacePixels;
184 i32 surfaceWidth;
185 i32 surfaceHeight;
186 #endif
187 } PlatformData;
188
189 //----------------------------------------------------------------------------------
190 // Global Variables Definition
191 //----------------------------------------------------------------------------------
192 extern CoreData CORE; // Global CORE state context
193
194 static PlatformData platform = { 0 }; // Platform specific
195
196 #if defined(__linux__) // prevent collision of raylibs KEY_<X> and linux/input.h KEY_<X>
197 #undef KEY_NULL
198 // Alphanumeric keys
199 #undef KEY_APOSTROPHE
200 #undef KEY_COMMA
201 #undef KEY_MINUS
202 #undef KEY_PERIOD
203 #undef KEY_SLASH
204 #undef KEY_ZERO
205 #undef KEY_ONE
206 #undef KEY_TWO
207 #undef KEY_THREE
208 #undef KEY_FOUR
209 #undef KEY_FIVE
210 #undef KEY_SIX
211 #undef KEY_SEVEN
212 #undef KEY_EIGHT
213 #undef KEY_NINE
214 #undef KEY_SEMICOLON
215 #undef KEY_EQUAL
216 #undef KEY_A
217 #undef KEY_B
218 #undef KEY_C
219 #undef KEY_D
220 #undef KEY_E
221 #undef KEY_F
222 #undef KEY_G
223 #undef KEY_H
224 #undef KEY_I
225 #undef KEY_J
226 #undef KEY_K
227 #undef KEY_L
228 #undef KEY_M
229 #undef KEY_N
230 #undef KEY_O
231 #undef KEY_P
232 #undef KEY_Q
233 #undef KEY_R
234 #undef KEY_S
235 #undef KEY_T
236 #undef KEY_U
237 #undef KEY_V
238 #undef KEY_W
239 #undef KEY_X
240 #undef KEY_Y
241 #undef KEY_Z
242 #undef KEY_LEFT_BRACKET
243 #undef KEY_BACKSLASH
244 #undef KEY_RIGHT_BRACKET
245 #undef KEY_GRAVE
246 // Function keys
247 #undef KEY_SPACE
248 #undef KEY_ESCAPE
249 #undef KEY_ENTER
250 #undef KEY_TAB
251 #undef KEY_BACKSPACE
252 #undef KEY_INSERT
253 #undef KEY_DELETE
254 #undef KEY_RIGHT
255 #undef KEY_LEFT
256 #undef KEY_DOWN
257 #undef KEY_UP
258 #undef KEY_PAGE_UP
259 #undef KEY_PAGE_DOWN
260 #undef KEY_HOME
261 #undef KEY_END
262 #undef KEY_CAPS_LOCK
263 #undef KEY_SCROLL_LOCK
264 #undef KEY_NUM_LOCK
265 #undef KEY_PRINT_SCREEN
266 #undef KEY_PAUSE
267 #undef KEY_F1
268 #undef KEY_F2
269 #undef KEY_F3
270 #undef KEY_F4
271 #undef KEY_F5
272 #undef KEY_F6
273 #undef KEY_F7
274 #undef KEY_F8
275 #undef KEY_F9
276 #undef KEY_F10
277 #undef KEY_F11
278 #undef KEY_F12
279 #undef KEY_LEFT_SHIFT
280 #undef KEY_LEFT_CONTROL
281 #undef KEY_LEFT_ALT
282 #undef KEY_LEFT_SUPER
283 #undef KEY_RIGHT_SHIFT
284 #undef KEY_RIGHT_CONTROL
285 #undef KEY_RIGHT_ALT
286 #undef KEY_RIGHT_SUPER
287 #undef KEY_KB_MENU
288 // Keypad keys
289 #undef KEY_KP_0
290 #undef KEY_KP_1
291 #undef KEY_KP_2
292 #undef KEY_KP_3
293 #undef KEY_KP_4
294 #undef KEY_KP_5
295 #undef KEY_KP_6
296 #undef KEY_KP_7
297 #undef KEY_KP_8
298 #undef KEY_KP_9
299 #undef KEY_KP_DECIMAL
300 #undef KEY_KP_DIVIDE
301 #undef KEY_KP_MULTIPLY
302 #undef KEY_KP_SUBTRACT
303 #undef KEY_KP_ADD
304 #undef KEY_KP_ENTER
305 #undef KEY_KP_EQUAL
306 #endif
307
308 static const unsigned short RGFW_keyConvertTable[] = {
309 [RGFW_keyNULL] = KEY_NULL,
310 [RGFW_apostrophe] = KEY_APOSTROPHE,
311 [RGFW_comma] = KEY_COMMA,
312 [RGFW_minus] = KEY_MINUS,
313 [RGFW_period] = KEY_PERIOD,
314 [RGFW_slash] = KEY_SLASH,
315 [RGFW_0] = KEY_ZERO,
316 [RGFW_1] = KEY_ONE,
317 [RGFW_2] = KEY_TWO,
318 [RGFW_3] = KEY_THREE,
319 [RGFW_4] = KEY_FOUR,
320 [RGFW_5] = KEY_FIVE,
321 [RGFW_6] = KEY_SIX,
322 [RGFW_7] = KEY_SEVEN,
323 [RGFW_8] = KEY_EIGHT,
324 [RGFW_9] = KEY_NINE,
325 [RGFW_semicolon] = KEY_SEMICOLON,
326 [RGFW_equals] = KEY_EQUAL,
327 [RGFW_a] = KEY_A,
328 [RGFW_b] = KEY_B,
329 [RGFW_c] = KEY_C,
330 [RGFW_d] = KEY_D,
331 [RGFW_e] = KEY_E,
332 [RGFW_f] = KEY_F,
333 [RGFW_g] = KEY_G,
334 [RGFW_h] = KEY_H,
335 [RGFW_i] = KEY_I,
336 [RGFW_j] = KEY_J,
337 [RGFW_k] = KEY_K,
338 [RGFW_l] = KEY_L,
339 [RGFW_m] = KEY_M,
340 [RGFW_n] = KEY_N,
341 [RGFW_o] = KEY_O,
342 [RGFW_p] = KEY_P,
343 [RGFW_q] = KEY_Q,
344 [RGFW_r] = KEY_R,
345 [RGFW_s] = KEY_S,
346 [RGFW_t] = KEY_T,
347 [RGFW_u] = KEY_U,
348 [RGFW_v] = KEY_V,
349 [RGFW_w] = KEY_W,
350 [RGFW_x] = KEY_X,
351 [RGFW_y] = KEY_Y,
352 [RGFW_z] = KEY_Z,
353 [RGFW_bracket] = KEY_LEFT_BRACKET,
354 [RGFW_backSlash] = KEY_BACKSLASH,
355 [RGFW_closeBracket] = KEY_RIGHT_BRACKET,
356 [RGFW_backtick] = KEY_GRAVE,
357 [RGFW_space] = KEY_SPACE,
358 [RGFW_escape] = KEY_ESCAPE,
359 [RGFW_return] = KEY_ENTER,
360 [RGFW_tab] = KEY_TAB,
361 [RGFW_backSpace] = KEY_BACKSPACE,
362 [RGFW_insert] = KEY_INSERT,
363 [RGFW_delete] = KEY_DELETE,
364 [RGFW_right] = KEY_RIGHT,
365 [RGFW_left] = KEY_LEFT,
366 [RGFW_down] = KEY_DOWN,
367 [RGFW_up] = KEY_UP,
368 [RGFW_pageUp] = KEY_PAGE_UP,
369 [RGFW_pageDown] = KEY_PAGE_DOWN,
370 [RGFW_home] = KEY_HOME,
371 [RGFW_end] = KEY_END,
372 [RGFW_capsLock] = KEY_CAPS_LOCK,
373 [RGFW_scrollLock] = KEY_SCROLL_LOCK,
374 [RGFW_numLock] = KEY_NUM_LOCK,
375 [RGFW_printScreen] = KEY_PRINT_SCREEN,
376 [RGFW_pause] = KEY_PAUSE,
377 [RGFW_F1] = KEY_F1,
378 [RGFW_F2] = KEY_F2,
379 [RGFW_F3] = KEY_F3,
380 [RGFW_F4] = KEY_F4,
381 [RGFW_F5] = KEY_F5,
382 [RGFW_F6] = KEY_F6,
383 [RGFW_F7] = KEY_F7,
384 [RGFW_F8] = KEY_F8,
385 [RGFW_F9] = KEY_F9,
386 [RGFW_F10] = KEY_F10,
387 [RGFW_F11] = KEY_F11,
388 [RGFW_F12] = KEY_F12,
389 [RGFW_shiftL] = KEY_LEFT_SHIFT,
390 [RGFW_controlL] = KEY_LEFT_CONTROL,
391 [RGFW_altL] = KEY_LEFT_ALT,
392 [RGFW_superL] = KEY_LEFT_SUPER,
393 // #ifndef RGFW_MACOS
394 [RGFW_shiftR] = KEY_RIGHT_SHIFT,
395 [RGFW_controlR] = KEY_RIGHT_CONTROL,
396 [RGFW_altR] = KEY_RIGHT_ALT,
397 [RGFW_superR] = KEY_RIGHT_SUPER,
398 // #endif
399 [RGFW_menu] = KEY_KB_MENU,
400 [RGFW_kp0] = KEY_KP_0,
401 [RGFW_kp1] = KEY_KP_1,
402 [RGFW_kp2] = KEY_KP_2,
403 [RGFW_kp3] = KEY_KP_3,
404 [RGFW_kp4] = KEY_KP_4,
405 [RGFW_kp5] = KEY_KP_5,
406 [RGFW_kp6] = KEY_KP_6,
407 [RGFW_kp7] = KEY_KP_7,
408 [RGFW_kp8] = KEY_KP_8,
409 [RGFW_kp9] = KEY_KP_9,
410 [RGFW_kpPeriod] = KEY_KP_DECIMAL,
411 [RGFW_kpSlash] = KEY_KP_DIVIDE,
412 [RGFW_kpMultiply] = KEY_KP_MULTIPLY,
413 [RGFW_kpMinus] = KEY_KP_SUBTRACT,
414 [RGFW_kpPlus] = KEY_KP_ADD,
415 [RGFW_kpReturn] = KEY_KP_ENTER,
416 [RGFW_kpEqual] = KEY_KP_EQUAL,
417 };
418
419 static int mg_buttonConvertTable[] = {
420 [MG_BUTTON_NORTH] = GAMEPAD_BUTTON_RIGHT_FACE_UP,
421 [MG_BUTTON_EAST] = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT,
422 [MG_BUTTON_SOUTH] = GAMEPAD_BUTTON_RIGHT_FACE_DOWN,
423 [MG_BUTTON_WEST] = GAMEPAD_BUTTON_RIGHT_FACE_LEFT,
424 [MG_BUTTON_LEFT_SHOULDER] = GAMEPAD_BUTTON_LEFT_TRIGGER_1,
425 [MG_BUTTON_RIGHT_SHOULDER] = GAMEPAD_BUTTON_RIGHT_TRIGGER_1,
426 [MG_BUTTON_LEFT_TRIGGER] = GAMEPAD_BUTTON_LEFT_TRIGGER_2,
427 [MG_BUTTON_RIGHT_TRIGGER] = GAMEPAD_BUTTON_RIGHT_TRIGGER_2,
428 [MG_BUTTON_BACK] = GAMEPAD_BUTTON_MIDDLE_LEFT,
429 [MG_BUTTON_GUIDE] = GAMEPAD_BUTTON_MIDDLE,
430 [MG_BUTTON_START] = GAMEPAD_BUTTON_MIDDLE_RIGHT,
431 [MG_BUTTON_DPAD_UP] = GAMEPAD_BUTTON_LEFT_FACE_UP,
432 [MG_BUTTON_DPAD_RIGHT] = GAMEPAD_BUTTON_LEFT_FACE_RIGHT,
433 [MG_BUTTON_DPAD_DOWN] = GAMEPAD_BUTTON_LEFT_FACE_DOWN,
434 [MG_BUTTON_DPAD_LEFT] = GAMEPAD_BUTTON_LEFT_FACE_LEFT,
435 [MG_BUTTON_LEFT_STICK] = GAMEPAD_BUTTON_LEFT_THUMB,
436 [MG_BUTTON_RIGHT_STICK] = GAMEPAD_BUTTON_RIGHT_THUMB,
437 };
438
439 static int mg_axisConvertTable[] = {
440 [MG_AXIS_LEFT_X] = GAMEPAD_AXIS_LEFT_X,
441 [MG_AXIS_LEFT_Y] = GAMEPAD_AXIS_LEFT_Y,
442 [MG_AXIS_RIGHT_X] = GAMEPAD_AXIS_RIGHT_X,
443 [MG_AXIS_RIGHT_Y] = GAMEPAD_AXIS_RIGHT_Y,
444 [MG_AXIS_LEFT_TRIGGER] = GAMEPAD_AXIS_LEFT_TRIGGER,
445 [MG_AXIS_RIGHT_TRIGGER] = GAMEPAD_AXIS_RIGHT_TRIGGER,
446
447 /* unsupported in raylib */
448 [MG_AXIS_HAT_DPAD_LEFT_RIGHT] = -1,
449 [MG_AXIS_HAT_DPAD_LEFT] = -1,
450 [MG_AXIS_HAT_DPAD_RIGHT] = -1,
451 [MG_AXIS_HAT_DPAD_UP_DOWN] = -1,
452 [MG_AXIS_HAT_DPAD_UP] = -1,
453 [MG_AXIS_HAT_DPAD_DOWN] = -1,
454 };
455
456 //----------------------------------------------------------------------------------
457 // Module Internal Functions Declaration
458 //----------------------------------------------------------------------------------
459 int InitPlatform(void); // Initialize platform (graphics, inputs and more)
460 bool InitGraphicsDevice(void); // Initialize graphics device
461
462 static KeyboardKey ConvertScancodeToKey(u32 keycode);
463
464 //----------------------------------------------------------------------------------
465 // Module Functions Declaration
466 //----------------------------------------------------------------------------------
467 // NOTE: Functions declaration is provided by raylib.h
468
469 //----------------------------------------------------------------------------------
470 // Module Functions Definition: Window and Graphics Device
471 //----------------------------------------------------------------------------------
472
473 // Check if application should close
474 bool WindowShouldClose(void)
475 {
476 if (CORE.Window.shouldClose == false)
477 CORE.Window.shouldClose = RGFW_window_shouldClose(platform.window);
478 if (CORE.Window.ready) return CORE.Window.shouldClose;
479 else return true;
480 }
481
482 // Toggle fullscreen mode
483 void ToggleFullscreen(void)
484 {
485 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
486 {
487 FLAG_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
488 // Store previous window position (in case of exiting fullscreen)
489 Vector2 currentPosition = GetWindowPosition();
490 CORE.Window.previousPosition.x = currentPosition.x;
491 CORE.Window.previousPosition.y = currentPosition.y;
492 CORE.Window.previousScreen = CORE.Window.screen;
493
494 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
495 RGFW_monitor_scaleToWindow(currentMonitor, platform.window);
496 RGFW_window_setFullscreen(platform.window, 1);
497 }
498 else
499 {
500 FLAG_CLEAR(CORE.Window.flags, FLAG_FULLSCREEN_MODE);
501
502 // Update the window position right away
503 CORE.Window.position = CORE.Window.previousPosition;
504 RGFW_window_setFullscreen(platform.window, 0);
505 RGFW_window_move(platform.window, CORE.Window.position.x, CORE.Window.position.y);
506 RGFW_window_resize(platform.window, CORE.Window.previousScreen.width, CORE.Window.previousScreen.height);
507 }
508
509 // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
510 // NOTE: V-Sync can be enabled by graphic driver configuration
511 if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) RGFW_window_swapInterval_OpenGL(platform.window, 1);
512 }
513
514 // Toggle borderless windowed mode
515 void ToggleBorderlessWindowed(void)
516 {
517 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
518 {
519 ToggleFullscreen();
520
521 // it seems like returning here is a more desireable outcome
522 return;
523 }
524
525 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
526 {
527 FLAG_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
528
529 Vector2 currentPosition = GetWindowPosition();
530 CORE.Window.previousPosition.x = (int)currentPosition.x;
531 CORE.Window.previousPosition.y = (int)currentPosition.y;
532 CORE.Window.previousScreen = CORE.Window.screen;
533
534 RGFW_monitor *currentMonitor = RGFW_window_getMonitor(platform.window);
535 RGFW_window_setBorder(platform.window, 0);
536 RGFW_window_move(platform.window, 0, 0);
537 RGFW_window_resize(platform.window, currentMonitor->mode.w, currentMonitor->mode.h);
538 }
539 else
540 {
541 FLAG_CLEAR(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE);
542 RGFW_window_setBorder(platform.window, 1);
543
544 CORE.Window.position = CORE.Window.previousPosition;
545
546 RGFW_window_resize(platform.window, CORE.Window.previousScreen.width, CORE.Window.previousScreen.height);
547 RGFW_window_move(platform.window, CORE.Window.position.x, CORE.Window.position.y);
548 }
549 }
550
551 // Set window state: maximized, if resizable
552 void MaximizeWindow(void)
553 {
554 RGFW_window_maximize(platform.window);
555 }
556
557 // Set window state: minimized
558 void MinimizeWindow(void)
559 {
560 RGFW_window_minimize(platform.window);
561 }
562
563 // Restore window from being minimized/maximized
564 void RestoreWindow(void)
565 {
566 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) RGFW_window_focus(platform.window);
567
568 RGFW_window_restore(platform.window);
569 }
570
571 // Set window configuration state using flags
572 void SetWindowState(unsigned int flags)
573 {
574 if (!CORE.Window.ready) TRACELOG(LOG_WARNING, "WINDOW: SetWindowState does nothing before window initialization, Use \"SetConfigFlags\" instead");
575
576 FLAG_SET(CORE.Window.flags, flags);
577
578 if (FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
579 {
580 RGFW_window_swapInterval_OpenGL(platform.window, 1);
581 }
582 if (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
583 {
584 ToggleFullscreen();
585 }
586 if (FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
587 {
588 RGFW_window_setMaxSize(platform.window, 0, 0);
589 RGFW_window_setMinSize(platform.window, 0, 0);
590 }
591 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
592 {
593 RGFW_window_setBorder(platform.window, 0);
594 }
595 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
596 {
597 RGFW_window_hide(platform.window);
598 }
599 if (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
600 {
601 RGFW_window_minimize(platform.window);
602 }
603 if (FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
604 {
605 RGFW_window_maximize(platform.window);
606 }
607 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
608 {
609 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED);
610 FLAG_CLEAR(platform.window->internal.flags, RGFW_windowFocusOnShow);
611 RGFW_window_setFlags(platform.window, platform.window->internal.flags);
612 }
613 if (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
614 {
615 RGFW_window_setFloating(platform.window, RGFW_TRUE);
616 }
617 if (FLAG_IS_SET(flags, FLAG_WINDOW_ALWAYS_RUN))
618 {
619 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_ALWAYS_RUN);
620 }
621 if (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
622 {
623 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
624 }
625 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
626 {
627 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
628 }
629 if (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
630 {
631 RGFW_window_setMousePassthrough(platform.window, 1);
632 }
633 if (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
634 {
635 ToggleBorderlessWindowed();
636 }
637 if (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
638 {
639 RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL();
640 hints->samples = 4;
641 RGFW_setGlobalHints_OpenGL(hints);
642 }
643 if (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
644 {
645 TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
646 }
647 }
648
649 // Clear window configuration state flags
650 void ClearWindowState(unsigned int flags)
651 {
652 FLAG_CLEAR(CORE.Window.flags, flags);
653
654 if (FLAG_IS_SET(flags, FLAG_VSYNC_HINT))
655 {
656 RGFW_window_swapInterval_OpenGL(platform.window, 0);
657 }
658 if (FLAG_IS_SET(flags, FLAG_FULLSCREEN_MODE))
659 {
660 ToggleFullscreen();
661 }
662 if (FLAG_IS_SET(flags, FLAG_WINDOW_RESIZABLE))
663 {
664 RGFW_window_setMaxSize(platform.window, platform.window->w, platform.window->h);
665 RGFW_window_setMinSize(platform.window, platform.window->w, platform.window->h);
666 }
667 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNDECORATED))
668 {
669 RGFW_window_setBorder(platform.window, 1);
670 }
671 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIDDEN))
672 {
673 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) RGFW_window_focus(platform.window);
674
675 RGFW_window_show(platform.window);
676 }
677 if (FLAG_IS_SET(flags, FLAG_WINDOW_MINIMIZED))
678 {
679 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) RGFW_window_focus(platform.window);
680
681 RGFW_window_restore(platform.window);
682 }
683 if (FLAG_IS_SET(flags, FLAG_WINDOW_MAXIMIZED))
684 {
685 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) RGFW_window_focus(platform.window);
686
687 RGFW_window_restore(platform.window);
688 }
689 if (FLAG_IS_SET(flags, FLAG_WINDOW_UNFOCUSED))
690 {
691 RGFW_window_setFlags(platform.window, platform.window->internal.flags | RGFW_windowFocusOnShow);
692 }
693 if (FLAG_IS_SET(flags, FLAG_WINDOW_TOPMOST))
694 {
695 RGFW_window_setFloating(platform.window, RGFW_FALSE);
696 }
697 if (FLAG_IS_SET(flags, FLAG_WINDOW_TRANSPARENT))
698 {
699 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only be configured before window initialization");
700 }
701 if (FLAG_IS_SET(flags, FLAG_WINDOW_HIGHDPI))
702 {
703 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
704 }
705 if (FLAG_IS_SET(flags, FLAG_WINDOW_MOUSE_PASSTHROUGH))
706 {
707 RGFW_window_setMousePassthrough(platform.window, 0);
708 }
709 if (FLAG_IS_SET(flags, FLAG_BORDERLESS_WINDOWED_MODE))
710 {
711 ToggleBorderlessWindowed();
712 }
713 if (FLAG_IS_SET(flags, FLAG_MSAA_4X_HINT))
714 {
715 RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL();
716 hints->samples = 0;
717 RGFW_setGlobalHints_OpenGL(hints);
718 }
719 if (FLAG_IS_SET(flags, FLAG_INTERLACED_HINT))
720 {
721 TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only be configured before window initialization");
722 }
723 }
724
725 // Set icon for window
726 void SetWindowIcon(Image image)
727 {
728 if (image.format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
729 {
730 TRACELOG(LOG_WARNING, "RGFW: Window icon image must be in R8G8B8A8 pixel format");
731 return;
732 }
733 RGFW_window_setIcon(platform.window, (u8 *)image.data, image.width, image.height, RGFW_formatRGBA8);
734 }
735
736 // Set icon for window
737 void SetWindowIcons(Image *images, int count)
738 {
739 if ((images == NULL) || (count <= 0))
740 {
741 RGFW_window_setIcon(platform.window, NULL, 0, 0, 0);
742 }
743 else
744 {
745 Image *bigIcon = NULL;
746 Image *smallIcon = NULL;
747
748 for (int i = 0; i < count; i++)
749 {
750 if (images[i].format != PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
751 {
752 TRACELOG(LOG_WARNING, "RGFW: Window icon image must be in R8G8B8A8 pixel format");
753 continue;
754 }
755 if ((bigIcon == NULL) || ((images[i].width > bigIcon->width) && (images[i].height > bigIcon->height))) bigIcon = &images[i];
756 if ((smallIcon == NULL) || ((images[i].width < smallIcon->width) && (images[i].height > smallIcon->height))) smallIcon = &images[i];
757 }
758
759 if (smallIcon != NULL) RGFW_window_setIconEx(platform.window, (u8 *)smallIcon->data, smallIcon->width, smallIcon->height, RGFW_formatRGBA8, RGFW_iconWindow);
760 if (bigIcon != NULL) RGFW_window_setIconEx(platform.window, (u8 *)bigIcon->data, bigIcon->width, bigIcon->height, RGFW_formatRGBA8, RGFW_iconTaskbar);
761 }
762 }
763
764 // Set title for window
765 void SetWindowTitle(const char *title)
766 {
767 RGFW_window_setName(platform.window, (char *)title);
768 CORE.Window.title = title;
769 }
770
771 // Set window position on screen (windowed mode)
772 void SetWindowPosition(int x, int y)
773 {
774 RGFW_window_move(platform.window, x, y);
775 }
776
777 // Set monitor for the current window
778 void SetWindowMonitor(int monitor)
779 {
780 RGFW_window_moveToMonitor(platform.window, RGFW_getMonitors(NULL)[monitor]);
781 }
782
783 // Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
784 void SetWindowMinSize(int width, int height)
785 {
786 RGFW_window_setMinSize(platform.window, width, height);
787 CORE.Window.screenMin.width = width;
788 CORE.Window.screenMin.height = height;
789 }
790
791 // Set window maximum dimensions (FLAG_WINDOW_RESIZABLE)
792 void SetWindowMaxSize(int width, int height)
793 {
794 RGFW_window_setMaxSize(platform.window, width, height);
795 CORE.Window.screenMax.width = width;
796 CORE.Window.screenMax.height = height;
797 }
798
799 // Set window dimensions
800 void SetWindowSize(int width, int height)
801 {
802 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
803 {
804 CORE.Window.screen.width = width;
805 CORE.Window.screen.height = height;
806
807 Vector2 scaleDpi = GetWindowScaleDPI();
808
809 #if defined(__APPLE__)
810 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
811 CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
812
813 CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
814 CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
815 CORE.Window.currentFbo.width = CORE.Window.render.width;
816 CORE.Window.currentFbo.height = CORE.Window.render.height;
817 #else
818 SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
819 CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
820 #endif
821
822 CORE.Window.currentFbo.width = CORE.Window.render.width;
823 CORE.Window.currentFbo.height = CORE.Window.render.height;
824 }
825 else
826 {
827 CORE.Window.screen.width = width;
828 CORE.Window.screen.height = height;
829 }
830
831 if (!CORE.Window.usingFbo)
832 {
833 SetupViewport(CORE.Window.screen.width, CORE.Window.screen.height);
834 }
835
836 RGFW_window_resize(platform.window, CORE.Window.screen.width, CORE.Window.screen.height);
837 }
838
839 // Set window opacity, value opacity is between 0.0 and 1.0
840 void SetWindowOpacity(float opacity)
841 {
842 RGFW_window_setOpacity(platform.window, opacity);
843 }
844
845 // Set window focused
846 void SetWindowFocused(void)
847 {
848 RGFW_window_focus(platform.window);
849 }
850
851 // Get native window handle
852 void *GetWindowHandle(void)
853 {
854 if (platform.window == NULL) return NULL;
855
856 #if defined(RGFW_WASM)
857 return (void *)&platform.window->src.ctx;
858 #elif defined(RGFW_WAYLAND)
859 return (void *)platform.window->src.surface;
860 #else
861 return (void *)platform.window->src.window;
862 #endif
863 }
864
865 // Get number of monitors
866 int GetMonitorCount(void)
867 {
868 size_t count = 0;
869 RGFW_getMonitors(&count);
870
871 return count;
872 }
873
874 // Get current monitor where window is placed
875 int GetCurrentMonitor(void)
876 {
877 RGFW_monitor **mons = RGFW_getMonitors(NULL);
878 RGFW_monitor *mon = NULL;
879
880 if (platform.window) mon = RGFW_window_getMonitor(platform.window);
881 else mon = RGFW_getPrimaryMonitor();
882
883 for (int i = 0; i < 6; i++)
884 {
885 if ((mons[i]->x == mon->x) && (mons[i]->y == mon->y)) return i;
886 }
887
888 return 0;
889 }
890
891 // Get selected monitor position
892 Vector2 GetMonitorPosition(int monitor)
893 {
894 RGFW_monitor **mons = RGFW_getMonitors(NULL);
895
896 return (Vector2){ (float)mons[monitor]->x, (float)mons[monitor]->y };
897 }
898
899 // Get selected monitor width (currently used by monitor)
900 int GetMonitorWidth(int monitor)
901 {
902 RGFW_monitor **mons = RGFW_getMonitors(NULL);
903
904 return mons[monitor]->mode.w;
905 }
906
907 // Get selected monitor height (currently used by monitor)
908 int GetMonitorHeight(int monitor)
909 {
910 RGFW_monitor **mons = RGFW_getMonitors(NULL);
911
912 return mons[monitor]->mode.h;
913 }
914
915 // Get selected monitor physical width in millimetres
916 int GetMonitorPhysicalWidth(int monitor)
917 {
918 RGFW_monitor **mons = RGFW_getMonitors(NULL);
919
920 return mons[monitor]->physW;
921 }
922
923 // Get selected monitor physical height in millimetres
924 int GetMonitorPhysicalHeight(int monitor)
925 {
926 RGFW_monitor **mons = RGFW_getMonitors(NULL);
927
928 return (int)mons[monitor]->physH;
929 }
930
931 // Get selected monitor refresh rate
932 int GetMonitorRefreshRate(int monitor)
933 {
934 RGFW_monitor **mons = RGFW_getMonitors(NULL);
935
936 return (int)mons[monitor]->mode.refreshRate;
937 }
938
939 // Get the human-readable, UTF-8 encoded name of the selected monitor
940 const char *GetMonitorName(int monitor)
941 {
942 RGFW_monitor **mons = RGFW_getMonitors(NULL);
943
944 return mons[monitor]->name;
945 }
946
947 // Get window position XY on monitor
948 Vector2 GetWindowPosition(void)
949 {
950 if (platform.window == NULL)
951 {
952 return (Vector2){ 0.0f, 0.0f };
953 }
954
955 if (RGFW_window_getPosition(platform.window, &platform.window->x, &platform.window->y)) {
956 return (Vector2){ (float)platform.window->x, (float)platform.window->y };
957 }
958
959 return (Vector2){ 0.0f, 0.0f };
960 }
961
962 // Get window scale DPI factor for current monitor
963 Vector2 GetWindowScaleDPI(void)
964 {
965 RGFW_monitor *monitor = NULL;
966
967 if (platform.window) monitor = RGFW_window_getMonitor(platform.window);
968 else monitor = RGFW_getPrimaryMonitor();
969
970 #if defined(__APPLE__)
971 // Apple does < 1.0f scaling, example: 0.66f, 0.5f
972 // it needs to be convert to be consistent
973 return (Vector2){ 1.0f/monitor->scaleX, 1.0f/monitor->scaleX };
974 #else
975 // Linux and Windows do >= 1.0f scaling, example: 1.0f, 1.25f, 2.0f
976 return (Vector2){ monitor->scaleX, monitor->scaleX };
977 #endif
978 }
979
980 // Get monitor pixel ratio
981 // WARNING: Function not used, neither exposed by raylib
982 float GetMonitorPixelRatio(void)
983 {
984 RGFW_monitor *monitor = NULL;
985
986 if (platform.window) monitor = RGFW_window_getMonitor(platform.window);
987 else monitor = RGFW_getPrimaryMonitor();
988
989 return monitor->pixelRatio;
990 }
991
992 // Set clipboard text content
993 void SetClipboardText(const char *text)
994 {
995 // add 1 for null terminator
996 RGFW_writeClipboard(text, strlen(text)+1);
997 }
998
999 // Get clipboard text content
1000 // NOTE: returned string is allocated and freed by RGFW
1001 const char *GetClipboardText(void)
1002 {
1003 return RGFW_readClipboard(NULL);
1004 }
1005
1006 #if SUPPORT_CLIPBOARD_IMAGE
1007 #if defined(_WIN32)
1008 #define WIN32_CLIPBOARD_IMPLEMENTATION
1009 #define WINUSER_ALREADY_INCLUDED
1010 #define WINBASE_ALREADY_INCLUDED
1011 #define WINGDI_ALREADY_INCLUDED
1012 #include "../external/win32_clipboard.h"
1013 #elif defined(__linux__) && defined(DRGFW_X11)
1014 #include <X11/Xlib.h>
1015 #include <X11/Xatom.h>
1016 #endif
1017 #endif
1018
1019 // Get clipboard image
1020 Image GetClipboardImage(void)
1021 {
1022 Image image = { 0 };
1023
1024 #if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES
1025 #if defined(_WIN32)
1026
1027 unsigned long long int dataSize = 0; // moved into _WIN32 scope until other platforms gain support
1028 void *fileData = NULL; // moved into _WIN32 scope until other platforms gain support
1029
1030 int width = 0;
1031 int height = 0;
1032 fileData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize);
1033
1034 if (fileData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data");
1035 else image = LoadImageFromMemory(".bmp", (const unsigned char *)fileData, dataSize);
1036
1037 #elif defined(__linux__) && defined(DRGFW_X11)
1038
1039 // REF: https://github.com/ColleagueRiley/Clipboard-Copy-Paste/blob/main/x11.c
1040 Display *dpy = XOpenDisplay(NULL);
1041 if (!dpy) return image;
1042
1043 Window root = DefaultRootWindow(dpy);
1044 Window win = XCreateSimpleWindow(
1045 dpy, // The connection to the X Server
1046 root, // The 'Parent' window (usually the desktop/root)
1047 0, 0, // X and Y position on the screen
1048 1, 1, // Width and Height (1x1 pixel)
1049 0, // Border width
1050 0, // Border color
1051 0 // Background color
1052 );
1053
1054 Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
1055 Atom targetType = XInternAtom(dpy, "image/png", False); // Ask for PNG
1056 Atom property = XInternAtom(dpy, "RAYLIB_CLIPBOARD_MANAGER", False);
1057
1058 // Request the data: "Convert whatever is in CLIPBOARD to image/png and put it in RAYLIB_CLIPBOARD_MANAGER"
1059 XConvertSelection(dpy, clipboard, targetType, property, win, CurrentTime);
1060
1061 // Wait for the SelectionNotify event
1062 XEvent ev = { 0 };
1063 XNextEvent(dpy, &ev);
1064
1065 Atom actualType = { 0 };
1066 int actualFormat = 0;
1067 unsigned long nitems = 0;
1068 unsigned long bytesAfter = 0;
1069 unsigned char *data = NULL;
1070
1071 // Read the data from our ghost window's property
1072 XGetWindowProperty(dpy, win, property, 0, ~0L, False, AnyPropertyType,
1073 &actualType, &actualFormat, &nitems, &bytesAfter, &data);
1074
1075 if (data != NULL)
1076 {
1077 image = LoadImageFromMemory(".png", data, (int)nitems);
1078 XFree(data);
1079 }
1080
1081 XDestroyWindow(dpy, win);
1082 XCloseDisplay(dpy);
1083 #else
1084 TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_DESKTOP_RGFW doesn't implement GetClipboardImage() for this OS");
1085 #endif // defined(_WIN32)
1086 #else
1087 TRACELOG(LOG_WARNING, "Clipboard image: SUPPORT_CLIPBOARD_IMAGE requires SUPPORT_MODULE_RTEXTURES to work properly");
1088 #endif // SUPPORT_CLIPBOARD_IMAGE
1089
1090 return image;
1091 }
1092
1093 // Show mouse cursor
1094 void ShowCursor(void)
1095 {
1096 RGFW_window_showMouse(platform.window, true);
1097 CORE.Input.Mouse.cursorHidden = false;
1098 }
1099
1100 // Hides mouse cursor
1101 void HideCursor(void)
1102 {
1103 RGFW_window_showMouse(platform.window, false);
1104 CORE.Input.Mouse.cursorHidden = true;
1105 }
1106
1107 // Enables cursor (unlock cursor)
1108 void EnableCursor(void)
1109 {
1110 RGFW_window_captureRawMouse(platform.window, false);
1111
1112 // Set cursor position in the middle
1113 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
1114 ShowCursor();
1115
1116 CORE.Input.Mouse.cursorLocked = true;
1117 }
1118
1119 // Disables cursor (lock cursor)
1120 void DisableCursor(void)
1121 {
1122 RGFW_window_captureRawMouse(platform.window, true);
1123 HideCursor();
1124
1125 CORE.Input.Mouse.cursorLocked = true;
1126 }
1127
1128 // Swap back buffer with front buffer (screen drawing)
1129 void SwapScreenBuffer(void)
1130 {
1131 #if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1132 {
1133 if (platform.surface)
1134 {
1135 // copy rlsw pixel data to the surface framebuffer
1136 swReadPixels(0, 0, platform.surfaceWidth, platform.surfaceHeight, SW_RGBA, SW_UNSIGNED_BYTE, platform.surfacePixels);
1137
1138 // Mac wants a different pixel order. I cant seem to get this to work any other way
1139 #if defined(__APPLE__)
1140 unsigned char temp = 0;
1141 unsigned char *p = NULL;
1142 for (int i = 0; i < (platform.surfaceWidth * platform.surfaceHeight); i += 1)
1143 {
1144 p = platform.surfacePixels + (i * 4);
1145 temp = p[0];
1146 p[0] = p[2];
1147 p[2] = temp;
1148 }
1149 #endif
1150
1151 // blit surface to the window
1152 RGFW_window_blitSurface(platform.window, platform.surface);
1153 }
1154 }
1155 #else
1156 {
1157 RGFW_window_swapBuffers_OpenGL(platform.window);
1158 }
1159 #endif
1160 }
1161
1162 //----------------------------------------------------------------------------------
1163 // Module Functions Definition: Misc
1164 //----------------------------------------------------------------------------------
1165
1166 // Get elapsed time measure in seconds since InitTimer()
1167 double GetTime(void)
1168 {
1169 // CORE.Time.base is nanoseconds as integer
1170 double baseTime = (double)CORE.Time.base*1e-9;
1171 double time = get_time_seconds() - baseTime;
1172
1173 return time;
1174 }
1175
1176 // Open URL with default system browser (if available)
1177 // NOTE: This function is only safe to use if you control the URL given
1178 // A user could craft a malicious string performing another action
1179 void OpenURL(const char *url)
1180 {
1181 // Security check to (partially) avoid malicious code on target platform
1182 if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
1183 else
1184 {
1185 char *cmd = (char *)RL_CALLOC(strlen(url) + 32, sizeof(char));
1186 #if defined(_WIN32)
1187 sprintf(cmd, "explorer \"%s\"", url);
1188 #endif
1189 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)
1190 sprintf(cmd, "xdg-open '%s'", url); // Alternatives: firefox, x-www-browser
1191 #endif
1192 #if defined(__APPLE__)
1193 sprintf(cmd, "open '%s'", url);
1194 #endif
1195 int result = system(cmd);
1196 if (result == -1) TRACELOG(LOG_WARNING, "OpenURL() child process could not be created");
1197 RL_FREE(cmd);
1198 }
1199 }
1200
1201 //----------------------------------------------------------------------------------
1202 // Module Functions Definition: Inputs
1203 //----------------------------------------------------------------------------------
1204
1205 // Set internal gamepad mappings
1206 int SetGamepadMappings(const char *mappings)
1207 {
1208 return mg_update_gamepad_mappings(&platform.minigamepad, mappings);
1209 }
1210
1211 // Set gamepad vibration
1212 void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
1213 {
1214 TRACELOG(LOG_WARNING, "SetGamepadVibration() unsupported on target platform");
1215 }
1216
1217 // Set mouse position XY
1218 void SetMousePosition(int x, int y)
1219 {
1220 RGFW_window_moveMouse(platform.window, x, y);
1221 CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
1222 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1223 }
1224
1225 // Set mouse cursor
1226 void SetMouseCursor(int cursor)
1227 {
1228 RGFW_window_setMouseStandard(platform.window, cursor);
1229 }
1230
1231 // Get physical key name
1232 const char *GetKeyName(int key)
1233 {
1234 TRACELOG(LOG_WARNING, "GetKeyName() unsupported on target platform");
1235
1236 return "";
1237 }
1238
1239 // Register all input events
1240 void PollInputEvents(void)
1241 {
1242 #if SUPPORT_GESTURES_SYSTEM
1243 // NOTE: Gestures update must be called every frame to reset gestures correctly
1244 // because ProcessGestureEvent() is called on an event, not every frame
1245 UpdateGestures();
1246 #endif
1247
1248 // Reset keys/chars pressed registered
1249 CORE.Input.Keyboard.keyPressedQueueCount = 0;
1250 CORE.Input.Keyboard.charPressedQueueCount = 0;
1251
1252 // Reset mouse wheel
1253 CORE.Input.Mouse.currentWheelMove.x = 0;
1254 CORE.Input.Mouse.currentWheelMove.y = 0;
1255
1256 // Register previous mouse position
1257
1258 // Reset last gamepad button/axis registered state
1259 for (int i = 0; i < MAX_GAMEPADS; i++)
1260 {
1261 // Check if gamepad is available
1262 if (CORE.Input.Gamepad.ready[i])
1263 {
1264 // Register previous gamepad button states
1265 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++)
1266 {
1267 CORE.Input.Gamepad.previousButtonState[i][k] = CORE.Input.Gamepad.currentButtonState[i][k];
1268 }
1269 }
1270 }
1271
1272 // Register previous touch states
1273 for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
1274
1275 // Map touch position to mouse position for convenience
1276 CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
1277
1278 int touchAction = -1; // 0-TOUCH_ACTION_UP, 1-TOUCH_ACTION_DOWN, 2-TOUCH_ACTION_MOVE
1279 bool realTouch = false; // Flag to differentiate real touch gestures from mouse ones
1280
1281 // Register previous keys states
1282 // NOTE: Android supports up to 260 keys
1283 for (int i = 0; i < MAX_KEYBOARD_KEYS; i++)
1284 {
1285 CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
1286 CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
1287 }
1288
1289 // Register previous mouse states
1290 for (int i = 0; i < MAX_MOUSE_BUTTONS; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
1291
1292 // Poll input events for current platform
1293 //-----------------------------------------------------------------------------
1294 CORE.Window.resizedLastFrame = false;
1295
1296 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1297 if (RGFW_window_isCaptured(platform.window))
1298 {
1299 CORE.Input.Mouse.previousPosition = (Vector2){ 0.0f, 0.0f };
1300 CORE.Input.Mouse.currentPosition = (Vector2){ 0.0f, 0.0f };
1301 }
1302 else
1303 {
1304 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
1305 }
1306
1307 if ((CORE.Window.eventWaiting) || (IsWindowState(FLAG_WINDOW_MINIMIZED) && !IsWindowState(FLAG_WINDOW_ALWAYS_RUN)))
1308 {
1309 CORE.Time.previous = GetTime();
1310 }
1311
1312 RGFW_event rgfw_event;
1313 while (RGFW_window_checkEvent(platform.window, &rgfw_event))
1314 {
1315 // All input events can be processed after polling
1316 switch (rgfw_event.type)
1317 {
1318 case RGFW_mouseEnter: CORE.Input.Mouse.cursorOnScreen = true; break;
1319 case RGFW_mouseLeave: CORE.Input.Mouse.cursorOnScreen = false; break;
1320 case RGFW_quit:
1321 RGFW_window_setShouldClose(platform.window, true);
1322 return;
1323 case RGFW_dataDrop: // Dropped file
1324 {
1325 for (int i = 0; i < rgfw_event.drop.count; i++)
1326 {
1327 if (CORE.Window.dropFileCount == 0)
1328 {
1329 // When a new file is dropped, reserve a fixed number of slots for all possible dropped files
1330 // at the moment limiting the number of drops at once to 1024 files but this behaviour should probably be reviewed
1331 // TODO: Pointers should probably be reallocated for any new file added...
1332 CORE.Window.dropFilepaths = (char **)RL_CALLOC(1024, sizeof(char *));
1333
1334 CORE.Window.dropFilepaths[CORE.Window.dropFileCount] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
1335 strcpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], rgfw_event.drop.files[i]);
1336
1337 CORE.Window.dropFileCount++;
1338 }
1339 else if (CORE.Window.dropFileCount < 1024)
1340 {
1341 CORE.Window.dropFilepaths[CORE.Window.dropFileCount] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
1342 strcpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], rgfw_event.drop.files[i]);
1343
1344 CORE.Window.dropFileCount++;
1345 }
1346 else TRACELOG(LOG_WARNING, "FILE: Maximum drag and drop files at once is limited to 1024 files!");
1347 }
1348 } break;
1349
1350 // Window events are also polled (Minimized, maximized, close...)
1351 case RGFW_windowResized:
1352 {
1353 // set flag that the window was resized
1354 CORE.Window.resizedLastFrame = true;
1355
1356 #if defined(__APPLE__)
1357 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1358 {
1359 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1360 SetupViewport(platform.window->w * currentMonitor->pixelRatio, platform.window->h * currentMonitor->pixelRatio);
1361 CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1362
1363 CORE.Window.screen.width = platform.window->w;
1364 CORE.Window.screen.height = platform.window->h;
1365 CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1366 CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1367 }
1368 else
1369 {
1370 SetupViewport(platform.window->w, platform.window->h);
1371 CORE.Window.screen.width = platform.window->w;
1372 CORE.Window.screen.height = platform.window->h;
1373 CORE.Window.render.width = CORE.Window.screen.width;
1374 CORE.Window.render.height = CORE.Window.screen.height;
1375 }
1376
1377 CORE.Window.currentFbo.width = CORE.Window.render.width;
1378 CORE.Window.currentFbo.height = CORE.Window.render.height;
1379 #elif defined(PLATFORM_WEB_RGFW)
1380 // do nothing but prevent other behavior
1381 #else
1382 SetupViewport(platform.window->w, platform.window->h);
1383
1384 // Consider content scaling if required
1385 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1386 {
1387 Vector2 scaleDpi = GetWindowScaleDPI();
1388 CORE.Window.screen.width = (int)(platform.window->w/scaleDpi.x);
1389 CORE.Window.screen.height = (int)(platform.window->h/scaleDpi.y);
1390 CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
1391
1392 // Mouse scale does not seem to be needed
1393 //SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
1394 }
1395 else
1396 {
1397 CORE.Window.screen.width = platform.window->w;
1398 CORE.Window.screen.height = platform.window->h;
1399 }
1400
1401 CORE.Window.currentFbo.width = CORE.Window.screen.width;
1402 CORE.Window.currentFbo.height = CORE.Window.screen.height;
1403 #endif
1404
1405 #if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1406 #if defined(__APPLE__)
1407 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1408 CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1409 SetupViewport(platform.window->w * currentMonitor->pixelRatio, platform.window->h * currentMonitor->pixelRatio);
1410
1411 CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1412 CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1413 CORE.Window.currentFbo.width = CORE.Window.render.width;
1414 CORE.Window.currentFbo.height = CORE.Window.render.height;
1415 #endif
1416 platform.surfaceWidth = CORE.Window.currentFbo.width;
1417 platform.surfaceHeight = CORE.Window.currentFbo.height;
1418
1419 // in software mode we dont have the viewport so we need to reverse the highdpi changes
1420 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1421 {
1422 Vector2 scaleDpi = GetWindowScaleDPI();
1423 platform.surfaceWidth *= scaleDpi.x;
1424 platform.surfaceHeight *= scaleDpi.y;
1425 }
1426
1427 if (platform.surfacePixels != NULL)
1428 {
1429 RL_FREE(platform.surfacePixels);
1430 platform.surfacePixels = RL_MALLOC(platform.surfaceWidth * platform.surfaceHeight * 4);
1431 }
1432
1433 if (platform.surface != NULL)
1434 {
1435 RGFW_surface_free(platform.surface);
1436 platform.surface = RGFW_window_createSurface(platform.window, platform.surfacePixels, platform.surfaceWidth, platform.surfaceHeight, RGFW_formatBGRA8);
1437 swResize(platform.surfaceWidth, platform.surfaceHeight);
1438 }
1439 #endif
1440 } break;
1441 case RGFW_windowMaximized:
1442 {
1443 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); // The window was maximized
1444 } break;
1445 case RGFW_windowMinimized:
1446 {
1447 FLAG_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); // The window was iconified
1448 } break;
1449 case RGFW_windowRestored:
1450 {
1451 if (RGFW_window_isMaximized(platform.window))
1452 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED); // The window was restored
1453 if (RGFW_window_isMinimized(platform.window))
1454 FLAG_CLEAR(CORE.Window.flags, FLAG_WINDOW_MINIMIZED); // The window was restored
1455 } break;
1456 case RGFW_windowMoved:
1457 {
1458 CORE.Window.position.x = platform.window->x;
1459 CORE.Window.position.y = platform.window->x;
1460 } break;
1461
1462 // Keyboard events
1463 case RGFW_keyPressed:
1464 {
1465 KeyboardKey key = ConvertScancodeToKey(rgfw_event.key.value);
1466 if (key != KEY_NULL)
1467 {
1468 // If key was up, add it to the key pressed queue
1469 if ((CORE.Input.Keyboard.currentKeyState[key] == 0) && (CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE))
1470 {
1471 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
1472 CORE.Input.Keyboard.keyPressedQueueCount++;
1473 }
1474
1475 CORE.Input.Keyboard.currentKeyState[key] = 1;
1476
1477 if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey]) RGFW_window_setShouldClose(platform.window, true);
1478 }
1479 } break;
1480 case RGFW_keyReleased:
1481 {
1482 KeyboardKey key = ConvertScancodeToKey(rgfw_event.key.value);
1483 if (key != KEY_NULL) CORE.Input.Keyboard.currentKeyState[key] = 0;
1484 } break;
1485
1486 case RGFW_keyChar:
1487 {
1488 // NOTE: event.text.text data comes an UTF-8 text sequence but registering codepoints (int)
1489 // Check if there is space available in the queue
1490 if (CORE.Input.Keyboard.charPressedQueueCount < MAX_CHAR_PRESSED_QUEUE)
1491 {
1492 // Add character (codepoint) to the queue
1493 CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = rgfw_event.keyChar.value;
1494 CORE.Input.Keyboard.charPressedQueueCount++;
1495 }
1496 } break;
1497
1498 // Check mouse events
1499 case RGFW_mouseScroll:
1500 {
1501 CORE.Input.Mouse.currentWheelMove.x += rgfw_event.scroll.x;
1502 CORE.Input.Mouse.currentWheelMove.y += rgfw_event.scroll.y;
1503 } break;
1504 case RGFW_mouseButtonPressed:
1505 {
1506 int btn = rgfw_event.button.value;
1507 if (btn == RGFW_mouseLeft) btn = 1;
1508 else if (btn == RGFW_mouseRight) btn = 2;
1509 else if (btn == RGFW_mouseMiddle) btn = 3;
1510
1511 CORE.Input.Mouse.currentButtonState[btn - 1] = 1;
1512 CORE.Input.Touch.currentTouchState[btn - 1] = 1;
1513
1514 touchAction = 1;
1515 } break;
1516 case RGFW_mouseButtonReleased:
1517 {
1518 int btn = rgfw_event.button.value;
1519 if (btn == RGFW_mouseLeft) btn = 1;
1520 else if (btn == RGFW_mouseRight) btn = 2;
1521 else if (btn == RGFW_mouseMiddle) btn = 3;
1522
1523 CORE.Input.Mouse.currentButtonState[btn - 1] = 0;
1524 CORE.Input.Touch.currentTouchState[btn - 1] = 0;
1525
1526 touchAction = 0;
1527 } break;
1528 case RGFW_mousePosChanged:
1529 {
1530 float mouseX = 0.0f;
1531 float mouseY = 0.0f;
1532 if (RGFW_window_isCaptured(platform.window))
1533 {
1534 mouseX = (float)rgfw_event.mouse.vecX;
1535 mouseY = (float)rgfw_event.mouse.vecY;
1536 }
1537 else
1538 {
1539 mouseX = (float)rgfw_event.mouse.x;
1540 mouseY = (float)rgfw_event.mouse.y;
1541 }
1542
1543 #if defined(__EMSCRIPTEN__)
1544 double canvasWidth = 0.0;
1545 double canvasHeight = 0.0;
1546 emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
1547 mouseX *= ((float)GetScreenWidth()/(float)canvasWidth);
1548 mouseY *= ((float)GetScreenHeight()/(float)canvasHeight);
1549 #endif
1550 if (RGFW_window_isCaptured(platform.window))
1551 {
1552 CORE.Input.Mouse.currentPosition.x += mouseX;
1553 CORE.Input.Mouse.currentPosition.y += mouseY;
1554 }
1555 else
1556 {
1557 CORE.Input.Mouse.currentPosition.x = mouseX;
1558 CORE.Input.Mouse.currentPosition.y = mouseY;
1559 }
1560
1561 CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
1562 touchAction = 2;
1563 } break;
1564 default: break;
1565 }
1566
1567 #if SUPPORT_GESTURES_SYSTEM
1568 if (touchAction > -1)
1569 {
1570 // Process mouse events as touches to be able to use mouse-gestures
1571 GestureEvent gestureEvent = { 0 };
1572
1573 // Register touch actions
1574 gestureEvent.touchAction = touchAction;
1575
1576 // Assign a pointer ID
1577 gestureEvent.pointId[0] = 0;
1578
1579 // Register touch points count
1580 gestureEvent.pointCount = 1;
1581
1582 // Register touch points position, only one point registered
1583 if (touchAction == 2 || realTouch) gestureEvent.position[0] = CORE.Input.Touch.position[0];
1584 else gestureEvent.position[0] = GetMousePosition();
1585
1586 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
1587 gestureEvent.position[0].x /= (float)GetScreenWidth();
1588 gestureEvent.position[0].y /= (float)GetScreenHeight();
1589
1590 // Gesture data is sent to gestures-system for processing
1591 ProcessGestureEvent(gestureEvent);
1592
1593 touchAction = -1;
1594 }
1595 #endif
1596 }
1597 //-----------------------------------------------------------------------------
1598
1599 mg_event gamepad_event;
1600 while (mg_gamepads_check_event(&platform.minigamepad, &gamepad_event)) {
1601 int gamepadIndex = gamepad_event.gamepad->index;
1602 switch (gamepad_event.type) {
1603 case MG_EVENT_BUTTON_PRESS:
1604 {
1605 int button = mg_buttonConvertTable[gamepad_event.button];
1606 if (button >= 0)
1607 {
1608 CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 1;
1609 CORE.Input.Gamepad.lastButtonPressed = button;
1610 }
1611 } break;
1612 case MG_EVENT_BUTTON_RELEASE:
1613 {
1614 int button = mg_buttonConvertTable[gamepad_event.button];
1615 if (button >= 0)
1616 {
1617 CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 0;
1618 if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1619 }
1620 } break;
1621 case MG_EVENT_AXIS_MOVE:
1622 {
1623 int axis = mg_axisConvertTable[gamepad_event.axis];
1624
1625 switch (axis) {
1626 case GAMEPAD_AXIS_LEFT_X:
1627 case GAMEPAD_AXIS_LEFT_Y:
1628 case GAMEPAD_AXIS_RIGHT_X:
1629 case GAMEPAD_AXIS_RIGHT_Y:
1630 CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value;
1631 break;
1632 case GAMEPAD_AXIS_LEFT_TRIGGER:
1633 case GAMEPAD_AXIS_RIGHT_TRIGGER:
1634 CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value;
1635
1636 /* trigger button press when axis is all the way */
1637 int button = (axis == GAMEPAD_AXIS_LEFT_TRIGGER) ? GAMEPAD_BUTTON_LEFT_TRIGGER_2 : GAMEPAD_BUTTON_RIGHT_TRIGGER_2;
1638 int pressed = (platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value >= 1.0f);
1639
1640 CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = pressed;
1641 if (pressed) CORE.Input.Gamepad.lastButtonPressed = button;
1642 else if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0;
1643 break;
1644 }
1645 } break;
1646 case MG_EVENT_GAMEPAD_CONNECT:
1647 CORE.Input.Gamepad.ready[gamepadIndex] = true;
1648 CORE.Input.Gamepad.axisState[gamepadIndex][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f;
1649 CORE.Input.Gamepad.axisState[gamepadIndex][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f;
1650
1651 int axisCount = 0;
1652 for (int i = 0; i < MG_AXIS_COUNT; i += 1) {
1653 if (platform.minigamepad.gamepads[gamepadIndex].axes[i].supported) {
1654 axisCount += 1;
1655 } else {
1656 break;
1657 }
1658 }
1659 CORE.Input.Gamepad.axisCount[gamepadIndex] = axisCount;
1660 strcpy(CORE.Input.Gamepad.name[gamepadIndex], platform.minigamepad.gamepads[gamepadIndex].name);
1661 break;
1662 case MG_EVENT_GAMEPAD_DISCONNECT:
1663 CORE.Input.Gamepad.ready[gamepadIndex] = false;
1664 break;
1665 default: break;
1666 }
1667 }
1668 }
1669
1670 //----------------------------------------------------------------------------------
1671 // Module Internal Functions Definition
1672 //----------------------------------------------------------------------------------
1673
1674 // Initialize platform: graphics, inputs and more
1675 int InitPlatform(void)
1676 {
1677 // Initialize RGFW internal global state, only required systems
1678 unsigned int flags = RGFW_windowCenter | RGFW_windowAllowDND;
1679
1680 // Check window creation flags
1681 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE))
1682 {
1683 FLAG_SET(flags, RGFW_windowFullscreen);
1684 }
1685
1686 if (FLAG_IS_SET(CORE.Window.flags, FLAG_BORDERLESS_WINDOWED_MODE))
1687 {
1688 FLAG_SET(flags, RGFW_windowedFullscreen);
1689 }
1690
1691 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNDECORATED)) FLAG_SET(flags, RGFW_windowNoBorder);
1692 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_RESIZABLE)) FLAG_SET(flags, RGFW_windowNoResize);
1693 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_TRANSPARENT)) FLAG_SET(flags, RGFW_windowTransparent);
1694 if (FLAG_IS_SET(CORE.Window.flags, FLAG_FULLSCREEN_MODE)) FLAG_SET(flags, RGFW_windowFullscreen);
1695 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIDDEN)) FLAG_SET(flags, RGFW_windowHide);
1696 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MAXIMIZED)) FLAG_SET(flags, RGFW_windowMaximize);
1697
1698 // NOTE: Some OpenGL context attributes must be set before window creation
1699 // Check selection OpenGL version
1700
1701 RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL();
1702 if (rlGetVersion() == RL_OPENGL_21)
1703 {
1704 hints->major = 2;
1705 hints->minor = 1;
1706 }
1707 else if (rlGetVersion() == RL_OPENGL_33)
1708 {
1709 hints->major = 3;
1710 hints->minor = 3;
1711 }
1712 else if (rlGetVersion() == RL_OPENGL_43)
1713 {
1714 hints->major = 4;
1715 hints->minor = 3;
1716 }
1717 else if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1718 {
1719 hints->major = 1;
1720 hints->minor = 1;
1721 hints->renderer = RGFW_glSoftware;
1722 }
1723
1724 if (FLAG_IS_SET(CORE.Window.flags, FLAG_MSAA_4X_HINT)) hints->samples = 4;
1725
1726 if (!FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_UNFOCUSED)) FLAG_SET(flags, RGFW_windowFocusOnShow | RGFW_windowFocus);
1727
1728 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI))
1729 {
1730 #if !defined(__APPLE__)
1731 CORE.Window.screen.width = CORE.Window.screen.width * GetWindowScaleDPI().x;
1732 CORE.Window.screen.height = CORE.Window.screen.height * GetWindowScaleDPI().y;
1733 #endif
1734 }
1735
1736 RGFW_setGlobalHints_OpenGL(hints);
1737 platform.window = RGFW_createWindow((CORE.Window.title != 0)? CORE.Window.title : " ", 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, flags | RGFW_windowOpenGL);
1738
1739 #ifndef PLATFORM_WEB_RGFW
1740 i32 screenSizeWidth;
1741 i32 screenSizeHeight;
1742 RGFW_window_getSize(platform.window, &screenSizeWidth, &screenSizeHeight);
1743 CORE.Window.display.width = screenSizeWidth;
1744 CORE.Window.display.height = screenSizeHeight;
1745 #else
1746 CORE.Window.display.width = CORE.Window.screen.width;
1747 CORE.Window.display.height = CORE.Window.screen.height;
1748 #endif
1749 if (FLAG_IS_SET(CORE.Window.flags, FLAG_VSYNC_HINT)) RGFW_window_swapInterval_OpenGL(platform.window, 1);
1750
1751 // Check surface and context activation
1752 if (platform.window != NULL)
1753 {
1754 CORE.Window.ready = true;
1755
1756 CORE.Window.render.width = CORE.Window.screen.width;
1757 CORE.Window.render.height = CORE.Window.screen.height;
1758 CORE.Window.currentFbo.width = CORE.Window.render.width;
1759 CORE.Window.currentFbo.height = CORE.Window.render.height;
1760 }
1761 else
1762 {
1763 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
1764 return -1;
1765 }
1766
1767 // NOTE: RGFW's exit key is distinct from raylib's exit key and
1768 // must be set to NULL to not interfere
1769 RGFW_window_setExitKey(platform.window, RGFW_keyNULL);
1770 RGFW_window_makeCurrentWindow_OpenGL(platform.window);
1771
1772 //----------------------------------------------------------------------------
1773
1774 // If everything work as expected, continue
1775 CORE.Window.position.x = platform.window->x;
1776 CORE.Window.position.y = platform.window->y;
1777 CORE.Window.render.width = CORE.Window.screen.width;
1778 CORE.Window.render.height = CORE.Window.screen.height;
1779 CORE.Window.currentFbo.width = CORE.Window.render.width;
1780 CORE.Window.currentFbo.height = CORE.Window.render.height;
1781
1782 // adjust scale if using highdpi
1783 if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)) {
1784 Vector2 scaleDpi = GetWindowScaleDPI();
1785
1786 #if defined(__APPLE__)
1787 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1788 CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1789
1790 CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1791 CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1792 CORE.Window.currentFbo.width = CORE.Window.render.width;
1793 CORE.Window.currentFbo.height = CORE.Window.render.height;
1794 #else
1795 SetMouseScale(1.0f/scaleDpi.x, 1.0f/scaleDpi.y);
1796 CORE.Window.screenScale = MatrixScale(scaleDpi.x, scaleDpi.y, 1.0f);
1797 CORE.Window.screen.width /= scaleDpi.x;
1798 CORE.Window.screen.height /= scaleDpi.y;
1799 #endif
1800 }
1801
1802 #if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1803 // apple always scales for retina
1804 #if defined(__APPLE__)
1805 RGFW_monitor* currentMonitor = RGFW_window_getMonitor(platform.window);
1806 CORE.Window.screenScale = MatrixScale(currentMonitor->pixelRatio, currentMonitor->pixelRatio, 1.0f);
1807
1808 CORE.Window.render.width = CORE.Window.screen.width * currentMonitor->pixelRatio;
1809 CORE.Window.render.height = CORE.Window.screen.height * currentMonitor->pixelRatio;
1810 CORE.Window.currentFbo.width = CORE.Window.render.width;
1811 CORE.Window.currentFbo.height = CORE.Window.render.height;
1812 #endif
1813
1814 platform.surfaceWidth = CORE.Window.currentFbo.width;
1815 platform.surfaceHeight = CORE.Window.currentFbo.height;
1816
1817 platform.surfacePixels = RL_MALLOC(platform.surfaceWidth * platform.surfaceHeight * 4);
1818 if (platform.surfacePixels == NULL)
1819 {
1820 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize software pixel buffer");
1821 return -1;
1822 }
1823
1824 platform.surface = RGFW_window_createSurface(platform.window, platform.surfacePixels, platform.surfaceWidth, platform.surfaceHeight, RGFW_formatBGRA8);
1825
1826 if (platform.surface == NULL)
1827 {
1828 RL_FREE(platform.surfacePixels);
1829
1830 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize software surface");
1831 return -1;
1832 }
1833 #endif
1834
1835 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully %s",
1836 FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_HIGHDPI)? "(HighDPI)" : "");
1837 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
1838 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
1839 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
1840 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
1841
1842 // Load OpenGL extensions
1843 // NOTE: GL procedures address loader is required to load extensions
1844 //----------------------------------------------------------------------------
1845 rlLoadExtensions((void *)RGFW_getProcAddress_OpenGL);
1846 //----------------------------------------------------------------------------
1847
1848 // Initialize timing system
1849 //----------------------------------------------------------------------------
1850 InitTimer();
1851 //----------------------------------------------------------------------------
1852
1853 // Initialize storage system
1854 //----------------------------------------------------------------------------
1855 #if defined(__APPLE__)
1856 // mac defaults to the users home folder for some reason
1857 // this is done to help them read relative paths to the binary
1858 ChangeDirectory(GetApplicationDirectory());
1859 #endif
1860
1861 CORE.Storage.basePath = GetWorkingDirectory();
1862 //----------------------------------------------------------------------------
1863
1864 #if defined(RGFW_WAYLAND)
1865 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1866 {
1867 if (RGFW_usingWayland()) TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Wayland, Software): Initialized successfully");
1868 else TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software (fallback)): Initialized successfully");
1869 }
1870 else
1871 {
1872 if (RGFW_usingWayland()) TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Wayland): Initialized successfully");
1873 else TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11 (fallback)): Initialized successfully");
1874 }
1875 #elif defined(RGFW_X11)
1876 #if defined(__APPLE__)
1877 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1878 {
1879 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software, (MacOS)): Initialized successfully");
1880 }
1881 else
1882 {
1883 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, (MacOS)): Initialized successfully");
1884 }
1885 #else
1886 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1887 {
1888 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11, Software): Initialized successfully");
1889 }
1890 else
1891 {
1892 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - X11): Initialized successfully");
1893 }
1894 #endif
1895 #elif defined (RGFW_WINDOWS)
1896 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1897 {
1898 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Win32, Software): Initialized successfully");
1899 }
1900 else
1901 {
1902 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - Win32): Initialized successfully");
1903 }
1904 #elif defined(RGFW_WASM)
1905 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1906 {
1907 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - WASMs, Software): Initialized successfully");
1908 }
1909 else
1910 {
1911 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - WASMs): Initialized successfully");
1912 }
1913 #elif defined(RGFW_MACOS)
1914 if (rlGetVersion() == RL_OPENGL_SOFTWARE)
1915 {
1916 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - MacOS, Software): Initialized successfully");
1917 }
1918 else
1919 {
1920 TRACELOG(LOG_INFO, "PLATFORM: DESKTOP (RGFW - MacOS): Initialized successfully");
1921 }
1922 #endif
1923
1924 mg_gamepads_init(&platform.minigamepad);
1925
1926 return 0;
1927 }
1928
1929 // Close platform
1930 void ClosePlatform(void)
1931 {
1932 mg_gamepads_free(&platform.minigamepad);
1933 RGFW_window_close(platform.window);
1934
1935 #if defined(GRAPHICS_API_OPENGL_SOFTWARE)
1936 if (platform.surfacePixels != NULL)
1937 {
1938 RL_FREE(platform.surfacePixels);
1939 }
1940
1941 if (platform.surface != NULL)
1942 {
1943 RGFW_surface_free(platform.surface);
1944 }
1945 #endif
1946 }
1947
1948 // Keycode mapping
1949 static KeyboardKey ConvertScancodeToKey(u32 keycode)
1950 {
1951 if (keycode > sizeof(RGFW_keyConvertTable)/sizeof(unsigned short)) return KEY_NULL;
1952
1953 return (KeyboardKey)RGFW_keyConvertTable[keycode];
1954 }
1955
1956 // Helper functions for Time
1957 double get_time_seconds(void)
1958 {
1959 double currentTime = 0.0;
1960
1961 #if defined(_WIN32)
1962 static LARGE_INTEGER freq = { 0 };
1963 static bool freqInitialized = false;
1964 LARGE_INTEGER counter = { 0 };
1965 if (!freqInitialized)
1966 {
1967 // Lazy initialization
1968 QueryPerformanceFrequency(&freq);
1969 freqInitialized = true;
1970 }
1971 QueryPerformanceCounter(&counter);
1972 currentTime = (double)counter.QuadPart/(double)freq.QuadPart;
1973 #elif defined(__EMSCRIPTEN__)
1974 currentTime = emscripten_get_now()/1000.0;
1975 #elif defined(__APPLE__)
1976 static mach_timebase_info_data_t tb = { 0 };
1977 static bool tbInitialized = false;
1978 if (!tbInitialized)
1979 {
1980 mach_timebase_info(&tb);
1981 tbInitialized = true;
1982 }
1983 uint64_t ticks = mach_absolute_time();
1984
1985 currentTime = (double)ticks*(double)tb.numer/(double)tb.denom/1e9;
1986 #elif defined(__linux__)
1987 struct timespec ts = { 0 };
1988 clock_gettime(CLOCK_MONOTONIC, &ts);
1989 currentTime = (double)ts.tv_sec + (double)ts.tv_nsec/1e9;
1990 #else
1991 // Fallback to cstd
1992 currentTime = (double)clock()/(double)CLOCKS_PER_SEC;
1993 #endif
1994
1995 return currentTime;
1996 }