1 /**********************************************************************************************
3 * raylib.core - Basic functions to manage windows, OpenGL context and input on multiple platforms
6 * - PLATFORM_DESKTOP: Windows (Win32, Win64)
7 * - PLATFORM_DESKTOP: Linux (X11 desktop mode)
8 * - PLATFORM_DESKTOP: FreeBSD, OpenBSD, NetBSD, DragonFly (X11 desktop)
9 * - PLATFORM_DESKTOP: OSX/macOS
10 * - PLATFORM_ANDROID: Android 4.0 (ARM, ARM64)
11 * - PLATFORM_RPI: Raspberry Pi 0,1,2,3,4 (Raspbian)
12 * - PLATFORM_DRM: Linux native mode, including Raspberry Pi 4 with V3D fkms driver
13 * - PLATFORM_WEB: HTML5 with asm.js (Chrome, Firefox)
14 * - PLATFORM_UWP: Windows 10 App, Windows Phone, Xbox One
18 * #define PLATFORM_DESKTOP
19 * Windowing and input system configured for desktop platforms: Windows, Linux, OSX, FreeBSD, OpenBSD, NetBSD, DragonFly
20 * NOTE: Oculus Rift CV1 requires PLATFORM_DESKTOP for mirror rendering - View [rlgl] module to enable it
22 * #define PLATFORM_ANDROID
23 * Windowing and input system configured for Android device, app activity managed internally in this module.
24 * NOTE: OpenGL ES 2.0 is required and graphic device is managed by EGL
26 * #define PLATFORM_RPI
27 * Windowing and input system configured for Raspberry Pi i native mode (no X.org required, tested on Raspbian),
28 * graphic device is managed by EGL and inputs are processed is raw mode, reading from /dev/input/
30 * #define PLATFORM_WEB
31 * Windowing and input system configured for HTML5 (run on browser), code converted from C to asm.js
32 * using emscripten compiler. OpenGL ES 2.0 required for direct translation to WebGL equivalent code.
34 * #define PLATFORM_UWP
35 * Universal Windows Platform support, using OpenGL ES 2.0 through ANGLE on multiple Windows platforms,
36 * including Windows 10 App, Windows Phone and Xbox One platforms.
38 * #define SUPPORT_DEFAULT_FONT (default)
39 * Default font is loaded on window initialization to be available for the user to render simple text.
40 * NOTE: If enabled, uses external module functions to load default raylib font (module: text)
42 * #define SUPPORT_CAMERA_SYSTEM
43 * Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
45 * #define SUPPORT_GESTURES_SYSTEM
46 * Gestures module is included (gestures.h) to support gestures detection: tap, hold, swipe, drag
48 * #define SUPPORT_MOUSE_GESTURES
49 * Mouse gestures are directly mapped like touches and processed by gestures system.
51 * #define SUPPORT_TOUCH_AS_MOUSE
52 * Touch input and mouse input are shared. Mouse functions also return touch information.
54 * #define SUPPORT_SSH_KEYBOARD_RPI (Raspberry Pi only)
55 * Reconfigure standard input to receive key inputs, works with SSH connection.
56 * WARNING: Reconfiguring standard input could lead to undesired effects, like breaking other running processes or
57 * blocking the device is not restored properly. Use with care.
59 * #define SUPPORT_MOUSE_CURSOR_NATIVE (Raspberry Pi and DRM only)
60 * Draw a mouse pointer on screen
62 * #define SUPPORT_BUSY_WAIT_LOOP
63 * Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used
65 * #define SUPPORT_HALFBUSY_WAIT_LOOP
66 * Use a half-busy wait loop, in this case frame sleeps for some time and runs a busy-wait-loop at the end
68 * #define SUPPORT_EVENTS_WAITING
69 * Wait for events passively (sleeping while no events) instead of polling them actively every frame
71 * #define SUPPORT_SCREEN_CAPTURE
72 * Allow automatic screen capture of current screen pressing F12, defined in KeyCallback()
74 * #define SUPPORT_GIF_RECORDING
75 * Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback()
77 * #define SUPPORT_COMPRESSION_API
78 * Support CompressData() and DecompressData() functions, those functions use zlib implementation
79 * provided by stb_image and stb_image_write libraries, so, those libraries must be enabled on textures module
82 * #define SUPPORT_DATA_STORAGE
83 * Support saving binary data automatically to a generated storage.data file. This file is managed internally
87 * rglfw - Manage graphic device, OpenGL context and inputs on PLATFORM_DESKTOP (Windows, Linux, OSX. FreeBSD, OpenBSD, NetBSD, DragonFly)
88 * raymath - 3D math functionality (Vector2, Vector3, Matrix, Quaternion)
89 * camera - Multiple 3D camera modes (free, orbital, 1st person, 3rd person)
90 * gestures - Gestures system for touch-ready devices (or simulated from mouse inputs)
93 * LICENSE: zlib/libpng
95 * Copyright (c) 2013-2021 Ramon Santamaria (@raysan5)
97 * This software is provided "as-is", without any express or implied warranty. In no event
98 * will the authors be held liable for any damages arising from the use of this software.
100 * Permission is granted to anyone to use this software for any purpose, including commercial
101 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
103 * 1. The origin of this software must not be misrepresented; you must not claim that you
104 * wrote the original software. If you use this software in a product, an acknowledgment
105 * in the product documentation would be appreciated but is not required.
107 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
108 * as being the original software.
110 * 3. This notice may not be removed or altered from any source distribution.
112 **********************************************************************************************/
114 #include "raylib.h" // Declares module functions
116 // Check if config flags have been externally provided on compilation line
117 #if !defined(EXTERNAL_CONFIG_FLAGS)
118 #include "config.h" // Defines module configuration flags
120 #define RAYLIB_VERSION "3.7"
123 #include "utils.h" // Required for: TRACELOG macros
125 #if (defined(__linux__) || defined(PLATFORM_WEB)) && _POSIX_C_SOURCE < 199309L
126 #undef _POSIX_C_SOURCE
127 #define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
130 #define RAYMATH_IMPLEMENTATION // Define external out-of-line implementation of raymath here
131 #include "raymath.h" // Required for: Vector3 and Matrix functions
133 #define RLGL_IMPLEMENTATION
134 #include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2
136 #if defined(SUPPORT_GESTURES_SYSTEM)
137 #define GESTURES_IMPLEMENTATION
138 #include "gestures.h" // Gestures detection functionality
141 #if defined(SUPPORT_CAMERA_SYSTEM)
142 #define CAMERA_IMPLEMENTATION
143 #include "camera.h" // Camera system functionality
146 #if defined(SUPPORT_GIF_RECORDING)
147 //#define MSF_GIF_MALLOC RL_MALLOC
148 //#define MSF_GIF_FREE RL_FREE
151 #include "external/msf_gif.h" // Support GIF recording
154 #if defined(SUPPORT_COMPRESSION_API)
155 #define SINFL_IMPLEMENTATION
156 #include "external/sinfl.h"
158 #define SDEFL_IMPLEMENTATION
159 #include "external/sdefl.h"
162 #include <stdlib.h> // Required for: srand(), rand(), atexit()
163 #include <stdio.h> // Required for: sprintf() [Used in OpenURL()]
164 #include <string.h> // Required for: strrchr(), strcmp(), strlen()
165 #include <time.h> // Required for: time() [Used in InitTimer()]
166 #include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
168 #include <sys/stat.h> // Required for: stat() [Used in GetFileModTime()]
170 #if (defined(PLATFORM_DESKTOP) || defined(PLATFORM_UWP)) && defined(_WIN32) && (defined(_MSC_VER) || defined(__TINYC__))
171 #define DIRENT_MALLOC RL_MALLOC
172 #define DIRENT_FREE RL_FREE
174 #include "external/dirent.h" // Required for: DIR, opendir(), closedir() [Used in GetDirectoryFiles()]
176 #include <dirent.h> // Required for: DIR, opendir(), closedir() [Used in GetDirectoryFiles()]
180 #include <direct.h> // Required for: _getch(), _chdir()
181 #define GETCWD _getcwd // NOTE: MSDN recommends not to use getcwd(), chdir()
183 #include <io.h> // Required for _access() [Used in FileExists()]
185 #include <unistd.h> // Required for: getch(), chdir() (POSIX), access()
186 #define GETCWD getcwd
190 #if defined(PLATFORM_DESKTOP)
191 #define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
192 // NOTE: Already provided by rlgl implementation (on glad.h)
193 #include "GLFW/glfw3.h" // GLFW3 library: Windows, OpenGL context and Input management
194 // NOTE: GLFW3 already includes gl.h (OpenGL) headers
196 // Support retrieving native window handlers
198 #define GLFW_EXPOSE_NATIVE_WIN32
199 #include "GLFW/glfw3native.h" // WARNING: It requires customization to avoid windows.h inclusion!
201 #if defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP)
202 // NOTE: Those functions require linking with winmm library
203 unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod);
204 unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
207 #if defined(__linux__) || defined(__FreeBSD__)
208 #include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
210 //#define GLFW_EXPOSE_NATIVE_X11 // WARNING: Exposing Xlib.h > X.h results in dup symbols for Font type
211 //#define GLFW_EXPOSE_NATIVE_WAYLAND
212 //#define GLFW_EXPOSE_NATIVE_MIR
213 #include "GLFW/glfw3native.h" // Required for: glfwGetX11Window()
215 #if defined(__APPLE__)
216 #include <unistd.h> // Required for: usleep()
218 //#define GLFW_EXPOSE_NATIVE_COCOA // WARNING: Fails due to type redefinition
219 #include "GLFW/glfw3native.h" // Required for: glfwGetCocoaWindow()
223 #if defined(PLATFORM_ANDROID)
224 //#include <android/sensor.h> // Android sensors functions (accelerometer, gyroscope, light...)
225 #include <android/window.h> // Defines AWINDOW_FLAG_FULLSCREEN and others
226 #include <android_native_app_glue.h> // Defines basic app state struct and manages activity
228 #include <EGL/egl.h> // Native platform windowing system interface
229 //#include <GLES2/gl2.h> // OpenGL ES 2.0 library (not required in this module)
232 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
233 #include <fcntl.h> // POSIX file control definitions - open(), creat(), fcntl()
234 #include <unistd.h> // POSIX standard function definitions - read(), close(), STDIN_FILENO
235 #include <termios.h> // POSIX terminal control definitions - tcgetattr(), tcsetattr()
236 #include <pthread.h> // POSIX threads management (inputs reading)
237 #include <dirent.h> // POSIX directory browsing
239 #include <sys/ioctl.h> // UNIX System call for device-specific input/output operations - ioctl()
240 #include <linux/kd.h> // Linux: KDSKBMODE, K_MEDIUMRAM constants definition
241 #include <linux/input.h> // Linux: Keycodes constants definition (KEY_A, ...)
242 #include <linux/joystick.h> // Linux: Joystick support library
244 #if defined(PLATFORM_RPI)
245 #include "bcm_host.h" // Raspberry Pi VideoCore IV access functions
248 #if defined(PLATFORM_DRM)
249 #include <gbm.h> // Generic Buffer Management
250 #include <xf86drm.h> // Direct Rendering Manager user-level library interface
251 #include <xf86drmMode.h> // Direct Rendering Manager modesetting interface
254 #include "EGL/egl.h" // Native platform windowing system interface
255 #include "EGL/eglext.h" // EGL extensions
256 //#include "GLES2/gl2.h" // OpenGL ES 2.0 library (not required in this module)
259 #if defined(PLATFORM_UWP)
260 #include "EGL/egl.h" // Native platform windowing system interface
261 #include "EGL/eglext.h" // EGL extensions
262 //#include "GLES2/gl2.h" // OpenGL ES 2.0 library (not required in this module)
263 #include "uwp_events.h" // UWP bootstrapping functions
266 #if defined(PLATFORM_WEB)
267 #define GLFW_INCLUDE_ES2 // GLFW3: Enable OpenGL ES 2.0 (translated to WebGL)
268 #include "GLFW/glfw3.h" // GLFW3 library: Windows, OpenGL context and Input management
269 #include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
271 #include <emscripten/emscripten.h> // Emscripten library - LLVM to JavaScript compiler
272 #include <emscripten/html5.h> // Emscripten HTML5 library
275 #if defined(SUPPORT_COMPRESSION_API)
276 // NOTE: Those declarations require stb_image and stb_image_write definitions, included in textures module
277 unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality);
278 char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen);
281 //----------------------------------------------------------------------------------
282 // Defines and Macros
283 //----------------------------------------------------------------------------------
284 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
285 #define USE_LAST_TOUCH_DEVICE // When multiple touchscreens are connected, only use the one with the highest event<N> number
287 #define DEFAULT_GAMEPAD_DEV "/dev/input/js" // Gamepad input (base dev for all gamepads: js0, js1, ...)
288 #define DEFAULT_EVDEV_PATH "/dev/input/" // Path to the linux input events
291 #ifndef MAX_FILEPATH_LENGTH
292 #if defined(__linux__)
293 #define MAX_FILEPATH_LENGTH 4096 // Maximum length for filepaths (Linux PATH_MAX default value)
295 #define MAX_FILEPATH_LENGTH 512 // Maximum length supported for filepaths
300 #define MAX_GAMEPADS 4 // Max number of gamepads supported
302 #ifndef MAX_GAMEPAD_AXIS
303 #define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
305 #ifndef MAX_GAMEPAD_BUTTONS
306 #define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad)
308 #ifndef MAX_TOUCH_POINTS
309 #define MAX_TOUCH_POINTS 10 // Maximum number of touch points supported
311 #ifndef MAX_KEY_PRESSED_QUEUE
312 #define MAX_KEY_PRESSED_QUEUE 16 // Max number of keys in the key input queue
314 #ifndef MAX_CHAR_PRESSED_QUEUE
315 #define MAX_CHAR_PRESSED_QUEUE 16 // Max number of characters in the char input queue
318 #if defined(SUPPORT_DATA_STORAGE)
319 #ifndef STORAGE_DATA_FILE
320 #define STORAGE_DATA_FILE "storage.data" // Automatic storage filename
324 #ifndef MAX_DECOMPRESSION_SIZE
325 #define MAX_DECOMPRESSION_SIZE 64 // Max size allocated for decompression in MB
328 // Flags operation macros
329 #define FLAG_SET(n, f) ((n) |= (f))
330 #define FLAG_CLEAR(n, f) ((n) &= ~(f))
331 #define FLAG_TOGGLE(n, f) ((n) ^= (f))
332 #define FLAG_CHECK(n, f) ((n) & (f))
334 //----------------------------------------------------------------------------------
335 // Types and Structures Definition
336 //----------------------------------------------------------------------------------
337 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
339 pthread_t threadId; // Event reading thread id
340 int fd; // File descriptor to the device it is assigned to
341 int eventNum; // Number of 'event<N>' device
342 Rectangle absRange; // Range of values for absolute pointing devices (touchscreens)
343 int touchSlot; // Hold the touch slot number of the currently being sent multitouch block
344 bool isMouse; // True if device supports relative X Y movements
345 bool isTouch; // True if device supports absolute X Y movements and has BTN_TOUCH
346 bool isMultitouch; // True if device supports multiple absolute movevents and has BTN_TOUCH
347 bool isKeyboard; // True if device has letter keycodes
348 bool isGamepad; // True if device has gamepad buttons
352 typedef struct { int x; int y; } Point;
353 typedef struct { unsigned int width; unsigned int height; } Size;
355 // Core global state context data
356 typedef struct CoreData {
358 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
359 GLFWwindow *handle; // Native window handle (graphic device)
361 #if defined(PLATFORM_RPI)
362 EGL_DISPMANX_WINDOW_T handle; // Native window handle (graphic device)
364 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
365 #if defined(PLATFORM_DRM)
366 int fd; // /dev/dri/... file descriptor
367 drmModeConnector *connector; // Direct Rendering Manager (DRM) mode connector
368 int modeIndex; // index of the used mode of connector->modes
369 drmModeCrtc *crtc; // crt controller
370 struct gbm_device *gbmDevice; // device of Generic Buffer Management (GBM, native platform for EGL on DRM)
371 struct gbm_surface *gbmSurface; // surface of GBM
372 struct gbm_bo *prevBO; // previous used GBM buffer object (during frame swapping)
373 uint32_t prevFB; // previous used GBM framebufer (during frame swapping)
375 EGLDisplay device; // Native display device (physical screen connection)
376 EGLSurface surface; // Surface to draw on, framebuffers (connected to context)
377 EGLContext context; // Graphic context, mode in which drawing can be done
378 EGLConfig config; // Graphic config
380 const char *title; // Window text title const pointer
381 unsigned int flags; // Configuration flags (bit based), keeps window state
382 bool ready; // Check if window has been initialized successfully
383 bool fullscreen; // Check if fullscreen mode is enabled
384 bool shouldClose; // Check if window set for closing
385 bool resizedLastFrame; // Check if window has been resized last frame
387 Point position; // Window position on screen (required on fullscreen toggle)
388 Size display; // Display width and height (monitor, device-screen, LCD, ...)
389 Size screen; // Screen width and height (used render area)
390 Size currentFbo; // Current render width and height, it could change on BeginTextureMode()
391 Size render; // Framebuffer width and height (render area, including black bars if required)
392 Point renderOffset; // Offset from render area (must be divided by 2)
393 Matrix screenScale; // Matrix to scale screen (framebuffer rendering)
395 char **dropFilesPath; // Store dropped files paths as strings
396 int dropFilesCount; // Count dropped files strings
399 #if defined(PLATFORM_ANDROID)
401 bool appEnabled; // Flag to detect if app is active ** = true
402 struct android_app *app; // Android activity
403 struct android_poll_source *source; // Android events polling source
404 const char *internalDataPath; // Android internal data path to write data (/data/data/<package>/files)
405 bool contextRebindRequired; // Used to know context rebind required
408 #if defined(PLATFORM_UWP)
410 const char *internalDataPath; // UWP App data path
414 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
415 InputEventWorker eventWorker[10]; // List of worker threads for every monitored "/dev/input/event<N>"
418 int exitKey; // Default exit key
419 char currentKeyState[512]; // Registers current frame key state
420 char previousKeyState[512]; // Registers previous frame key state
422 int keyPressedQueue[MAX_KEY_PRESSED_QUEUE]; // Input keys queue
423 int keyPressedQueueCount; // Input keys queue count
425 int charPressedQueue[MAX_CHAR_PRESSED_QUEUE]; // Input characters queue
426 int charPressedQueueCount; // Input characters queue count
428 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
429 int defaultMode; // Default keyboard mode
430 struct termios defaultSettings; // Default keyboard settings
431 int fd; // File descriptor for the evdev keyboard
435 Vector2 position; // Mouse position on screen
436 Vector2 offset; // Mouse offset
437 Vector2 scale; // Mouse scaling
439 int cursor; // Tracks current mouse cursor
440 bool cursorHidden; // Track if cursor is hidden
441 bool cursorOnScreen; // Tracks if cursor is inside client area
443 char currentButtonState[3]; // Registers current mouse button state
444 char previousButtonState[3]; // Registers previous mouse button state
445 float currentWheelMove; // Registers current mouse wheel variation
446 float previousWheelMove; // Registers previous mouse wheel variation
447 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
448 char currentButtonStateEvdev[3]; // Holds the new mouse state for the next polling event to grab (Can't be written directly due to multithreading, app could miss the update)
452 Vector2 position[MAX_TOUCH_POINTS]; // Touch position on screen
453 char currentTouchState[MAX_TOUCH_POINTS]; // Registers current touch state
454 char previousTouchState[MAX_TOUCH_POINTS]; // Registers previous touch state
457 int lastButtonPressed; // Register last gamepad button pressed
458 int axisCount; // Register number of available gamepad axis
459 bool ready[MAX_GAMEPADS]; // Flag to know if gamepad is ready
460 float axisState[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state
461 char currentState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Current gamepad buttons state
462 char previousState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Previous gamepad buttons state
463 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
464 pthread_t threadId; // Gamepad reading thread id
465 int streamId[MAX_GAMEPADS]; // Gamepad device file descriptor
466 char name[64]; // Gamepad name holder
471 double current; // Current time measure
472 double previous; // Previous time measure
473 double update; // Time measure for frame update
474 double draw; // Time measure for frame draw
475 double frame; // Time measure for one frame
476 double target; // Desired time for one frame, if 0 not applied
477 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
478 unsigned long long base; // Base time measure for hi-res timer
483 //----------------------------------------------------------------------------------
484 // Global Variables Definition
485 //----------------------------------------------------------------------------------
486 static CoreData CORE = { 0 }; // Global CORE state context
488 static char **dirFilesPath = NULL; // Store directory files paths as strings
489 static int dirFilesCount = 0; // Count directory files strings
491 #if defined(SUPPORT_SCREEN_CAPTURE)
492 static int screenshotCounter = 0; // Screenshots counter
495 #if defined(SUPPORT_GIF_RECORDING)
496 static int gifFramesCounter = 0; // GIF frames counter
497 static bool gifRecording = false; // GIF recording state
498 static MsfGifState gifState = { 0 }; // MSGIF context state
500 //-----------------------------------------------------------------------------------
502 //----------------------------------------------------------------------------------
503 // Other Modules Functions Declaration (required by core)
504 //----------------------------------------------------------------------------------
505 #if defined(SUPPORT_DEFAULT_FONT)
506 extern void LoadFontDefault(void); // [Module: text] Loads default font on InitWindow()
507 extern void UnloadFontDefault(void); // [Module: text] Unloads default font from GPU memory
510 //----------------------------------------------------------------------------------
511 // Module specific Functions Declaration
512 //----------------------------------------------------------------------------------
513 static bool InitGraphicsDevice(int width, int height); // Initialize graphics device
514 static void SetupFramebuffer(int width, int height); // Setup main framebuffer
515 static void SetupViewport(int width, int height); // Set viewport for a provided width and height
516 static void SwapBuffers(void); // Copy back buffer to front buffer
518 static void InitTimer(void); // Initialize timer
519 static void Wait(float ms); // Wait for some milliseconds (stop program execution)
521 static void PollInputEvents(void); // Register user events
523 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
524 static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
525 // Window callbacks events
526 static void WindowSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
527 #if !defined(PLATFORM_WEB)
528 static void WindowMaximizeCallback(GLFWwindow* window, int maximized); // GLFW3 Window Maximize Callback, runs when window is maximized
530 static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
531 static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
532 static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
533 // Input callbacks events
534 static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
535 static void CharCallback(GLFWwindow *window, unsigned int key); // GLFW3 Char Key Callback, runs on key pressed (get char value)
536 static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods); // GLFW3 Mouse Button Callback, runs on mouse button pressed
537 static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); // GLFW3 Cursor Position Callback, runs on mouse move
538 static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset); // GLFW3 Srolling Callback, runs on mouse wheel
539 static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
542 #if defined(PLATFORM_ANDROID)
543 static void AndroidCommandCallback(struct android_app *app, int32_t cmd); // Process Android activity lifecycle commands
544 static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event); // Process Android inputs
547 #if defined(PLATFORM_WEB)
548 static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
549 static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData);
552 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
553 #if defined(SUPPORT_SSH_KEYBOARD_RPI)
554 static void InitKeyboard(void); // Init raw keyboard system (standard input reading)
555 static void ProcessKeyboard(void); // Process keyboard events
556 static void RestoreKeyboard(void); // Restore keyboard system
558 static void InitTerminal(void); // Init terminal (block echo and signal short cuts)
559 static void RestoreTerminal(void); // Restore terminal
562 static void InitEvdevInput(void); // Evdev inputs initialization
563 static void ConfigureEvdevDevice(char *device); // Identifies a input device and configures it for use if appropriate
564 static void PollKeyboardEvents(void); // Process evdev keyboard events.
565 static void *EventThread(void *arg); // Input device events reading thread
567 static void InitGamepad(void); // Init raw gamepad input
568 static void *GamepadThread(void *arg); // Mouse reading thread
570 #if defined(PLATFORM_DRM)
571 static int FindMatchingConnectorMode(const drmModeConnector *connector, const drmModeModeInfo *mode); // Search matching DRM mode in connector's mode list
572 static int FindExactConnectorMode(const drmModeConnector *connector, uint width, uint height, uint fps, bool allowInterlaced); // Search exactly matching DRM connector mode in connector's list
573 static int FindNearestConnectorMode(const drmModeConnector *connector, uint width, uint height, uint fps, bool allowInterlaced); // Search the nearest matching DRM connector mode in connector's list
576 #endif // PLATFORM_RPI || PLATFORM_DRM
579 // NOTE: We include Sleep() function signature here to avoid windows.h inclusion (kernel32 lib)
580 void __stdcall Sleep(unsigned long msTimeout); // Required for Wait()
583 //----------------------------------------------------------------------------------
584 // Module Functions Definition - Window and OpenGL Context Functions
585 //----------------------------------------------------------------------------------
587 #if defined(PLATFORM_ANDROID)
588 // To allow easier porting to android, we allow the user to define a
589 // main function which we call from android_main, defined by ourselves
590 extern int main(int argc, char *argv[]);
592 void android_main(struct android_app *app)
594 char arg0[] = "raylib"; // NOTE: argv[] are mutable
595 CORE.Android.app = app;
597 // TODO: Should we maybe report != 0 return codes somewhere?
598 (void)main(1, (char *[]) { arg0, NULL });
601 // TODO: Add this to header (if apps really need it)
602 struct android_app *GetAndroidApp(void)
604 return CORE.Android.app;
607 #if (defined(PLATFORM_RPI) || defined(PLATFORM_DRM)) && !defined(SUPPORT_SSH_KEYBOARD_RPI)
608 // Init terminal (block echo and signal short cuts)
609 static void InitTerminal(void)
611 TRACELOG(LOG_INFO, "RPI: Reconfiguring terminal...");
613 // Save terminal keyboard settings and reconfigure terminal with new settings
614 struct termios keyboardNewSettings;
615 tcgetattr(STDIN_FILENO, &CORE.Input.Keyboard.defaultSettings); // Get current keyboard settings
616 keyboardNewSettings = CORE.Input.Keyboard.defaultSettings;
618 // New terminal settings for keyboard: turn off buffering (non-canonical mode), echo
619 // NOTE: ISIG controls if ^C and ^Z generate break signals or not
620 keyboardNewSettings.c_lflag &= ~(ICANON | ECHO | ISIG);
621 keyboardNewSettings.c_cc[VMIN] = 1;
622 keyboardNewSettings.c_cc[VTIME] = 0;
624 // Set new keyboard settings (change occurs immediately)
625 tcsetattr(STDIN_FILENO, TCSANOW, &keyboardNewSettings);
627 // Save old keyboard mode to restore it at the end
628 if (ioctl(STDIN_FILENO, KDGKBMODE, &CORE.Input.Keyboard.defaultMode) < 0)
630 // NOTE: It could mean we are using a remote keyboard through ssh or from the desktop
631 TRACELOG(LOG_WARNING, "RPI: Failed to change keyboard mode (not a local terminal)");
633 else ioctl(STDIN_FILENO, KDSKBMODE, K_XLATE);
635 // Register terminal restore when program finishes
636 atexit(RestoreTerminal);
639 static void RestoreTerminal(void)
641 TRACELOG(LOG_INFO, "RPI: Restoring terminal...");
643 // Reset to default keyboard settings
644 tcsetattr(STDIN_FILENO, TCSANOW, &CORE.Input.Keyboard.defaultSettings);
646 // Reconfigure keyboard to default mode
647 ioctl(STDIN_FILENO, KDSKBMODE, CORE.Input.Keyboard.defaultMode);
650 // Initialize window and OpenGL context
651 // NOTE: data parameter could be used to pass any kind of required data to the initialization
652 void InitWindow(int width, int height, const char *title)
654 #if defined(PLATFORM_UWP)
655 if (!UWPIsConfigured())
657 TRACELOG(LOG_ERROR, "UWP Functions have not been set yet, please set these before initializing raylib!");
662 TRACELOG(LOG_INFO, "Initializing raylib %s", RAYLIB_VERSION);
664 if ((title != NULL) && (title[0] != 0)) CORE.Window.title = title;
666 // Initialize required global values different than 0
667 CORE.Input.Keyboard.exitKey = KEY_ESCAPE;
668 CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f };
669 CORE.Input.Mouse.cursor = MOUSE_CURSOR_ARROW;
670 CORE.Input.Gamepad.lastButtonPressed = -1;
672 #if defined(PLATFORM_UWP)
673 // The axis count is 6 (2 thumbsticks and left and right trigger)
674 CORE.Input.Gamepad.axisCount = 6;
677 #if defined(PLATFORM_ANDROID)
678 CORE.Window.screen.width = width;
679 CORE.Window.screen.height = height;
680 CORE.Window.currentFbo.width = width;
681 CORE.Window.currentFbo.height = height;
683 // Input data is android app pointer
684 CORE.Android.internalDataPath = CORE.Android.app->activity->internalDataPath;
686 // Set desired windows flags before initializing anything
687 ANativeActivity_setWindowFlags(CORE.Android.app->activity, AWINDOW_FLAG_FULLSCREEN, 0); //AWINDOW_FLAG_SCALED, AWINDOW_FLAG_DITHER
689 int orientation = AConfiguration_getOrientation(CORE.Android.app->config);
691 if (orientation == ACONFIGURATION_ORIENTATION_PORT) TRACELOG(LOG_INFO, "ANDROID: Window orientation set as portrait");
692 else if (orientation == ACONFIGURATION_ORIENTATION_LAND) TRACELOG(LOG_INFO, "ANDROID: Window orientation set as landscape");
694 // TODO: Automatic orientation doesn't seem to work
697 AConfiguration_setOrientation(CORE.Android.app->config, ACONFIGURATION_ORIENTATION_PORT);
698 TRACELOG(LOG_WARNING, "ANDROID: Window orientation changed to portrait");
702 AConfiguration_setOrientation(CORE.Android.app->config, ACONFIGURATION_ORIENTATION_LAND);
703 TRACELOG(LOG_WARNING, "ANDROID: Window orientation changed to landscape");
706 //AConfiguration_getDensity(CORE.Android.app->config);
707 //AConfiguration_getKeyboard(CORE.Android.app->config);
708 //AConfiguration_getScreenSize(CORE.Android.app->config);
709 //AConfiguration_getScreenLong(CORE.Android.app->config);
711 CORE.Android.app->onAppCmd = AndroidCommandCallback;
712 CORE.Android.app->onInputEvent = AndroidInputCallback;
714 InitAssetManager(CORE.Android.app->activity->assetManager, CORE.Android.app->activity->internalDataPath);
716 TRACELOG(LOG_INFO, "ANDROID: App initialized successfully");
718 // Android ALooper_pollAll() variables
722 // Wait for window to be initialized (display and context)
723 while (!CORE.Window.ready)
725 // Process events loop
726 while ((pollResult = ALooper_pollAll(0, NULL, &pollEvents, (void**)&CORE.Android.source)) >= 0)
728 // Process this event
729 if (CORE.Android.source != NULL) CORE.Android.source->process(CORE.Android.app, CORE.Android.source);
731 // NOTE: Never close window, native activity is controlled by the system!
732 //if (CORE.Android.app->destroyRequested != 0) CORE.Window.shouldClose = true;
736 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_UWP) || defined(PLATFORM_DRM)
737 // Init graphics device (display device and OpenGL context)
738 // NOTE: returns true if window and graphic device has been initialized successfully
739 CORE.Window.ready = InitGraphicsDevice(width, height);
741 if (!CORE.Window.ready) return;
746 #if defined(SUPPORT_DEFAULT_FONT)
748 // NOTE: External functions (defined in module: text)
750 Rectangle rec = GetFontDefault().recs[95];
751 // NOTE: We setup a 1px padding on char rectangle to avoid pixel bleeding on MSAA filtering
752 SetShapesTexture(GetFontDefault().texture, (Rectangle){ rec.x + 1, rec.y + 1, rec.width - 2, rec.height - 2 });
754 // Set default internal texture (1px white) and rectangle to be used for shapes drawing
755 SetShapesTexture(rlGetTextureDefault(), (Rectangle){ 0.0f, 0.0f, 1.0f, 1.0f });
757 #if defined(PLATFORM_DESKTOP)
758 if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
760 // Set default font texture filter for HighDPI (blurry)
761 SetTextureFilter(GetFontDefault().texture, TEXTURE_FILTER_BILINEAR);
765 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
766 // Init raw input system
767 InitEvdevInput(); // Evdev inputs initialization
768 InitGamepad(); // Gamepad init
769 #if defined(SUPPORT_SSH_KEYBOARD_RPI)
770 InitKeyboard(); // Keyboard init
772 InitTerminal(); // Terminal init
776 #if defined(PLATFORM_WEB)
777 // Detect fullscreen change events
778 //emscripten_set_fullscreenchange_callback("#canvas", NULL, 1, EmscriptenFullscreenChangeCallback);
779 //emscripten_set_resize_callback("#canvas", NULL, 1, EmscriptenResizeCallback);
781 // Support keyboard events
782 //emscripten_set_keypress_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback);
783 //emscripten_set_keydown_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback);
785 // Support touch events
786 emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
787 emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
788 emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
789 emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
791 // Support gamepad events (not provided by GLFW3 on emscripten)
792 emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback);
793 emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback);
796 CORE.Input.Mouse.position.x = (float)CORE.Window.screen.width/2.0f;
797 CORE.Input.Mouse.position.y = (float)CORE.Window.screen.height/2.0f;
798 #endif // PLATFORM_ANDROID
801 // Close window and unload OpenGL context
802 void CloseWindow(void)
804 #if defined(SUPPORT_GIF_RECORDING)
807 MsfGifResult result = msf_gif_end(&gifState);
808 msf_gif_free(result);
809 gifRecording = false;
813 #if defined(SUPPORT_DEFAULT_FONT)
817 rlglClose(); // De-init rlgl
819 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
820 glfwDestroyWindow(CORE.Window.handle);
824 #if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_UWP)
825 timeEndPeriod(1); // Restore time period
828 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
829 #if defined(PLATFORM_DRM)
830 if (CORE.Window.prevFB)
832 drmModeRmFB(CORE.Window.fd, CORE.Window.prevFB);
833 CORE.Window.prevFB = 0;
836 if (CORE.Window.prevBO)
838 gbm_surface_release_buffer(CORE.Window.gbmSurface, CORE.Window.prevBO);
839 CORE.Window.prevBO = NULL;
842 if (CORE.Window.gbmSurface)
844 gbm_surface_destroy(CORE.Window.gbmSurface);
845 CORE.Window.gbmSurface = NULL;
848 if (CORE.Window.gbmDevice)
850 gbm_device_destroy(CORE.Window.gbmDevice);
851 CORE.Window.gbmDevice = NULL;
854 if (CORE.Window.crtc)
856 if (CORE.Window.connector)
858 drmModeSetCrtc(CORE.Window.fd, CORE.Window.crtc->crtc_id, CORE.Window.crtc->buffer_id,
859 CORE.Window.crtc->x, CORE.Window.crtc->y, &CORE.Window.connector->connector_id, 1, &CORE.Window.crtc->mode);
860 drmModeFreeConnector(CORE.Window.connector);
861 CORE.Window.connector = NULL;
864 drmModeFreeCrtc(CORE.Window.crtc);
865 CORE.Window.crtc = NULL;
868 if (CORE.Window.fd != -1)
870 close(CORE.Window.fd);
875 // Close surface, context and display
876 if (CORE.Window.device != EGL_NO_DISPLAY)
878 #if !defined(PLATFORM_DRM)
879 eglMakeCurrent(CORE.Window.device, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
881 if (CORE.Window.surface != EGL_NO_SURFACE)
883 eglDestroySurface(CORE.Window.device, CORE.Window.surface);
884 CORE.Window.surface = EGL_NO_SURFACE;
887 if (CORE.Window.context != EGL_NO_CONTEXT)
889 eglDestroyContext(CORE.Window.device, CORE.Window.context);
890 CORE.Window.context = EGL_NO_CONTEXT;
893 eglTerminate(CORE.Window.device);
894 CORE.Window.device = EGL_NO_DISPLAY;
898 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
899 // Wait for mouse and gamepad threads to finish before closing
900 // NOTE: Those threads should already have finished at this point
901 // because they are controlled by CORE.Window.shouldClose variable
903 CORE.Window.shouldClose = true; // Added to force threads to exit when the close window is called
905 // Close the evdev keyboard
906 if (CORE.Input.Keyboard.fd != -1)
908 close(CORE.Input.Keyboard.fd);
909 CORE.Input.Keyboard.fd = -1;
912 for (int i = 0; i < sizeof(CORE.Input.eventWorker)/sizeof(InputEventWorker); ++i)
914 if (CORE.Input.eventWorker[i].threadId)
916 pthread_join(CORE.Input.eventWorker[i].threadId, NULL);
921 if (CORE.Input.Gamepad.threadId) pthread_join(CORE.Input.Gamepad.threadId, NULL);
924 TRACELOG(LOG_INFO, "Window closed successfully");
927 // Check if KEY_ESCAPE pressed or Close icon pressed
928 bool WindowShouldClose(void)
930 #if defined(PLATFORM_WEB)
931 // Emterpreter-Async required to run sync code
932 // https://github.com/emscripten-core/emscripten/wiki/Emterpreter#emterpreter-async-run-synchronous-code
933 // By default, this function is never called on a web-ready raylib example because we encapsulate
934 // frame code in a UpdateDrawFrame() function, to allow browser manage execution asynchronously
935 // but now emscripten allows sync code to be executed in an interpreted way, using emterpreter!
936 emscripten_sleep(16);
940 #if defined(PLATFORM_DESKTOP)
941 if (CORE.Window.ready)
943 // While window minimized, stop loop execution
944 while (IsWindowState(FLAG_WINDOW_MINIMIZED) && !IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) glfwWaitEvents();
946 CORE.Window.shouldClose = glfwWindowShouldClose(CORE.Window.handle);
948 // Reset close status for next frame
949 glfwSetWindowShouldClose(CORE.Window.handle, GLFW_FALSE);
951 return CORE.Window.shouldClose;
956 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
957 if (CORE.Window.ready) return CORE.Window.shouldClose;
962 // Check if window has been initialized successfully
963 bool IsWindowReady(void)
965 return CORE.Window.ready;
968 // Check if window is currently fullscreen
969 bool IsWindowFullscreen(void)
971 return CORE.Window.fullscreen;
974 // Check if window is currently hidden
975 bool IsWindowHidden(void)
977 #if defined(PLATFORM_DESKTOP)
978 return ((CORE.Window.flags & FLAG_WINDOW_HIDDEN) > 0);
983 // Check if window has been minimized
984 bool IsWindowMinimized(void)
986 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
987 return ((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) > 0);
993 // Check if window has been maximized (only PLATFORM_DESKTOP)
994 bool IsWindowMaximized(void)
996 #if defined(PLATFORM_DESKTOP)
997 return ((CORE.Window.flags & FLAG_WINDOW_MAXIMIZED) > 0);
1003 // Check if window has the focus
1004 bool IsWindowFocused(void)
1006 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
1007 return ((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) == 0); // TODO!
1013 // Check if window has been resizedLastFrame
1014 bool IsWindowResized(void)
1016 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
1017 return CORE.Window.resizedLastFrame;
1023 // Check if one specific window flag is enabled
1024 bool IsWindowState(unsigned int flag)
1026 return ((CORE.Window.flags & flag) > 0);
1029 // Toggle fullscreen mode (only PLATFORM_DESKTOP)
1030 void ToggleFullscreen(void)
1032 #if defined(PLATFORM_DESKTOP)
1033 // NOTE: glfwSetWindowMonitor() doesn't work properly (bugs)
1034 if (!CORE.Window.fullscreen)
1036 // Store previous window position (in case we exit fullscreen)
1037 glfwGetWindowPos(CORE.Window.handle, &CORE.Window.position.x, &CORE.Window.position.y);
1039 int monitorCount = 0;
1040 GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
1042 int monitorIndex = GetCurrentMonitor();
1044 // Use current monitor, so we correctly get the display the window is on
1045 GLFWmonitor* monitor = monitorIndex < monitorCount ? monitors[monitorIndex] : NULL;
1049 TRACELOG(LOG_WARNING, "GLFW: Failed to get monitor");
1051 CORE.Window.fullscreen = false; // Toggle fullscreen flag
1052 CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
1054 glfwSetWindowMonitor(CORE.Window.handle, NULL, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
1058 CORE.Window.fullscreen = true; // Toggle fullscreen flag
1059 CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
1061 glfwSetWindowMonitor(CORE.Window.handle, monitor, 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
1065 CORE.Window.fullscreen = false; // Toggle fullscreen flag
1066 CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
1068 glfwSetWindowMonitor(CORE.Window.handle, NULL, CORE.Window.position.x, CORE.Window.position.y, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
1071 // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
1072 // NOTE: V-Sync can be enabled by graphic driver configuration
1073 if (CORE.Window.flags & FLAG_VSYNC_HINT) glfwSwapInterval(1);
1075 #if defined(PLATFORM_WEB)
1078 // This strategy works well while using raylib minimal web shell for emscripten,
1079 // it re-scales the canvas to fullscreen using monitor resolution, for tools this
1080 // is a good strategy but maybe games prefer to keep current canvas resolution and
1081 // display it in fullscreen, adjusting monitor resolution if possible
1082 if (document.fullscreenElement) document.exitFullscreen();
1083 else Module.requestFullscreen(false, true);
1087 if (!CORE.Window.fullscreen)
1089 // Option 1: Request fullscreen for the canvas element
1090 // This option does not seem to work at all
1091 //emscripten_request_fullscreen("#canvas", false);
1093 // Option 2: Request fullscreen for the canvas element with strategy
1094 // This option does not seem to work at all
1095 // Ref: https://github.com/emscripten-core/emscripten/issues/5124
1096 // EmscriptenFullscreenStrategy strategy = {
1097 // .scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH, //EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT,
1098 // .canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF,
1099 // .filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
1100 // .canvasResizedCallback = EmscriptenWindowResizedCallback,
1101 // .canvasResizedCallbackUserData = NULL
1103 //emscripten_request_fullscreen_strategy("#canvas", EM_FALSE, &strategy);
1105 // Option 3: Request fullscreen for the canvas element with strategy
1106 // It works as expected but only inside the browser (client area)
1107 EmscriptenFullscreenStrategy strategy = {
1108 .scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT,
1109 .canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF,
1110 .filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
1111 .canvasResizedCallback = EmscriptenWindowResizedCallback,
1112 .canvasResizedCallbackUserData = NULL
1114 emscripten_enter_soft_fullscreen("#canvas", &strategy);
1117 emscripten_get_canvas_element_size("#canvas", &width, &height);
1118 TRACELOG(LOG_WARNING, "Emscripten: Enter fullscreen: Canvas size: %i x %i", width, height);
1122 //emscripten_exit_fullscreen();
1123 emscripten_exit_soft_fullscreen();
1126 emscripten_get_canvas_element_size("#canvas", &width, &height);
1127 TRACELOG(LOG_WARNING, "Emscripten: Exit fullscreen: Canvas size: %i x %i", width, height);
1131 CORE.Window.fullscreen = !CORE.Window.fullscreen; // Toggle fullscreen flag
1132 CORE.Window.flags ^= FLAG_FULLSCREEN_MODE;
1134 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
1135 TRACELOG(LOG_WARNING, "SYSTEM: Failed to toggle to windowed mode");
1139 // Set window state: maximized, if resizable (only PLATFORM_DESKTOP)
1140 void MaximizeWindow(void)
1142 #if defined(PLATFORM_DESKTOP)
1143 if (glfwGetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE) == GLFW_TRUE)
1145 glfwMaximizeWindow(CORE.Window.handle);
1146 CORE.Window.flags |= FLAG_WINDOW_MAXIMIZED;
1151 // Set window state: minimized (only PLATFORM_DESKTOP)
1152 void MinimizeWindow(void)
1154 #if defined(PLATFORM_DESKTOP)
1155 // NOTE: Following function launches callback that sets appropiate flag!
1156 glfwIconifyWindow(CORE.Window.handle);
1160 // Set window state: not minimized/maximized (only PLATFORM_DESKTOP)
1161 void RestoreWindow(void)
1163 #if defined(PLATFORM_DESKTOP)
1164 if (glfwGetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE) == GLFW_TRUE)
1166 // Restores the specified window if it was previously iconified (minimized) or maximized
1167 glfwRestoreWindow(CORE.Window.handle);
1168 CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED;
1169 CORE.Window.flags &= ~FLAG_WINDOW_MAXIMIZED;
1174 // Set window configuration state using flags
1175 void SetWindowState(unsigned int flags)
1177 #if defined(PLATFORM_DESKTOP)
1178 // Check previous state and requested state to apply required changes
1179 // NOTE: In most cases the functions already change the flags internally
1181 // State change: FLAG_VSYNC_HINT
1182 if (((CORE.Window.flags & FLAG_VSYNC_HINT) != (flags & FLAG_VSYNC_HINT)) && ((flags & FLAG_VSYNC_HINT) > 0))
1184 glfwSwapInterval(1);
1185 CORE.Window.flags |= FLAG_VSYNC_HINT;
1188 // State change: FLAG_FULLSCREEN_MODE
1189 if ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) != (flags & FLAG_FULLSCREEN_MODE))
1191 ToggleFullscreen(); // NOTE: Window state flag updated inside function
1194 // State change: FLAG_WINDOW_RESIZABLE
1195 if (((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) != (flags & FLAG_WINDOW_RESIZABLE)) && ((flags & FLAG_WINDOW_RESIZABLE) > 0))
1197 glfwSetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE, GLFW_TRUE);
1198 CORE.Window.flags |= FLAG_WINDOW_RESIZABLE;
1201 // State change: FLAG_WINDOW_UNDECORATED
1202 if (((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) != (flags & FLAG_WINDOW_UNDECORATED)) && (flags & FLAG_WINDOW_UNDECORATED))
1204 glfwSetWindowAttrib(CORE.Window.handle, GLFW_DECORATED, GLFW_FALSE);
1205 CORE.Window.flags |= FLAG_WINDOW_UNDECORATED;
1208 // State change: FLAG_WINDOW_HIDDEN
1209 if (((CORE.Window.flags & FLAG_WINDOW_HIDDEN) != (flags & FLAG_WINDOW_HIDDEN)) && ((flags & FLAG_WINDOW_HIDDEN) > 0))
1211 glfwHideWindow(CORE.Window.handle);
1212 CORE.Window.flags |= FLAG_WINDOW_HIDDEN;
1215 // State change: FLAG_WINDOW_MINIMIZED
1216 if (((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) != (flags & FLAG_WINDOW_MINIMIZED)) && ((flags & FLAG_WINDOW_MINIMIZED) > 0))
1219 MinimizeWindow(); // NOTE: Window state flag updated inside function
1222 // State change: FLAG_WINDOW_MAXIMIZED
1223 if (((CORE.Window.flags & FLAG_WINDOW_MAXIMIZED) != (flags & FLAG_WINDOW_MAXIMIZED)) && ((flags & FLAG_WINDOW_MAXIMIZED) > 0))
1226 MaximizeWindow(); // NOTE: Window state flag updated inside function
1229 // State change: FLAG_WINDOW_UNFOCUSED
1230 if (((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) != (flags & FLAG_WINDOW_UNFOCUSED)) && ((flags & FLAG_WINDOW_UNFOCUSED) > 0))
1232 glfwSetWindowAttrib(CORE.Window.handle, GLFW_FOCUS_ON_SHOW, GLFW_FALSE);
1233 CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED;
1236 // State change: FLAG_WINDOW_TOPMOST
1237 if (((CORE.Window.flags & FLAG_WINDOW_TOPMOST) != (flags & FLAG_WINDOW_TOPMOST)) && ((flags & FLAG_WINDOW_TOPMOST) > 0))
1239 glfwSetWindowAttrib(CORE.Window.handle, GLFW_FLOATING, GLFW_TRUE);
1240 CORE.Window.flags |= FLAG_WINDOW_TOPMOST;
1243 // State change: FLAG_WINDOW_ALWAYS_RUN
1244 if (((CORE.Window.flags & FLAG_WINDOW_ALWAYS_RUN) != (flags & FLAG_WINDOW_ALWAYS_RUN)) && ((flags & FLAG_WINDOW_ALWAYS_RUN) > 0))
1246 CORE.Window.flags |= FLAG_WINDOW_ALWAYS_RUN;
1249 // The following states can not be changed after window creation
1251 // State change: FLAG_WINDOW_TRANSPARENT
1252 if (((CORE.Window.flags & FLAG_WINDOW_TRANSPARENT) != (flags & FLAG_WINDOW_TRANSPARENT)) && ((flags & FLAG_WINDOW_TRANSPARENT) > 0))
1254 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only by configured before window initialization");
1257 // State change: FLAG_WINDOW_HIGHDPI
1258 if (((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) != (flags & FLAG_WINDOW_HIGHDPI)) && ((flags & FLAG_WINDOW_HIGHDPI) > 0))
1260 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
1263 // State change: FLAG_MSAA_4X_HINT
1264 if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) != (flags & FLAG_MSAA_4X_HINT)) && ((flags & FLAG_MSAA_4X_HINT) > 0))
1266 TRACELOG(LOG_WARNING, "WINDOW: MSAA can only by configured before window initialization");
1269 // State change: FLAG_INTERLACED_HINT
1270 if (((CORE.Window.flags & FLAG_INTERLACED_HINT) != (flags & FLAG_INTERLACED_HINT)) && ((flags & FLAG_INTERLACED_HINT) > 0))
1272 TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only by configured before window initialization");
1277 // Clear window configuration state flags
1278 void ClearWindowState(unsigned int flags)
1280 #if defined(PLATFORM_DESKTOP)
1281 // Check previous state and requested state to apply required changes
1282 // NOTE: In most cases the functions already change the flags internally
1284 // State change: FLAG_VSYNC_HINT
1285 if (((CORE.Window.flags & FLAG_VSYNC_HINT) > 0) && ((flags & FLAG_VSYNC_HINT) > 0))
1287 glfwSwapInterval(0);
1288 CORE.Window.flags &= ~FLAG_VSYNC_HINT;
1291 // State change: FLAG_FULLSCREEN_MODE
1292 if (((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0) && ((flags & FLAG_FULLSCREEN_MODE) > 0))
1294 ToggleFullscreen(); // NOTE: Window state flag updated inside function
1297 // State change: FLAG_WINDOW_RESIZABLE
1298 if (((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) > 0) && ((flags & FLAG_WINDOW_RESIZABLE) > 0))
1300 glfwSetWindowAttrib(CORE.Window.handle, GLFW_RESIZABLE, GLFW_FALSE);
1301 CORE.Window.flags &= ~FLAG_WINDOW_RESIZABLE;
1304 // State change: FLAG_WINDOW_UNDECORATED
1305 if (((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) > 0) && ((flags & FLAG_WINDOW_UNDECORATED) > 0))
1307 glfwSetWindowAttrib(CORE.Window.handle, GLFW_DECORATED, GLFW_TRUE);
1308 CORE.Window.flags &= ~FLAG_WINDOW_UNDECORATED;
1311 // State change: FLAG_WINDOW_HIDDEN
1312 if (((CORE.Window.flags & FLAG_WINDOW_HIDDEN) > 0) && ((flags & FLAG_WINDOW_HIDDEN) > 0))
1314 glfwShowWindow(CORE.Window.handle);
1315 CORE.Window.flags &= ~FLAG_WINDOW_HIDDEN;
1318 // State change: FLAG_WINDOW_MINIMIZED
1319 if (((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) > 0) && ((flags & FLAG_WINDOW_MINIMIZED) > 0))
1321 RestoreWindow(); // NOTE: Window state flag updated inside function
1324 // State change: FLAG_WINDOW_MAXIMIZED
1325 if (((CORE.Window.flags & FLAG_WINDOW_MAXIMIZED) > 0) && ((flags & FLAG_WINDOW_MAXIMIZED) > 0))
1327 RestoreWindow(); // NOTE: Window state flag updated inside function
1330 // State change: FLAG_WINDOW_UNFOCUSED
1331 if (((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) > 0) && ((flags & FLAG_WINDOW_UNFOCUSED) > 0))
1333 glfwSetWindowAttrib(CORE.Window.handle, GLFW_FOCUS_ON_SHOW, GLFW_TRUE);
1334 CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED;
1337 // State change: FLAG_WINDOW_TOPMOST
1338 if (((CORE.Window.flags & FLAG_WINDOW_TOPMOST) > 0) && ((flags & FLAG_WINDOW_TOPMOST) > 0))
1340 glfwSetWindowAttrib(CORE.Window.handle, GLFW_FLOATING, GLFW_FALSE);
1341 CORE.Window.flags &= ~FLAG_WINDOW_TOPMOST;
1344 // State change: FLAG_WINDOW_ALWAYS_RUN
1345 if (((CORE.Window.flags & FLAG_WINDOW_ALWAYS_RUN) > 0) && ((flags & FLAG_WINDOW_ALWAYS_RUN) > 0))
1347 CORE.Window.flags &= ~FLAG_WINDOW_ALWAYS_RUN;
1350 // The following states can not be changed after window creation
1352 // State change: FLAG_WINDOW_TRANSPARENT
1353 if (((CORE.Window.flags & FLAG_WINDOW_TRANSPARENT) > 0) && ((flags & FLAG_WINDOW_TRANSPARENT) > 0))
1355 TRACELOG(LOG_WARNING, "WINDOW: Framebuffer transparency can only by configured before window initialization");
1358 // State change: FLAG_WINDOW_HIGHDPI
1359 if (((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0) && ((flags & FLAG_WINDOW_HIGHDPI) > 0))
1361 TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
1364 // State change: FLAG_MSAA_4X_HINT
1365 if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) > 0) && ((flags & FLAG_MSAA_4X_HINT) > 0))
1367 TRACELOG(LOG_WARNING, "WINDOW: MSAA can only by configured before window initialization");
1370 // State change: FLAG_INTERLACED_HINT
1371 if (((CORE.Window.flags & FLAG_INTERLACED_HINT) > 0) && ((flags & FLAG_INTERLACED_HINT) > 0))
1373 TRACELOG(LOG_WARNING, "RPI: Interlaced mode can only by configured before window initialization");
1378 // Set icon for window (only PLATFORM_DESKTOP)
1379 // NOTE: Image must be in RGBA format, 8bit per channel
1380 void SetWindowIcon(Image image)
1382 #if defined(PLATFORM_DESKTOP)
1383 if (image.format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8)
1385 GLFWimage icon[1] = { 0 };
1387 icon[0].width = image.width;
1388 icon[0].height = image.height;
1389 icon[0].pixels = (unsigned char *)image.data;
1391 // NOTE 1: We only support one image icon
1392 // NOTE 2: The specified image data is copied before this function returns
1393 glfwSetWindowIcon(CORE.Window.handle, 1, icon);
1395 else TRACELOG(LOG_WARNING, "GLFW: Window icon image must be in R8G8B8A8 pixel format");
1399 // Set title for window (only PLATFORM_DESKTOP)
1400 void SetWindowTitle(const char *title)
1402 CORE.Window.title = title;
1403 #if defined(PLATFORM_DESKTOP)
1404 glfwSetWindowTitle(CORE.Window.handle, title);
1408 // Set window position on screen (windowed mode)
1409 void SetWindowPosition(int x, int y)
1411 #if defined(PLATFORM_DESKTOP)
1412 glfwSetWindowPos(CORE.Window.handle, x, y);
1416 // Set monitor for the current window (fullscreen mode)
1417 void SetWindowMonitor(int monitor)
1419 #if defined(PLATFORM_DESKTOP)
1420 int monitorCount = 0;
1421 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1423 if ((monitor >= 0) && (monitor < monitorCount))
1425 TRACELOG(LOG_INFO, "GLFW: Selected fullscreen monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
1427 const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]);
1428 glfwSetWindowMonitor(CORE.Window.handle, monitors[monitor], 0, 0, mode->width, mode->height, mode->refreshRate);
1430 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1434 // Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
1435 void SetWindowMinSize(int width, int height)
1437 #if defined(PLATFORM_DESKTOP)
1438 const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
1439 glfwSetWindowSizeLimits(CORE.Window.handle, width, height, mode->width, mode->height);
1443 // Set window dimensions
1444 // TODO: Issues on HighDPI scaling
1445 void SetWindowSize(int width, int height)
1447 #if defined(PLATFORM_DESKTOP)
1448 glfwSetWindowSize(CORE.Window.handle, width, height);
1450 #if defined(PLATFORM_WEB)
1451 //emscripten_set_canvas_size(width, height); // DEPRECATED!
1453 // TODO: Below functions should be used to replace previous one but
1454 // they do not seem to work properly
1455 //emscripten_set_canvas_element_size("canvas", width, height);
1456 //emscripten_set_element_css_size("canvas", width, height);
1460 // Get current screen width
1461 int GetScreenWidth(void)
1463 return CORE.Window.currentFbo.width;
1466 // Get current screen height
1467 int GetScreenHeight(void)
1469 return CORE.Window.currentFbo.height;
1472 // Get native window handle
1473 void *GetWindowHandle(void)
1475 #if defined(PLATFORM_DESKTOP) && defined(_WIN32)
1476 // NOTE: Returned handle is: void *HWND (windows.h)
1477 return glfwGetWin32Window(CORE.Window.handle);
1479 #if defined(__linux__)
1480 // NOTE: Returned handle is: unsigned long Window (X.h)
1481 // typedef unsigned long XID;
1482 // typedef XID Window;
1483 //unsigned long id = (unsigned long)glfwGetX11Window(window);
1484 return NULL; // TODO: Find a way to return value... cast to void *?
1486 #if defined(__APPLE__)
1487 // NOTE: Returned handle is: (objc_object *)
1488 return NULL; // TODO: return (void *)glfwGetCocoaWindow(window);
1494 // Get number of monitors
1495 int GetMonitorCount(void)
1497 #if defined(PLATFORM_DESKTOP)
1499 glfwGetMonitors(&monitorCount);
1500 return monitorCount;
1506 // Get number of monitors
1507 int GetCurrentMonitor(void)
1509 #if defined(PLATFORM_DESKTOP)
1511 GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
1512 GLFWmonitor* monitor = NULL;
1514 if (monitorCount == 1) // easy out
1517 if (IsWindowFullscreen())
1519 monitor = glfwGetWindowMonitor(CORE.Window.handle);
1520 for (int i = 0; i < monitorCount; i++)
1522 if (monitors[i] == monitor)
1532 glfwGetWindowPos(CORE.Window.handle, &x, &y);
1534 for (int i = 0; i < monitorCount; i++)
1542 monitor = monitors[i];
1543 glfwGetMonitorWorkarea(monitor, &mx, &my, &width, &height);
1544 if (x >= mx && x <= (mx + width) && y >= my && y <= (my + height))
1554 // Get selected monitor width
1555 Vector2 GetMonitorPosition(int monitor)
1557 #if defined(PLATFORM_DESKTOP)
1559 GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
1561 if ((monitor >= 0) && (monitor < monitorCount))
1564 glfwGetMonitorPos(monitors[monitor], &x, &y);
1566 return (Vector2){ (float)x, (float)y };
1568 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1570 return (Vector2){ 0, 0 };
1573 // Get selected monitor width (max available by monitor)
1574 int GetMonitorWidth(int monitor)
1576 #if defined(PLATFORM_DESKTOP)
1578 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1580 if ((monitor >= 0) && (monitor < monitorCount))
1583 const GLFWvidmode *modes = glfwGetVideoModes(monitors[monitor], &count);
1585 // We return the maximum resolution available, the last one in the modes array
1586 if (count > 0) return modes[count - 1].width;
1587 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
1589 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1594 // Get selected monitor width (max available by monitor)
1595 int GetMonitorHeight(int monitor)
1597 #if defined(PLATFORM_DESKTOP)
1599 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1601 if ((monitor >= 0) && (monitor < monitorCount))
1604 const GLFWvidmode *modes = glfwGetVideoModes(monitors[monitor], &count);
1606 // We return the maximum resolution available, the last one in the modes array
1607 if (count > 0) return modes[count - 1].height;
1608 else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
1610 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1615 // Get selected monitor physical width in millimetres
1616 int GetMonitorPhysicalWidth(int monitor)
1618 #if defined(PLATFORM_DESKTOP)
1620 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1622 if ((monitor >= 0) && (monitor < monitorCount))
1625 glfwGetMonitorPhysicalSize(monitors[monitor], &physicalWidth, NULL);
1626 return physicalWidth;
1628 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1633 // Get primary monitor physical height in millimetres
1634 int GetMonitorPhysicalHeight(int monitor)
1636 #if defined(PLATFORM_DESKTOP)
1638 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1640 if ((monitor >= 0) && (monitor < monitorCount))
1643 glfwGetMonitorPhysicalSize(monitors[monitor], NULL, &physicalHeight);
1644 return physicalHeight;
1646 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1651 int GetMonitorRefreshRate(int monitor)
1653 #if defined(PLATFORM_DESKTOP)
1655 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1657 if ((monitor >= 0) && (monitor < monitorCount))
1659 const GLFWvidmode *vidmode = glfwGetVideoMode(monitors[monitor]);
1660 return vidmode->refreshRate;
1662 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1664 #if defined(PLATFORM_DRM)
1665 if ((CORE.Window.connector) && (CORE.Window.modeIndex >= 0))
1667 return CORE.Window.connector->modes[CORE.Window.modeIndex].vrefresh;
1673 // Get window position XY on monitor
1674 Vector2 GetWindowPosition(void)
1678 #if defined(PLATFORM_DESKTOP)
1679 glfwGetWindowPos(CORE.Window.handle, &x, &y);
1681 return (Vector2){ (float)x, (float)y };
1684 // Get window scale DPI factor
1685 Vector2 GetWindowScaleDPI(void)
1687 Vector2 scale = { 1.0f, 1.0f };
1689 #if defined(PLATFORM_DESKTOP)
1692 Vector2 windowPos = GetWindowPosition();
1694 int monitorCount = 0;
1695 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1697 // Check window monitor
1698 for (int i = 0; i < monitorCount; i++)
1700 glfwGetMonitorContentScale(monitors[i], &xdpi, &ydpi);
1702 int xpos, ypos, width, height;
1703 glfwGetMonitorWorkarea(monitors[i], &xpos, &ypos, &width, &height);
1705 if ((windowPos.x >= xpos) && (windowPos.x < xpos + width) &&
1706 (windowPos.y >= ypos) && (windowPos.y < ypos + height))
1718 // Get the human-readable, UTF-8 encoded name of the primary monitor
1719 const char *GetMonitorName(int monitor)
1721 #if defined(PLATFORM_DESKTOP)
1723 GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
1725 if ((monitor >= 0) && (monitor < monitorCount))
1727 return glfwGetMonitorName(monitors[monitor]);
1729 else TRACELOG(LOG_WARNING, "GLFW: Failed to find selected monitor");
1734 // Get clipboard text content
1735 // NOTE: returned string is allocated and freed by GLFW
1736 const char *GetClipboardText(void)
1738 #if defined(PLATFORM_DESKTOP)
1739 return glfwGetClipboardString(CORE.Window.handle);
1745 // Set clipboard text content
1746 void SetClipboardText(const char *text)
1748 #if defined(PLATFORM_DESKTOP)
1749 glfwSetClipboardString(CORE.Window.handle, text);
1753 // Show mouse cursor
1754 void ShowCursor(void)
1756 #if defined(PLATFORM_DESKTOP)
1757 glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
1759 #if defined(PLATFORM_UWP)
1760 UWPGetMouseShowFunc()();
1762 CORE.Input.Mouse.cursorHidden = false;
1765 // Hides mouse cursor
1766 void HideCursor(void)
1768 #if defined(PLATFORM_DESKTOP)
1769 glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
1771 #if defined(PLATFORM_UWP)
1772 UWPGetMouseHideFunc()();
1774 CORE.Input.Mouse.cursorHidden = true;
1777 // Check if cursor is not visible
1778 bool IsCursorHidden(void)
1780 return CORE.Input.Mouse.cursorHidden;
1783 // Enables cursor (unlock cursor)
1784 void EnableCursor(void)
1786 #if defined(PLATFORM_DESKTOP)
1787 glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
1789 #if defined(PLATFORM_WEB)
1790 emscripten_exit_pointerlock();
1792 #if defined(PLATFORM_UWP)
1793 UWPGetMouseUnlockFunc()();
1795 CORE.Input.Mouse.cursorHidden = false;
1798 // Disables cursor (lock cursor)
1799 void DisableCursor(void)
1801 #if defined(PLATFORM_DESKTOP)
1802 glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
1804 #if defined(PLATFORM_WEB)
1805 emscripten_request_pointerlock("#canvas", 1);
1807 #if defined(PLATFORM_UWP)
1808 UWPGetMouseLockFunc()();
1810 CORE.Input.Mouse.cursorHidden = true;
1813 // Check if cursor is on the current screen.
1814 bool IsCursorOnScreen(void)
1816 return CORE.Input.Mouse.cursorOnScreen;
1819 // Set background color (framebuffer clear color)
1820 void ClearBackground(Color color)
1822 rlClearColor(color.r, color.g, color.b, color.a); // Set clear color
1823 rlClearScreenBuffers(); // Clear current framebuffers
1826 // Setup canvas (framebuffer) to start drawing
1827 void BeginDrawing(void)
1829 CORE.Time.current = GetTime(); // Number of elapsed seconds since InitTimer()
1830 CORE.Time.update = CORE.Time.current - CORE.Time.previous;
1831 CORE.Time.previous = CORE.Time.current;
1833 rlLoadIdentity(); // Reset current matrix (modelview)
1834 rlMultMatrixf(MatrixToFloat(CORE.Window.screenScale)); // Apply screen scaling
1836 //rlTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL 1.1
1837 // NOTE: Not required with OpenGL 3.3+
1840 // End canvas drawing and swap buffers (double buffering)
1841 void EndDrawing(void)
1843 #if (defined(PLATFORM_RPI) || defined(PLATFORM_DRM)) && defined(SUPPORT_MOUSE_CURSOR_NATIVE)
1844 // On native mode we have no system mouse cursor, so,
1845 // we draw a small rectangle for user reference
1846 if (!CORE.Input.Mouse.cursorHidden)
1848 DrawRectangle(CORE.Input.Mouse.position.x, CORE.Input.Mouse.position.y, 3, 3, MAROON);
1852 rlDrawRenderBatchActive(); // Update and draw internal render batch
1854 #if defined(SUPPORT_GIF_RECORDING)
1855 #define GIF_RECORD_FRAMERATE 10
1861 // NOTE: We record one gif frame every 10 game frames
1862 if ((gifFramesCounter%GIF_RECORD_FRAMERATE) == 0)
1864 // Get image data for the current frame (from backbuffer)
1865 // NOTE: This process is quite slow... :(
1866 unsigned char *screenData = rlReadScreenPixels(CORE.Window.screen.width, CORE.Window.screen.height);
1867 msf_gif_frame(&gifState, screenData, 10, 16, CORE.Window.screen.width*4);
1869 RL_FREE(screenData); // Free image data
1872 if (((gifFramesCounter/15)%2) == 1)
1874 DrawCircle(30, CORE.Window.screen.height - 20, 10, RED);
1875 DrawText("RECORDING", 50, CORE.Window.screen.height - 25, 10, MAROON);
1878 rlDrawRenderBatchActive(); // Update and draw internal render batch
1882 SwapBuffers(); // Copy back buffer to front buffer
1884 // Frame time control system
1885 CORE.Time.current = GetTime();
1886 CORE.Time.draw = CORE.Time.current - CORE.Time.previous;
1887 CORE.Time.previous = CORE.Time.current;
1889 CORE.Time.frame = CORE.Time.update + CORE.Time.draw;
1891 // Wait for some milliseconds...
1892 if (CORE.Time.frame < CORE.Time.target)
1894 Wait((float)(CORE.Time.target - CORE.Time.frame)*1000.0f);
1896 CORE.Time.current = GetTime();
1897 double waitTime = CORE.Time.current - CORE.Time.previous;
1898 CORE.Time.previous = CORE.Time.current;
1900 CORE.Time.frame += waitTime; // Total frame time: update + draw + wait
1903 PollInputEvents(); // Poll user events
1906 // Initialize 2D mode with custom camera (2D)
1907 void BeginMode2D(Camera2D camera)
1909 rlDrawRenderBatchActive(); // Update and draw internal render batch
1911 rlLoadIdentity(); // Reset current matrix (modelview)
1913 // Apply 2d camera transformation to modelview
1914 rlMultMatrixf(MatrixToFloat(GetCameraMatrix2D(camera)));
1916 // Apply screen scaling if required
1917 rlMultMatrixf(MatrixToFloat(CORE.Window.screenScale));
1920 // Ends 2D mode with custom camera
1921 void EndMode2D(void)
1923 rlDrawRenderBatchActive(); // Update and draw internal render batch
1925 rlLoadIdentity(); // Reset current matrix (modelview)
1926 rlMultMatrixf(MatrixToFloat(CORE.Window.screenScale)); // Apply screen scaling if required
1929 // Initializes 3D mode with custom camera (3D)
1930 void BeginMode3D(Camera3D camera)
1932 rlDrawRenderBatchActive(); // Update and draw internal render batch
1934 rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
1935 rlPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection
1936 rlLoadIdentity(); // Reset current matrix (projection)
1938 float aspect = (float)CORE.Window.currentFbo.width/(float)CORE.Window.currentFbo.height;
1940 // NOTE: zNear and zFar values are important when computing depth buffer values
1941 if (camera.projection == CAMERA_PERSPECTIVE)
1943 // Setup perspective projection
1944 double top = RL_CULL_DISTANCE_NEAR*tan(camera.fovy*0.5*DEG2RAD);
1945 double right = top*aspect;
1947 rlFrustum(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1949 else if (camera.projection == CAMERA_ORTHOGRAPHIC)
1951 // Setup orthographic projection
1952 double top = camera.fovy/2.0;
1953 double right = top*aspect;
1955 rlOrtho(-right, right, -top,top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1958 rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
1959 rlLoadIdentity(); // Reset current matrix (modelview)
1961 // Setup Camera view
1962 Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
1963 rlMultMatrixf(MatrixToFloat(matView)); // Multiply modelview matrix by view matrix (camera)
1965 rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
1968 // Ends 3D mode and returns to default 2D orthographic mode
1969 void EndMode3D(void)
1971 rlDrawRenderBatchActive(); // Update and draw internal render batch
1973 rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
1974 rlPopMatrix(); // Restore previous matrix (projection) from matrix stack
1976 rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
1977 rlLoadIdentity(); // Reset current matrix (modelview)
1979 rlMultMatrixf(MatrixToFloat(CORE.Window.screenScale)); // Apply screen scaling if required
1981 rlDisableDepthTest(); // Disable DEPTH_TEST for 2D
1984 // Initializes render texture for drawing
1985 void BeginTextureMode(RenderTexture2D target)
1987 rlDrawRenderBatchActive(); // Update and draw internal render batch
1989 rlEnableFramebuffer(target.id); // Enable render target
1991 // Set viewport to framebuffer size
1992 rlViewport(0, 0, target.texture.width, target.texture.height);
1994 rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
1995 rlLoadIdentity(); // Reset current matrix (projection)
1997 // Set orthographic projection to current framebuffer size
1998 // NOTE: Configured top-left corner as (0, 0)
1999 rlOrtho(0, target.texture.width, target.texture.height, 0, 0.0f, 1.0f);
2001 rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
2002 rlLoadIdentity(); // Reset current matrix (modelview)
2004 //rlScalef(0.0f, -1.0f, 0.0f); // Flip Y-drawing (?)
2006 // Setup current width/height for proper aspect ratio
2007 // calculation when using BeginMode3D()
2008 CORE.Window.currentFbo.width = target.texture.width;
2009 CORE.Window.currentFbo.height = target.texture.height;
2012 // Ends drawing to render texture
2013 void EndTextureMode(void)
2015 rlDrawRenderBatchActive(); // Update and draw internal render batch
2017 rlDisableFramebuffer(); // Disable render target (fbo)
2019 // Set viewport to default framebuffer size
2020 SetupViewport(CORE.Window.render.width, CORE.Window.render.height);
2022 // Reset current fbo to screen size
2023 CORE.Window.currentFbo.width = CORE.Window.screen.width;
2024 CORE.Window.currentFbo.height = CORE.Window.screen.height;
2027 // Begin custom shader mode
2028 void BeginShaderMode(Shader shader)
2030 rlSetShader(shader);
2033 // End custom shader mode (returns to default shader)
2034 void EndShaderMode(void)
2036 rlSetShader(rlGetShaderDefault());
2039 // Begin blending mode (alpha, additive, multiplied)
2040 // NOTE: Only 3 blending modes supported, default blend mode is alpha
2041 void BeginBlendMode(int mode)
2043 rlSetBlendMode(mode);
2046 // End blending mode (reset to default: alpha blending)
2047 void EndBlendMode(void)
2049 rlSetBlendMode(BLEND_ALPHA);
2052 // Begin scissor mode (define screen area for following drawing)
2053 // NOTE: Scissor rec refers to bottom-left corner, we change it to upper-left
2054 void BeginScissorMode(int x, int y, int width, int height)
2056 rlDrawRenderBatchActive(); // Update and draw internal render batch
2058 rlEnableScissorTest();
2059 rlScissor(x, CORE.Window.currentFbo.height - (y + height), width, height);
2063 void EndScissorMode(void)
2065 rlDrawRenderBatchActive(); // Update and draw internal render batch
2066 rlDisableScissorTest();
2069 // Begin VR drawing configuration
2070 void BeginVrStereoMode(VrStereoConfig config)
2072 rlEnableStereoRender();
2074 // Set stereo render matrices
2075 rlSetMatrixProjectionStereo(config.projection[0], config.projection[1]);
2076 rlSetMatrixViewOffsetStereo(config.viewOffset[0], config.viewOffset[1]);
2079 // End VR drawing process (and desktop mirror)
2080 void EndVrStereoMode(void)
2082 rlDisableStereoRender();
2085 // Load VR stereo config for VR simulator device parameters
2086 VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device)
2088 VrStereoConfig config = { 0 };
2090 #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
2091 // Compute aspect ratio
2092 float aspect = ((float)device.hResolution*0.5f)/(float)device.vResolution;
2094 // Compute lens parameters
2095 float lensShift = (device.hScreenSize*0.25f - device.lensSeparationDistance*0.5f)/device.hScreenSize;
2096 config.leftLensCenter[0] = 0.25f + lensShift;
2097 config.leftLensCenter[1] = 0.5f;
2098 config.rightLensCenter[0] = 0.75f - lensShift;
2099 config.rightLensCenter[1] = 0.5f;
2100 config.leftScreenCenter[0] = 0.25f;
2101 config.leftScreenCenter[1] = 0.5f;
2102 config.rightScreenCenter[0] = 0.75f;
2103 config.rightScreenCenter[1] = 0.5f;
2105 // Compute distortion scale parameters
2106 // NOTE: To get lens max radius, lensShift must be normalized to [-1..1]
2107 float lensRadius = fabsf(-1.0f - 4.0f*lensShift);
2108 float lensRadiusSq = lensRadius*lensRadius;
2109 float distortionScale = device.lensDistortionValues[0] +
2110 device.lensDistortionValues[1]*lensRadiusSq +
2111 device.lensDistortionValues[2]*lensRadiusSq*lensRadiusSq +
2112 device.lensDistortionValues[3]*lensRadiusSq*lensRadiusSq*lensRadiusSq;
2114 float normScreenWidth = 0.5f;
2115 float normScreenHeight = 1.0f;
2116 config.scaleIn[0] = 2.0f/normScreenWidth;
2117 config.scaleIn[1] = 2.0f/normScreenHeight/aspect;
2118 config.scale[0] = normScreenWidth*0.5f/distortionScale;
2119 config.scale[1] = normScreenHeight*0.5f*aspect/distortionScale;
2121 // Fovy is normally computed with: 2*atan2f(device.vScreenSize, 2*device.eyeToScreenDistance)
2122 // ...but with lens distortion it is increased (see Oculus SDK Documentation)
2123 //float fovy = 2.0f*atan2f(device.vScreenSize*0.5f*distortionScale, device.eyeToScreenDistance); // Really need distortionScale?
2124 float fovy = 2.0f*(float)atan2f(device.vScreenSize*0.5f, device.eyeToScreenDistance);
2126 // Compute camera projection matrices
2127 float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
2128 Matrix proj = MatrixPerspective(fovy, aspect, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
2130 config.projection[0] = MatrixMultiply(proj, MatrixTranslate(projOffset, 0.0f, 0.0f));
2131 config.projection[1] = MatrixMultiply(proj, MatrixTranslate(-projOffset, 0.0f, 0.0f));
2133 // Compute camera transformation matrices
2134 // NOTE: Camera movement might seem more natural if we model the head.
2135 // Our axis of rotation is the base of our head, so we might want to add
2136 // some y (base of head to eye level) and -z (center of head to eye protrusion) to the camera positions.
2137 config.viewOffset[0] = MatrixTranslate(-device.interpupillaryDistance*0.5f, 0.075f, 0.045f);
2138 config.viewOffset[1] = MatrixTranslate(device.interpupillaryDistance*0.5f, 0.075f, 0.045f);
2140 // Compute eyes Viewports
2142 config.eyeViewportRight[0] = 0;
2143 config.eyeViewportRight[1] = 0;
2144 config.eyeViewportRight[2] = device.hResolution/2;
2145 config.eyeViewportRight[3] = device.vResolution;
2147 config.eyeViewportLeft[0] = device.hResolution/2;
2148 config.eyeViewportLeft[1] = 0;
2149 config.eyeViewportLeft[2] = device.hResolution/2;
2150 config.eyeViewportLeft[3] = device.vResolution;
2153 TRACELOG(LOG_WARNING, "RLGL: VR Simulator not supported on OpenGL 1.1");
2159 // Unload VR stereo config properties
2160 void UnloadVrStereoConfig(VrStereoConfig config)
2165 // Load shader from files and bind default locations
2166 // NOTE: If shader string is NULL, using default vertex/fragment shaders
2167 Shader LoadShader(const char *vsFileName, const char *fsFileName)
2169 Shader shader = { 0 };
2170 shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int));
2172 // NOTE: All locations must be reseted to -1 (no location)
2173 for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1;
2175 char *vShaderStr = NULL;
2176 char *fShaderStr = NULL;
2178 if (vsFileName != NULL) vShaderStr = LoadFileText(vsFileName);
2179 if (fsFileName != NULL) fShaderStr = LoadFileText(fsFileName);
2181 shader.id = rlLoadShaderCode(vShaderStr, fShaderStr);
2183 if (vShaderStr != NULL) RL_FREE(vShaderStr);
2184 if (fShaderStr != NULL) RL_FREE(fShaderStr);
2186 // After shader loading, we TRY to set default location names
2189 // Default shader attrib locations have been fixed before linking:
2190 // vertex position location = 0
2191 // vertex texcoord location = 1
2192 // vertex normal location = 2
2193 // vertex color location = 3
2194 // vertex tangent location = 4
2195 // vertex texcoord2 location = 5
2197 // NOTE: If any location is not found, loc point becomes -1
2199 // Get handles to GLSL input attibute locations
2200 shader.locs[SHADER_LOC_VERTEX_POSITION] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_POSITION);
2201 shader.locs[SHADER_LOC_VERTEX_TEXCOORD01] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
2202 shader.locs[SHADER_LOC_VERTEX_TEXCOORD02] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2);
2203 shader.locs[SHADER_LOC_VERTEX_NORMAL] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
2204 shader.locs[SHADER_LOC_VERTEX_TANGENT] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TANGENT);
2205 shader.locs[SHADER_LOC_VERTEX_COLOR] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_COLOR);
2207 // Get handles to GLSL uniform locations (vertex shader)
2208 shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, "mvp");
2209 shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, "view");
2210 shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, "projection");
2211 shader.locs[SHADER_LOC_MATRIX_NORMAL] = rlGetLocationUniform(shader.id, "matNormal");
2213 // Get handles to GLSL uniform locations (fragment shader)
2214 shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, "colDiffuse");
2215 shader.locs[SHADER_LOC_MAP_DIFFUSE] = rlGetLocationUniform(shader.id, "texture0");
2216 shader.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, "texture1");
2217 shader.locs[SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, "texture2");
2223 // Load shader from code strings and bind default locations
2224 RLAPI Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode)
2226 Shader shader = { 0 };
2227 shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int));
2229 shader.id = rlLoadShaderCode(vsCode, fsCode);
2231 // After shader loading, we TRY to set default location names
2234 // Default shader attrib locations have been fixed before linking:
2235 // vertex position location = 0
2236 // vertex texcoord location = 1
2237 // vertex normal location = 2
2238 // vertex color location = 3
2239 // vertex tangent location = 4
2240 // vertex texcoord2 location = 5
2242 // NOTE: If any location is not found, loc point becomes -1
2244 // Get handles to GLSL input attibute locations
2245 shader.locs[SHADER_LOC_VERTEX_POSITION] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_POSITION);
2246 shader.locs[SHADER_LOC_VERTEX_TEXCOORD01] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD);
2247 shader.locs[SHADER_LOC_VERTEX_TEXCOORD02] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2);
2248 shader.locs[SHADER_LOC_VERTEX_NORMAL] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_NORMAL);
2249 shader.locs[SHADER_LOC_VERTEX_TANGENT] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_TANGENT);
2250 shader.locs[SHADER_LOC_VERTEX_COLOR] = rlGetLocationAttrib(shader.id, DEFAULT_SHADER_ATTRIB_NAME_COLOR);
2252 // Get handles to GLSL uniform locations (vertex shader)
2253 shader.locs[SHADER_LOC_MATRIX_MVP] = rlGetLocationUniform(shader.id, "mvp");
2254 shader.locs[SHADER_LOC_MATRIX_PROJECTION] = rlGetLocationUniform(shader.id, "projection");
2255 shader.locs[SHADER_LOC_MATRIX_VIEW] = rlGetLocationUniform(shader.id, "view");
2257 // Get handles to GLSL uniform locations (fragment shader)
2258 shader.locs[SHADER_LOC_COLOR_DIFFUSE] = rlGetLocationUniform(shader.id, "colDiffuse");
2259 shader.locs[SHADER_LOC_MAP_DIFFUSE] = rlGetLocationUniform(shader.id, "texture0");
2260 shader.locs[SHADER_LOC_MAP_SPECULAR] = rlGetLocationUniform(shader.id, "texture1");
2261 shader.locs[SHADER_LOC_MAP_NORMAL] = rlGetLocationUniform(shader.id, "texture2");
2267 // Unload shader from GPU memory (VRAM)
2268 void UnloadShader(Shader shader)
2270 if (shader.id != rlGetShaderDefault().id)
2272 rlUnloadShaderProgram(shader.id);
2273 RL_FREE(shader.locs);
2277 // Get shader uniform location
2278 int GetShaderLocation(Shader shader, const char *uniformName)
2280 return rlGetLocationUniform(shader.id, uniformName);
2283 // Get shader attribute location
2284 int GetShaderLocationAttrib(Shader shader, const char *attribName)
2286 return rlGetLocationAttrib(shader.id, attribName);
2289 // Set shader uniform value
2290 void SetShaderValue(Shader shader, int locIndex, const void *value, int uniformType)
2292 SetShaderValueV(shader, locIndex, value, uniformType, 1);
2295 // Set shader uniform value vector
2296 void SetShaderValueV(Shader shader, int locIndex, const void *value, int uniformType, int count)
2298 rlEnableShader(shader.id);
2299 rlSetUniform(locIndex, value, uniformType, count);
2300 //rlDisableShader(); // Avoid reseting current shader program, in case other uniforms are set
2303 // Set shader uniform value (matrix 4x4)
2304 void SetShaderValueMatrix(Shader shader, int locIndex, Matrix mat)
2306 rlEnableShader(shader.id);
2307 rlSetUniformMatrix(locIndex, mat);
2308 //rlDisableShader();
2311 // Set shader uniform value for texture
2312 void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture)
2314 rlEnableShader(shader.id);
2315 rlSetUniformSampler(locIndex, texture.id);
2316 //rlDisableShader();
2319 // Returns a ray trace from mouse position
2320 Ray GetMouseRay(Vector2 mouse, Camera camera)
2324 // Calculate normalized device coordinates
2325 // NOTE: y value is negative
2326 float x = (2.0f*mouse.x)/(float)GetScreenWidth() - 1.0f;
2327 float y = 1.0f - (2.0f*mouse.y)/(float)GetScreenHeight();
2330 // Store values in a vector
2331 Vector3 deviceCoords = { x, y, z };
2333 // Calculate view matrix from camera look at
2334 Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
2336 Matrix matProj = MatrixIdentity();
2338 if (camera.projection == CAMERA_PERSPECTIVE)
2340 // Calculate projection matrix from perspective
2341 matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
2343 else if (camera.projection == CAMERA_ORTHOGRAPHIC)
2345 float aspect = (float)CORE.Window.screen.width/(float)CORE.Window.screen.height;
2346 double top = camera.fovy/2.0;
2347 double right = top*aspect;
2349 // Calculate projection matrix from orthographic
2350 matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0);
2353 // Unproject far/near points
2354 Vector3 nearPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView);
2355 Vector3 farPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView);
2357 // Unproject the mouse cursor in the near plane.
2358 // We need this as the source position because orthographic projects, compared to perspect doesn't have a
2359 // convergence point, meaning that the "eye" of the camera is more like a plane than a point.
2360 Vector3 cameraPlanePointerPos = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, -1.0f }, matProj, matView);
2362 // Calculate normalized direction vector
2363 Vector3 direction = Vector3Normalize(Vector3Subtract(farPoint, nearPoint));
2365 if (camera.projection == CAMERA_PERSPECTIVE) ray.position = camera.position;
2366 else if (camera.projection == CAMERA_ORTHOGRAPHIC) ray.position = cameraPlanePointerPos;
2368 // Apply calculated vectors to ray
2369 ray.direction = direction;
2374 // Get transform matrix for camera
2375 Matrix GetCameraMatrix(Camera camera)
2377 return MatrixLookAt(camera.position, camera.target, camera.up);
2380 // Returns camera 2d transform matrix
2381 Matrix GetCameraMatrix2D(Camera2D camera)
2383 Matrix matTransform = { 0 };
2384 // The camera in world-space is set by
2385 // 1. Move it to target
2386 // 2. Rotate by -rotation and scale by (1/zoom)
2387 // When setting higher scale, it's more intuitive for the world to become bigger (= camera become smaller),
2388 // not for the camera getting bigger, hence the invert. Same deal with rotation.
2389 // 3. Move it by (-offset);
2390 // Offset defines target transform relative to screen, but since we're effectively "moving" screen (camera)
2391 // we need to do it into opposite direction (inverse transform)
2393 // Having camera transform in world-space, inverse of it gives the modelview transform.
2394 // Since (A*B*C)' = C'*B'*A', the modelview is
2395 // 1. Move to offset
2396 // 2. Rotate and Scale
2397 // 3. Move by -target
2398 Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f);
2399 Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD);
2400 Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f);
2401 Matrix matTranslation = MatrixTranslate(camera.offset.x, camera.offset.y, 0.0f);
2403 matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation);
2405 return matTransform;
2408 // Returns the screen space position from a 3d world space position
2409 Vector2 GetWorldToScreen(Vector3 position, Camera camera)
2411 Vector2 screenPosition = GetWorldToScreenEx(position, camera, GetScreenWidth(), GetScreenHeight());
2413 return screenPosition;
2416 // Returns size position for a 3d world space position (useful for texture drawing)
2417 Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int height)
2419 // Calculate projection matrix (from perspective instead of frustum
2420 Matrix matProj = MatrixIdentity();
2422 if (camera.projection == CAMERA_PERSPECTIVE)
2424 // Calculate projection matrix from perspective
2425 matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
2427 else if (camera.projection == CAMERA_ORTHOGRAPHIC)
2429 float aspect = (float)CORE.Window.screen.width/(float)CORE.Window.screen.height;
2430 double top = camera.fovy/2.0;
2431 double right = top*aspect;
2433 // Calculate projection matrix from orthographic
2434 matProj = MatrixOrtho(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
2437 // Calculate view matrix from camera look at (and transpose it)
2438 Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
2440 // Convert world position vector to quaternion
2441 Quaternion worldPos = { position.x, position.y, position.z, 1.0f };
2443 // Transform world position to view
2444 worldPos = QuaternionTransform(worldPos, matView);
2446 // Transform result to projection (clip space position)
2447 worldPos = QuaternionTransform(worldPos, matProj);
2449 // Calculate normalized device coordinates (inverted y)
2450 Vector3 ndcPos = { worldPos.x/worldPos.w, -worldPos.y/worldPos.w, worldPos.z/worldPos.w };
2452 // Calculate 2d screen position vector
2453 Vector2 screenPosition = { (ndcPos.x + 1.0f)/2.0f*(float)width, (ndcPos.y + 1.0f)/2.0f*(float)height };
2455 return screenPosition;
2458 // Returns the screen space position for a 2d camera world space position
2459 Vector2 GetWorldToScreen2D(Vector2 position, Camera2D camera)
2461 Matrix matCamera = GetCameraMatrix2D(camera);
2462 Vector3 transform = Vector3Transform((Vector3){ position.x, position.y, 0 }, matCamera);
2464 return (Vector2){ transform.x, transform.y };
2467 // Returns the world space position for a 2d camera screen space position
2468 Vector2 GetScreenToWorld2D(Vector2 position, Camera2D camera)
2470 Matrix invMatCamera = MatrixInvert(GetCameraMatrix2D(camera));
2471 Vector3 transform = Vector3Transform((Vector3){ position.x, position.y, 0 }, invMatCamera);
2473 return (Vector2){ transform.x, transform.y };
2476 // Set target FPS (maximum)
2477 void SetTargetFPS(int fps)
2479 if (fps < 1) CORE.Time.target = 0.0;
2480 else CORE.Time.target = 1.0/(double)fps;
2482 TRACELOG(LOG_INFO, "TIMER: Target time per frame: %02.03f milliseconds", (float)CORE.Time.target*1000);
2485 // Returns current FPS
2486 // NOTE: We calculate an average framerate
2489 #define FPS_CAPTURE_FRAMES_COUNT 30 // 30 captures
2490 #define FPS_AVERAGE_TIME_SECONDS 0.5f // 500 millisecondes
2491 #define FPS_STEP (FPS_AVERAGE_TIME_SECONDS/FPS_CAPTURE_FRAMES_COUNT)
2493 static int index = 0;
2494 static float history[FPS_CAPTURE_FRAMES_COUNT] = { 0 };
2495 static float average = 0, last = 0;
2496 float fpsFrame = GetFrameTime();
2498 if (fpsFrame == 0) return 0;
2500 if ((GetTime() - last) > FPS_STEP)
2502 last = (float)GetTime();
2503 index = (index + 1)%FPS_CAPTURE_FRAMES_COUNT;
2504 average -= history[index];
2505 history[index] = fpsFrame/FPS_CAPTURE_FRAMES_COUNT;
2506 average += history[index];
2509 return (int)roundf(1.0f/average);
2512 // Returns time in seconds for last frame drawn (delta time)
2513 float GetFrameTime(void)
2515 return (float)CORE.Time.frame;
2518 // Get elapsed time measure in seconds since InitTimer()
2519 // NOTE: On PLATFORM_DESKTOP InitTimer() is called on InitWindow()
2520 // NOTE: On PLATFORM_DESKTOP, timer is initialized on glfwInit()
2521 double GetTime(void)
2523 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
2524 return glfwGetTime(); // Elapsed time since glfwInit()
2527 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
2529 clock_gettime(CLOCK_MONOTONIC, &ts);
2530 unsigned long long int time = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec;
2532 return (double)(time - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
2535 #if defined(PLATFORM_UWP)
2536 return UWPGetQueryTimeFunc()();
2540 // Setup window configuration flags (view FLAGS)
2541 // NOTE: This function is expected to be called before window creation,
2542 // because it setups some flags for the window creation process.
2543 // To configure window states after creation, just use SetWindowState()
2544 void SetConfigFlags(unsigned int flags)
2546 // Selected flags are set but not evaluated at this point,
2547 // flag evaluation happens at InitWindow() or SetWindowState()
2548 CORE.Window.flags |= flags;
2551 // NOTE TRACELOG() function is located in [utils.h]
2553 // Takes a screenshot of current screen (saved a .png)
2554 // NOTE: This function could work in any platform but some platforms: PLATFORM_ANDROID and PLATFORM_WEB
2555 // have their own internal file-systems, to dowload image to user file-system some additional mechanism is required
2556 void TakeScreenshot(const char *fileName)
2558 unsigned char *imgData = rlReadScreenPixels(CORE.Window.render.width, CORE.Window.render.height);
2559 Image image = { imgData, CORE.Window.render.width, CORE.Window.render.height, 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 };
2561 char path[512] = { 0 };
2562 #if defined(PLATFORM_ANDROID)
2563 strcpy(path, CORE.Android.internalDataPath);
2565 strcat(path, fileName);
2566 #elif defined(PLATFORM_UWP)
2567 strcpy(path, CORE.UWP.internalDataPath);
2569 strcat(path, fileName);
2571 strcpy(path, fileName);
2574 ExportImage(image, path);
2577 #if defined(PLATFORM_WEB)
2578 // Download file from MEMFS (emscripten memory filesystem)
2579 // saveFileFromMEMFSToDisk() function is defined in raylib/src/shell.html
2580 emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", GetFileName(path), GetFileName(path)));
2583 // TODO: Verification required for log
2584 TRACELOG(LOG_INFO, "SYSTEM: [%s] Screenshot taken successfully", path);
2587 // Returns a random value between min and max (both included)
2588 int GetRandomValue(int min, int max)
2597 return (rand()%(abs(max - min) + 1) + min);
2600 // Check if the file exists
2601 bool FileExists(const char *fileName)
2603 bool result = false;
2606 if (_access(fileName, 0) != -1) result = true;
2608 if (access(fileName, F_OK) != -1) result = true;
2614 // Check file extension
2615 // NOTE: Extensions checking is not case-sensitive
2616 bool IsFileExtension(const char *fileName, const char *ext)
2618 bool result = false;
2619 const char *fileExt = GetFileExtension(fileName);
2621 if (fileExt != NULL)
2623 #if defined(SUPPORT_TEXT_MANIPULATION)
2625 const char **checkExts = TextSplit(ext, ';', &extCount);
2627 char fileExtLower[16] = { 0 };
2628 strcpy(fileExtLower, TextToLower(fileExt));
2630 for (int i = 0; i < extCount; i++)
2632 if (TextIsEqual(fileExtLower, TextToLower(checkExts[i])))
2639 if (strcmp(fileExt, ext) == 0) result = true;
2646 // Check if a directory path exists
2647 bool DirectoryExists(const char *dirPath)
2649 bool result = false;
2650 DIR *dir = opendir(dirPath);
2661 // Get pointer to extension for a filename string (includes the dot: .png)
2662 const char *GetFileExtension(const char *fileName)
2664 const char *dot = strrchr(fileName, '.');
2666 if (!dot || dot == fileName) return NULL;
2671 // String pointer reverse break: returns right-most occurrence of charset in s
2672 static const char *strprbrk(const char *s, const char *charset)
2674 const char *latestMatch = NULL;
2675 for (; s = strpbrk(s, charset), s != NULL; latestMatch = s++) { }
2679 // Get pointer to filename for a path string
2680 const char *GetFileName(const char *filePath)
2682 const char *fileName = NULL;
2683 if (filePath != NULL) fileName = strprbrk(filePath, "\\/");
2685 if (!fileName) return filePath;
2687 return fileName + 1;
2690 // Get filename string without extension (uses static string)
2691 const char *GetFileNameWithoutExt(const char *filePath)
2693 #define MAX_FILENAMEWITHOUTEXT_LENGTH 128
2695 static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH];
2696 memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH);
2698 if (filePath != NULL) strcpy(fileName, GetFileName(filePath)); // Get filename with extension
2700 int len = (int)strlen(fileName);
2702 for (int i = 0; (i < len) && (i < MAX_FILENAMEWITHOUTEXT_LENGTH); i++)
2704 if (fileName[i] == '.')
2706 // NOTE: We break on first '.' found
2715 // Get directory for a given filePath
2716 const char *GetDirectoryPath(const char *filePath)
2719 // NOTE: Directory separator is different in Windows and other platforms,
2720 // fortunately, Windows also support the '/' separator, that's the one should be used
2722 char separator = '\\';
2724 char separator = '/';
2727 const char *lastSlash = NULL;
2728 static char dirPath[MAX_FILEPATH_LENGTH];
2729 memset(dirPath, 0, MAX_FILEPATH_LENGTH);
2731 // In case provided path does not contain a root drive letter (C:\, D:\) nor leading path separator (\, /),
2732 // we add the current directory path to dirPath
2733 if (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/')
2735 // For security, we set starting path to current directory,
2736 // obtained path will be concated to this
2741 lastSlash = strprbrk(filePath, "\\/");
2744 if (lastSlash == filePath)
2746 // The last and only slash is the leading one: path is in a root directory
2747 dirPath[0] = filePath[0];
2752 // NOTE: Be careful, strncpy() is not safe, it does not care about '\0'
2753 memcpy(dirPath + (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/' ? 2 : 0), filePath, strlen(filePath) - (strlen(lastSlash) - 1));
2754 dirPath[strlen(filePath) - strlen(lastSlash) + (filePath[1] != ':' && filePath[0] != '\\' && filePath[0] != '/' ? 2 : 0)] = '\0'; // Add '\0' manually
2761 // Get previous directory path for a given path
2762 const char *GetPrevDirectoryPath(const char *dirPath)
2764 static char prevDirPath[MAX_FILEPATH_LENGTH];
2765 memset(prevDirPath, 0, MAX_FILEPATH_LENGTH);
2766 int pathLen = (int)strlen(dirPath);
2768 if (pathLen <= 3) strcpy(prevDirPath, dirPath);
2770 for (int i = (pathLen - 1); (i >= 0) && (pathLen > 3); i--)
2772 if ((dirPath[i] == '\\') || (dirPath[i] == '/'))
2774 // Check for root: "C:\" or "/"
2775 if (((i == 2) && (dirPath[1] ==':')) || (i == 0)) i++;
2777 strncpy(prevDirPath, dirPath, i);
2785 // Get current working directory
2786 const char *GetWorkingDirectory(void)
2788 static char currentDir[MAX_FILEPATH_LENGTH];
2789 memset(currentDir, 0, MAX_FILEPATH_LENGTH);
2791 char *ptr = GETCWD(currentDir, MAX_FILEPATH_LENGTH - 1);
2796 // Get filenames in a directory path (max 512 files)
2797 // NOTE: Files count is returned by parameters pointer
2798 char **GetDirectoryFiles(const char *dirPath, int *fileCount)
2800 #define MAX_DIRECTORY_FILES 512
2802 ClearDirectoryFiles();
2804 // Memory allocation for MAX_DIRECTORY_FILES
2805 dirFilesPath = (char **)RL_MALLOC(sizeof(char *)*MAX_DIRECTORY_FILES);
2806 for (int i = 0; i < MAX_DIRECTORY_FILES; i++) dirFilesPath[i] = (char *)RL_MALLOC(sizeof(char)*MAX_FILEPATH_LENGTH);
2809 struct dirent *entity;
2810 DIR *dir = opendir(dirPath);
2812 if (dir != NULL) // It's a directory
2814 // TODO: Reading could be done in two passes,
2815 // first one to count files and second one to read names
2816 // That way we can allocate required memory, instead of a limited pool
2818 while ((entity = readdir(dir)) != NULL)
2820 strcpy(dirFilesPath[counter], entity->d_name);
2826 else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file...
2828 dirFilesCount = counter;
2829 *fileCount = dirFilesCount;
2831 return dirFilesPath;
2834 // Clear directory files paths buffers
2835 void ClearDirectoryFiles(void)
2837 if (dirFilesCount > 0)
2839 for (int i = 0; i < MAX_DIRECTORY_FILES; i++) RL_FREE(dirFilesPath[i]);
2841 RL_FREE(dirFilesPath);
2847 // Change working directory, returns true on success
2848 bool ChangeDirectory(const char *dir)
2850 bool result = CHDIR(dir);
2852 if (result != 0) TRACELOG(LOG_WARNING, "SYSTEM: Failed to change to directory: %s", dir);
2854 return (result == 0);
2857 // Check if a file has been dropped into window
2858 bool IsFileDropped(void)
2860 if (CORE.Window.dropFilesCount > 0) return true;
2864 // Get dropped files names
2865 char **GetDroppedFiles(int *count)
2867 *count = CORE.Window.dropFilesCount;
2868 return CORE.Window.dropFilesPath;
2871 // Clear dropped files paths buffer
2872 void ClearDroppedFiles(void)
2874 if (CORE.Window.dropFilesCount > 0)
2876 for (int i = 0; i < CORE.Window.dropFilesCount; i++) RL_FREE(CORE.Window.dropFilesPath[i]);
2878 RL_FREE(CORE.Window.dropFilesPath);
2880 CORE.Window.dropFilesCount = 0;
2884 // Get file modification time (last write time)
2885 long GetFileModTime(const char *fileName)
2887 struct stat result = { 0 };
2889 if (stat(fileName, &result) == 0)
2891 time_t mod = result.st_mtime;
2899 // Compress data (DEFLATE algorythm)
2900 unsigned char *CompressData(unsigned char *data, int dataLength, int *compDataLength)
2902 #define COMPRESSION_QUALITY_DEFLATE 8
2904 unsigned char *compData = NULL;
2906 #if defined(SUPPORT_COMPRESSION_API)
2907 // Compress data and generate a valid DEFLATE stream
2908 struct sdefl sdefl = { 0 };
2909 int bounds = sdefl_bound(dataLength);
2910 compData = (unsigned char *)RL_CALLOC(bounds, 1);
2911 *compDataLength = sdeflate(&sdefl, compData, data, dataLength, COMPRESSION_QUALITY_DEFLATE); // Compression level 8, same as stbwi
2913 TraceLog(LOG_INFO, "SYSTEM: Compress data: Original size: %i -> Comp. size: %i", dataLength, compDataLength);
2919 // Decompress data (DEFLATE algorythm)
2920 unsigned char *DecompressData(unsigned char *compData, int compDataLength, int *dataLength)
2922 unsigned char *data = NULL;
2924 #if defined(SUPPORT_COMPRESSION_API)
2925 // Decompress data from a valid DEFLATE stream
2926 data = RL_CALLOC(MAX_DECOMPRESSION_SIZE*1024*1024, 1);
2927 int length = sinflate(data, compData, compDataLength);
2928 unsigned char *temp = RL_REALLOC(data, length);
2930 if (temp != NULL) data = temp;
2931 else TRACELOG(LOG_WARNING, "SYSTEM: Failed to re-allocate required decompression memory");
2933 *dataLength = length;
2935 TraceLog(LOG_INFO, "SYSTEM: Decompress data: Comp. size: %i -> Original size: %i", compDataLength, dataLength);
2941 // Save integer value to storage file (to defined position)
2942 // NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
2943 bool SaveStorageValue(unsigned int position, int value)
2945 bool success = false;
2947 #if defined(SUPPORT_DATA_STORAGE)
2948 char path[512] = { 0 };
2949 #if defined(PLATFORM_ANDROID)
2950 strcpy(path, CORE.Android.internalDataPath);
2952 strcat(path, STORAGE_DATA_FILE);
2953 #elif defined(PLATFORM_UWP)
2954 strcpy(path, CORE.UWP.internalDataPath);
2956 strcat(path, STORAGE_DATA_FILE);
2958 strcpy(path, STORAGE_DATA_FILE);
2961 unsigned int dataSize = 0;
2962 unsigned int newDataSize = 0;
2963 unsigned char *fileData = LoadFileData(path, &dataSize);
2964 unsigned char *newFileData = NULL;
2966 if (fileData != NULL)
2968 if (dataSize <= (position*sizeof(int)))
2970 // Increase data size up to position and store value
2971 newDataSize = (position + 1)*sizeof(int);
2972 newFileData = (unsigned char *)RL_REALLOC(fileData, newDataSize);
2974 if (newFileData != NULL)
2976 // RL_REALLOC succeded
2977 int *dataPtr = (int *)newFileData;
2978 dataPtr[position] = value;
2982 // RL_REALLOC failed
2983 TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to realloc data (%u), position in bytes (%u) bigger than actual file size", path, dataSize, position*sizeof(int));
2985 // We store the old size of the file
2986 newFileData = fileData;
2987 newDataSize = dataSize;
2992 // Store the old size of the file
2993 newFileData = fileData;
2994 newDataSize = dataSize;
2996 // Replace value on selected position
2997 int *dataPtr = (int *)newFileData;
2998 dataPtr[position] = value;
3001 success = SaveFileData(path, newFileData, newDataSize);
3002 RL_FREE(newFileData);
3006 TRACELOG(LOG_INFO, "FILEIO: [%s] File not found, creating it", path);
3008 dataSize = (position + 1)*sizeof(int);
3009 fileData = (unsigned char *)RL_MALLOC(dataSize);
3010 int *dataPtr = (int *)fileData;
3011 dataPtr[position] = value;
3013 success = SaveFileData(path, fileData, dataSize);
3014 UnloadFileData(fileData);
3021 // Load integer value from storage file (from defined position)
3022 // NOTE: If requested position could not be found, value 0 is returned
3023 int LoadStorageValue(unsigned int position)
3027 #if defined(SUPPORT_DATA_STORAGE)
3028 char path[512] = { 0 };
3029 #if defined(PLATFORM_ANDROID)
3030 strcpy(path, CORE.Android.internalDataPath);
3032 strcat(path, STORAGE_DATA_FILE);
3033 #elif defined(PLATFORM_UWP)
3034 strcpy(path, CORE.UWP.internalDataPath);
3036 strcat(path, STORAGE_DATA_FILE);
3038 strcpy(path, STORAGE_DATA_FILE);
3041 unsigned int dataSize = 0;
3042 unsigned char *fileData = LoadFileData(path, &dataSize);
3044 if (fileData != NULL)
3046 if (dataSize < (position*4)) TRACELOG(LOG_WARNING, "SYSTEM: Failed to find storage position");
3049 int *dataPtr = (int *)fileData;
3050 value = dataPtr[position];
3053 UnloadFileData(fileData);
3059 // Open URL with default system browser (if available)
3060 // NOTE: This function is only safe to use if you control the URL given.
3061 // A user could craft a malicious string performing another action.
3062 // Only call this function yourself not with user input or make sure to check the string yourself.
3063 // Ref: https://github.com/raysan5/raylib/issues/686
3064 void OpenURL(const char *url)
3066 // Small security check trying to avoid (partially) malicious code...
3067 // sorry for the inconvenience when you hit this point...
3068 if (strchr(url, '\'') != NULL)
3070 TRACELOG(LOG_WARNING, "SYSTEM: Provided URL is not valid");
3074 #if defined(PLATFORM_DESKTOP)
3075 char *cmd = (char *)RL_CALLOC(strlen(url) + 10, sizeof(char));
3077 sprintf(cmd, "explorer %s", url);
3079 #if defined(__linux__) || defined(__FreeBSD__)
3080 sprintf(cmd, "xdg-open '%s'", url); // Alternatives: firefox, x-www-browser
3082 #if defined(__APPLE__)
3083 sprintf(cmd, "open '%s'", url);
3088 #if defined(PLATFORM_WEB)
3089 emscripten_run_script(TextFormat("window.open('%s', '_blank')", url));
3094 //----------------------------------------------------------------------------------
3095 // Module Functions Definition - Input (Keyboard, Mouse, Gamepad) Functions
3096 //----------------------------------------------------------------------------------
3097 // Detect if a key has been pressed once
3098 bool IsKeyPressed(int key)
3100 bool pressed = false;
3102 if ((CORE.Input.Keyboard.previousKeyState[key] == 0) && (CORE.Input.Keyboard.currentKeyState[key] == 1)) pressed = true;
3103 else pressed = false;
3108 // Detect if a key is being pressed (key held down)
3109 bool IsKeyDown(int key)
3111 if (CORE.Input.Keyboard.currentKeyState[key] == 1) return true;
3115 // Detect if a key has been released once
3116 bool IsKeyReleased(int key)
3118 bool released = false;
3120 if ((CORE.Input.Keyboard.previousKeyState[key] == 1) && (CORE.Input.Keyboard.currentKeyState[key] == 0)) released = true;
3121 else released = false;
3126 // Detect if a key is NOT being pressed (key not held down)
3127 bool IsKeyUp(int key)
3129 if (CORE.Input.Keyboard.currentKeyState[key] == 0) return true;
3133 // Get the last key pressed
3134 int GetKeyPressed(void)
3138 if (CORE.Input.Keyboard.keyPressedQueueCount > 0)
3140 // Get character from the queue head
3141 value = CORE.Input.Keyboard.keyPressedQueue[0];
3143 // Shift elements 1 step toward the head.
3144 for (int i = 0; i < (CORE.Input.Keyboard.keyPressedQueueCount - 1); i++)
3145 CORE.Input.Keyboard.keyPressedQueue[i] = CORE.Input.Keyboard.keyPressedQueue[i + 1];
3147 // Reset last character in the queue
3148 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = 0;
3149 CORE.Input.Keyboard.keyPressedQueueCount--;
3155 // Get the last char pressed
3156 int GetCharPressed(void)
3160 if (CORE.Input.Keyboard.charPressedQueueCount > 0)
3162 // Get character from the queue head
3163 value = CORE.Input.Keyboard.charPressedQueue[0];
3165 // Shift elements 1 step toward the head.
3166 for (int i = 0; i < (CORE.Input.Keyboard.charPressedQueueCount - 1); i++)
3167 CORE.Input.Keyboard.charPressedQueue[i] = CORE.Input.Keyboard.charPressedQueue[i + 1];
3169 // Reset last character in the queue
3170 CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = 0;
3171 CORE.Input.Keyboard.charPressedQueueCount--;
3177 // Set a custom key to exit program
3178 // NOTE: default exitKey is ESCAPE
3179 void SetExitKey(int key)
3181 #if !defined(PLATFORM_ANDROID)
3182 CORE.Input.Keyboard.exitKey = key;
3186 // NOTE: Gamepad support not implemented in emscripten GLFW3 (PLATFORM_WEB)
3188 // Detect if a gamepad is available
3189 bool IsGamepadAvailable(int gamepad)
3191 bool result = false;
3193 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad]) result = true;
3198 // Check gamepad name (if available)
3199 bool IsGamepadName(int gamepad, const char *name)
3201 bool result = false;
3202 const char *currentName = NULL;
3204 if (CORE.Input.Gamepad.ready[gamepad]) currentName = GetGamepadName(gamepad);
3205 if ((name != NULL) && (currentName != NULL)) result = (strcmp(name, currentName) == 0);
3210 // Return gamepad internal name id
3211 const char *GetGamepadName(int gamepad)
3213 #if defined(PLATFORM_DESKTOP)
3214 if (CORE.Input.Gamepad.ready[gamepad]) return glfwGetJoystickName(gamepad);
3217 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
3218 if (CORE.Input.Gamepad.ready[gamepad]) ioctl(CORE.Input.Gamepad.streamId[gamepad], JSIOCGNAME(64), &CORE.Input.Gamepad.name);
3219 return CORE.Input.Gamepad.name;
3224 // Return gamepad axis count
3225 int GetGamepadAxisCount(int gamepad)
3227 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
3229 if (CORE.Input.Gamepad.ready[gamepad]) ioctl(CORE.Input.Gamepad.streamId[gamepad], JSIOCGAXES, &axisCount);
3230 CORE.Input.Gamepad.axisCount = axisCount;
3233 return CORE.Input.Gamepad.axisCount;
3236 // Return axis movement vector for a gamepad
3237 float GetGamepadAxisMovement(int gamepad, int axis)
3241 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS) &&
3242 (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) > 0.1f)) value = CORE.Input.Gamepad.axisState[gamepad][axis]; // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA
3247 // Detect if a gamepad button has been pressed once
3248 bool IsGamepadButtonPressed(int gamepad, int button)
3250 bool pressed = false;
3252 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
3253 (CORE.Input.Gamepad.previousState[gamepad][button] == 0) && (CORE.Input.Gamepad.currentState[gamepad][button] == 1)) pressed = true;
3254 else pressed = false;
3259 // Detect if a gamepad button is being pressed
3260 bool IsGamepadButtonDown(int gamepad, int button)
3262 bool result = false;
3264 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
3265 (CORE.Input.Gamepad.currentState[gamepad][button] == 1)) result = true;
3270 // Detect if a gamepad button has NOT been pressed once
3271 bool IsGamepadButtonReleased(int gamepad, int button)
3273 bool released = false;
3275 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
3276 (CORE.Input.Gamepad.previousState[gamepad][button] == 1) && (CORE.Input.Gamepad.currentState[gamepad][button] == 0)) released = true;
3277 else released = false;
3282 // Detect if a gamepad button is NOT being pressed
3283 bool IsGamepadButtonUp(int gamepad, int button)
3285 bool result = false;
3287 if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
3288 (CORE.Input.Gamepad.currentState[gamepad][button] == 0)) result = true;
3293 // Get the last gamepad button pressed
3294 int GetGamepadButtonPressed(void)
3296 return CORE.Input.Gamepad.lastButtonPressed;
3299 // Set internal gamepad mappings
3300 int SetGamepadMappings(const char *mappings)
3304 #if defined(PLATFORM_DESKTOP)
3305 result = glfwUpdateGamepadMappings(mappings);
3311 // Detect if a mouse button has been pressed once
3312 bool IsMouseButtonPressed(int button)
3314 bool pressed = false;
3316 if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) pressed = true;
3318 // Map touches to mouse buttons checking
3319 if ((CORE.Input.Touch.currentTouchState[button] == 1) && (CORE.Input.Touch.previousTouchState[button] == 0)) pressed = true;
3324 // Detect if a mouse button is being pressed
3325 bool IsMouseButtonDown(int button)
3329 if (CORE.Input.Mouse.currentButtonState[button] == 1) down = true;
3331 // Map touches to mouse buttons checking
3332 if (CORE.Input.Touch.currentTouchState[button] == 1) down = true;
3337 // Detect if a mouse button has been released once
3338 bool IsMouseButtonReleased(int button)
3340 bool released = false;
3342 if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) released = true;
3344 // Map touches to mouse buttons checking
3345 if ((CORE.Input.Touch.currentTouchState[button] == 0) && (CORE.Input.Touch.previousTouchState[button] == 1)) released = true;
3350 // Detect if a mouse button is NOT being pressed
3351 bool IsMouseButtonUp(int button)
3353 return !IsMouseButtonDown(button);
3356 // Returns mouse position X
3359 #if defined(PLATFORM_ANDROID)
3360 return (int)CORE.Input.Touch.position[0].x;
3362 return (int)((CORE.Input.Mouse.position.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x);
3366 // Returns mouse position Y
3369 #if defined(PLATFORM_ANDROID)
3370 return (int)CORE.Input.Touch.position[0].y;
3372 return (int)((CORE.Input.Mouse.position.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y);
3376 // Returns mouse position XY
3377 Vector2 GetMousePosition(void)
3379 Vector2 position = { 0 };
3381 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
3382 position = GetTouchPosition(0);
3384 position.x = (CORE.Input.Mouse.position.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x;
3385 position.y = (CORE.Input.Mouse.position.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y;
3391 // Set mouse position XY
3392 void SetMousePosition(int x, int y)
3394 CORE.Input.Mouse.position = (Vector2){ (float)x, (float)y };
3395 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
3396 // NOTE: emscripten not implemented
3397 glfwSetCursorPos(CORE.Window.handle, CORE.Input.Mouse.position.x, CORE.Input.Mouse.position.y);
3399 #if defined(PLATFORM_UWP)
3400 UWPGetMouseSetPosFunc()(x, y);
3405 // NOTE: Useful when rendering to different size targets
3406 void SetMouseOffset(int offsetX, int offsetY)
3408 CORE.Input.Mouse.offset = (Vector2){ (float)offsetX, (float)offsetY };
3411 // Set mouse scaling
3412 // NOTE: Useful when rendering to different size targets
3413 void SetMouseScale(float scaleX, float scaleY)
3415 CORE.Input.Mouse.scale = (Vector2){ scaleX, scaleY };
3418 // Returns mouse wheel movement Y
3419 float GetMouseWheelMove(void)
3421 #if defined(PLATFORM_ANDROID)
3424 #if defined(PLATFORM_WEB)
3425 return CORE.Input.Mouse.previousWheelMove/100.0f;
3428 return CORE.Input.Mouse.previousWheelMove;
3432 // NOTE: This is a no-op on platforms other than PLATFORM_DESKTOP
3433 void SetMouseCursor(int cursor)
3435 #if defined(PLATFORM_DESKTOP)
3436 CORE.Input.Mouse.cursor = cursor;
3437 if (cursor == MOUSE_CURSOR_DEFAULT) glfwSetCursor(CORE.Window.handle, NULL);
3440 // NOTE: We are relating internal GLFW enum values to our MouseCursor enum values
3441 glfwSetCursor(CORE.Window.handle, glfwCreateStandardCursor(0x00036000 + cursor));
3446 // Returns touch position X for touch point 0 (relative to screen size)
3449 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
3450 return (int)CORE.Input.Touch.position[0].x;
3451 #else // PLATFORM_DESKTOP, PLATFORM_RPI, PLATFORM_DRM
3456 // Returns touch position Y for touch point 0 (relative to screen size)
3459 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
3460 return (int)CORE.Input.Touch.position[0].y;
3461 #else // PLATFORM_DESKTOP, PLATFORM_RPI, PLATFORM_DRM
3466 // Returns touch position XY for a touch point index (relative to screen size)
3467 // TODO: Touch position should be scaled depending on display size and render size
3468 Vector2 GetTouchPosition(int index)
3470 Vector2 position = { -1.0f, -1.0f };
3472 #if defined(PLATFORM_DESKTOP)
3473 // TODO: GLFW does not support multi-touch input just yet
3474 // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch
3475 // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages
3476 if (index == 0) position = GetMousePosition();
3478 #if defined(PLATFORM_ANDROID)
3479 if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index];
3480 else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
3482 if ((CORE.Window.screen.width > CORE.Window.display.width) || (CORE.Window.screen.height > CORE.Window.display.height))
3484 position.x = position.x*((float)CORE.Window.screen.width/(float)(CORE.Window.display.width - CORE.Window.renderOffset.x)) - CORE.Window.renderOffset.x/2;
3485 position.y = position.y*((float)CORE.Window.screen.height/(float)(CORE.Window.display.height - CORE.Window.renderOffset.y)) - CORE.Window.renderOffset.y/2;
3489 position.x = position.x*((float)CORE.Window.render.width/(float)CORE.Window.display.width) - CORE.Window.renderOffset.x/2;
3490 position.y = position.y*((float)CORE.Window.render.height/(float)CORE.Window.display.height) - CORE.Window.renderOffset.y/2;
3493 #if defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
3494 if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index];
3495 else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
3497 // TODO: Touch position scaling required?
3503 //----------------------------------------------------------------------------------
3504 // Module specific Functions Definition
3505 //----------------------------------------------------------------------------------
3507 // Initialize display device and framebuffer
3508 // NOTE: width and height represent the screen (framebuffer) desired size, not actual display size
3509 // If width or height are 0, default display size will be used for framebuffer size
3510 // NOTE: returns false in case graphic device could not be created
3511 static bool InitGraphicsDevice(int width, int height)
3513 CORE.Window.screen.width = width; // User desired width
3514 CORE.Window.screen.height = height; // User desired height
3515 CORE.Window.screenScale = MatrixIdentity(); // No draw scaling required by default
3517 // NOTE: Framebuffer (render area - CORE.Window.render.width, CORE.Window.render.height) could include black bars...
3518 // ...in top-down or left-right to match display aspect ratio (no weird scalings)
3520 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
3521 glfwSetErrorCallback(ErrorCallback);
3523 #if defined(__APPLE__)
3524 glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
3529 TRACELOG(LOG_WARNING, "GLFW: Failed to initialize GLFW");
3533 // NOTE: Getting video modes is not implemented in emscripten GLFW3 version
3534 #if defined(PLATFORM_DESKTOP)
3535 // Find monitor resolution
3536 GLFWmonitor *monitor = glfwGetPrimaryMonitor();
3539 TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
3542 const GLFWvidmode *mode = glfwGetVideoMode(monitor);
3544 CORE.Window.display.width = mode->width;
3545 CORE.Window.display.height = mode->height;
3547 // Screen size security check
3548 if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
3549 if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
3550 #endif // PLATFORM_DESKTOP
3552 #if defined(PLATFORM_WEB)
3553 CORE.Window.display.width = CORE.Window.screen.width;
3554 CORE.Window.display.height = CORE.Window.screen.height;
3555 #endif // PLATFORM_WEB
3557 glfwDefaultWindowHints(); // Set default windows hints
3558 //glfwWindowHint(GLFW_RED_BITS, 8); // Framebuffer red color component bits
3559 //glfwWindowHint(GLFW_GREEN_BITS, 8); // Framebuffer green color component bits
3560 //glfwWindowHint(GLFW_BLUE_BITS, 8); // Framebuffer blue color component bits
3561 //glfwWindowHint(GLFW_ALPHA_BITS, 8); // Framebuffer alpha color component bits
3562 //glfwWindowHint(GLFW_DEPTH_BITS, 24); // Depthbuffer bits
3563 //glfwWindowHint(GLFW_REFRESH_RATE, 0); // Refresh rate for fullscreen window
3564 //glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); // OpenGL API to use. Alternative: GLFW_OPENGL_ES_API
3565 //glfwWindowHint(GLFW_AUX_BUFFERS, 0); // Number of auxiliar buffers
3567 // Check window creation flags
3568 if ((CORE.Window.flags & FLAG_FULLSCREEN_MODE) > 0) CORE.Window.fullscreen = true;
3570 if ((CORE.Window.flags & FLAG_WINDOW_HIDDEN) > 0) glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // Visible window
3571 else glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); // Window initially hidden
3573 if ((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) > 0) glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // Border and buttons on Window
3574 else glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); // Decorated window
3576 if ((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) > 0) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Resizable window
3577 else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Avoid window being resizable
3579 // Disable FLAG_WINDOW_MINIMIZED, not supported on initialization
3580 if ((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) > 0) CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED;
3582 // Disable FLAG_WINDOW_MAXIMIZED, not supported on initialization
3583 if ((CORE.Window.flags & FLAG_WINDOW_MAXIMIZED) > 0) CORE.Window.flags &= ~FLAG_WINDOW_MAXIMIZED;
3585 if ((CORE.Window.flags & FLAG_WINDOW_UNFOCUSED) > 0) glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE);
3586 else glfwWindowHint(GLFW_FOCUSED, GLFW_TRUE);
3588 if ((CORE.Window.flags & FLAG_WINDOW_TOPMOST) > 0) glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
3589 else glfwWindowHint(GLFW_FLOATING, GLFW_FALSE);
3591 // NOTE: Some GLFW flags are not supported on HTML5
3592 #if defined(PLATFORM_DESKTOP)
3593 if ((CORE.Window.flags & FLAG_WINDOW_TRANSPARENT) > 0) glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); // Transparent framebuffer
3594 else glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_FALSE); // Opaque framebuffer
3596 if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
3598 // Resize window content area based on the monitor content scale.
3599 // NOTE: This hint only has an effect on platforms where screen coordinates and pixels always map 1:1 such as Windows and X11.
3600 // On platforms like macOS the resolution of the framebuffer is changed independently of the window size.
3601 glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); // Scale content area based on the monitor content scale where window is placed on
3602 #if defined(__APPLE__)
3603 glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
3606 else glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
3609 if (CORE.Window.flags & FLAG_MSAA_4X_HINT)
3611 // NOTE: MSAA is only enabled for main framebuffer, not user-created FBOs
3612 TRACELOG(LOG_INFO, "DISPLAY: Trying to enable MSAA x4");
3613 glfwWindowHint(GLFW_SAMPLES, 4); // Tries to enable multisampling x4 (MSAA), default is 0
3616 // NOTE: When asking for an OpenGL context version, most drivers provide highest supported version
3617 // with forward compatibility to older OpenGL versions.
3618 // For example, if using OpenGL 1.1, driver can provide a 4.3 context forward compatible.
3620 // Check selection OpenGL version
3621 if (rlGetVersion() == OPENGL_21)
3623 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint)
3624 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint)
3626 else if (rlGetVersion() == OPENGL_33)
3628 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint)
3629 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint)
3630 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above!
3631 // Values: GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE
3632 #if defined(__APPLE__)
3633 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // OSX Requires fordward compatibility
3635 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE); // Fordward Compatibility Hint: Only 3.3 and above!
3637 //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); // Request OpenGL DEBUG context
3639 else if (rlGetVersion() == OPENGL_ES_20) // Request OpenGL ES 2.0 context
3641 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
3642 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
3643 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
3644 #if defined(PLATFORM_DESKTOP)
3645 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
3647 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API);
3651 #if defined(PLATFORM_DESKTOP)
3652 // NOTE: GLFW 3.4+ defers initialization of the Joystick subsystem on the first call to any Joystick related functions.
3653 // Forcing this initialization here avoids doing it on `PollInputEvents` called by `EndDrawing` after first frame has been just drawn.
3654 // The initialization will still happen and possible delays still occur, but before the window is shown, which is a nicer experience.
3655 // REF: https://github.com/raysan5/raylib/issues/1554
3656 if (MAX_GAMEPADS > 0) glfwSetJoystickCallback(NULL);
3659 if (CORE.Window.fullscreen)
3661 // remember center for switchinging from fullscreen to window
3662 CORE.Window.position.x = CORE.Window.display.width/2 - CORE.Window.screen.width/2;
3663 CORE.Window.position.y = CORE.Window.display.height/2 - CORE.Window.screen.height/2;
3665 if (CORE.Window.position.x < 0) CORE.Window.position.x = 0;
3666 if (CORE.Window.position.y < 0) CORE.Window.position.y = 0;
3668 // Obtain recommended CORE.Window.display.width/CORE.Window.display.height from a valid videomode for the monitor
3670 const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
3672 // Get closest video mode to desired CORE.Window.screen.width/CORE.Window.screen.height
3673 for (int i = 0; i < count; i++)
3675 if ((unsigned int)modes[i].width >= CORE.Window.screen.width)
3677 if ((unsigned int)modes[i].height >= CORE.Window.screen.height)
3679 CORE.Window.display.width = modes[i].width;
3680 CORE.Window.display.height = modes[i].height;
3686 #if defined(PLATFORM_DESKTOP)
3687 // If we are windowed fullscreen, ensures that window does not minimize when focus is lost
3688 if ((CORE.Window.screen.height == CORE.Window.display.height) && (CORE.Window.screen.width == CORE.Window.display.width))
3690 glfwWindowHint(GLFW_AUTO_ICONIFY, 0);
3693 TRACELOG(LOG_WARNING, "SYSTEM: Closest fullscreen videomode: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
3695 // NOTE: ISSUE: Closest videomode could not match monitor aspect-ratio, for example,
3696 // for a desired screen size of 800x450 (16:9), closest supported videomode is 800x600 (4:3),
3697 // framebuffer is rendered correctly but once displayed on a 16:9 monitor, it gets stretched
3698 // by the sides to fit all monitor space...
3700 // Try to setup the most appropiate fullscreen framebuffer for the requested screenWidth/screenHeight
3701 // It considers device display resolution mode and setups a framebuffer with black bars if required (render size/offset)
3702 // Modified global variables: CORE.Window.screen.width/CORE.Window.screen.height - CORE.Window.render.width/CORE.Window.render.height - CORE.Window.renderOffset.x/CORE.Window.renderOffset.y - CORE.Window.screenScale
3703 // TODO: It is a quite cumbersome solution to display size vs requested size, it should be reviewed or removed...
3704 // HighDPI monitors are properly considered in a following similar function: SetupViewport()
3705 SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height);
3707 CORE.Window.handle = glfwCreateWindow(CORE.Window.display.width, CORE.Window.display.height, (CORE.Window.title != 0)? CORE.Window.title : " ", glfwGetPrimaryMonitor(), NULL);
3709 // NOTE: Full-screen change, not working properly...
3710 //glfwSetWindowMonitor(CORE.Window.handle, glfwGetPrimaryMonitor(), 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
3714 // No-fullscreen window creation
3715 CORE.Window.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", NULL, NULL);
3717 if (CORE.Window.handle)
3719 #if defined(PLATFORM_DESKTOP)
3720 // Center window on screen
3721 int windowPosX = CORE.Window.display.width/2 - CORE.Window.screen.width/2;
3722 int windowPosY = CORE.Window.display.height/2 - CORE.Window.screen.height/2;
3724 if (windowPosX < 0) windowPosX = 0;
3725 if (windowPosY < 0) windowPosY = 0;
3727 glfwSetWindowPos(CORE.Window.handle, windowPosX, windowPosY);
3729 CORE.Window.render.width = CORE.Window.screen.width;
3730 CORE.Window.render.height = CORE.Window.screen.height;
3734 if (!CORE.Window.handle)
3737 TRACELOG(LOG_WARNING, "GLFW: Failed to initialize Window");
3742 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
3743 #if defined(PLATFORM_DESKTOP)
3744 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
3746 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
3747 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
3748 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
3751 // Set window callback events
3752 glfwSetWindowSizeCallback(CORE.Window.handle, WindowSizeCallback); // NOTE: Resizing not allowed by default!
3753 #if !defined(PLATFORM_WEB)
3754 glfwSetWindowMaximizeCallback(CORE.Window.handle, WindowMaximizeCallback);
3756 glfwSetWindowIconifyCallback(CORE.Window.handle, WindowIconifyCallback);
3757 glfwSetWindowFocusCallback(CORE.Window.handle, WindowFocusCallback);
3758 glfwSetDropCallback(CORE.Window.handle, WindowDropCallback);
3759 // Set input callback events
3760 glfwSetKeyCallback(CORE.Window.handle, KeyCallback);
3761 glfwSetCharCallback(CORE.Window.handle, CharCallback);
3762 glfwSetMouseButtonCallback(CORE.Window.handle, MouseButtonCallback);
3763 glfwSetCursorPosCallback(CORE.Window.handle, MouseCursorPosCallback); // Track mouse position changes
3764 glfwSetScrollCallback(CORE.Window.handle, MouseScrollCallback);
3765 glfwSetCursorEnterCallback(CORE.Window.handle, CursorEnterCallback);
3767 glfwMakeContextCurrent(CORE.Window.handle);
3769 #if !defined(PLATFORM_WEB)
3770 glfwSwapInterval(0); // No V-Sync by default
3773 // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
3774 // NOTE: V-Sync can be enabled by graphic driver configuration
3775 if (CORE.Window.flags & FLAG_VSYNC_HINT)
3777 // WARNING: It seems to hits a critical render path in Intel HD Graphics
3778 glfwSwapInterval(1);
3779 TRACELOG(LOG_INFO, "DISPLAY: Trying to enable VSYNC");
3781 #endif // PLATFORM_DESKTOP || PLATFORM_WEB
3783 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
3784 CORE.Window.fullscreen = true;
3785 CORE.Window.flags |= FLAG_FULLSCREEN_MODE;
3787 #if defined(PLATFORM_RPI)
3790 DISPMANX_ELEMENT_HANDLE_T dispmanElement;
3791 DISPMANX_DISPLAY_HANDLE_T dispmanDisplay;
3792 DISPMANX_UPDATE_HANDLE_T dispmanUpdate;
3798 #if defined(PLATFORM_DRM)
3799 CORE.Window.fd = -1;
3800 CORE.Window.connector = NULL;
3801 CORE.Window.modeIndex = -1;
3802 CORE.Window.crtc = NULL;
3803 CORE.Window.gbmDevice = NULL;
3804 CORE.Window.gbmSurface = NULL;
3805 CORE.Window.prevBO = NULL;
3806 CORE.Window.prevFB = 0;
3808 #if defined(DEFAULT_GRAPHIC_DEVICE_DRM)
3809 CORE.Window.fd = open(DEFAULT_GRAPHIC_DEVICE_DRM, O_RDWR);
3811 TRACELOG(LOG_INFO, "DISPLAY: No graphic card set, trying card1");
3812 CORE.Window.fd = open("/dev/dri/card1", O_RDWR); // VideoCore VI (Raspberry Pi 4)
3813 if ((-1 == CORE.Window.fd) || (drmModeGetResources(CORE.Window.fd) == NULL))
3815 TRACELOG(LOG_INFO, "DISPLAY: Failed to open graphic card1, trying card0");
3816 CORE.Window.fd = open("/dev/dri/card0", O_RDWR); // VideoCore IV (Raspberry Pi 1-3)
3819 if (-1 == CORE.Window.fd)
3821 TRACELOG(LOG_WARNING, "DISPLAY: Failed to open graphic card");
3825 drmModeRes *res = drmModeGetResources(CORE.Window.fd);
3828 TRACELOG(LOG_WARNING, "DISPLAY: Failed get DRM resources");
3832 TRACELOG(LOG_TRACE, "DISPLAY: Connectors found: %i", res->count_connectors);
3833 for (size_t i = 0; i < res->count_connectors; i++)
3835 TRACELOG(LOG_TRACE, "DISPLAY: Connector index %i", i);
3836 drmModeConnector *con = drmModeGetConnector(CORE.Window.fd, res->connectors[i]);
3837 TRACELOG(LOG_TRACE, "DISPLAY: Connector modes detected: %i", con->count_modes);
3838 if ((con->connection == DRM_MODE_CONNECTED) && (con->encoder_id))
3840 TRACELOG(LOG_TRACE, "DISPLAY: DRM mode connected");
3841 CORE.Window.connector = con;
3846 TRACELOG(LOG_TRACE, "DISPLAY: DRM mode NOT connected (deleting)");
3847 drmModeFreeConnector(con);
3850 if (!CORE.Window.connector)
3852 TRACELOG(LOG_WARNING, "DISPLAY: No suitable DRM connector found");
3853 drmModeFreeResources(res);
3857 drmModeEncoder *enc = drmModeGetEncoder(CORE.Window.fd, CORE.Window.connector->encoder_id);
3860 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get DRM mode encoder");
3861 drmModeFreeResources(res);
3865 CORE.Window.crtc = drmModeGetCrtc(CORE.Window.fd, enc->crtc_id);
3866 if (!CORE.Window.crtc)
3868 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get DRM mode crtc");
3869 drmModeFreeEncoder(enc);
3870 drmModeFreeResources(res);
3874 // If InitWindow should use the current mode find it in the connector's mode list
3875 if ((CORE.Window.screen.width <= 0) || (CORE.Window.screen.height <= 0))
3877 TRACELOG(LOG_TRACE, "DISPLAY: Selecting DRM connector mode for current used mode...");
3879 CORE.Window.modeIndex = FindMatchingConnectorMode(CORE.Window.connector, &CORE.Window.crtc->mode);
3881 if (CORE.Window.modeIndex < 0)
3883 TRACELOG(LOG_WARNING, "DISPLAY: No matching DRM connector mode found");
3884 drmModeFreeEncoder(enc);
3885 drmModeFreeResources(res);
3889 CORE.Window.screen.width = CORE.Window.display.width;
3890 CORE.Window.screen.height = CORE.Window.display.height;
3893 const bool allowInterlaced = CORE.Window.flags & FLAG_INTERLACED_HINT;
3894 const int fps = (CORE.Time.target > 0) ? (1.0/CORE.Time.target) : 60;
3895 // try to find an exact matching mode
3896 CORE.Window.modeIndex = FindExactConnectorMode(CORE.Window.connector, CORE.Window.screen.width, CORE.Window.screen.height, fps, allowInterlaced);
3897 // if nothing found, try to find a nearly matching mode
3898 if (CORE.Window.modeIndex < 0)
3899 CORE.Window.modeIndex = FindNearestConnectorMode(CORE.Window.connector, CORE.Window.screen.width, CORE.Window.screen.height, fps, allowInterlaced);
3900 // if nothing found, try to find an exactly matching mode including interlaced
3901 if (CORE.Window.modeIndex < 0)
3902 CORE.Window.modeIndex = FindExactConnectorMode(CORE.Window.connector, CORE.Window.screen.width, CORE.Window.screen.height, fps, true);
3903 // if nothing found, try to find a nearly matching mode including interlaced
3904 if (CORE.Window.modeIndex < 0)
3905 CORE.Window.modeIndex = FindNearestConnectorMode(CORE.Window.connector, CORE.Window.screen.width, CORE.Window.screen.height, fps, true);
3906 // if nothing found, there is no suitable mode
3907 if (CORE.Window.modeIndex < 0)
3909 TRACELOG(LOG_WARNING, "DISPLAY: Failed to find a suitable DRM connector mode");
3910 drmModeFreeEncoder(enc);
3911 drmModeFreeResources(res);
3915 CORE.Window.display.width = CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay;
3916 CORE.Window.display.height = CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay;
3918 TRACELOG(LOG_INFO, "DISPLAY: Selected DRM connector mode %s (%ux%u%c@%u)", CORE.Window.connector->modes[CORE.Window.modeIndex].name,
3919 CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay, CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay,
3920 (CORE.Window.connector->modes[CORE.Window.modeIndex].flags & DRM_MODE_FLAG_INTERLACE) ? 'i' : 'p',
3921 CORE.Window.connector->modes[CORE.Window.modeIndex].vrefresh);
3923 // Use the width and height of the surface for render
3924 CORE.Window.render.width = CORE.Window.screen.width;
3925 CORE.Window.render.height = CORE.Window.screen.height;
3927 drmModeFreeEncoder(enc);
3930 drmModeFreeResources(res);
3933 CORE.Window.gbmDevice = gbm_create_device(CORE.Window.fd);
3934 if (!CORE.Window.gbmDevice)
3936 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create GBM device");
3940 CORE.Window.gbmSurface = gbm_surface_create(CORE.Window.gbmDevice, CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay,
3941 CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay, GBM_FORMAT_ARGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
3942 if (!CORE.Window.gbmSurface)
3944 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create GBM surface");
3950 EGLint sampleBuffer = 0;
3951 if (CORE.Window.flags & FLAG_MSAA_4X_HINT)
3955 TRACELOG(LOG_INFO, "DISPLAY: Trying to enable MSAA x4");
3958 const EGLint framebufferAttribs[] =
3960 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // Type of context support -> Required on RPI?
3961 #if defined(PLATFORM_DRM)
3962 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, // Don't use it on Android!
3964 EGL_RED_SIZE, 8, // RED color bit depth (alternative: 5)
3965 EGL_GREEN_SIZE, 8, // GREEN color bit depth (alternative: 6)
3966 EGL_BLUE_SIZE, 8, // BLUE color bit depth (alternative: 5)
3967 #if defined(PLATFORM_DRM)
3968 EGL_ALPHA_SIZE, 8, // ALPHA bit depth (required for transparent framebuffer)
3970 //EGL_TRANSPARENT_TYPE, EGL_NONE, // Request transparent framebuffer (EGL_TRANSPARENT_RGB does not work on RPI)
3971 EGL_DEPTH_SIZE, 16, // Depth buffer size (Required to use Depth testing!)
3972 //EGL_STENCIL_SIZE, 8, // Stencil buffer size
3973 EGL_SAMPLE_BUFFERS, sampleBuffer, // Activate MSAA
3974 EGL_SAMPLES, samples, // 4x Antialiasing if activated (Free on MALI GPUs)
3978 const EGLint contextAttribs[] =
3980 EGL_CONTEXT_CLIENT_VERSION, 2,
3984 #if defined(PLATFORM_UWP)
3985 const EGLint surfaceAttributes[] =
3987 // EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER is part of the same optimization as EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER (see above).
3988 // If you have compilation issues with it then please update your Visual Studio templates.
3989 EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, EGL_TRUE,
3993 const EGLint defaultDisplayAttributes[] =
3995 // These are the default display attributes, used to request ANGLE's D3D11 renderer.
3996 // eglInitialize will only succeed with these attributes if the hardware supports D3D11 Feature Level 10_0+.
3997 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
3999 // EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices.
4000 // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it.
4001 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
4003 // EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call
4004 // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended.
4005 // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement.
4006 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
4010 const EGLint fl9_3DisplayAttributes[] =
4012 // These can be used to request ANGLE's D3D11 renderer, with D3D11 Feature Level 9_3.
4013 // These attributes are used if the call to eglInitialize fails with the default display attributes.
4014 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
4015 EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
4016 EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
4017 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
4018 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
4022 const EGLint warpDisplayAttributes[] =
4024 // These attributes can be used to request D3D11 WARP.
4025 // They are used if eglInitialize fails with both the default display attributes and the 9_3 display attributes.
4026 EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
4027 EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE,
4028 EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
4029 EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE,
4033 // eglGetPlatformDisplayEXT is an alternative to eglGetDisplay. It allows us to pass in display attributes, used to configure D3D11.
4034 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)(eglGetProcAddress("eglGetPlatformDisplayEXT"));
4035 if (!eglGetPlatformDisplayEXT)
4037 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get function pointer: eglGetPlatformDisplayEXT()");
4042 // To initialize the display, we make three sets of calls to eglGetPlatformDisplayEXT and eglInitialize, with varying
4043 // parameters passed to eglGetPlatformDisplayEXT:
4044 // 1) The first calls uses "defaultDisplayAttributes" as a parameter. This corresponds to D3D11 Feature Level 10_0+.
4045 // 2) If eglInitialize fails for step 1 (e.g. because 10_0+ isn't supported by the default GPU), then we try again
4046 // using "fl9_3DisplayAttributes". This corresponds to D3D11 Feature Level 9_3.
4047 // 3) If eglInitialize fails for step 2 (e.g. because 9_3+ isn't supported by the default GPU), then we try again
4048 // using "warpDisplayAttributes". This corresponds to D3D11 Feature Level 11_0 on WARP, a D3D11 software rasterizer.
4051 // This tries to initialize EGL to D3D11 Feature Level 10_0+. See above comment for details.
4052 CORE.Window.device = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes);
4053 if (CORE.Window.device == EGL_NO_DISPLAY)
4055 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4059 if (eglInitialize(CORE.Window.device, NULL, NULL) == EGL_FALSE)
4061 // This tries to initialize EGL to D3D11 Feature Level 9_3, if 10_0+ is unavailable (e.g. on some mobile devices).
4062 CORE.Window.device = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes);
4063 if (CORE.Window.device == EGL_NO_DISPLAY)
4065 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4069 if (eglInitialize(CORE.Window.device, NULL, NULL) == EGL_FALSE)
4071 // This initializes EGL to D3D11 Feature Level 11_0 on WARP, if 9_3+ is unavailable on the default GPU.
4072 CORE.Window.device = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes);
4073 if (CORE.Window.device == EGL_NO_DISPLAY)
4075 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4079 if (eglInitialize(CORE.Window.device, NULL, NULL) == EGL_FALSE)
4081 // If all of the calls to eglInitialize returned EGL_FALSE then an error has occurred.
4082 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4088 EGLint numConfigs = 0;
4089 if ((eglChooseConfig(CORE.Window.device, framebufferAttribs, &CORE.Window.config, 1, &numConfigs) == EGL_FALSE) || (numConfigs == 0))
4091 TRACELOG(LOG_WARNING, "DISPLAY: Failed to choose first EGL configuration");
4095 // Create a PropertySet and initialize with the EGLNativeWindowType.
4096 //PropertySet^ surfaceCreationProperties = ref new PropertySet();
4097 //surfaceCreationProperties->Insert(ref new String(EGLNativeWindowTypeProperty), window); // CoreWindow^ window
4099 // You can configure the surface to render at a lower resolution and be scaled up to
4100 // the full window size. The scaling is often free on mobile hardware.
4102 // One way to configure the SwapChainPanel is to specify precisely which resolution it should render at.
4103 // Size customRenderSurfaceSize = Size(800, 600);
4104 // surfaceCreationProperties->Insert(ref new String(EGLRenderSurfaceSizeProperty), PropertyValue::CreateSize(customRenderSurfaceSize));
4106 // Another way is to tell the SwapChainPanel to render at a certain scale factor compared to its size.
4107 // e.g. if the SwapChainPanel is 1920x1280 then setting a factor of 0.5f will make the app render at 960x640
4108 // float customResolutionScale = 0.5f;
4109 // surfaceCreationProperties->Insert(ref new String(EGLRenderResolutionScaleProperty), PropertyValue::CreateSingle(customResolutionScale));
4112 // eglCreateWindowSurface() requires a EGLNativeWindowType parameter,
4113 // In Windows platform: typedef HWND EGLNativeWindowType;
4116 // Property: EGLNativeWindowTypeProperty
4117 // Type: IInspectable
4118 // Description: Set this property to specify the window type to use for creating a surface.
4119 // If this property is missing, surface creation will fail.
4121 //const wchar_t EGLNativeWindowTypeProperty[] = L"EGLNativeWindowTypeProperty";
4123 //https://stackoverflow.com/questions/46550182/how-to-create-eglsurface-using-c-winrt-and-angle
4125 //CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, reinterpret_cast<IInspectable*>(surfaceCreationProperties), surfaceAttributes);
4126 CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, (EGLNativeWindowType) UWPGetCoreWindowPtr(), surfaceAttributes);
4127 if (CORE.Window.surface == EGL_NO_SURFACE)
4129 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create EGL fullscreen surface");
4133 CORE.Window.context = eglCreateContext(CORE.Window.device, CORE.Window.config, EGL_NO_CONTEXT, contextAttribs);
4134 if (CORE.Window.context == EGL_NO_CONTEXT)
4136 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create EGL context");
4140 // Get EGL device window size
4141 eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_WIDTH, &CORE.Window.screen.width);
4142 eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_HEIGHT, &CORE.Window.screen.height);
4145 UWPGetDisplaySizeFunc()(&CORE.Window.display.width, &CORE.Window.display.height);
4147 // Use the width and height of the surface for render
4148 CORE.Window.render.width = CORE.Window.screen.width;
4149 CORE.Window.render.height = CORE.Window.screen.height;
4151 #endif // PLATFORM_UWP
4153 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
4154 EGLint numConfigs = 0;
4156 // Get an EGL device connection
4157 #if defined(PLATFORM_DRM)
4158 CORE.Window.device = eglGetDisplay((EGLNativeDisplayType)CORE.Window.gbmDevice);
4160 CORE.Window.device = eglGetDisplay(EGL_DEFAULT_DISPLAY);
4162 if (CORE.Window.device == EGL_NO_DISPLAY)
4164 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4168 // Initialize the EGL device connection
4169 if (eglInitialize(CORE.Window.device, NULL, NULL) == EGL_FALSE)
4171 // If all of the calls to eglInitialize returned EGL_FALSE then an error has occurred.
4172 TRACELOG(LOG_WARNING, "DISPLAY: Failed to initialize EGL device");
4176 #if defined(PLATFORM_DRM)
4177 if (!eglChooseConfig(CORE.Window.device, NULL, NULL, 0, &numConfigs))
4179 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get EGL config count: 0x%x", eglGetError());
4183 TRACELOG(LOG_TRACE, "DISPLAY: EGL configs available: %d", numConfigs);
4185 EGLConfig *configs = RL_CALLOC(numConfigs, sizeof(*configs));
4188 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get memory for EGL configs");
4192 EGLint matchingNumConfigs = 0;
4193 if (!eglChooseConfig(CORE.Window.device, framebufferAttribs, configs, numConfigs, &matchingNumConfigs))
4195 TRACELOG(LOG_WARNING, "DISPLAY: Failed to choose EGL config: 0x%x", eglGetError());
4200 TRACELOG(LOG_TRACE, "DISPLAY: EGL matching configs available: %d", matchingNumConfigs);
4202 // find the EGL config that matches the previously setup GBM format
4204 for (EGLint i = 0; i < matchingNumConfigs; ++i)
4207 if (!eglGetConfigAttrib(CORE.Window.device, configs[i], EGL_NATIVE_VISUAL_ID, &id))
4209 TRACELOG(LOG_WARNING, "DISPLAY: Failed to get EGL config attribute: 0x%x", eglGetError());
4213 if (GBM_FORMAT_ARGB8888 == id)
4215 TRACELOG(LOG_TRACE, "DISPLAY: Using EGL config: %d", i);
4216 CORE.Window.config = configs[i];
4226 TRACELOG(LOG_WARNING, "DISPLAY: Failed to find a suitable EGL config");
4230 // Get an appropriate EGL framebuffer configuration
4231 eglChooseConfig(CORE.Window.device, framebufferAttribs, &CORE.Window.config, 1, &numConfigs);
4234 // Set rendering API
4235 eglBindAPI(EGL_OPENGL_ES_API);
4237 // Create an EGL rendering context
4238 CORE.Window.context = eglCreateContext(CORE.Window.device, CORE.Window.config, EGL_NO_CONTEXT, contextAttribs);
4239 if (CORE.Window.context == EGL_NO_CONTEXT)
4241 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create EGL context");
4246 // Create an EGL window surface
4247 //---------------------------------------------------------------------------------
4248 #if defined(PLATFORM_ANDROID)
4249 EGLint displayFormat = 0;
4251 // EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is guaranteed to be accepted by ANativeWindow_setBuffersGeometry()
4252 // As soon as we picked a EGLConfig, we can safely reconfigure the ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID
4253 eglGetConfigAttrib(CORE.Window.device, CORE.Window.config, EGL_NATIVE_VISUAL_ID, &displayFormat);
4255 // At this point we need to manage render size vs screen size
4256 // NOTE: This function use and modify global module variables:
4257 // -> CORE.Window.screen.width/CORE.Window.screen.height
4258 // -> CORE.Window.render.width/CORE.Window.render.height
4259 // -> CORE.Window.screenScale
4260 SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height);
4262 ANativeWindow_setBuffersGeometry(CORE.Android.app->window, CORE.Window.render.width, CORE.Window.render.height, displayFormat);
4263 //ANativeWindow_setBuffersGeometry(CORE.Android.app->window, 0, 0, displayFormat); // Force use of native display size
4265 CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, CORE.Android.app->window, NULL);
4266 #endif // PLATFORM_ANDROID
4268 #if defined(PLATFORM_RPI)
4269 graphics_get_display_size(0, &CORE.Window.display.width, &CORE.Window.display.height);
4271 // Screen size security check
4272 if (CORE.Window.screen.width <= 0) CORE.Window.screen.width = CORE.Window.display.width;
4273 if (CORE.Window.screen.height <= 0) CORE.Window.screen.height = CORE.Window.display.height;
4275 // At this point we need to manage render size vs screen size
4276 // NOTE: This function use and modify global module variables:
4277 // -> CORE.Window.screen.width/CORE.Window.screen.height
4278 // -> CORE.Window.render.width/CORE.Window.render.height
4279 // -> CORE.Window.screenScale
4280 SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height);
4284 dstRect.width = CORE.Window.display.width;
4285 dstRect.height = CORE.Window.display.height;
4289 srcRect.width = CORE.Window.render.width << 16;
4290 srcRect.height = CORE.Window.render.height << 16;
4292 // NOTE: RPI dispmanx windowing system takes care of source rectangle scaling to destination rectangle by hardware (no cost)
4293 // Take care that renderWidth/renderHeight fit on displayWidth/displayHeight aspect ratio
4295 VC_DISPMANX_ALPHA_T alpha;
4296 alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS;
4297 //alpha.flags = DISPMANX_FLAGS_ALPHA_FROM_SOURCE; // TODO: Allow transparent framebuffer! -> FLAG_WINDOW_TRANSPARENT
4298 alpha.opacity = 255; // Set transparency level for framebuffer, requires EGLAttrib: EGL_TRANSPARENT_TYPE
4301 dispmanDisplay = vc_dispmanx_display_open(0); // LCD
4302 dispmanUpdate = vc_dispmanx_update_start(0);
4304 dispmanElement = vc_dispmanx_element_add(dispmanUpdate, dispmanDisplay, 0/*layer*/, &dstRect, 0/*src*/,
4305 &srcRect, DISPMANX_PROTECTION_NONE, &alpha, 0/*clamp*/, DISPMANX_NO_ROTATE);
4307 CORE.Window.handle.element = dispmanElement;
4308 CORE.Window.handle.width = CORE.Window.render.width;
4309 CORE.Window.handle.height = CORE.Window.render.height;
4310 vc_dispmanx_update_submit_sync(dispmanUpdate);
4312 CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, &CORE.Window.handle, NULL);
4314 const unsigned char *const renderer = glGetString(GL_RENDERER);
4315 if (renderer) TRACELOG(LOG_INFO, "DISPLAY: Renderer name is: %s", renderer);
4316 else TRACELOG(LOG_WARNING, "DISPLAY: Failed to get renderer name");
4317 //---------------------------------------------------------------------------------
4318 #endif // PLATFORM_RPI
4320 #if defined(PLATFORM_DRM)
4321 CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, (EGLNativeWindowType)CORE.Window.gbmSurface, NULL);
4322 if (EGL_NO_SURFACE == CORE.Window.surface)
4324 TRACELOG(LOG_WARNING, "DISPLAY: Failed to create EGL window surface: 0x%04x", eglGetError());
4328 // At this point we need to manage render size vs screen size
4329 // NOTE: This function use and modify global module variables:
4330 // -> CORE.Window.screen.width/CORE.Window.screen.height
4331 // -> CORE.Window.render.width/CORE.Window.render.height
4332 // -> CORE.Window.screenScale
4333 SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height);
4334 #endif // PLATFORM_DRM
4336 // There must be at least one frame displayed before the buffers are swapped
4337 //eglSwapInterval(CORE.Window.device, 1);
4339 if (eglMakeCurrent(CORE.Window.device, CORE.Window.surface, CORE.Window.surface, CORE.Window.context) == EGL_FALSE)
4341 TRACELOG(LOG_WARNING, "DISPLAY: Failed to attach EGL rendering context to EGL surface");
4346 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
4347 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
4348 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
4349 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
4350 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
4352 #endif // PLATFORM_ANDROID || PLATFORM_RPI || PLATFORM_DRM || PLATFORM_UWP
4354 // Load OpenGL extensions
4355 // NOTE: GL procedures address loader is required to load extensions
4356 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
4357 rlLoadExtensions(glfwGetProcAddress);
4359 rlLoadExtensions(eglGetProcAddress);
4362 // Initialize OpenGL context (states and resources)
4363 // NOTE: CORE.Window.screen.width and CORE.Window.screen.height not used, just stored as globals in rlgl
4364 rlglInit(CORE.Window.screen.width, CORE.Window.screen.height);
4366 int fbWidth = CORE.Window.render.width;
4367 int fbHeight = CORE.Window.render.height;
4369 #if defined(PLATFORM_DESKTOP)
4370 if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) > 0)
4372 // NOTE: On APPLE platforms system should manage window/input scaling and also framebuffer scaling
4373 // Framebuffer scaling should be activated with: glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_TRUE);
4374 #if !defined(__APPLE__)
4375 glfwGetFramebufferSize(CORE.Window.handle, &fbWidth, &fbHeight);
4377 // Screen scaling matrix is required in case desired screen area is different than display area
4378 CORE.Window.screenScale = MatrixScale((float)fbWidth/CORE.Window.screen.width, (float)fbHeight/CORE.Window.screen.height, 1.0f);
4380 // Mouse input scaling for the new screen size
4381 SetMouseScale((float)CORE.Window.screen.width/fbWidth, (float)CORE.Window.screen.height/fbHeight);
4386 // Setup default viewport
4387 SetupViewport(fbWidth, fbHeight);
4389 CORE.Window.currentFbo.width = CORE.Window.screen.width;
4390 CORE.Window.currentFbo.height = CORE.Window.screen.height;
4392 ClearBackground(RAYWHITE); // Default background color for raylib games :P
4394 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_UWP)
4395 CORE.Window.ready = true;
4398 if ((CORE.Window.flags & FLAG_WINDOW_MINIMIZED) > 0) MinimizeWindow();
4403 // Set viewport for a provided width and height
4404 static void SetupViewport(int width, int height)
4406 CORE.Window.render.width = width;
4407 CORE.Window.render.height = height;
4409 // Set viewport width and height
4410 // NOTE: We consider render size (scaled) and offset in case black bars are required and
4411 // render area does not match full display area (this situation is only applicable on fullscreen mode)
4412 #if defined(__APPLE__)
4413 float xScale = 1.0f, yScale = 1.0f;
4414 glfwGetWindowContentScale(CORE.Window.handle, &xScale, &yScale);
4415 rlViewport(CORE.Window.renderOffset.x/2*xScale, CORE.Window.renderOffset.y/2*yScale, (CORE.Window.render.width - CORE.Window.renderOffset.x)*xScale, (CORE.Window.render.height - CORE.Window.renderOffset.y)*yScale);
4417 rlViewport(CORE.Window.renderOffset.x/2, CORE.Window.renderOffset.y/2, CORE.Window.render.width - CORE.Window.renderOffset.x, CORE.Window.render.height - CORE.Window.renderOffset.y);
4420 rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
4421 rlLoadIdentity(); // Reset current matrix (projection)
4423 // Set orthographic projection to current framebuffer size
4424 // NOTE: Configured top-left corner as (0, 0)
4425 rlOrtho(0, CORE.Window.render.width, CORE.Window.render.height, 0, 0.0f, 1.0f);
4427 rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
4428 rlLoadIdentity(); // Reset current matrix (modelview)
4431 // Compute framebuffer size relative to screen size and display size
4432 // NOTE: Global variables CORE.Window.render.width/CORE.Window.render.height and CORE.Window.renderOffset.x/CORE.Window.renderOffset.y can be modified
4433 static void SetupFramebuffer(int width, int height)
4435 // Calculate CORE.Window.render.width and CORE.Window.render.height, we have the display size (input params) and the desired screen size (global var)
4436 if ((CORE.Window.screen.width > CORE.Window.display.width) || (CORE.Window.screen.height > CORE.Window.display.height))
4438 TRACELOG(LOG_WARNING, "DISPLAY: Downscaling required: Screen size (%ix%i) is bigger than display size (%ix%i)", CORE.Window.screen.width, CORE.Window.screen.height, CORE.Window.display.width, CORE.Window.display.height);
4440 // Downscaling to fit display with border-bars
4441 float widthRatio = (float)CORE.Window.display.width/(float)CORE.Window.screen.width;
4442 float heightRatio = (float)CORE.Window.display.height/(float)CORE.Window.screen.height;
4444 if (widthRatio <= heightRatio)
4446 CORE.Window.render.width = CORE.Window.display.width;
4447 CORE.Window.render.height = (int)round((float)CORE.Window.screen.height*widthRatio);
4448 CORE.Window.renderOffset.x = 0;
4449 CORE.Window.renderOffset.y = (CORE.Window.display.height - CORE.Window.render.height);
4453 CORE.Window.render.width = (int)round((float)CORE.Window.screen.width*heightRatio);
4454 CORE.Window.render.height = CORE.Window.display.height;
4455 CORE.Window.renderOffset.x = (CORE.Window.display.width - CORE.Window.render.width);
4456 CORE.Window.renderOffset.y = 0;
4459 // Screen scaling required
4460 float scaleRatio = (float)CORE.Window.render.width/(float)CORE.Window.screen.width;
4461 CORE.Window.screenScale = MatrixScale(scaleRatio, scaleRatio, 1.0f);
4463 // NOTE: We render to full display resolution!
4464 // We just need to calculate above parameters for downscale matrix and offsets
4465 CORE.Window.render.width = CORE.Window.display.width;
4466 CORE.Window.render.height = CORE.Window.display.height;
4468 TRACELOG(LOG_WARNING, "DISPLAY: Downscale matrix generated, content will be rendered at (%ix%i)", CORE.Window.render.width, CORE.Window.render.height);
4470 else if ((CORE.Window.screen.width < CORE.Window.display.width) || (CORE.Window.screen.height < CORE.Window.display.height))
4472 // Required screen size is smaller than display size
4473 TRACELOG(LOG_INFO, "DISPLAY: Upscaling required: Screen size (%ix%i) smaller than display size (%ix%i)", CORE.Window.screen.width, CORE.Window.screen.height, CORE.Window.display.width, CORE.Window.display.height);
4475 if ((CORE.Window.screen.width == 0) || (CORE.Window.screen.height == 0))
4477 CORE.Window.screen.width = CORE.Window.display.width;
4478 CORE.Window.screen.height = CORE.Window.display.height;
4481 // Upscaling to fit display with border-bars
4482 float displayRatio = (float)CORE.Window.display.width/(float)CORE.Window.display.height;
4483 float screenRatio = (float)CORE.Window.screen.width/(float)CORE.Window.screen.height;
4485 if (displayRatio <= screenRatio)
4487 CORE.Window.render.width = CORE.Window.screen.width;
4488 CORE.Window.render.height = (int)round((float)CORE.Window.screen.width/displayRatio);
4489 CORE.Window.renderOffset.x = 0;
4490 CORE.Window.renderOffset.y = (CORE.Window.render.height - CORE.Window.screen.height);
4494 CORE.Window.render.width = (int)round((float)CORE.Window.screen.height*displayRatio);
4495 CORE.Window.render.height = CORE.Window.screen.height;
4496 CORE.Window.renderOffset.x = (CORE.Window.render.width - CORE.Window.screen.width);
4497 CORE.Window.renderOffset.y = 0;
4502 CORE.Window.render.width = CORE.Window.screen.width;
4503 CORE.Window.render.height = CORE.Window.screen.height;
4504 CORE.Window.renderOffset.x = 0;
4505 CORE.Window.renderOffset.y = 0;
4509 // Initialize hi-resolution timer
4510 static void InitTimer(void)
4512 srand((unsigned int)time(NULL)); // Initialize random seed
4514 // Setting a higher resolution can improve the accuracy of time-out intervals in wait functions.
4515 // However, it can also reduce overall system performance, because the thread scheduler switches tasks more often.
4516 // High resolutions can also prevent the CPU power management system from entering power-saving modes.
4517 // Setting a higher resolution does not improve the accuracy of the high-resolution performance counter.
4518 #if defined(_WIN32) && defined(SUPPORT_WINMM_HIGHRES_TIMER) && !defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_UWP)
4519 timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms)
4522 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
4523 struct timespec now = { 0 };
4525 if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success
4527 CORE.Time.base = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec;
4529 else TRACELOG(LOG_WARNING, "TIMER: Hi-resolution timer not available");
4532 CORE.Time.previous = GetTime(); // Get time as double
4535 // Wait for some milliseconds (stop program execution)
4536 // NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could
4537 // take longer than expected... for that reason we use the busy wait loop
4538 // Ref: http://stackoverflow.com/questions/43057578/c-programming-win32-games-sleep-taking-longer-than-expected
4539 // Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timming on Win32!
4540 static void Wait(float ms)
4542 #if defined(PLATFORM_UWP)
4543 UWPGetSleepFunc()(ms/1000);
4547 #if defined(SUPPORT_BUSY_WAIT_LOOP)
4548 double prevTime = GetTime();
4549 double nextTime = 0.0;
4552 while ((nextTime - prevTime) < ms/1000.0f) nextTime = GetTime();
4554 #if defined(SUPPORT_HALFBUSY_WAIT_LOOP)
4555 #define MAX_HALFBUSY_WAIT_TIME 4
4556 double destTime = GetTime() + ms/1000;
4557 if (ms > MAX_HALFBUSY_WAIT_TIME) ms -= MAX_HALFBUSY_WAIT_TIME;
4561 Sleep((unsigned int)ms);
4563 #if defined(__linux__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__)
4564 struct timespec req = { 0 };
4565 time_t sec = (int)(ms/1000.0f);
4568 req.tv_nsec = ms*1000000L;
4570 // NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated.
4571 while (nanosleep(&req, &req) == -1) continue;
4573 #if defined(__APPLE__)
4577 #if defined(SUPPORT_HALFBUSY_WAIT_LOOP)
4578 while (GetTime() < destTime) { }
4583 // Poll (store) all input events
4584 static void PollInputEvents(void)
4586 #if defined(SUPPORT_GESTURES_SYSTEM)
4587 // NOTE: Gestures update must be called every frame to reset gestures correctly
4588 // because ProcessGestureEvent() is just called on an event, not every frame
4592 // Reset keys/chars pressed registered
4593 CORE.Input.Keyboard.keyPressedQueueCount = 0;
4594 CORE.Input.Keyboard.charPressedQueueCount = 0;
4596 #if !(defined(PLATFORM_RPI) || defined(PLATFORM_DRM))
4597 // Reset last gamepad button/axis registered state
4598 CORE.Input.Gamepad.lastButtonPressed = -1;
4599 CORE.Input.Gamepad.axisCount = 0;
4602 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
4603 // Register previous keys states
4604 for (int i = 0; i < 512; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
4606 PollKeyboardEvents();
4608 // Register previous mouse states
4609 CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
4610 CORE.Input.Mouse.currentWheelMove = 0.0f;
4611 for (int i = 0; i < 3; i++)
4613 CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
4614 CORE.Input.Mouse.currentButtonState[i] = CORE.Input.Mouse.currentButtonStateEvdev[i];
4617 // Register gamepads buttons events
4618 for (int i = 0; i < MAX_GAMEPADS; i++)
4620 if (CORE.Input.Gamepad.ready[i]) // Check if gamepad is available
4622 // Register previous gamepad states
4623 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousState[i][k] = CORE.Input.Gamepad.currentState[i][k];
4628 #if defined(PLATFORM_UWP)
4629 // Register previous keys states
4630 for (int i = 0; i < 512; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
4632 for (int i = 0; i < MAX_GAMEPADS; i++)
4634 if (CORE.Input.Gamepad.ready[i])
4636 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousState[i][k] = CORE.Input.Gamepad.currentState[i][k];
4640 // Register previous mouse states
4641 CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
4642 CORE.Input.Mouse.currentWheelMove = 0.0f;
4644 for (int i = 0; i < 3; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
4645 #endif // PLATFORM_UWP
4647 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
4648 // Keyboard/Mouse input polling (automatically managed by GLFW3 through callback)
4650 // Register previous keys states
4651 for (int i = 0; i < 512; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
4653 // Register previous mouse states
4654 for (int i = 0; i < 3; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
4656 // Register previous mouse wheel state
4657 CORE.Input.Mouse.previousWheelMove = CORE.Input.Mouse.currentWheelMove;
4658 CORE.Input.Mouse.currentWheelMove = 0.0f;
4661 // Register previous touch states
4662 for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
4664 #if defined(PLATFORM_DESKTOP)
4665 // Check if gamepads are ready
4666 // NOTE: We do it here in case of disconnection
4667 for (int i = 0; i < MAX_GAMEPADS; i++)
4669 if (glfwJoystickPresent(i)) CORE.Input.Gamepad.ready[i] = true;
4670 else CORE.Input.Gamepad.ready[i] = false;
4673 // Register gamepads buttons events
4674 for (int i = 0; i < MAX_GAMEPADS; i++)
4676 if (CORE.Input.Gamepad.ready[i]) // Check if gamepad is available
4678 // Register previous gamepad states
4679 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousState[i][k] = CORE.Input.Gamepad.currentState[i][k];
4681 // Get current gamepad state
4682 // NOTE: There is no callback available, so we get it manually
4683 // Get remapped buttons
4684 GLFWgamepadstate state = { 0 };
4685 glfwGetGamepadState(i, &state); // This remapps all gamepads so they have their buttons mapped like an xbox controller
4687 const unsigned char *buttons = state.buttons;
4689 for (int k = 0; (buttons != NULL) && (k < GLFW_GAMEPAD_BUTTON_DPAD_LEFT + 1) && (k < MAX_GAMEPAD_BUTTONS); k++)
4691 GamepadButton button = -1;
4695 case GLFW_GAMEPAD_BUTTON_Y: button = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
4696 case GLFW_GAMEPAD_BUTTON_B: button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
4697 case GLFW_GAMEPAD_BUTTON_A: button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
4698 case GLFW_GAMEPAD_BUTTON_X: button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
4700 case GLFW_GAMEPAD_BUTTON_LEFT_BUMPER: button = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
4701 case GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
4703 case GLFW_GAMEPAD_BUTTON_BACK: button = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
4704 case GLFW_GAMEPAD_BUTTON_GUIDE: button = GAMEPAD_BUTTON_MIDDLE; break;
4705 case GLFW_GAMEPAD_BUTTON_START: button = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
4707 case GLFW_GAMEPAD_BUTTON_DPAD_UP: button = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
4708 case GLFW_GAMEPAD_BUTTON_DPAD_RIGHT: button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
4709 case GLFW_GAMEPAD_BUTTON_DPAD_DOWN: button = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
4710 case GLFW_GAMEPAD_BUTTON_DPAD_LEFT: button = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
4712 case GLFW_GAMEPAD_BUTTON_LEFT_THUMB: button = GAMEPAD_BUTTON_LEFT_THUMB; break;
4713 case GLFW_GAMEPAD_BUTTON_RIGHT_THUMB: button = GAMEPAD_BUTTON_RIGHT_THUMB; break;
4717 if (button != -1) // Check for valid button
4719 if (buttons[k] == GLFW_PRESS)
4721 CORE.Input.Gamepad.currentState[i][button] = 1;
4722 CORE.Input.Gamepad.lastButtonPressed = button;
4724 else CORE.Input.Gamepad.currentState[i][button] = 0;
4728 // Get current axis state
4729 const float *axes = state.axes;
4731 for (int k = 0; (axes != NULL) && (k < GLFW_GAMEPAD_AXIS_LAST + 1) && (k < MAX_GAMEPAD_AXIS); k++)
4733 CORE.Input.Gamepad.axisState[i][k] = axes[k];
4736 // Register buttons for 2nd triggers (because GLFW doesn't count these as buttons but rather axis)
4737 CORE.Input.Gamepad.currentState[i][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1);
4738 CORE.Input.Gamepad.currentState[i][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[i][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1);
4740 CORE.Input.Gamepad.axisCount = GLFW_GAMEPAD_AXIS_LAST + 1;
4744 CORE.Window.resizedLastFrame = false;
4746 #if defined(SUPPORT_EVENTS_WAITING)
4749 glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
4751 #endif // PLATFORM_DESKTOP
4753 // Gamepad support using emscripten API
4754 // NOTE: GLFW3 joystick functionality not available in web
4755 #if defined(PLATFORM_WEB)
4756 // Get number of gamepads connected
4757 int numGamepads = 0;
4758 if (emscripten_sample_gamepad_data() == EMSCRIPTEN_RESULT_SUCCESS) numGamepads = emscripten_get_num_gamepads();
4760 for (int i = 0; (i < numGamepads) && (i < MAX_GAMEPADS); i++)
4762 // Register previous gamepad button states
4763 for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousState[i][k] = CORE.Input.Gamepad.currentState[i][k];
4765 EmscriptenGamepadEvent gamepadState;
4767 int result = emscripten_get_gamepad_status(i, &gamepadState);
4769 if (result == EMSCRIPTEN_RESULT_SUCCESS)
4771 // Register buttons data for every connected gamepad
4772 for (int j = 0; (j < gamepadState.numButtons) && (j < MAX_GAMEPAD_BUTTONS); j++)
4774 GamepadButton button = -1;
4776 // Gamepad Buttons reference: https://www.w3.org/TR/gamepad/#gamepad-interface
4779 case 0: button = GAMEPAD_BUTTON_RIGHT_FACE_DOWN; break;
4780 case 1: button = GAMEPAD_BUTTON_RIGHT_FACE_RIGHT; break;
4781 case 2: button = GAMEPAD_BUTTON_RIGHT_FACE_LEFT; break;
4782 case 3: button = GAMEPAD_BUTTON_RIGHT_FACE_UP; break;
4783 case 4: button = GAMEPAD_BUTTON_LEFT_TRIGGER_1; break;
4784 case 5: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_1; break;
4785 case 6: button = GAMEPAD_BUTTON_LEFT_TRIGGER_2; break;
4786 case 7: button = GAMEPAD_BUTTON_RIGHT_TRIGGER_2; break;
4787 case 8: button = GAMEPAD_BUTTON_MIDDLE_LEFT; break;
4788 case 9: button = GAMEPAD_BUTTON_MIDDLE_RIGHT; break;
4789 case 10: button = GAMEPAD_BUTTON_LEFT_THUMB; break;
4790 case 11: button = GAMEPAD_BUTTON_RIGHT_THUMB; break;
4791 case 12: button = GAMEPAD_BUTTON_LEFT_FACE_UP; break;
4792 case 13: button = GAMEPAD_BUTTON_LEFT_FACE_DOWN; break;
4793 case 14: button = GAMEPAD_BUTTON_LEFT_FACE_LEFT; break;
4794 case 15: button = GAMEPAD_BUTTON_LEFT_FACE_RIGHT; break;
4798 if (button != -1) // Check for valid button
4800 if (gamepadState.digitalButton[j] == 1)
4802 CORE.Input.Gamepad.currentState[i][button] = 1;
4803 CORE.Input.Gamepad.lastButtonPressed = button;
4805 else CORE.Input.Gamepad.currentState[i][button] = 0;
4808 //TRACELOGD("INPUT: Gamepad %d, button %d: Digital: %d, Analog: %g", gamepadState.index, j, gamepadState.digitalButton[j], gamepadState.analogButton[j]);
4811 // Register axis data for every connected gamepad
4812 for (int j = 0; (j < gamepadState.numAxes) && (j < MAX_GAMEPAD_AXIS); j++)
4814 CORE.Input.Gamepad.axisState[i][j] = gamepadState.axis[j];
4817 CORE.Input.Gamepad.axisCount = gamepadState.numAxes;
4822 #if defined(PLATFORM_ANDROID)
4823 // Register previous keys states
4824 // NOTE: Android supports up to 260 keys
4825 for (int i = 0; i < 260; i++) CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
4827 // Android ALooper_pollAll() variables
4831 // Poll Events (registered events)
4832 // NOTE: Activity is paused if not enabled (CORE.Android.appEnabled)
4833 while ((pollResult = ALooper_pollAll(CORE.Android.appEnabled? 0 : -1, NULL, &pollEvents, (void**)&CORE.Android.source)) >= 0)
4835 // Process this event
4836 if (CORE.Android.source != NULL) CORE.Android.source->process(CORE.Android.app, CORE.Android.source);
4838 // NOTE: Never close window, native activity is controlled by the system!
4839 if (CORE.Android.app->destroyRequested != 0)
4841 //CORE.Window.shouldClose = true;
4842 //ANativeActivity_finish(CORE.Android.app->activity);
4847 #if (defined(PLATFORM_RPI) || defined(PLATFORM_DRM)) && defined(SUPPORT_SSH_KEYBOARD_RPI)
4848 // NOTE: Keyboard reading could be done using input_event(s) reading or just read from stdin,
4849 // we now use both methods inside here. 2nd method is still used for legacy purposes (Allows for input trough SSH console)
4852 // NOTE: Mouse input events polling is done asynchronously in another pthread - EventThread()
4853 // NOTE: Gamepad (Joystick) input events polling is done asynchonously in another pthread - GamepadThread()
4857 // Copy back buffer to front buffers
4858 static void SwapBuffers(void)
4860 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
4861 glfwSwapBuffers(CORE.Window.handle);
4864 #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(PLATFORM_DRM) || defined(PLATFORM_UWP)
4865 eglSwapBuffers(CORE.Window.device, CORE.Window.surface);
4867 #if defined(PLATFORM_DRM)
4868 if (!CORE.Window.gbmSurface || (-1 == CORE.Window.fd) || !CORE.Window.connector || !CORE.Window.crtc)
4870 TRACELOG(LOG_ERROR, "DISPLAY: DRM initialization failed to swap");
4874 struct gbm_bo *bo = gbm_surface_lock_front_buffer(CORE.Window.gbmSurface);
4877 TRACELOG(LOG_ERROR, "DISPLAY: Failed GBM to lock front buffer");
4882 int result = drmModeAddFB(CORE.Window.fd, CORE.Window.connector->modes[CORE.Window.modeIndex].hdisplay,
4883 CORE.Window.connector->modes[CORE.Window.modeIndex].vdisplay, 24, 32, gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, &fb);
4886 TRACELOG(LOG_ERROR, "DISPLAY: drmModeAddFB() failed with result: %d", result);
4890 result = drmModeSetCrtc(CORE.Window.fd, CORE.Window.crtc->crtc_id, fb, 0, 0,
4891 &CORE.Window.connector->connector_id, 1, &CORE.Window.connector->modes[CORE.Window.modeIndex]);
4894 TRACELOG(LOG_ERROR, "DISPLAY: drmModeSetCrtc() failed with result: %d", result);
4898 if (CORE.Window.prevFB)
4900 result = drmModeRmFB(CORE.Window.fd, CORE.Window.prevFB);
4903 TRACELOG(LOG_ERROR, "DISPLAY: drmModeRmFB() failed with result: %d", result);
4907 CORE.Window.prevFB = fb;
4909 if (CORE.Window.prevBO)
4911 gbm_surface_release_buffer(CORE.Window.gbmSurface, CORE.Window.prevBO);
4913 CORE.Window.prevBO = bo;
4914 #endif // PLATFORM_DRM
4915 #endif // PLATFORM_ANDROID || PLATFORM_RPI || PLATFORM_DRM || PLATFORM_UWP
4918 #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
4919 // GLFW3 Error Callback, runs on GLFW3 error
4920 static void ErrorCallback(int error, const char *description)
4922 TRACELOG(LOG_WARNING, "GLFW: Error: %i Description: %s", error, description);
4926 // GLFW3 WindowSize Callback, runs when window is resizedLastFrame
4927 // NOTE: Window resizing not allowed by default
4928 static void WindowSizeCallback(GLFWwindow *window, int width, int height)
4930 SetupViewport(width, height); // Reset viewport and projection matrix for new size
4931 CORE.Window.currentFbo.width = width;
4932 CORE.Window.currentFbo.height = height;
4933 CORE.Window.resizedLastFrame = true;
4935 if (IsWindowFullscreen()) return;
4937 // Set current screen size
4938 CORE.Window.screen.width = width;
4939 CORE.Window.screen.height = height;
4940 // NOTE: Postprocessing texture is not scaled to new size
4944 // GLFW3 WindowIconify Callback, runs when window is minimized/restored
4945 static void WindowIconifyCallback(GLFWwindow *window, int iconified)
4947 if (iconified) CORE.Window.flags |= FLAG_WINDOW_MINIMIZED; // The window was iconified
4948 else CORE.Window.flags &= ~FLAG_WINDOW_MINIMIZED; // The window was restored
4951 #if !defined(PLATFORM_WEB)
4952 // GLFW3 WindowMaximize Callback, runs when window is maximized/restored
4953 static void WindowMaximizeCallback(GLFWwindow *window, int maximized)
4955 if (maximized) CORE.Window.flags |= FLAG_WINDOW_MAXIMIZED; // The window was maximized
4956 else CORE.Window.flags &= ~FLAG_WINDOW_MAXIMIZED; // The window was restored
4960 // GLFW3 WindowFocus Callback, runs when window get/lose focus
4961 static void WindowFocusCallback(GLFWwindow *window, int focused)
4963 if (focused) CORE.Window.flags &= ~FLAG_WINDOW_UNFOCUSED; // The window was focused
4964 else CORE.Window.flags |= FLAG_WINDOW_UNFOCUSED; // The window lost focus
4967 // GLFW3 Keyboard Callback, runs on key pressed
4968 static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
4970 //TRACELOG(LOG_DEBUG, "Key Callback: KEY:%i(%c) - SCANCODE:%i (STATE:%i)", key, key, scancode, action);
4972 if (key == CORE.Input.Keyboard.exitKey && action == GLFW_PRESS)
4974 glfwSetWindowShouldClose(CORE.Window.handle, GLFW_TRUE);
4976 // NOTE: Before closing window, while loop must be left!
4978 #if defined(SUPPORT_SCREEN_CAPTURE)
4979 else if (key == GLFW_KEY_F12 && action == GLFW_PRESS)
4981 #if defined(SUPPORT_GIF_RECORDING)
4982 if (mods == GLFW_MOD_CONTROL)
4986 gifRecording = false;
4988 MsfGifResult result = msf_gif_end(&gifState);
4990 char path[512] = { 0 };
4991 #if defined(PLATFORM_ANDROID)
4992 strcpy(path, CORE.Android.internalDataPath);
4993 strcat(path, TextFormat("./screenrec%03i.gif", screenshotCounter));
4995 strcpy(path, TextFormat("./screenrec%03i.gif", screenshotCounter));
4998 SaveFileData(path, result.data, (unsigned int)result.dataSize);
4999 msf_gif_free(result);
5001 #if defined(PLATFORM_WEB)
5002 // Download file from MEMFS (emscripten memory filesystem)
5003 // saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html
5004 emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1)));
5007 TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
5011 gifRecording = true;
5012 gifFramesCounter = 0;
5014 msf_gif_begin(&gifState, CORE.Window.screen.width, CORE.Window.screen.height);
5015 screenshotCounter++;
5017 TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
5021 #endif // SUPPORT_GIF_RECORDING
5023 TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
5024 screenshotCounter++;
5027 #endif // SUPPORT_SCREEN_CAPTURE
5030 // WARNING: GLFW could return GLFW_REPEAT, we need to consider it as 1
5031 // to work properly with our implementation (IsKeyDown/IsKeyUp checks)
5032 if (action == GLFW_RELEASE) CORE.Input.Keyboard.currentKeyState[key] = 0;
5033 else CORE.Input.Keyboard.currentKeyState[key] = 1;
5035 // Check if there is space available in the key queue
5036 if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
5038 // Add character to the queue
5039 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
5040 CORE.Input.Keyboard.keyPressedQueueCount++;
5045 // GLFW3 Char Key Callback, runs on key down (gets equivalent unicode char value)
5046 static void CharCallback(GLFWwindow *window, unsigned int key)
5048 //TRACELOG(LOG_DEBUG, "Char Callback: KEY:%i(%c)", key, key);
5050 // NOTE: Registers any key down considering OS keyboard layout but
5051 // do not detects action events, those should be managed by user...
5052 // Ref: https://github.com/glfw/glfw/issues/668#issuecomment-166794907
5053 // Ref: https://www.glfw.org/docs/latest/input_guide.html#input_char
5055 // Check if there is space available in the queue
5056 if (CORE.Input.Keyboard.charPressedQueueCount < MAX_KEY_PRESSED_QUEUE)
5058 // Add character to the queue
5059 CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount] = key;
5060 CORE.Input.Keyboard.charPressedQueueCount++;
5064 // GLFW3 Mouse Button Callback, runs on mouse button pressed
5065 static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
5067 // WARNING: GLFW could only return GLFW_PRESS (1) or GLFW_RELEASE (0) for now,
5068 // but future releases may add more actions (i.e. GLFW_REPEAT)
5069 CORE.Input.Mouse.currentButtonState[button] = action;
5071 #if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
5072 // Process mouse events as touches to be able to use mouse-gestures
5073 GestureEvent gestureEvent = { 0 };
5075 // Register touch actions
5076 if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_DOWN;
5077 else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_UP;
5079 // NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
5081 // Assign a pointer ID
5082 gestureEvent.pointerId[0] = 0;
5084 // Register touch points count
5085 gestureEvent.pointCount = 1;
5087 // Register touch points position, only one point registered
5088 gestureEvent.position[0] = GetMousePosition();
5090 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
5091 gestureEvent.position[0].x /= (float)GetScreenWidth();
5092 gestureEvent.position[0].y /= (float)GetScreenHeight();
5094 // Gesture data is sent to gestures system for processing
5095 ProcessGestureEvent(gestureEvent);
5099 // GLFW3 Cursor Position Callback, runs on mouse move
5100 static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
5102 CORE.Input.Mouse.position.x = (float)x;
5103 CORE.Input.Mouse.position.y = (float)y;
5104 CORE.Input.Touch.position[0] = CORE.Input.Mouse.position;
5106 #if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
5107 // Process mouse events as touches to be able to use mouse-gestures
5108 GestureEvent gestureEvent = { 0 };
5110 gestureEvent.touchAction = TOUCH_MOVE;
5112 // Assign a pointer ID
5113 gestureEvent.pointerId[0] = 0;
5115 // Register touch points count
5116 gestureEvent.pointCount = 1;
5118 // Register touch points position, only one point registered
5119 gestureEvent.position[0] = CORE.Input.Touch.position[0];
5121 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
5122 gestureEvent.position[0].x /= (float)GetScreenWidth();
5123 gestureEvent.position[0].y /= (float)GetScreenHeight();
5125 // Gesture data is sent to gestures system for processing
5126 ProcessGestureEvent(gestureEvent);
5130 // GLFW3 Srolling Callback, runs on mouse wheel
5131 static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
5133 CORE.Input.Mouse.currentWheelMove = (float)yoffset;
5136 // GLFW3 CursorEnter Callback, when cursor enters the window
5137 static void CursorEnterCallback(GLFWwindow *window, int enter)
5139 if (enter == true) CORE.Input.Mouse.cursorOnScreen = true;
5140 else CORE.Input.Mouse.cursorOnScreen = false;
5143 // GLFW3 Window Drop Callback, runs when drop files into window
5144 // NOTE: Paths are stored in dynamic memory for further retrieval
5145 // Everytime new files are dropped, old ones are discarded
5146 static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
5148 ClearDroppedFiles();
5150 CORE.Window.dropFilesPath = (char **)RL_MALLOC(sizeof(char *)*count);
5152 for (int i = 0; i < count; i++)
5154 CORE.Window.dropFilesPath[i] = (char *)RL_MALLOC(sizeof(char)*MAX_FILEPATH_LENGTH);
5155 strcpy(CORE.Window.dropFilesPath[i], paths[i]);
5158 CORE.Window.dropFilesCount = count;
5162 #if defined(PLATFORM_ANDROID)
5163 // ANDROID: Process activity lifecycle commands
5164 static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
5172 case APP_CMD_RESUME: break;
5173 case APP_CMD_INIT_WINDOW:
5175 if (app->window != NULL)
5177 if (CORE.Android.contextRebindRequired)
5179 // Reset screen scaling to full display size
5180 EGLint displayFormat;
5181 eglGetConfigAttrib(CORE.Window.device, CORE.Window.config, EGL_NATIVE_VISUAL_ID, &displayFormat);
5182 ANativeWindow_setBuffersGeometry(app->window, CORE.Window.render.width, CORE.Window.render.height, displayFormat);
5184 // Recreate display surface and re-attach OpenGL context
5185 CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, app->window, NULL);
5186 eglMakeCurrent(CORE.Window.device, CORE.Window.surface, CORE.Window.surface, CORE.Window.context);
5188 CORE.Android.contextRebindRequired = false;
5192 CORE.Window.display.width = ANativeWindow_getWidth(CORE.Android.app->window);
5193 CORE.Window.display.height = ANativeWindow_getHeight(CORE.Android.app->window);
5195 // Init graphics device (display device and OpenGL context)
5196 InitGraphicsDevice(CORE.Window.screen.width, CORE.Window.screen.height);
5198 // Init hi-res timer
5201 #if defined(SUPPORT_DEFAULT_FONT)
5202 // Load default font
5203 // NOTE: External function (defined in module: text)
5205 Rectangle rec = GetFontDefault().recs[95];
5206 // NOTE: We setup a 1px padding on char rectangle to avoid pixel bleeding on MSAA filtering
5207 SetShapesTexture(GetFontDefault().texture, (Rectangle){ rec.x + 1, rec.y + 1, rec.width - 2, rec.height - 2 });
5210 // TODO: GPU assets reload in case of lost focus (lost context)
5211 // NOTE: This problem has been solved just unbinding and rebinding context from display
5213 if (assetsReloadRequired)
5215 for (int i = 0; i < assetsCount; i++)
5217 // TODO: Unload old asset if required
5219 // Load texture again to pointed texture
5220 (*textureAsset + i) = LoadTexture(assetPath[i]);
5227 case APP_CMD_GAINED_FOCUS:
5229 CORE.Android.appEnabled = true;
5230 //ResumeMusicStream();
5232 case APP_CMD_PAUSE: break;
5233 case APP_CMD_LOST_FOCUS:
5235 CORE.Android.appEnabled = false;
5236 //PauseMusicStream();
5238 case APP_CMD_TERM_WINDOW:
5240 // Dettach OpenGL context and destroy display surface
5241 // NOTE 1: Detaching context before destroying display surface avoids losing our resources (textures, shaders, VBOs...)
5242 // NOTE 2: In some cases (too many context loaded), OS could unload context automatically... :(
5243 eglMakeCurrent(CORE.Window.device, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
5244 eglDestroySurface(CORE.Window.device, CORE.Window.surface);
5246 CORE.Android.contextRebindRequired = true;
5248 case APP_CMD_SAVE_STATE: break;
5249 case APP_CMD_STOP: break;
5250 case APP_CMD_DESTROY:
5252 // TODO: Finish activity?
5253 //ANativeActivity_finish(CORE.Android.app->activity);
5255 case APP_CMD_CONFIG_CHANGED:
5257 //AConfiguration_fromAssetManager(CORE.Android.app->config, CORE.Android.app->activity->assetManager);
5258 //print_cur_config(CORE.Android.app);
5260 // Check screen orientation here!
5266 // ANDROID: Get input events
5267 static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
5269 // If additional inputs are required check:
5270 // https://developer.android.com/ndk/reference/group/input
5271 // https://developer.android.com/training/game-controllers/controller-input
5273 int type = AInputEvent_getType(event);
5274 int source = AInputEvent_getSource(event);
5276 if (type == AINPUT_EVENT_TYPE_MOTION)
5278 if (((source & AINPUT_SOURCE_JOYSTICK) == AINPUT_SOURCE_JOYSTICK) ||
5279 ((source & AINPUT_SOURCE_GAMEPAD) == AINPUT_SOURCE_GAMEPAD))
5281 // Get first touch position
5282 CORE.Input.Touch.position[0].x = AMotionEvent_getX(event, 0);
5283 CORE.Input.Touch.position[0].y = AMotionEvent_getY(event, 0);
5285 // Get second touch position
5286 CORE.Input.Touch.position[1].x = AMotionEvent_getX(event, 1);
5287 CORE.Input.Touch.position[1].y = AMotionEvent_getY(event, 1);
5289 int32_t keycode = AKeyEvent_getKeyCode(event);
5290 if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN)
5292 CORE.Input.Keyboard.currentKeyState[keycode] = 1; // Key down
5294 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode;
5295 CORE.Input.Keyboard.keyPressedQueueCount++;
5297 else CORE.Input.Keyboard.currentKeyState[keycode] = 0; // Key up
5299 // Stop processing gamepad buttons
5303 else if (type == AINPUT_EVENT_TYPE_KEY)
5305 int32_t keycode = AKeyEvent_getKeyCode(event);
5306 //int32_t AKeyEvent_getMetaState(event);
5308 // Save current button and its state
5309 // NOTE: Android key action is 0 for down and 1 for up
5310 if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN)
5312 CORE.Input.Keyboard.currentKeyState[keycode] = 1; // Key down
5314 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode;
5315 CORE.Input.Keyboard.keyPressedQueueCount++;
5317 else CORE.Input.Keyboard.currentKeyState[keycode] = 0; // Key up
5319 if (keycode == AKEYCODE_POWER)
5321 // Let the OS handle input to avoid app stuck. Behaviour: CMD_PAUSE -> CMD_SAVE_STATE -> CMD_STOP -> CMD_CONFIG_CHANGED -> CMD_LOST_FOCUS
5322 // Resuming Behaviour: CMD_START -> CMD_RESUME -> CMD_CONFIG_CHANGED -> CMD_CONFIG_CHANGED -> CMD_GAINED_FOCUS
5323 // It seems like locking mobile, screen size (CMD_CONFIG_CHANGED) is affected.
5324 // NOTE: AndroidManifest.xml must have <activity android:configChanges="orientation|keyboardHidden|screenSize" >
5325 // Before that change, activity was calling CMD_TERM_WINDOW and CMD_DESTROY when locking mobile, so that was not a normal behaviour
5328 else if ((keycode == AKEYCODE_BACK) || (keycode == AKEYCODE_MENU))
5330 // Eat BACK_BUTTON and AKEYCODE_MENU, just do nothing... and don't let to be handled by OS!
5333 else if ((keycode == AKEYCODE_VOLUME_UP) || (keycode == AKEYCODE_VOLUME_DOWN))
5335 // Set default OS behaviour
5342 CORE.Input.Touch.position[0].x = AMotionEvent_getX(event, 0);
5343 CORE.Input.Touch.position[0].y = AMotionEvent_getY(event, 0);
5345 int32_t action = AMotionEvent_getAction(event);
5346 unsigned int flags = action & AMOTION_EVENT_ACTION_MASK;
5348 if (flags == AMOTION_EVENT_ACTION_DOWN || flags == AMOTION_EVENT_ACTION_MOVE)
5350 CORE.Input.Touch.currentTouchState[MOUSE_LEFT_BUTTON] = 1;
5352 else if (flags == AMOTION_EVENT_ACTION_UP)
5354 CORE.Input.Touch.currentTouchState[MOUSE_LEFT_BUTTON] = 0;
5357 #if defined(SUPPORT_GESTURES_SYSTEM)
5359 GestureEvent gestureEvent;
5361 // Register touch actions
5362 if (flags == AMOTION_EVENT_ACTION_DOWN) gestureEvent.touchAction = TOUCH_DOWN;
5363 else if (flags == AMOTION_EVENT_ACTION_UP) gestureEvent.touchAction = TOUCH_UP;
5364 else if (flags == AMOTION_EVENT_ACTION_MOVE) gestureEvent.touchAction = TOUCH_MOVE;
5366 // Register touch points count
5367 // NOTE: Documentation says pointerCount is Always >= 1,
5368 // but in practice it can be 0 or over a million
5369 gestureEvent.pointCount = AMotionEvent_getPointerCount(event);
5371 // Only enable gestures for 1-3 touch points
5372 if ((gestureEvent.pointCount > 0) && (gestureEvent.pointCount < 4))
5374 // Register touch points id
5375 // NOTE: Only two points registered
5376 gestureEvent.pointerId[0] = AMotionEvent_getPointerId(event, 0);
5377 gestureEvent.pointerId[1] = AMotionEvent_getPointerId(event, 1);
5379 // Register touch points position
5380 gestureEvent.position[0] = (Vector2){ AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) };
5381 gestureEvent.position[1] = (Vector2){ AMotionEvent_getX(event, 1), AMotionEvent_getY(event, 1) };
5383 // Normalize gestureEvent.position[x] for screenWidth and screenHeight
5384 gestureEvent.position[0].x /= (float)GetScreenWidth();
5385 gestureEvent.position[0].y /= (float)GetScreenHeight();
5387 gestureEvent.position[1].x /= (float)GetScreenWidth();
5388 gestureEvent.position[1].y /= (float)GetScreenHeight();
5390 // Gesture data is sent to gestures system for processing
5391 ProcessGestureEvent(gestureEvent);
5399 #if defined(PLATFORM_WEB)
5400 // Register touch input events
5401 static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
5403 for (int i = 0; i < touchEvent->numTouches; i++)
5405 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) CORE.Input.Touch.currentTouchState[i] = 1;
5406 else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) CORE.Input.Touch.currentTouchState[i] = 0;
5409 #if defined(SUPPORT_GESTURES_SYSTEM)
5410 GestureEvent gestureEvent = { 0 };
5412 // Register touch actions
5413 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) gestureEvent.touchAction = TOUCH_DOWN;
5414 else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) gestureEvent.touchAction = TOUCH_UP;
5415 else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) gestureEvent.touchAction = TOUCH_MOVE;
5417 // Register touch points count
5418 gestureEvent.pointCount = touchEvent->numTouches;
5420 // Register touch points id
5421 gestureEvent.pointerId[0] = touchEvent->touches[0].identifier;
5422 gestureEvent.pointerId[1] = touchEvent->touches[1].identifier;
5424 // Register touch points position
5425 // NOTE: Only two points registered
5426 gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
5427 gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY };
5429 double canvasWidth, canvasHeight;
5430 // NOTE: emscripten_get_canvas_element_size() returns canvas.width and canvas.height but
5431 // we are looking for actual CSS size: canvas.style.width and canvas.style.height
5432 //EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
5433 emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
5435 // Normalize gestureEvent.position[x] for CORE.Window.screen.width and CORE.Window.screen.height
5436 gestureEvent.position[0].x *= ((float)GetScreenWidth()/(float)canvasWidth);
5437 gestureEvent.position[0].y *= ((float)GetScreenHeight()/(float)canvasHeight);
5438 gestureEvent.position[1].x *= ((float)GetScreenWidth()/(float)canvasWidth);
5439 gestureEvent.position[1].y *= ((float)GetScreenHeight()/(float)canvasHeight);
5441 CORE.Input.Touch.position[0] = gestureEvent.position[0];
5442 CORE.Input.Touch.position[1] = gestureEvent.position[1];
5444 // Gesture data is sent to gestures system for processing
5445 ProcessGestureEvent(gestureEvent);
5447 // Support only simple touch position
5448 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART)
5450 // Get first touch position
5451 CORE.Input.Touch.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
5453 double canvasWidth, canvasHeight;
5454 //EMSCRIPTEN_RESULT res = emscripten_get_canvas_element_size("#canvas", &canvasWidth, &canvasHeight);
5455 emscripten_get_element_css_size("#canvas", &canvasWidth, &canvasHeight);
5457 // Normalize gestureEvent.position[x] for screenWidth and screenHeight
5458 CORE.Input.Touch.position[0].x *= ((float)GetScreenWidth()/(float)canvasWidth);
5459 CORE.Input.Touch.position[0].y *= ((float)GetScreenHeight()/(float)canvasHeight);
5466 // Register connected/disconnected gamepads events
5467 static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
5470 TRACELOGD("%s: timeStamp: %g, connected: %d, index: %ld, numAxes: %d, numButtons: %d, id: \"%s\", mapping: \"%s\"",
5471 eventType != 0? emscripten_event_type_to_string(eventType) : "Gamepad state",
5472 gamepadEvent->timestamp, gamepadEvent->connected, gamepadEvent->index, gamepadEvent->numAxes, gamepadEvent->numButtons, gamepadEvent->id, gamepadEvent->mapping);
5474 for (int i = 0; i < gamepadEvent->numAxes; ++i) TRACELOGD("Axis %d: %g", i, gamepadEvent->axis[i]);
5475 for (int i = 0; i < gamepadEvent->numButtons; ++i) TRACELOGD("Button %d: Digital: %d, Analog: %g", i, gamepadEvent->digitalButton[i], gamepadEvent->analogButton[i]);
5478 if ((gamepadEvent->connected) && (gamepadEvent->index < MAX_GAMEPADS)) CORE.Input.Gamepad.ready[gamepadEvent->index] = true;
5479 else CORE.Input.Gamepad.ready[gamepadEvent->index] = false;
5481 // TODO: Test gamepadEvent->index
5487 #if defined(PLATFORM_RPI) || defined(PLATFORM_DRM)
5489 #if defined(SUPPORT_SSH_KEYBOARD_RPI)
5490 // Initialize Keyboard system (using standard input)
5491 static void InitKeyboard(void)
5493 // NOTE: We read directly from Standard Input (stdin) - STDIN_FILENO file descriptor
5495 // Make stdin non-blocking (not enough, need to configure to non-canonical mode)
5496 int flags = fcntl(STDIN_FILENO, F_GETFL, 0); // F_GETFL: Get the file access mode and the file status flags
5497 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); // F_SETFL: Set the file status flags to the value specified
5499 // Save terminal keyboard settings and reconfigure terminal with new settings
5500 struct termios keyboardNewSettings;
5501 tcgetattr(STDIN_FILENO, &CORE.Input.Keyboard.defaultSettings); // Get current keyboard settings
5502 keyboardNewSettings = CORE.Input.Keyboard.defaultSettings;
5504 // New terminal settings for keyboard: turn off buffering (non-canonical mode), echo and key processing
5505 // NOTE: ISIG controls if ^C and ^Z generate break signals or not
5506 keyboardNewSettings.c_lflag &= ~(ICANON | ECHO | ISIG);
5507 //keyboardNewSettings.c_iflag &= ~(ISTRIP | INLCR | ICRNL | IGNCR | IXON | IXOFF);
5508 keyboardNewSettings.c_cc[VMIN] = 1;
5509 keyboardNewSettings.c_cc[VTIME] = 0;
5511 // Set new keyboard settings (change occurs immediately)
5512 tcsetattr(STDIN_FILENO, TCSANOW, &keyboardNewSettings);
5514 // NOTE: Reading directly from stdin will give chars already key-mapped by kernel to ASCII or UNICODE
5516 // Save old keyboard mode to restore it at the end
5517 if (ioctl(STDIN_FILENO, KDGKBMODE, &CORE.Input.Keyboard.defaultMode) < 0)
5519 // NOTE: It could mean we are using a remote keyboard through ssh!
5520 TRACELOG(LOG_WARNING, "RPI: Failed to change keyboard mode (SSH keyboard?)");
5524 // We reconfigure keyboard mode to get:
5525 // - scancodes (K_RAW)
5526 // - keycodes (K_MEDIUMRAW)
5527 // - ASCII chars (K_XLATE)
5528 // - UNICODE chars (K_UNICODE)
5529 ioctl(STDIN_FILENO, KDSKBMODE, K_XLATE);
5532 // Register keyboard restore when program finishes
5533 atexit(RestoreKeyboard);
5536 // Process keyboard inputs
5537 // TODO: Most probably input reading and processing should be in a separate thread
5538 static void ProcessKeyboard(void)
5540 #define MAX_KEYBUFFER_SIZE 32 // Max size in bytes to read
5542 // Keyboard input polling (fill keys[256] array with status)
5543 int bufferByteCount = 0; // Bytes available on the buffer
5544 char keysBuffer[MAX_KEYBUFFER_SIZE]; // Max keys to be read at a time
5546 // Read availables keycodes from stdin
5547 bufferByteCount = read(STDIN_FILENO, keysBuffer, MAX_KEYBUFFER_SIZE); // POSIX system call
5549 // Reset pressed keys array (it will be filled below)
5550 if (bufferByteCount > 0) for (int i = 0; i < 512; i++) CORE.Input.Keyboard.currentKeyState[i] = 0;
5552 // Check keys from event input workers (This is the new keyboard reading method)
5553 //for (int i = 0; i < 512; i++) CORE.Input.Keyboard.currentKeyState[i] = CORE.Input.Keyboard.currentKeyStateEvdev[i];
5555 // Fill all read bytes (looking for keys)
5556 for (int i = 0; i < bufferByteCount; i++)
5558 // NOTE: If (key == 0x1b), depending on next key, it could be a special keymap code!
5559 // Up -> 1b 5b 41 / Left -> 1b 5b 44 / Right -> 1b 5b 43 / Down -> 1b 5b 42
5560 if (keysBuffer[i] == 0x1b)
5562 // Detect ESC to stop program
5563 if (bufferByteCount == 1) CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] = 1;
5566 if (keysBuffer[i + 1] == 0x5b) // Special function key
5568 if ((keysBuffer[i + 2] == 0x5b) || (keysBuffer[i + 2] == 0x31) || (keysBuffer[i + 2] == 0x32))
5570 // Process special function keys (F1 - F12)
5571 switch (keysBuffer[i + 3])
5573 case 0x41: CORE.Input.Keyboard.currentKeyState[290] = 1; break; // raylib KEY_F1
5574 case 0x42: CORE.Input.Keyboard.currentKeyState[291] = 1; break; // raylib KEY_F2
5575 case 0x43: CORE.Input.Keyboard.currentKeyState[292] = 1; break; // raylib KEY_F3
5576 case 0x44: CORE.Input.Keyboard.currentKeyState[293] = 1; break; // raylib KEY_F4
5577 case 0x45: CORE.Input.Keyboard.currentKeyState[294] = 1; break; // raylib KEY_F5
5578 case 0x37: CORE.Input.Keyboard.currentKeyState[295] = 1; break; // raylib KEY_F6
5579 case 0x38: CORE.Input.Keyboard.currentKeyState[296] = 1; break; // raylib KEY_F7
5580 case 0x39: CORE.Input.Keyboard.currentKeyState[297] = 1; break; // raylib KEY_F8
5581 case 0x30: CORE.Input.Keyboard.currentKeyState[298] = 1; break; // raylib KEY_F9
5582 case 0x31: CORE.Input.Keyboard.currentKeyState[299] = 1; break; // raylib KEY_F10
5583 case 0x33: CORE.Input.Keyboard.currentKeyState[300] = 1; break; // raylib KEY_F11
5584 case 0x34: CORE.Input.Keyboard.currentKeyState[301] = 1; break; // raylib KEY_F12
5588 if (keysBuffer[i + 2] == 0x5b) i += 4;
5589 else if ((keysBuffer[i + 2] == 0x31) || (keysBuffer[i + 2] == 0x32)) i += 5;
5593 switch (keysBuffer[i + 2])
5595 case 0x41: CORE.Input.Keyboard.currentKeyState[265] = 1; break; // raylib KEY_UP
5596 case 0x42: CORE.Input.Keyboard.currentKeyState[264] = 1; break; // raylib KEY_DOWN
5597 case 0x43: CORE.Input.Keyboard.currentKeyState[262] = 1; break; // raylib KEY_RIGHT
5598 case 0x44: CORE.Input.Keyboard.currentKeyState[263] = 1; break; // raylib KEY_LEFT
5602 i += 3; // Jump to next key
5605 // NOTE: Some keys are not directly keymapped (CTRL, ALT, SHIFT)
5609 else if (keysBuffer[i] == 0x0a) // raylib KEY_ENTER (don't mix with <linux/input.h> KEY_*)
5611 CORE.Input.Keyboard.currentKeyState[257] = 1;
5613 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = 257; // Add keys pressed into queue
5614 CORE.Input.Keyboard.keyPressedQueueCount++;
5616 else if (keysBuffer[i] == 0x7f) // raylib KEY_BACKSPACE
5618 CORE.Input.Keyboard.currentKeyState[259] = 1;
5620 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = 257; // Add keys pressed into queue
5621 CORE.Input.Keyboard.keyPressedQueueCount++;
5625 // Translate lowercase a-z letters to A-Z
5626 if ((keysBuffer[i] >= 97) && (keysBuffer[i] <= 122))
5628 CORE.Input.Keyboard.currentKeyState[(int)keysBuffer[i] - 32] = 1;
5630 else CORE.Input.Keyboard.currentKeyState[(int)keysBuffer[i]] = 1;
5632 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keysBuffer[i]; // Add keys pressed into queue
5633 CORE.Input.Keyboard.keyPressedQueueCount++;
5637 // Check exit key (same functionality as GLFW3 KeyCallback())
5638 if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] == 1) CORE.Window.shouldClose = true;
5640 #if defined(SUPPORT_SCREEN_CAPTURE)
5641 // Check screen capture key (raylib key: KEY_F12)
5642 if (CORE.Input.Keyboard.currentKeyState[301] == 1)
5644 TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
5645 screenshotCounter++;
5650 // Restore default keyboard input
5651 static void RestoreKeyboard(void)
5653 // Reset to default keyboard settings
5654 tcsetattr(STDIN_FILENO, TCSANOW, &CORE.Input.Keyboard.defaultSettings);
5656 // Reconfigure keyboard to default mode
5657 ioctl(STDIN_FILENO, KDSKBMODE, CORE.Input.Keyboard.defaultMode);
5659 #endif // SUPPORT_SSH_KEYBOARD_RPI
5661 // Initialise user input from evdev(/dev/input/event<N>) this means mouse, keyboard or gamepad devices
5662 static void InitEvdevInput(void)
5664 char path[MAX_FILEPATH_LENGTH];
5666 struct dirent *entity;
5668 // Initialise keyboard file descriptor
5669 CORE.Input.Keyboard.fd = -1;
5672 for (int i = 0; i < MAX_TOUCH_POINTS; ++i)
5674 CORE.Input.Touch.position[i].x = -1;
5675 CORE.Input.Touch.position[i].y = -1;
5678 // Reset keyboard key state
5679 for (int i = 0; i < 512; i++) CORE.Input.Keyboard.currentKeyState[i] = 0;
5681 // Open the linux directory of "/dev/input"
5682 directory = opendir(DEFAULT_EVDEV_PATH);
5686 while ((entity = readdir(directory)) != NULL)
5688 if (strncmp("event", entity->d_name, strlen("event")) == 0) // Search for devices named "event*"
5690 sprintf(path, "%s%s", DEFAULT_EVDEV_PATH, entity->d_name);
5691 ConfigureEvdevDevice(path); // Configure the device if appropriate
5695 closedir(directory);
5697 else TRACELOG(LOG_WARNING, "RPI: Failed to open linux event directory: %s", DEFAULT_EVDEV_PATH);
5700 // Identifies a input device and configures it for use if appropriate
5701 static void ConfigureEvdevDevice(char *device)
5703 #define BITS_PER_LONG (8*sizeof(long))
5704 #define NBITS(x) ((((x) - 1)/BITS_PER_LONG) + 1)
5705 #define OFF(x) ((x)%BITS_PER_LONG)
5706 #define BIT(x) (1UL<<OFF(x))
5707 #define LONG(x) ((x)/BITS_PER_LONG)
5708 #define TEST_BIT(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
5710 struct input_absinfo absinfo;
5711 unsigned long evBits[NBITS(EV_MAX)];
5712 unsigned long absBits[NBITS(ABS_MAX)];
5713 unsigned long relBits[NBITS(REL_MAX)];
5714 unsigned long keyBits[NBITS(KEY_MAX)];
5715 bool hasAbs = false;
5716 bool hasRel = false;
5717 bool hasAbsMulti = false;
5718 int freeWorkerId = -1;
5721 InputEventWorker *worker;
5723 // Open the device and allocate worker
5724 //-------------------------------------------------------------------------------------------------------
5725 // Find a free spot in the workers array
5726 for (int i = 0; i < sizeof(CORE.Input.eventWorker)/sizeof(InputEventWorker); ++i)
5728 if (CORE.Input.eventWorker[i].threadId == 0)
5735 // Select the free worker from array
5736 if (freeWorkerId >= 0)
5738 worker = &(CORE.Input.eventWorker[freeWorkerId]); // Grab a pointer to the worker
5739 memset(worker, 0, sizeof(InputEventWorker)); // Clear the worker
5743 TRACELOG(LOG_WARNING, "RPI: Failed to create input device thread for %s, out of worker slots", device);
5748 fd = open(device, O_RDONLY | O_NONBLOCK);
5751 TRACELOG(LOG_WARNING, "RPI: Failed to open input device %s", device);
5756 // Grab number on the end of the devices name "event<N>"
5758 char *ptrDevName = strrchr(device, 't');
5759 worker->eventNum = -1;
5761 if (ptrDevName != NULL)
5763 if (sscanf(ptrDevName, "t%d", &devNum) == 1)
5764 worker->eventNum = devNum;
5767 // At this point we have a connection to the device, but we don't yet know what the device is.
5768 // It could be many things, even as simple as a power button...
5769 //-------------------------------------------------------------------------------------------------------
5771 // Identify the device
5772 //-------------------------------------------------------------------------------------------------------
5773 ioctl(fd, EVIOCGBIT(0, sizeof(evBits)), evBits); // Read a bitfield of the available device properties
5775 // Check for absolute input devices
5776 if (TEST_BIT(evBits, EV_ABS))
5778 ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absBits)), absBits);
5780 // Check for absolute movement support (usualy touchscreens, but also joysticks)
5781 if (TEST_BIT(absBits, ABS_X) && TEST_BIT(absBits, ABS_Y))
5785 // Get the scaling values
5786 ioctl(fd, EVIOCGABS(ABS_X), &absinfo);
5787 worker->absRange.x = absinfo.minimum;
5788 worker->absRange.width = absinfo.maximum - absinfo.minimum;
5789 ioctl(fd, EVIOCGABS(ABS_Y), &absinfo);
5790 worker->absRange.y = absinfo.minimum;
5791 worker->absRange.height = absinfo.maximum - absinfo.minimum;
5794 // Check for multiple absolute movement support (usualy multitouch touchscreens)
5795 if (TEST_BIT(absBits, ABS_MT_POSITION_X) && TEST_BIT(absBits, ABS_MT_POSITION_Y))
5799 // Get the scaling values
5800 ioctl(fd, EVIOCGABS(ABS_X), &absinfo);
5801 worker->absRange.x = absinfo.minimum;
5802 worker->absRange.width = absinfo.maximum - absinfo.minimum;
5803 ioctl(fd, EVIOCGABS(ABS_Y), &absinfo);
5804 worker->absRange.y = absinfo.minimum;
5805 worker->absRange.height = absinfo.maximum - absinfo.minimum;
5809 // Check for relative movement support (usualy mouse)
5810 if (TEST_BIT(evBits, EV_REL))
5812 ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relBits)), relBits);
5814 if (TEST_BIT(relBits, REL_X) && TEST_BIT(relBits, REL_Y)) hasRel = true;
5817 // Check for button support to determine the device type(usualy on all input devices)
5818 if (TEST_BIT(evBits, EV_KEY))
5820 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keyBits)), keyBits);
5822 if (hasAbs || hasAbsMulti)
5824 if (TEST_BIT(keyBits, BTN_TOUCH)) worker->isTouch = true; // This is a touchscreen
5825 if (TEST_BIT(keyBits, BTN_TOOL_FINGER)) worker->isTouch = true; // This is a drawing tablet
5826 if (TEST_BIT(keyBits, BTN_TOOL_PEN)) worker->isTouch = true; // This is a drawing tablet
5827 if (TEST_BIT(keyBits, BTN_STYLUS)) worker->isTouch = true; // This is a drawing tablet
5828 if (worker->isTouch || hasAbsMulti) worker->isMultitouch = true; // This is a multitouch capable device
5833 if (TEST_BIT(keyBits, BTN_LEFT)) worker->isMouse = true; // This is a mouse
5834 if (TEST_BIT(keyBits, BTN_RIGHT)) worker->isMouse = true; // This is a mouse
5837 if (TEST_BIT(keyBits, BTN_A)) worker->isGamepad = true; // This is a gamepad
5838 if (TEST_BIT(keyBits, BTN_TRIGGER)) worker->isGamepad = true; // This is a gamepad
5839 if (TEST_BIT(keyBits, BTN_START)) worker->isGamepad = true; // This is a gamepad
5840 if (TEST_BIT(keyBits, BTN_TL)) worker->isGamepad = true; // This is a gamepad
5841 if (TEST_BIT(keyBits, BTN_TL)) worker->isGamepad = true; // This is a gamepad
5843 if (TEST_BIT(keyBits, KEY_SPACE)) worker->isKeyboard = true; // This is a keyboard
5845 //-------------------------------------------------------------------------------------------------------
5847 // Decide what to do with the device
5848 //-------------------------------------------------------------------------------------------------------
5849 if (worker->isKeyboard && CORE.Input.Keyboard.fd == -1)
5851 // Use the first keyboard encountered. This assumes that a device that says it's a keyboard is just a
5852 // keyboard. The keyboard is polled synchronously, whereas other input devices are polled in separate
5853 // threads so that they don't drop events when the frame rate is slow.
5854 TRACELOG(LOG_INFO, "RPI: Opening keyboard device: %s", device);
5855 CORE.Input.Keyboard.fd = worker->fd;
5857 else if (worker->isTouch || worker->isMouse)
5859 // Looks like an interesting device
5860 TRACELOG(LOG_INFO, "RPI: Opening input device: %s (%s%s%s%s)", device,
5861 worker->isMouse? "mouse " : "",
5862 worker->isMultitouch? "multitouch " : "",
5863 worker->isTouch? "touchscreen " : "",
5864 worker->isGamepad? "gamepad " : "");
5866 // Create a thread for this device
5867 int error = pthread_create(&worker->threadId, NULL, &EventThread, (void *)worker);
5870 TRACELOG(LOG_WARNING, "RPI: Failed to create input device thread: %s (error: %d)", device, error);
5871 worker->threadId = 0;
5875 #if defined(USE_LAST_TOUCH_DEVICE)
5876 // Find touchscreen with the highest index
5877 int maxTouchNumber = -1;
5879 for (int i = 0; i < sizeof(CORE.Input.eventWorker)/sizeof(InputEventWorker); ++i)
5881 if (CORE.Input.eventWorker[i].isTouch && (CORE.Input.eventWorker[i].eventNum > maxTouchNumber)) maxTouchNumber = CORE.Input.eventWorker[i].eventNum;
5884 // Find touchscreens with lower indexes
5885 for (int i = 0; i < sizeof(CORE.Input.eventWorker)/sizeof(InputEventWorker); ++i)
5887 if (CORE.Input.eventWorker[i].isTouch && (CORE.Input.eventWorker[i].eventNum < maxTouchNumber))
5889 if (CORE.Input.eventWorker[i].threadId != 0)
5891 TRACELOG(LOG_WARNING, "RPI: Found duplicate touchscreen, killing touchscreen on event: %d", i);
5892 pthread_cancel(CORE.Input.eventWorker[i].threadId);
5893 close(CORE.Input.eventWorker[i].fd);
5899 else close(fd); // We are not interested in this device
5900 //-------------------------------------------------------------------------------------------------------
5903 static void PollKeyboardEvents(void)
5905 // Scancode to keycode mapping for US keyboards
5906 // TODO: Probably replace this with a keymap from the X11 to get the correct regional map for the keyboard:
5907 // Currently non US keyboards will have the wrong mapping for some keys
5908 static const int keymap_US[] =
5909 { 0,256,49,50,51,52,53,54,55,56,57,48,45,61,259,258,81,87,69,82,84,
5910 89,85,73,79,80,91,93,257,341,65,83,68,70,71,72,74,75,76,59,39,96,
5911 340,92,90,88,67,86,66,78,77,44,46,47,344,332,342,32,280,290,291,
5912 292,293,294,295,296,297,298,299,282,281,327,328,329,333,324,325,
5913 326,334,321,322,323,320,330,0,85,86,300,301,89,90,91,92,93,94,95,
5914 335,345,331,283,346,101,268,265,266,263,262,269,264,267,260,261,
5915 112,113,114,115,116,117,118,119,120,121,122,123,124,125,347,127,
5916 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
5917 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
5918 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
5919 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
5920 192,193,194,0,0,0,0,0,200,201,202,203,204,205,206,207,208,209,210,
5921 211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,
5922 227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,
5923 243,244,245,246,247,248,0,0,0,0,0,0,0, };
5925 int fd = CORE.Input.Keyboard.fd;
5926 if (fd == -1) return;
5928 struct input_event event;
5931 // Try to read data from the keyboard and only continue if successful
5932 while (read(fd, &event, sizeof(event)) == (int)sizeof(event))
5935 if (event.type == EV_KEY)
5937 // Keyboard button parsing
5938 if ((event.code >= 1) && (event.code <= 255)) //Keyboard keys appear for codes 1 to 255
5940 keycode = keymap_US[event.code & 0xFF]; // The code we get is a scancode so we look up the apropriate keycode
5942 // Make sure we got a valid keycode
5943 if ((keycode > 0) && (keycode < sizeof(CORE.Input.Keyboard.currentKeyState)))
5945 // WARNING: https://www.kernel.org/doc/Documentation/input/input.txt
5946 // Event interface: 'value' is the value the event carries. Either a relative change for EV_REL,
5947 // absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat
5948 CORE.Input.Keyboard.currentKeyState[keycode] = (event.value >= 1)? 1 : 0;
5949 if (event.value >= 1)
5951 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode; // Register last key pressed
5952 CORE.Input.Keyboard.keyPressedQueueCount++;
5955 #if defined(SUPPORT_SCREEN_CAPTURE)
5956 // Check screen capture key (raylib key: KEY_F12)
5957 if (CORE.Input.Keyboard.currentKeyState[301] == 1)
5959 TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
5960 screenshotCounter++;
5964 if (CORE.Input.Keyboard.currentKeyState[CORE.Input.Keyboard.exitKey] == 1) CORE.Window.shouldClose = true;
5966 TRACELOGD("RPI: KEY_%s ScanCode: %4i KeyCode: %4i", event.value == 0 ? "UP":"DOWN", event.code, keycode);
5973 // Input device events reading thread
5974 static void *EventThread(void *arg)
5976 struct input_event event;
5977 InputEventWorker *worker = (InputEventWorker *)arg;
5979 int touchAction = -1;
5980 bool gestureUpdate = false;
5982 while (!CORE.Window.shouldClose)
5984 // Try to read data from the device and only continue if successful
5985 while (read(worker->fd, &event, sizeof(event)) == (int)sizeof(event))
5987 // Relative movement parsing
5988 if (event.type == EV_REL)
5990 if (event.code == REL_X)
5992 CORE.Input.Mouse.position.x += event.value;
5993 CORE.Input.Touch.position[0].x = CORE.Input.Mouse.position.x;
5995 #if defined(SUPPORT_GESTURES_SYSTEM)
5996 touchAction = TOUCH_MOVE;
5997 gestureUpdate = true;
6001 if (event.code == REL_Y)
6003 CORE.Input.Mouse.position.y += event.value;
6004 CORE.Input.Touch.position[0].y = CORE.Input.Mouse.position.y;
6006 #if defined(SUPPORT_GESTURES_SYSTEM)
6007 touchAction = TOUCH_MOVE;
6008 gestureUpdate = true;
6012 if (event.code == REL_WHEEL) CORE.Input.Mouse.currentWheelMove += event.value;
6015 // Absolute movement parsing
6016 if (event.type == EV_ABS)
6019 if (event.code == ABS_X)
6021 CORE.Input.Mouse.position.x = (event.value - worker->absRange.x)*CORE.Window.screen.width/worker->absRange.width; // Scale acording to absRange
6022 CORE.Input.Touch.position[0].x = (event.value - worker->absRange.x)*CORE.Window.screen.width/worker->absRange.width; // Scale acording to absRange
6024 #if defined(SUPPORT_GESTURES_SYSTEM)
6025 touchAction = TOUCH_MOVE;
6026 gestureUpdate = true;
6030 if (event.code == ABS_Y)
6032 CORE.Input.Mouse.position.y = (event.value - worker->absRange.y)*CORE.Window.screen.height/worker->absRange.height; // Scale acording to absRange
6033 CORE.Input.Touch.position[0].y = (event.value - worker->absRange.y)*CORE.Window.screen.height/worker->absRange.height; // Scale acording to absRange
6035 #if defined(SUPPORT_GESTURES_SYSTEM)
6036 touchAction = TOUCH_MOVE;
6037 gestureUpdate = true;
6041 // Multitouch movement
6042 if (event.code == ABS_MT_SLOT) worker->touchSlot = event.value; // Remember the slot number for the folowing events
6044 if (event.code == ABS_MT_POSITION_X)
6046 if (worker->touchSlot < MAX_TOUCH_POINTS) CORE.Input.Touch.position[worker->touchSlot].x = (event.value - worker->absRange.x)*CORE.Window.screen.width/worker->absRange.width; // Scale acording to absRange
6049 if (event.code == ABS_MT_POSITION_Y)
6051 if (worker->touchSlot < MAX_TOUCH_POINTS) CORE.Input.Touch.position[worker->touchSlot].y = (event.value - worker->absRange.y)*CORE.Window.screen.height/worker->absRange.height; // Scale acording to absRange
6054 if (event.code == ABS_MT_TRACKING_ID)
6056 if ((event.value < 0) && (worker->touchSlot < MAX_TOUCH_POINTS))
6058 // Touch has ended for this point
6059 CORE.Input.Touch.position[worker->touchSlot].x = -1;
6060 CORE.Input.Touch.position[worker->touchSlot].y = -1;
6065 if (event.code == ABS_PRESSURE)
6067 int previousMouseLeftButtonState = CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_LEFT_BUTTON];
6069 if (!event.value && previousMouseLeftButtonState)
6071 CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_LEFT_BUTTON] = 0;
6073 #if defined(SUPPORT_GESTURES_SYSTEM)
6074 touchAction = TOUCH_UP;
6075 gestureUpdate = true;
6079 if (event.value && !previousMouseLeftButtonState)
6081 CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_LEFT_BUTTON] = 1;
6083 #if defined(SUPPORT_GESTURES_SYSTEM)
6084 touchAction = TOUCH_DOWN;
6085 gestureUpdate = true;
6093 if (event.type == EV_KEY)
6095 // Mouse button parsing
6096 if ((event.code == BTN_TOUCH) || (event.code == BTN_LEFT))
6098 CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_LEFT_BUTTON] = event.value;
6100 #if defined(SUPPORT_GESTURES_SYSTEM)
6101 if (event.value > 0) touchAction = TOUCH_DOWN;
6102 else touchAction = TOUCH_UP;
6103 gestureUpdate = true;
6107 if (event.code == BTN_RIGHT) CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_RIGHT_BUTTON] = event.value;
6108 if (event.code == BTN_MIDDLE) CORE.Input.Mouse.currentButtonStateEvdev[MOUSE_MIDDLE_BUTTON] = event.value;
6111 // Screen confinement
6112 if (!CORE.Input.Mouse.cursorHidden)
6114 if (CORE.Input.Mouse.position.x < 0) CORE.Input.Mouse.position.x = 0;
6115 if (CORE.Input.Mouse.position.x > CORE.Window.screen.width/CORE.Input.Mouse.scale.x) CORE.Input.Mouse.position.x = CORE.Window.screen.width/CORE.Input.Mouse.scale.x;
6117 if (CORE.Input.Mouse.position.y < 0) CORE.Input.Mouse.position.y = 0;
6118 if (CORE.Input.Mouse.position.y > CORE.Window.screen.height/CORE.Input.Mouse.scale.y) CORE.Input.Mouse.position.y = CORE.Window.screen.height/CORE.Input.Mouse.scale.y;
6124 #if defined(SUPPORT_GESTURES_SYSTEM)
6125 GestureEvent gestureEvent = { 0 };
6127 gestureEvent.pointCount = 0;
6128 gestureEvent.touchAction = touchAction;
6130 if (CORE.Input.Touch.position[0].x >= 0) gestureEvent.pointCount++;
6131 if (CORE.Input.Touch.position[1].x >= 0) gestureEvent.pointCount++;
6132 if (CORE.Input.Touch.position[2].x >= 0) gestureEvent.pointCount++;
6133 if (CORE.Input.Touch.position[3].x >= 0) gestureEvent.pointCount++;
6135 gestureEvent.pointerId[0] = 0;
6136 gestureEvent.pointerId[1] = 1;
6137 gestureEvent.pointerId[2] = 2;
6138 gestureEvent.pointerId[3] = 3;
6140 gestureEvent.position[0] = CORE.Input.Touch.position[0];
6141 gestureEvent.position[1] = CORE.Input.Touch.position[1];
6142 gestureEvent.position[2] = CORE.Input.Touch.position[2];
6143 gestureEvent.position[3] = CORE.Input.Touch.position[3];
6145 ProcessGestureEvent(gestureEvent);
6149 Wait(5); // Sleep for 5ms to avoid hogging CPU time
6157 // Init gamepad system
6158 static void InitGamepad(void)
6160 char gamepadDev[128] = "";
6162 for (int i = 0; i < MAX_GAMEPADS; i++)
6164 sprintf(gamepadDev, "%s%i", DEFAULT_GAMEPAD_DEV, i);
6166 if ((CORE.Input.Gamepad.streamId[i] = open(gamepadDev, O_RDONLY|O_NONBLOCK)) < 0)
6168 // NOTE: Only show message for first gamepad
6169 if (i == 0) TRACELOG(LOG_WARNING, "RPI: Failed to open Gamepad device, no gamepad available");
6173 CORE.Input.Gamepad.ready[i] = true;
6175 // NOTE: Only create one thread
6178 int error = pthread_create(&CORE.Input.Gamepad.threadId, NULL, &GamepadThread, NULL);
6180 if (error != 0) TRACELOG(LOG_WARNING, "RPI: Failed to create gamepad input event thread");
6181 else TRACELOG(LOG_INFO, "RPI: Gamepad device initialized successfully");
6187 // Process Gamepad (/dev/input/js0)
6188 static void *GamepadThread(void *arg)
6190 #define JS_EVENT_BUTTON 0x01 // Button pressed/released
6191 #define JS_EVENT_AXIS 0x02 // Joystick axis moved
6192 #define JS_EVENT_INIT 0x80 // Initial state of device
6195 unsigned int time; // event timestamp in milliseconds
6196 short value; // event value
6197 unsigned char type; // event type
6198 unsigned char number; // event axis/button number
6201 // Read gamepad event
6202 struct js_event gamepadEvent;
6204 while (!CORE.Window.shouldClose)
6206 for (int i = 0; i < MAX_GAMEPADS; i++)
6208 if (read(CORE.Input.Gamepad.streamId[i], &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event))
6210 gamepadEvent.type &= ~JS_EVENT_INIT; // Ignore synthetic events
6212 // Process gamepad events by type
6213 if (gamepadEvent.type == JS_EVENT_BUTTON)
6215 //TRACELOG(LOG_WARNING, "RPI: Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
6217 if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS)
6219 // 1 - button pressed, 0 - button released
6220 CORE.Input.Gamepad.currentState[i][gamepadEvent.number] = (int)gamepadEvent.value;
6222 if ((int)gamepadEvent.value == 1) CORE.Input.Gamepad.lastButtonPressed = gamepadEvent.number;
6223 else CORE.Input.Gamepad.lastButtonPressed = -1;
6226 else if (gamepadEvent.type == JS_EVENT_AXIS)
6228 //TRACELOG(LOG_WARNING, "RPI: Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
6230 if (gamepadEvent.number < MAX_GAMEPAD_AXIS)
6232 // NOTE: Scaling of gamepadEvent.value to get values between -1..1
6233 CORE.Input.Gamepad.axisState[i][gamepadEvent.number] = (float)gamepadEvent.value/32768;
6237 else Wait(1); // Sleep for 1 ms to avoid hogging CPU time
6243 #endif // PLATFORM_RPI || PLATFORM_DRM
6245 #if defined(PLATFORM_UWP)
6246 // UWP function pointers
6247 // NOTE: Those pointers are set by UWP App
6248 static UWPQueryTimeFunc uwpQueryTimeFunc = NULL;
6249 static UWPSleepFunc uwpSleepFunc = NULL;
6250 static UWPDisplaySizeFunc uwpDisplaySizeFunc = NULL;
6251 static UWPMouseFunc uwpMouseLockFunc = NULL;
6252 static UWPMouseFunc uwpMouseUnlockFunc = NULL;
6253 static UWPMouseFunc uwpMouseShowFunc = NULL;
6254 static UWPMouseFunc uwpMouseHideFunc = NULL;
6255 static UWPMouseSetPosFunc uwpMouseSetPosFunc = NULL;
6256 static void *uwpCoreWindow = NULL;
6258 // Check all required UWP function pointers have been set
6259 bool UWPIsConfigured()
6263 if (uwpQueryTimeFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetQueryTimeFunc() must be called with a valid function before InitWindow()"); pass = false; }
6264 if (uwpSleepFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetSleepFunc() must be called with a valid function before InitWindow()"); pass = false; }
6265 if (uwpDisplaySizeFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetDisplaySizeFunc() must be called with a valid function before InitWindow()"); pass = false; }
6266 if (uwpMouseLockFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseLockFunc() must be called with a valid function before InitWindow()"); pass = false; }
6267 if (uwpMouseUnlockFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseUnlockFunc() must be called with a valid function before InitWindow()"); pass = false; }
6268 if (uwpMouseShowFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseShowFunc() must be called with a valid function before InitWindow()"); pass = false; }
6269 if (uwpMouseHideFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseHideFunc() must be called with a valid function before InitWindow()"); pass = false; }
6270 if (uwpMouseSetPosFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseSetPosFunc() must be called with a valid function before InitWindow()"); pass = false; }
6271 if (uwpCoreWindow == NULL) { TRACELOG(LOG_ERROR, "UWP: A pointer to the UWP core window must be set before InitWindow()"); pass = false; }
6276 // UWP function handlers get/set
6277 void UWPSetDataPath(const char* path) { CORE.UWP.internalDataPath = path; }
6278 UWPQueryTimeFunc UWPGetQueryTimeFunc(void) { return uwpQueryTimeFunc; }
6279 void UWPSetQueryTimeFunc(UWPQueryTimeFunc func) { uwpQueryTimeFunc = func; }
6280 UWPSleepFunc UWPGetSleepFunc(void) { return uwpSleepFunc; }
6281 void UWPSetSleepFunc(UWPSleepFunc func) { uwpSleepFunc = func; }
6282 UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void) { return uwpDisplaySizeFunc; }
6283 void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func) { uwpDisplaySizeFunc = func; }
6284 UWPMouseFunc UWPGetMouseLockFunc() { return uwpMouseLockFunc; }
6285 void UWPSetMouseLockFunc(UWPMouseFunc func) { uwpMouseLockFunc = func; }
6286 UWPMouseFunc UWPGetMouseUnlockFunc() { return uwpMouseUnlockFunc; }
6287 void UWPSetMouseUnlockFunc(UWPMouseFunc func) { uwpMouseUnlockFunc = func; }
6288 UWPMouseFunc UWPGetMouseShowFunc() { return uwpMouseShowFunc; }
6289 void UWPSetMouseShowFunc(UWPMouseFunc func) { uwpMouseShowFunc = func; }
6290 UWPMouseFunc UWPGetMouseHideFunc() { return uwpMouseHideFunc; }
6291 void UWPSetMouseHideFunc(UWPMouseFunc func) { uwpMouseHideFunc = func; }
6292 UWPMouseSetPosFunc UWPGetMouseSetPosFunc() { return uwpMouseSetPosFunc; }
6293 void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func) { uwpMouseSetPosFunc = func; }
6295 void *UWPGetCoreWindowPtr() { return uwpCoreWindow; }
6296 void UWPSetCoreWindowPtr(void* ptr) { uwpCoreWindow = ptr; }
6297 void UWPMouseWheelEvent(int deltaY) { CORE.Input.Mouse.currentWheelMove = (float)deltaY; }
6299 void UWPKeyDownEvent(int key, bool down, bool controlKey)
6301 if (key == CORE.Input.Keyboard.exitKey && down)
6303 // Time to close the window.
6304 CORE.Window.shouldClose = true;
6306 #if defined(SUPPORT_SCREEN_CAPTURE)
6307 else if (key == KEY_F12 && down)
6309 #if defined(SUPPORT_GIF_RECORDING)
6314 gifRecording = false;
6316 MsfGifResult result = msf_gif_end(&gifState);
6318 SaveFileData(TextFormat("%s/screenrec%03i.gif", CORE.UWP.internalDataPath, screenshotCounter), result.data, result.dataSize);
6319 msf_gif_free(result);
6321 #if defined(PLATFORM_WEB)
6322 // Download file from MEMFS (emscripten memory filesystem)
6323 // saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html
6324 emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1)));
6326 TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
6330 gifRecording = true;
6331 gifFramesCounter = 0;
6333 msf_gif_begin(&gifState, CORE.Window.screen.width, CORE.Window.screen.height);
6334 screenshotCounter++;
6336 TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
6340 #endif // SUPPORT_GIF_RECORDING
6342 TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
6343 screenshotCounter++;
6346 #endif // SUPPORT_SCREEN_CAPTURE
6349 CORE.Input.Keyboard.currentKeyState[key] = down;
6353 void UWPKeyCharEvent(int key)
6355 if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE)
6357 // Add character to the queue
6358 CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
6359 CORE.Input.Keyboard.keyPressedQueueCount++;
6363 void UWPMouseButtonEvent(int button, bool down)
6365 CORE.Input.Mouse.currentButtonState[button] = down;
6367 #if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
6368 // Process mouse events as touches to be able to use mouse-gestures
6369 GestureEvent gestureEvent = { 0 };
6371 // Register touch actions
6372 if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_DOWN;
6373 else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_UP;
6375 // NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
6377 // Assign a pointer ID
6378 gestureEvent.pointerId[0] = 0;
6380 // Register touch points count
6381 gestureEvent.pointCount = 1;
6383 // Register touch points position, only one point registered
6384 gestureEvent.position[0] = GetMousePosition();
6386 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
6387 gestureEvent.position[0].x /= (float)GetScreenWidth();
6388 gestureEvent.position[0].y /= (float)GetScreenHeight();
6390 // Gesture data is sent to gestures system for processing
6391 ProcessGestureEvent(gestureEvent);
6395 void UWPMousePosEvent(double x, double y)
6397 CORE.Input.Mouse.position.x = (float)x;
6398 CORE.Input.Mouse.position.y = (float)y;
6399 CORE.Input.Touch.position[0] = CORE.Input.Mouse.position;
6401 #if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
6402 // Process mouse events as touches to be able to use mouse-gestures
6403 GestureEvent gestureEvent = { 0 };
6405 gestureEvent.touchAction = TOUCH_MOVE;
6407 // Assign a pointer ID
6408 gestureEvent.pointerId[0] = 0;
6410 // Register touch points count
6411 gestureEvent.pointCount = 1;
6413 // Register touch points position, only one point registered
6414 gestureEvent.position[0] = CORE.Input.Mouse.position;
6416 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
6417 gestureEvent.position[0].x /= (float)GetScreenWidth();
6418 gestureEvent.position[0].y /= (float)GetScreenHeight();
6420 // Gesture data is sent to gestures system for processing
6421 ProcessGestureEvent(gestureEvent);
6425 void UWPResizeEvent(int width, int height)
6427 SetupViewport(width, height); // Reset viewport and projection matrix for new size
6429 // Set current screen size
6430 CORE.Window.screen.width = width;
6431 CORE.Window.screen.height = height;
6432 CORE.Window.currentFbo.width = width;
6433 CORE.Window.currentFbo.height = height;
6435 // NOTE: Postprocessing texture is not scaled to new size
6437 CORE.Window.resizedLastFrame = true;
6440 void UWPActivateGamepadEvent(int gamepad, bool active)
6442 if (gamepad < MAX_GAMEPADS) CORE.Input.Gamepad.ready[gamepad] = active;
6445 void UWPRegisterGamepadButton(int gamepad, int button, bool down)
6447 if (gamepad < MAX_GAMEPADS)
6449 if (button < MAX_GAMEPAD_BUTTONS)
6451 CORE.Input.Gamepad.currentState[gamepad][button] = down;
6452 CORE.Input.Gamepad.lastButtonPressed = button;
6457 void UWPRegisterGamepadAxis(int gamepad, int axis, float value)
6459 if (gamepad < MAX_GAMEPADS)
6461 if (axis < MAX_GAMEPAD_AXIS) CORE.Input.Gamepad.axisState[gamepad][axis] = value;
6465 void UWPGestureMove(int pointer, float x, float y)
6467 #if defined(SUPPORT_GESTURES_SYSTEM)
6468 GestureEvent gestureEvent = { 0 };
6470 // Assign the pointer ID and touch action
6471 gestureEvent.pointerId[0] = pointer;
6472 gestureEvent.touchAction = TOUCH_MOVE;
6474 // Register touch points count
6475 gestureEvent.pointCount = 1;
6477 // Register touch points position, only one point registered
6478 gestureEvent.position[0].x = x;
6479 gestureEvent.position[0].y = y;
6481 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
6482 gestureEvent.position[0].x /= (float)GetScreenWidth();
6483 gestureEvent.position[0].y /= (float)GetScreenHeight();
6485 // Gesture data is sent to gestures system for processing
6486 ProcessGestureEvent(gestureEvent);
6490 void UWPGestureTouch(int pointer, float x, float y, bool touch)
6492 #if defined(SUPPORT_GESTURES_SYSTEM)
6493 GestureEvent gestureEvent = { 0 };
6495 // Assign the pointer ID and touch action
6496 gestureEvent.pointerId[0] = pointer;
6497 gestureEvent.touchAction = touch ? TOUCH_DOWN : TOUCH_UP;
6499 // Register touch points count
6500 gestureEvent.pointCount = 1;
6502 // Register touch points position, only one point registered
6503 gestureEvent.position[0].x = x;
6504 gestureEvent.position[0].y = y;
6506 // Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
6507 gestureEvent.position[0].x /= (float)GetScreenWidth();
6508 gestureEvent.position[0].y /= (float)GetScreenHeight();
6510 // Gesture data is sent to gestures system for processing
6511 ProcessGestureEvent(gestureEvent);
6515 #endif // PLATFORM_UWP
6517 #if defined(PLATFORM_DRM)
6518 // Search matching DRM mode in connector's mode list
6519 static int FindMatchingConnectorMode(const drmModeConnector *connector, const drmModeModeInfo *mode)
6521 if (NULL == connector) return -1;
6522 if (NULL == mode) return -1;
6524 // safe bitwise comparison of two modes
6525 #define BINCMP(a, b) memcmp((a), (b), (sizeof(a) < sizeof(b)) ? sizeof(a) : sizeof(b))
6527 for (size_t i = 0; i < connector->count_modes; i++)
6529 TRACELOG(LOG_TRACE, "DISPLAY: DRM mode: %d %ux%u@%u %s", i, connector->modes[i].hdisplay, connector->modes[i].vdisplay,
6530 connector->modes[i].vrefresh, (connector->modes[i].flags & DRM_MODE_FLAG_INTERLACE) ? "interlaced" : "progressive");
6532 if (0 == BINCMP(&CORE.Window.crtc->mode, &CORE.Window.connector->modes[i])) return i;
6540 // Search exactly matching DRM connector mode in connector's list
6541 static int FindExactConnectorMode(const drmModeConnector *connector, uint width, uint height, uint fps, bool allowInterlaced)
6543 TRACELOG(LOG_TRACE, "DISPLAY: Searching exact connector mode for %ux%u@%u, selecting an interlaced mode is allowed: %s", width, height, fps, allowInterlaced ? "yes" : "no");
6545 if (NULL == connector) return -1;
6547 for (int i = 0; i < CORE.Window.connector->count_modes; i++)
6549 const drmModeModeInfo *const mode = &CORE.Window.connector->modes[i];
6551 TRACELOG(LOG_TRACE, "DISPLAY: DRM Mode %d %ux%u@%u %s", i, mode->hdisplay, mode->vdisplay, mode->vrefresh, (mode->flags & DRM_MODE_FLAG_INTERLACE) ? "interlaced" : "progressive");
6553 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && (!allowInterlaced)) continue;
6555 if ((mode->hdisplay == width) && (mode->vdisplay == height) && (mode->vrefresh == fps)) return i;
6558 TRACELOG(LOG_TRACE, "DISPLAY: No DRM exact matching mode found");
6562 // Search the nearest matching DRM connector mode in connector's list
6563 static int FindNearestConnectorMode(const drmModeConnector *connector, uint width, uint height, uint fps, bool allowInterlaced)
6565 TRACELOG(LOG_TRACE, "DISPLAY: Searching nearest connector mode for %ux%u@%u, selecting an interlaced mode is allowed: %s", width, height, fps, allowInterlaced ? "yes" : "no");
6567 if (NULL == connector) return -1;
6569 int nearestIndex = -1;
6570 for (int i = 0; i < CORE.Window.connector->count_modes; i++)
6572 const drmModeModeInfo *const mode = &CORE.Window.connector->modes[i];
6574 TRACELOG(LOG_TRACE, "DISPLAY: DRM mode: %d %ux%u@%u %s", i, mode->hdisplay, mode->vdisplay, mode->vrefresh,
6575 (mode->flags & DRM_MODE_FLAG_INTERLACE) ? "interlaced" : "progressive");
6577 if ((mode->hdisplay < width) || (mode->vdisplay < height) | (mode->vrefresh < fps))
6579 TRACELOG(LOG_TRACE, "DISPLAY: DRM mode is too small");
6583 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && (!allowInterlaced))
6585 TRACELOG(LOG_TRACE, "DISPLAY: DRM shouldn't choose an interlaced mode");
6589 if ((mode->hdisplay >= width) && (mode->vdisplay >= height) && (mode->vrefresh >= fps))
6591 const int widthDiff = mode->hdisplay - width;
6592 const int heightDiff = mode->vdisplay - height;
6593 const int fpsDiff = mode->vrefresh - fps;
6595 if (nearestIndex < 0)
6601 const int nearestWidthDiff = CORE.Window.connector->modes[nearestIndex].hdisplay - width;
6602 const int nearestHeightDiff = CORE.Window.connector->modes[nearestIndex].vdisplay - height;
6603 const int nearestFpsDiff = CORE.Window.connector->modes[nearestIndex].vrefresh - fps;
6605 if ((widthDiff < nearestWidthDiff) || (heightDiff < nearestHeightDiff) || (fpsDiff < nearestFpsDiff)) nearestIndex = i;
6609 return nearestIndex;