From 3948656d84da2e02ba4c2fda80ac813f6c81813e Mon Sep 17 00:00:00 2001 From: Konrad Gutvik Grande Date: Fri, 7 Jun 2024 10:21:02 +0200 Subject: [PATCH 1/3] The example always showed a xbox controller, never a ps3 controller (#4040) --- examples/core/core_input_gamepad.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/core/core_input_gamepad.c b/examples/core/core_input_gamepad.c index 843d5d90..656f7c24 100644 --- a/examples/core/core_input_gamepad.c +++ b/examples/core/core_input_gamepad.c @@ -22,7 +22,7 @@ // NOTE: Gamepad name ID depends on drivers and OS #define XBOX360_LEGACY_NAME_ID "Xbox Controller" #define XBOX360_NAME_ID "Xbox 360 Controller" -#define PS3_NAME_ID "PLAYSTATION(R)3 Controller" +#define PS3_NAME_ID "Sony PLAYSTATION(R)3 Controller" //------------------------------------------------------------------------------------ // Program main entry point @@ -67,7 +67,7 @@ int main(void) { DrawText(TextFormat("GP%d: %s", gamepad, GetGamepadName(gamepad)), 10, 10, 10, BLACK); - if (true) + if (TextIsEqual(GetGamepadName(gamepad), XBOX360_LEGACY_NAME_ID) || TextIsEqual(GetGamepadName(gamepad), XBOX360_NAME_ID)) { DrawTexture(texXboxPad, 0, 0, DARKGRAY); From 7b92b5bde77f857dbf25a2e0aaacb79f592c18b2 Mon Sep 17 00:00:00 2001 From: fruzitent <40008647+fruzitent@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:22:24 +0300 Subject: [PATCH 2/3] fix: infer CMAKE_MODULE_PATH in super-build (#4042) --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57719691..a5e3408c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ cmake_policy(SET CMP0063 NEW) # Directory for easier includes # Anywhere you see include(...) you can check /cmake for that file -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # RAYLIB_IS_MAIN determines whether the project is being used from root # or if it is added as a dependency (through add_subdirectory for example). @@ -51,7 +51,7 @@ add_subdirectory(src raylib) # Uninstall target if(NOT TARGET uninstall) configure_file( - "${CMAKE_MODULE_PATH}/Uninstall.cmake" + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Uninstall.cmake" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY) From 8c712f82d12c08fd4710468d908be765ee54e748 Mon Sep 17 00:00:00 2001 From: Colleague Riley Date: Sat, 8 Jun 2024 16:26:46 -0400 Subject: [PATCH 3/3] Update RGFW (#4048) * Fix Makefile issues (RGFW) (linux) (macOS) * Do not use nanosleep on windows at all (PLATFORM_DESKTOP_RGFW) * remove #define RGFWDEF and make the #undefs only happen for their OS * Update RGFW.h * fix to match the RGFW updates * remove line that shows the cursor for no reason --- src/external/RGFW.h | 882 ++++++++++++++++++----------- src/platforms/rcore_desktop_rgfw.c | 8 +- 2 files changed, 561 insertions(+), 329 deletions(-) diff --git a/src/external/RGFW.h b/src/external/RGFW.h index 67c67e91..a0ca21ce 100644 --- a/src/external/RGFW.h +++ b/src/external/RGFW.h @@ -34,7 +34,9 @@ #define RGFW_BUFFER - (optional) just draw directly to (RGFW) window pixel buffer that is drawn to screen (the buffer is in the RGBA format) #define RGFW_EGL - (optional) use EGL for loading an OpenGL context (instead of the system's opengl api) #define RGFW_OPENGL_ES1 - (optional) use EGL to load and use Opengl ES (version 1) for backend rendering (instead of the system's opengl api) + This version doesn't work for desktops (I'm pretty sure) #define RGFW_OPENGL_ES2 - (optional) use OpenGL ES (version 2) + #define RGFW_OPENGL_ES3 - (optional) use OpenGL ES (version 3) #define RGFW_VULKAN - (optional) use vulkan for the rendering backend (rather than opengl) #define RGFW_DIRECTX - (optional) use directX for the rendering backend (rather than opengl) (windows only, defaults to opengl for unix) #define RGFW_NO_API - (optional) don't use any rendering API (no opengl, no vulkan, no directX) @@ -90,12 +92,16 @@ #ifndef RGFWDEF #ifdef __APPLE__ -#define RGFWDEF extern inline +#define RGFWDEF static inline #else #define RGFWDEF inline #endif #endif +#ifndef RGFW_UNUSED +#define RGFW_UNUSED(x) if (x){} +#endif + #ifdef __cplusplus extern "C" { #endif @@ -106,7 +112,7 @@ extern "C" { #define RGFW_HEADER #if !defined(u8) -#include + #include typedef uint8_t u8; typedef int8_t i8; @@ -130,12 +136,16 @@ extern "C" { #define RGFW_WINDOWS -#if defined(_WIN32) +#if defined(_WIN32) && !defined(WIN32) #define WIN32 #endif #if defined(_WIN64) + +#ifndef WIN64 #define WIN64 +#endif + #define _AMD64_ #undef _X86_ #else @@ -163,7 +173,7 @@ extern "C" { #define RGFW_MACOS #endif -#if (defined(RGFW_OPENGL_ES1) || defined(RGFW_OPENGL_ES2)) && !defined(RGFW_EGL) +#if (defined(RGFW_OPENGL_ES1) || defined(RGFW_OPENGL_ES2) || defined(RGFW_OPENGL_ES3)) && !defined(RGFW_EGL) #define RGFW_EGL #endif #if defined(RGFW_EGL) && defined(__APPLE__) @@ -193,7 +203,7 @@ extern "C" { #include #endif -#if defined(RGFW_X11) && defined(RGFW_OPENGL) +#if defined(RGFW_X11) && (defined(RGFW_OPENGL)) #ifndef GLX_MESA_swap_control #define GLX_MESA_swap_control #endif @@ -384,10 +394,10 @@ typedef struct { i32 x, y; } RGFW_vector; RGFW_vector point; /*!< mouse x, y of event (or drop point) */ u32 keyCode; /*!< keycode of event !!Keycodes defined at the bottom of the header file!! */ - u32 inFocus; /*if the window is in focus or not*/ - u32 fps; /*the current fps of the window [the fps is checked when events are checked]*/ - u32 current_ticks, frames; /* this is used for counting the fps */ + u64 frameTime, frameTime2; /* this is used for counting the fps */ + + u8 inFocus; /*if the window is in focus or not*/ u8 lockState; @@ -416,9 +426,10 @@ typedef struct { i32 x, y; } RGFW_vector; u32 display; void* displayLink; void* window; + u8 dndPassed; #endif -#if defined(RGFW_OPENGL) && !defined(RGFW_OSMESA) +#if (defined(RGFW_OPENGL)) && !defined(RGFW_OSMESA) #ifdef RGFW_MACOS void* rSurf; /*!< source graphics context */ #endif @@ -460,6 +471,7 @@ typedef struct { i32 x, y; } RGFW_vector; #ifdef RGFW_EGL EGLSurface EGL_surface; EGLDisplay EGL_display; + EGLContext EGL_context; #endif #if defined(RGFW_OSMESA) || defined(RGFW_BUFFER) @@ -508,7 +520,7 @@ typedef struct { i32 x, y; } RGFW_vector; RGFW_rect r; /* the x, y, w and h of the struct */ - u8 fpsCap; /*!< the fps cap of the window should run at (change this var to change the fps cap, 0 = no limit)*/ + u32 fpsCap; /*!< the fps cap of the window should run at (change this var to change the fps cap, 0 = no limit)*/ /*[the fps is capped when events are checked]*/ } RGFW_window; /*!< Window structure for managing the window */ @@ -627,7 +639,6 @@ typedef struct { i32 x, y; } RGFW_vector; this is run by default if the user uses the arg `RGFW_SCALE_TO_MONITOR` during window creation */ RGFWDEF void RGFW_window_scaleToMonitor(RGFW_window* win); - /* get the struct of the window's monitor */ RGFWDEF RGFW_monitor RGFW_window_getMonitor(RGFW_window* win); @@ -635,11 +646,16 @@ typedef struct { i32 x, y; } RGFW_vector; RGFWDEF void RGFW_window_makeCurrent(RGFW_window* win); /*error handling*/ - RGFWDEF u8 RGFW_Error(); /* returns true if an error has occurred (doesn't print errors itself) */ + RGFWDEF u8 RGFW_Error(void); /* returns true if an error has occurred (doesn't print errors itself) */ /*!< if window == NULL, it checks if the key is pressed globally. Otherwise, it checks only if the key is pressed while the window in focus.*/ RGFWDEF u8 RGFW_isPressedI(RGFW_window* win, u32 key); /*!< if key is pressed (key code)*/ + RGFWDEF u8 RGFW_wasPressedI(RGFW_window* win, u32 key); /*!< if key was pressed (checks prev keymap only) (key code)*/ + + RGFWDEF u8 RGFW_isHeldI(RGFW_window* win, u32 key); /*!< if key is held (key code)*/ + RGFWDEF u8 RGFW_isReleasedI(RGFW_window* win, u32 key); /*!< if key is released (key code)*/ + /* !!Keycodes defined at the bottom of the header file!! */ @@ -676,7 +692,14 @@ typedef struct { i32 x, y; } RGFW_vector; if you're going to use sili which is a good idea generally */ - RGFWDEF RGFW_thread RGFW_createThread(void* (*function_ptr)(void*), void* args); /*!< create a thread*/ + + #if defined(__unix__) || defined(__APPLE__) + typedef void* (* RGFW_threadFunc_ptr)(void*); + #else + typedef DWORD (* RGFW_threadFunc_ptr)(void*); + #endif + + RGFWDEF RGFW_thread RGFW_createThread(RGFW_threadFunc_ptr ptr, void* args); /*!< create a thread*/ RGFWDEF void RGFW_cancelThread(RGFW_thread thread); /*!< cancels a thread*/ RGFWDEF void RGFW_joinThread(RGFW_thread thread); /*!< join thread to current thread */ RGFWDEF void RGFW_setThreadPriority(RGFW_thread thread, u8 priority); /*!< sets the priority priority */ @@ -694,7 +717,7 @@ typedef struct { i32 x, y; } RGFW_vector; /*! native opengl functions */ #ifdef RGFW_OPENGL /*! Get max OpenGL version */ - RGFWDEF u8* RGFW_getMaxGLVersion(); + RGFWDEF u8* RGFW_getMaxGLVersion(void); /* OpenGL init hints */ RGFWDEF void RGFW_setGLStencil(i32 stencil); /* set stencil buffer bit size (8 by default) */ RGFWDEF void RGFW_setGLSamples(i32 samples); /* set number of sampiling buffers (4 by default) */ @@ -754,8 +777,8 @@ typedef struct { i32 x, y; } RGFW_vector; RGFWDEF void RGFW_createSurface(VkInstance instance, RGFW_window* win); int RGFW_deviceInitialization(RGFW_window* win); int RGFW_createSwapchain(RGFW_window* win); - RGFWDEF int RGFW_createRenderPass(); - int RGFW_createCommandPool(); + RGFWDEF int RGFW_createRenderPass(void); + int RGFW_createCommandPool(void); int RGFW_createCommandBuffers(RGFW_window* win); int RGFW_createSyncObjects(RGFW_window* win); RGFWDEF int RGFW_createFramebuffers(RGFW_window* win); @@ -780,8 +803,7 @@ typedef struct { i32 x, y; } RGFW_vector; RGFWDEF void RGFW_window_checkFPS(RGFW_window* win); /*!< updates fps / sets fps to cap (ran by RGFW_window_checkEvent)*/ RGFWDEF u64 RGFW_getTime(void); /* get time in seconds */ RGFWDEF u64 RGFW_getTimeNS(void); /* get time in nanoseconds */ - RGFWDEF u32 RGFW_getFPS(void); /* get current FPS (win->event.fps) */ - RGFWDEF void RGFW_sleep(u32 microsecond); /* sleep for a set time */ + RGFWDEF void RGFW_sleep(u64 microsecond); /* sleep for a set time */ #endif /* RGFW_HEADER */ /* @@ -859,20 +881,19 @@ typedef struct { i32 x, y; } RGFW_vector; #ifdef RGFW_WINDOWS -#define WIN32_LEAN_AND_MEAN - #include #endif #ifdef RGFW_MACOS -#include - /* based on silicon.h */ +#ifndef GL_SILENCE_DEPRECATION #define GL_SILENCE_DEPRECATION +#endif + #include #include #include @@ -914,7 +935,7 @@ typedef struct { i32 x, y; } RGFW_vector; #define abi_objc_msgSend_fpret objc_msgSend_fpret #endif -#define NSAlloc(nsclass) objc_msgSend_id(nsclass, sel_registerName("alloc")) +#define NSAlloc(nsclass) objc_msgSend_id((id)nsclass, sel_registerName("alloc")) #define objc_msgSend_bool ((BOOL (*)(id, SEL))objc_msgSend) #define objc_msgSend_void ((void (*)(id, SEL))objc_msgSend) #define objc_msgSend_void_id ((void (*)(id, SEL, id))objc_msgSend) @@ -948,7 +969,7 @@ typedef struct { i32 x, y; } RGFW_vector; loadFunc("stringWithUTF8String:"); return ((id(*)(id, SEL, const char*))objc_msgSend) - (objc_getClass("NSString"), func, str); + ((id)objc_getClass("NSString"), func, str); } const char* NSString_to_char(NSString* str) { @@ -1053,7 +1074,7 @@ typedef struct { i32 x, y; } RGFW_vector; void* func = sel_registerName("initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bitmapFormat:bytesPerRow:bitsPerPixel:"); return (NSBitmapImageRep*) ((id(*)(id, SEL, unsigned char**, NSInteger, NSInteger, NSInteger, NSInteger, bool, bool, const char*, NSBitmapFormat, NSInteger, NSInteger))objc_msgSend) - (NSAlloc(objc_getClass("NSBitmapImageRep")), func, planes, width, height, bps, spp, alpha, isPlanar, NSString_stringWithUTF8String(colorSpaceName), bitmapFormat, rowBytes, pixelBits); + (NSAlloc((id)objc_getClass("NSBitmapImageRep")), func, planes, width, height, bps, spp, alpha, isPlanar, NSString_stringWithUTF8String(colorSpaceName), bitmapFormat, rowBytes, pixelBits); } NSColor* NSColor_colorWithSRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) { @@ -1079,7 +1100,7 @@ typedef struct { i32 x, y; } RGFW_vector; NSImage* NSImage_initWithSize(NSSize size) { void* func = sel_registerName("initWithSize:"); return ((id(*)(id, SEL, NSSize))objc_msgSend) - (NSAlloc(objc_getClass("NSImage")), func, size); + (NSAlloc((id)objc_getClass("NSImage")), func, size); } #define NS_OPENGL_ENUM_DEPRECATED(minVers, maxVers) API_AVAILABLE(macos(minVers)) typedef NS_ENUM(NSInteger, NSOpenGLContextParameter) { @@ -1111,13 +1132,13 @@ typedef struct { i32 x, y; } RGFW_vector; void* NSOpenGLPixelFormat_initWithAttributes(const uint32_t* attribs) { void* func = sel_registerName("initWithAttributes:"); return (void*) ((id(*)(id, SEL, const uint32_t*))objc_msgSend) - (NSAlloc(objc_getClass("NSOpenGLPixelFormat")), func, attribs); + (NSAlloc((id)objc_getClass("NSOpenGLPixelFormat")), func, attribs); } NSOpenGLView* NSOpenGLView_initWithFrame(NSRect frameRect, uint32_t* format) { void* func = sel_registerName("initWithFrame:pixelFormat:"); return (NSOpenGLView*) ((id(*)(id, SEL, NSRect, uint32_t*))objc_msgSend) - (NSAlloc(objc_getClass("NSOpenGLView")), func, frameRect, format); + (NSAlloc((id)objc_getClass("NSOpenGLView")), func, frameRect, format); } void NSCursor_performSelector(NSCursor* cursor, void* selector) { @@ -1126,7 +1147,7 @@ typedef struct { i32 x, y; } RGFW_vector; } NSPasteboard* NSPasteboard_generalPasteboard(void) { - return (NSPasteboard*) objc_msgSend_id(objc_getClass("NSPasteboard"), sel_registerName("generalPasteboard")); + return (NSPasteboard*) objc_msgSend_id((id)objc_getClass("NSPasteboard"), sel_registerName("generalPasteboard")); } NSString** cstrToNSStringArray(char** strs, size_t len) { @@ -1144,27 +1165,22 @@ typedef struct { i32 x, y; } RGFW_vector; } NSArray* c_array_to_NSArray(void* array, size_t len) { - void* func = sel_registerName("initWithObjects:count:"); + SEL func = sel_registerName("initWithObjects:count:"); void* nsclass = objc_getClass("NSArray"); - - return ((id(*)(id, SEL, void*, NSUInteger))objc_msgSend) - (NSAlloc(nsclass), func, array, len); + return ((id (*)(id, SEL, void*, NSUInteger))objc_msgSend) + (NSAlloc(nsclass), func, array, len); } - + void NSregisterForDraggedTypes(void* view, NSPasteboardType* newTypes, size_t len) { - NSString** ntypes = cstrToNSStringArray(newTypes, len); - - void* func = sel_registerName("registerForDraggedTypes:"); + NSString** ntypes = cstrToNSStringArray((char**)newTypes, len); NSArray* array = c_array_to_NSArray(ntypes, len); - objc_msgSend_void_id(view, sel_registerName("registerForDraggedTypes:"), array); - NSRelease(array); } NSInteger NSPasteBoard_declareTypes(NSPasteboard* pasteboard, NSPasteboardType* newTypes, size_t len, void* owner) { - NSString** ntypes = cstrToNSStringArray(newTypes, len); + NSString** ntypes = cstrToNSStringArray((char**)newTypes, len); void* func = sel_registerName("declareTypes:owner:"); @@ -1252,13 +1268,17 @@ typedef struct { i32 x, y; } RGFW_vector; (pasteboard, func, array, options); NSRelease(array); - NSUInteger count = NSArray_count(output); const char** res = si_array_init_reserve(sizeof(const char*), count); - for (NSUInteger i = 0; i < count; i++) - res[i] = NSString_to_char(NSArray_objectAtIndex(output, i)); + void* path_func = sel_registerName("path"); + + for (NSUInteger i = 0; i < count; i++) { + void* url = NSArray_objectAtIndex(output, i); + NSString* url_str = ((id(*)(id, SEL))objc_msgSend)(url, path_func); + res[i] = NSString_to_char(url_str); + } return res; } @@ -1301,12 +1321,20 @@ typedef struct { i32 x, y; } RGFW_vector; } #endif + #ifdef RGFW_WINDOWS + __declspec(dllimport) u32 __stdcall timeBeginPeriod(u32 uPeriod); + #endif + RGFWDEF RGFW_window* RGFW_window_basic_init(RGFW_rect rect, u16 args); RGFWDEF void RGFW_init_buffer(RGFW_window* win); RGFW_window* RGFW_window_basic_init(RGFW_rect rect, u16 args) { RGFW_window* win = (RGFW_window*) RGFW_MALLOC(sizeof(RGFW_window)); /* make a new RGFW struct */ + #ifdef RGFW_WINDOWS + timeBeginPeriod(1); + #endif + #ifdef RGFW_ALLOC_DROPFILES win->event.droppedFiles = (char**) RGFW_MALLOC(sizeof(char*) * RGFW_MAX_DROPS); u32 i; @@ -1406,6 +1434,8 @@ typedef struct { i32 x, y; } RGFW_vector; win->src.hdcMem = CreateCompatibleDC(win->src.hdc); #endif +#else +RGFW_UNUSED(win); /* if buffer rendering is not being used */ #endif } @@ -1827,9 +1857,18 @@ typedef struct { i32 x, y; } RGFW_vector; #endif #if defined(RGFW_MACOS) - u8 RGFW_keyMap[128] = { 0 }; + u8 RGFW_keyBoard[128] = { 0 }; + u8 RGFW_keyBoard_prev[128]; #endif + u8 RGFW_isHeldI(RGFW_window* win, u32 key) { + return (RGFW_isPressedI(win, key) && RGFW_wasPressedI(win, key)); + } + + u8 RGFW_isReleasedI(RGFW_window* win, u32 key) { + return (!RGFW_isPressedI(win, key) && RGFW_wasPressedI(win, key)); + } + char* RGFW_keyCodeTokeyStr(u64 key) { #if defined(RGFW_MACOS) static char* keyStrs[128] = { "a", "s", "d", "f", "h", "g", "z", "x", "c", "v", "0", "b", "q", "w", "e", "r", "y", "t", "1", "2", "3", "4", "6", "5", "Equals", "9", "7", "Minus", "8", "0", "CloseBracket", "o", "u", "Bracket", "i", "p", "Return", "l", "j", "Apostrophe", "k", "Semicolon", "BackSlash", "Comma", "Slash", "n", "m", "Period", "Tab", "Space", "Backtick", "BackSpace", "0", "Escape", "0", "Super", "Shift", "CapsLock", "Alt", "Control", "0", "0", "0", "0", "0", "KP_Period", "0", "KP_Minus", "0", "0", "0", "0", "Numlock", "0", "0", "0", "KP_Multiply", "KP_Return", "0", "0", "0", "0", "KP_Slash", "KP_0", "KP_1", "KP_2", "KP_3", "KP_4", "KP_5", "KP_6", "KP_7", "0", "KP_8", "KP_9", "0", "0", "0", "F5", "F6", "F7", "F3", "F8", "F9", "0", "F11", "0", "F13", "0", "F14", "0", "F10", "0", "F12", "0", "F15", "Insert", "Home", "PageUp", "Delete", "F4", "End", "F2", "PageDown", "Left", "Right", "Down", "Up", "F1" }; @@ -1998,12 +2037,14 @@ typedef struct { i32 x, y; } RGFW_vector; u32 RGFW_isPressedJS(RGFW_window* win, u16 c, u8 button) { return win->src.jsPressed[c][button]; } #else - typedef DWORD (WINAPI * PFN_XInputGetState)(DWORD,XINPUT_STATE*); + typedef u64 (WINAPI * PFN_XInputGetState)(DWORD,XINPUT_STATE*); PFN_XInputGetState XInputGetStateSRC = NULL; #define XInputGetState XInputGetStateSRC static HMODULE RGFW_XInput_dll = NULL; u32 RGFW_isPressedJS(RGFW_window* win, u16 c, u8 button) { + RGFW_UNUSED(win) + XINPUT_STATE state; if (XInputGetState == NULL || XInputGetState(c, &state) == ERROR_DEVICE_NOT_CONNECTED) return 0; @@ -2027,9 +2068,15 @@ typedef struct { i32 x, y; } RGFW_vector; } #endif -#ifdef RGFW_OPENGL +#if defined(RGFW_OPENGL) || defined(RGFW_EGL) i32 RGFW_majorVersion = 0, RGFW_minorVersion = 0; + + #ifndef RGFW_EGL i32 RGFW_STENCIL = 8, RGFW_SAMPLES = 4, RGFW_STEREO = GL_FALSE, RGFW_AUX_BUFFERS = 0; + #else + i32 RGFW_STENCIL = 0, RGFW_SAMPLES = 0, RGFW_STEREO = GL_FALSE, RGFW_AUX_BUFFERS = 0; + #endif + void RGFW_setGLStencil(i32 stencil) { RGFW_STENCIL = stencil; } void RGFW_setGLSamples(i32 samples) { RGFW_SAMPLES = samples; } @@ -2041,7 +2088,7 @@ typedef struct { i32 x, y; } RGFW_vector; RGFW_minorVersion = minor; } - u8* RGFW_getMaxGLVersion() { + u8* RGFW_getMaxGLVersion(void) { RGFW_window* dummy = RGFW_createWindow("dummy", RGFW_RECT(0, 0, 1, 1), 0); const char* versionStr = (const char*) glGetString(GL_VERSION); @@ -2055,6 +2102,8 @@ typedef struct { i32 x, y; } RGFW_vector; return version; } +#ifndef RGFW_EGL + #define RGFW_GL_RENDER_TYPE RGFW_OS_BASED_VALUE(GLX_X_VISUAL_TYPE, 0x2003, 73) #define RGFW_GL_ALPHA_SIZE RGFW_OS_BASED_VALUE(GLX_ALPHA_SIZE, 0x201b, 11) #define RGFW_GL_DEPTH_SIZE RGFW_OS_BASED_VALUE(GLX_DEPTH_SIZE, 0x2022, 12) @@ -2087,6 +2136,7 @@ typedef struct { i32 x, y; } RGFW_vector; #endif static u32* RGFW_initAttribs(u32 useSoftware) { + RGFW_UNUSED(useSoftware); static u32 attribs[] = { #ifndef RGFW_MACOS RGFW_GL_RENDER_TYPE, @@ -2165,9 +2215,9 @@ typedef struct { i32 x, y; } RGFW_vector; return attribs; } -#endif +#else -#ifdef RGFW_EGL +#include #if defined(RGFW_LINK_EGL) typedef EGLBoolean(EGLAPIENTRY* PFN_eglInitialize)(EGLDisplay, EGLint*, EGLint*); @@ -2202,8 +2252,8 @@ typedef struct { i32 x, y; } RGFW_vector; #endif -#define EGL_CONTEXT_MAJOR_VERSION_KHR 0x3098 -#define EGL_CONTEXT_MINOR_VERSION_KHR 0x30fb +#define EGL_SURFACE_MAJOR_VERSION_KHR 0x3098 +#define EGL_SURFACE_MINOR_VERSION_KHR 0x30fb #ifndef RGFW_GL_ADD_ATTRIB #define RGFW_GL_ADD_ATTRIB(attrib, attVal) \ @@ -2214,9 +2264,8 @@ typedef struct { i32 x, y; } RGFW_vector; } #endif - void RGFW_createOpenGLContext(RGFW_window* win) { - static EGLContext globalCtx = EGL_NO_CONTEXT; + void RGFW_createOpenGLContext(RGFW_window* win) { #if defined(RGFW_LINK_EGL) eglInitializeSource = (PFNEGLINITIALIZEPROC) eglGetProcAddress("eglInitialize"); eglGetConfigsSource = (PFNEGLGETCONFIGSPROC) eglGetProcAddress("eglGetConfigs"); @@ -2233,69 +2282,101 @@ typedef struct { i32 x, y; } RGFW_vector; eglDestroySurfaceSource = (PFNEGLDESTROYSURFACEPROC) eglGetProcAddress("eglDestroySurface"); #endif /* RGFW_LINK_EGL */ + #ifdef RGFW_WINDOWS + win->src.EGL_display = eglGetDisplay((EGLNativeDisplayType) win->src.hdc); + #else win->src.EGL_display = eglGetDisplay((EGLNativeDisplayType) win->src.display); + #endif EGLint major, minor; eglInitialize(win->src.EGL_display, &major, &minor); - EGLint config_attribs[] = { + #ifndef EGL_OPENGL_ES1_BIT + #define EGL_OPENGL_ES1_BIT 0x1 + #endif + + EGLint egl_config[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, #ifdef RGFW_OPENGL_ES1 EGL_OPENGL_ES1_BIT, - #endif - #ifdef RGFW_OPENGL_ES2 + #elif defined(RGFW_OPENGL_ES3) + EGL_OPENGL_ES3_BIT, + #elif defined(RGFW_OPENGL_ES2) EGL_OPENGL_ES2_BIT, #else EGL_OPENGL_BIT, #endif - EGL_NONE + EGL_NONE, EGL_NONE }; EGLConfig config; - EGLint num_configs; - eglChooseConfig(win->src.EGL_display, config_attribs, &config, 1, &num_configs); + EGLint numConfigs; + eglChooseConfig(win->src.EGL_display, egl_config, &config, 1, &numConfigs); -#if defined(RGFW_OPENGL_ES2) || defined(RGFW_OPENGL_ES1) - eglBindAPI(EGL_OPENGL_ES_API); -#else - eglBindAPI(EGL_OPENGL_API); -#endif - EGLint attribs[]{ - EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT, - 0, 0, 0, 0 - }; - - size_t index = 2; - RGFW_GL_ADD_ATTRIB(EGL_STENCIL_SIZE, RGFW_STENCIL); - RGFW_GL_ADD_ATTRIB(EGL_SAMPLES, RGFW_SAMPLES); - RGFW_GL_ADD_ATTRIB(EGL_CONTEXT_MAJOR_VERSION, RGFW_majorVersion); - RGFW_GL_ADD_ATTRIB(EGL_CONTEXT_MINOR_VERSION, RGFW_minorVersion); - - win->src.rSurf = eglCreateContext(win->src.EGL_display, config, globalCtx, attribs); win->src.EGL_surface = eglCreateWindowSurface(win->src.EGL_display, config, (EGLNativeWindowType) win->src.window, NULL); - if (globalCtx == EGL_NO_CONTEXT) - RGFW_EGLglobalContext = win->src.rSurf; + EGLint attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, + #ifdef RGFW_OPENGL_ES1 + 1, + #else + 2, + #endif + EGL_NONE, EGL_NONE, EGL_NONE, EGL_NONE, EGL_NONE, EGL_NONE + }; - eglMakeCurrent(win->src.EGL_display, win->src.EGL_surface, win->src.EGL_surface, win->src.rSurf); + size_t index = 4; + RGFW_GL_ADD_ATTRIB(EGL_STENCIL_SIZE, RGFW_STENCIL); + RGFW_GL_ADD_ATTRIB(EGL_SAMPLES, RGFW_SAMPLES); + + if (RGFW_majorVersion) { + attribs[1] = RGFW_majorVersion; + RGFW_GL_ADD_ATTRIB(EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT); + RGFW_GL_ADD_ATTRIB(EGL_CONTEXT_MAJOR_VERSION, RGFW_majorVersion); + RGFW_GL_ADD_ATTRIB(EGL_CONTEXT_MINOR_VERSION, RGFW_minorVersion); + } + + #if defined(RGFW_OPENGL_ES1) || defined(RGFW_OPENGL_ES2) || defined(RGFW_OPENGL_ES3) + eglBindAPI(EGL_OPENGL_ES_API); + #else + eglBindAPI(EGL_OPENGL_API); + #endif + + win->src.EGL_context = eglCreateContext(win->src.EGL_display, config, EGL_NO_CONTEXT, attribs); + + eglMakeCurrent(win->src.EGL_display, win->src.EGL_surface, win->src.EGL_surface, win->src.EGL_context); eglSwapBuffers(win->src.EGL_display, win->src.EGL_surface); - - eglSwapInterval(win->src.EGL_display, 1); } - void* RGFW_getProcAddress(const char* procname) { return (void*) eglGetProcAddress(procname); } + #ifdef RGFW_APPLE + void* RGFWnsglFramework = NULL; + #elif defined(RGFW_WINDOWS) + static HMODULE wglinstance = NULL; + #endif + + void* RGFW_getProcAddress(const char* procname) { + #if defined(RGFW_WINDOWS) + void* proc = (void*) GetProcAddress(wglinstance, procname); + + if (proc) + return proc; + #endif + + return (void*) eglGetProcAddress(procname); + } void RGFW_closeEGL(RGFW_window* win) { eglDestroySurface(win->src.EGL_display, win->src.EGL_surface); - eglDestroyContext(win->src.EGL_display, win->src.rSurf); + eglDestroyContext(win->src.EGL_display, win->src.EGL_context); eglTerminate(win->src.EGL_display); } #endif /* RGFW_EGL */ +#endif /* RGFW_GL stuff? */ /* This is where OS specific stuff starts @@ -2388,7 +2469,7 @@ typedef struct { i32 x, y; } RGFW_vector; } u32 i; - for (i = 0; i < fbcount; i++) { + for (i = 0; i < (u32)fbcount; i++) { XVisualInfo* vi = glXGetVisualFromFBConfig((Display*) win->src.display, fbc[i]); if (vi == NULL) continue; @@ -2398,8 +2479,9 @@ typedef struct { i32 x, y; } RGFW_vector; i32 samp_buf, samples; glXGetFBConfigAttrib((Display*) win->src.display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf); glXGetFBConfigAttrib((Display*) win->src.display, fbc[i], GLX_SAMPLES, &samples); - if ((best_fbc < 0 || samp_buf) && (samples == RGFW_SAMPLES)) + if ((best_fbc < 0 || samp_buf) && (samples == RGFW_SAMPLES || best_fbc == -1)) { best_fbc = i; + } } if (best_fbc == -1) { @@ -2471,11 +2553,6 @@ typedef struct { i32 x, y; } RGFW_vector; win->src.rSurf = glXCreateContextAttribsARB((Display*) win->src.display, bestFbc, ctx, True, context_attribs); #endif - -#ifdef RGFW_EGL - RGFW_createOpenGLContext(win); -#endif - if (RGFW_root == NULL) RGFW_root = win; @@ -2558,6 +2635,10 @@ typedef struct { i32 x, y; } RGFW_vector; PropModeReplace, (u8*) &version, 1); /* turns on drag and drop */ } + #ifdef RGFW_EGL + RGFW_createOpenGLContext(win); + #endif + RGFW_window_setMouseDefault(win); RGFW_windowsOpen++; @@ -2593,9 +2674,11 @@ typedef struct { i32 x, y; } RGFW_vector; int xAxis = 0, yAxis = 0; + char RGFW_keyboard[32]; + char RGFW_keyboard_prev[32]; + RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) { assert(win != NULL); - win->event.type = 0; #ifdef __linux__ @@ -2650,13 +2733,6 @@ typedef struct { i32 x, y; } RGFW_vector; } u32 i; - - if (win->event.droppedFilesCount) { - for (i = 0; i < win->event.droppedFilesCount; i++) - win->event.droppedFiles[i][0] = '\0'; - } - - win->event.droppedFilesCount = 0; win->event.type = 0; @@ -2676,6 +2752,11 @@ typedef struct { i32 x, y; } RGFW_vector; win->event.keyCode = XkbKeycodeToKeysym((Display*) win->src.display, E.xkey.keycode, 0, E.xkey.state & ShiftMask ? 1 : 0); win->event.keyName = XKeysymToString(win->event.keyCode); /* convert to string */ + if (RGFW_isPressedI(win, win->event.keyCode)) + RGFW_keyboard_prev[E.xkey.keycode >> 3] |= (1 << (E.xkey.keycode & 7)); + else + RGFW_keyboard_prev[E.xkey.keycode >> 3] |= 0; + /* get keystate data */ win->event.type = (E.type == KeyPress) ? RGFW_keyPressed : RGFW_keyReleased; @@ -2690,6 +2771,8 @@ typedef struct { i32 x, y; } RGFW_vector; else if (win->event.keyCode == XK_Num_Lock) win->event.lockState |= RGFW_NUMLOCK; } + + XQueryKeymap(win->src.display, RGFW_keyboard); /* query the keymap */ break; case ButtonPress: @@ -2718,6 +2801,14 @@ typedef struct { i32 x, y; } RGFW_vector; win->event.type = RGFW_quit; break; } + + /* reset DND values */ + if (win->event.droppedFilesCount) { + for (i = 0; i < win->event.droppedFilesCount; i++) + win->event.droppedFiles[i][0] = '\0'; + } + + win->event.droppedFilesCount = 0; /* much of this event (drag and drop code) is source from glfw @@ -3002,13 +3093,13 @@ typedef struct { i32 x, y; } RGFW_vector; win->src.winArgs &= ~RGFW_MOUSE_CHANGED; } - if (win->src.winArgs & RGFW_HOLD_MOUSE && win->event.inFocus && win->event.type == RGFW_mousePosChanged) { - RGFW_window_moveMouse(win, RGFW_VECTOR(win->r.x + (win->r.w / 2), win->r.y + (win->r.h / 2))); - - if (XEventsQueued((Display*) win->src.display, QueuedAfterReading) <= 1) - XSync(win->src.display, True); - } + if (win->src.winArgs & RGFW_HOLD_MOUSE && win->event.inFocus && win->event.type == RGFW_mousePosChanged) { + RGFW_window_moveMouse(win, RGFW_VECTOR(win->r.x + (win->r.w / 2), win->r.y + (win->r.h / 2))); + if (XEventsQueued((Display*) win->src.display, QueuedAfterReading) <= 1) + XSync(win->src.display, True); + } + XFlush((Display*) win->src.display); @@ -3022,7 +3113,7 @@ typedef struct { i32 x, y; } RGFW_vector; assert(win != NULL); #ifdef RGFW_VULKAN - for (int i = 0; i < win->src.image_count; i++) { + for (u32 i = 0; i < win->src.image_count; i++) { vkDestroyImageView(RGFW_vulkan_info.device, win->src.swapchain_image_views[i], NULL); } @@ -3212,7 +3303,7 @@ typedef struct { i32 x, y; } RGFW_vector; #ifndef RGFW_NO_X11_CURSOR /* free the previous cursor */ - if (win->src.cursor && win->src.cursor != -1) + if (win->src.cursor) XFreeCursor((Display*) win->src.display, (Cursor) win->src.cursor); XcursorImage* native = XcursorImageCreate(a.w, a.h); @@ -3235,6 +3326,8 @@ typedef struct { i32 x, y; } RGFW_vector; win->src.cursor = XcursorImageLoadCursor((Display*) win->src.display, native); XcursorImageDestroy(native); +#else + RGFW_UNUSED(image) RGFW_UNUSED(a.w) RGFW_UNUSED(channels) #endif } @@ -3256,7 +3349,7 @@ typedef struct { i32 x, y; } RGFW_vector; } RGFWDEF void RGFW_window_disableMouse(RGFW_window* win) { - + RGFW_UNUSED(win); } void RGFW_window_setMouseDefault(RGFW_window* win) { @@ -3267,7 +3360,7 @@ typedef struct { i32 x, y; } RGFW_vector; assert(win != NULL); /* free the previous cursor */ - if (win->src.cursor && win->src.cursor != -1) + if (win->src.cursor) XFreeCursor((Display*) win->src.display, (Cursor) win->src.cursor); win->src.winArgs |= RGFW_MOUSE_CHANGED; @@ -3615,7 +3708,7 @@ typedef struct { i32 x, y; } RGFW_vector; monitor.physW = (monitor.rect.w * 25.4f / 96.f); monitor.physH = (monitor.rect.h * 25.4f / 96.f); - strncpy(monitor.name, DisplayString(display), 128); + strcpy(monitor.name, DisplayString(display)); XGetSystemContentScale(display, &monitor.scaleX, &monitor.scaleY); @@ -3648,7 +3741,7 @@ typedef struct { i32 x, y; } RGFW_vector; RGFW_monitor RGFW_monitors[6]; RGFW_monitor* RGFW_getMonitors(void) { size_t i; - for (i = 0; i < ScreenCount(RGFW_root->src.display) && i < 6; i++) + for (i = 0; i < (size_t)ScreenCount(RGFW_root->src.display) && i < 6; i++) RGFW_monitors[i] = RGFW_XCreateMonitor(i); return RGFW_monitors; @@ -3684,9 +3777,7 @@ typedef struct { i32 x, y; } RGFW_vector; RGFW_monitor RGFW_window_getMonitor(RGFW_window* win) { return RGFW_XCreateMonitor(DefaultScreen(win->src.display)); } - - char keyboard[32]; - + u8 RGFW_isPressedI(RGFW_window* win, u32 key) { Display* d; if (win == (RGFW_window*) 0) @@ -3695,11 +3786,22 @@ typedef struct { i32 x, y; } RGFW_vector; return 0; else d = (Display*) win->src.display; - - XQueryKeymap(d, keyboard); /* query the keymap */ - + KeyCode kc2 = XKeysymToKeycode(d, key); /* convert the key to a keycode */ - return !!(keyboard[kc2 >> 3] & (1 << (kc2 & 7))); /* check if the key is pressed */ + return (RGFW_keyboard[kc2 >> 3] & (1 << (kc2 & 7))); /* check if the key is pressed */ + } + + u8 RGFW_wasPressedI(RGFW_window* win, u32 key) { + Display* d; + if (win == (RGFW_window*) 0) + d = RGFW_root->src.display; + else if (!win->event.inFocus) + return 0; + else + d = (Display*) win->src.display; + + KeyCode kc2 = XKeysymToKeycode(d, key); /* convert the key to a keycode */ + return !!(RGFW_keyboard_prev[kc2 >> 3] & (1 << (kc2 & 7))); /* check if the key is pressed */ } #endif @@ -3742,8 +3844,8 @@ typedef struct { i32 x, y; } RGFW_vector; void* RGFWjoystickApi = NULL; /* these two wgl functions need to be preloaded */ - typedef HGLRC(WINAPI* wglCreateContextAttribsARB_type)(HDC hdc, HGLRC hShareContext, - const i32* attribList); + typedef long long int (WINAPI* wglCreateContextAttribsARB_type)(HDC hdc, HGLRC hShareContext, + const int* attribList); wglCreateContextAttribsARB_type wglCreateContextAttribsARB = NULL; /* defines for creating ARB attributes */ @@ -3785,7 +3887,9 @@ typedef struct { i32 x, y; } RGFW_vector; #define WGL_SAMPLES_ARB 0x2042 #define WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20a9 +#ifndef RGFW_EGL static HMODULE wglinstance = NULL; +#endif #ifdef RGFW_WGL_LOAD typedef HGLRC(WINAPI* PFN_wglCreateContext)(HDC); @@ -3802,13 +3906,13 @@ static HMODULE wglinstance = NULL; PFN_wglGetCurrentDC wglGetCurrentDCSRC; PFN_wglGetCurrentContext wglGetCurrentContextSRC; -#define wglCreateContext wglCreateContextSRC -#define wglDeleteContext wglDeleteContextSRC -#define wglGetProcAddress wglGetProcAddressSRC -#define wglMakeCurrent wglMakeCurrentSRC + #define wglCreateContext wglCreateContextSRC + #define wglDeleteContext wglDeleteContextSRC + #define wglGetProcAddress wglGetProcAddressSRC + #define wglMakeCurrent wglMakeCurrentSRC -#define wglGetCurrentDC wglGetCurrentDCSRC -#define wglGetCurrentContext wglGetCurrentContextSRC + #define wglGetCurrentDC wglGetCurrentDCSRC + #define wglGetCurrentContext wglGetCurrentContextSRC #endif #ifdef RGFW_OPENGL @@ -3820,11 +3924,11 @@ static HMODULE wglinstance = NULL; return (void*) GetProcAddress(wglinstance, procname); } - typedef BOOL(APIENTRY* PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int* piAttribIList, const FLOAT* pfAttribFList, UINT nMaxFormats, int* piFormats, UINT* nNumFormats); + typedef u64 (APIENTRY* PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int* piAttribIList, const FLOAT* pfAttribFList, UINT nMaxFormats, int* piFormats, UINT* nNumFormats); static PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = NULL; #endif - RGFW_window RGFW_eventWindow = { {NULL} }; + RGFW_window RGFW_eventWindow; LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { @@ -3845,7 +3949,7 @@ static HMODULE wglinstance = NULL; #ifndef RGFW_NO_DPI static HMODULE RGFW_Shcore_dll = NULL; - typedef HRESULT (WINAPI * PFN_GetDpiForMonitor)(HMONITOR,MONITOR_DPI_TYPE,UINT*,UINT*); + typedef u64 (WINAPI * PFN_GetDpiForMonitor)(HMONITOR,MONITOR_DPI_TYPE,UINT*,UINT*); PFN_GetDpiForMonitor GetDpiForMonitorSRC = NULL; #define GetDpiForMonitor GetDpiForMonitorSRC #endif @@ -3898,6 +4002,7 @@ static HMODULE wglinstance = NULL; if (name[0] == 0) name = (char*) " "; RGFW_eventWindow.r = RGFW_RECT(-1, -1, -1, -1); + RGFW_eventWindow.src.window = NULL; RGFW_window* win = RGFW_window_basic_init(rect, args); @@ -4036,7 +4141,7 @@ static HMODULE wglinstance = NULL; ReleaseDC(dummyWin, dummy_dc); if (wglCreateContextAttribsARB != NULL) { - PIXELFORMATDESCRIPTOR pfd = { sizeof(pfd), 1, PFD_TYPE_RGBA, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 32, 8, PFD_MAIN_PLANE, 24, 8 }; + PIXELFORMATDESCRIPTOR pfd = (PIXELFORMATDESCRIPTOR){ sizeof(pfd), 1, PFD_TYPE_RGBA, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 32, 8, PFD_MAIN_PLANE, 24, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; if (args & RGFW_OPENGL_SOFTWARE) pfd.dwFlags |= PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED; @@ -4059,15 +4164,15 @@ static HMODULE wglinstance = NULL; u32 index = 0; i32 attribs[40]; -#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 - SET_ATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB); + + SET_ATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); if (RGFW_majorVersion || RGFW_minorVersion) { SET_ATTRIB(WGL_CONTEXT_MAJOR_VERSION_ARB, RGFW_majorVersion); SET_ATTRIB(WGL_CONTEXT_MINOR_VERSION_ARB, RGFW_minorVersion); } - SET_ATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB); + SET_ATTRIB(WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB); if (RGFW_majorVersion || RGFW_minorVersion) { SET_ATTRIB(WGL_CONTEXT_MAJOR_VERSION_ARB, RGFW_majorVersion); @@ -4076,7 +4181,7 @@ static HMODULE wglinstance = NULL; SET_ATTRIB(0, 0); - win->src.rSurf = wglCreateContextAttribsARB(win->src.hdc, NULL, attribs); + win->src.rSurf = (HGLRC)wglCreateContextAttribsARB(win->src.hdc, NULL, attribs); } else { fprintf(stderr, "Failed to create an accelerated OpenGL Context\n"); @@ -4293,6 +4398,9 @@ static HMODULE wglinstance = NULL; return 0; } + BYTE RGFW_keyBoard[256]; + BYTE RGFW_keyBoard_prev[256]; + RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) { assert(win != NULL); @@ -4317,14 +4425,6 @@ static HMODULE wglinstance = NULL; return &win->event; } - if (win->event.droppedFilesCount) { - u32 i; - for (i = 0; i < win->event.droppedFilesCount; i++) - win->event.droppedFiles[i][0] = '\0'; - } - - win->event.droppedFilesCount = 0; - win->event.inFocus = (GetForegroundWindow() == win->src.window); if (RGFW_checkXInput(&win->event)) @@ -4334,7 +4434,6 @@ static HMODULE wglinstance = NULL; return NULL; static BYTE keyboardState[256]; - GetKeyboardState(keyboardState); if (PeekMessageA(&msg, win->src.window, 0u, 0u, PM_REMOVE)) { switch (msg.message) { @@ -4345,24 +4444,37 @@ static HMODULE wglinstance = NULL; case WM_KEYUP: win->event.keyCode = (u32) msg.wParam; + if (RGFW_isPressedI(win, win->event.keyCode)) + RGFW_keyBoard_prev[win->event.keyCode] |= 0x80; + else + RGFW_keyBoard_prev[win->event.keyCode] = 0; + strncpy(win->event.keyName, RGFW_keyCodeTokeyStr(msg.lParam), 16); - if (GetKeyState(VK_SHIFT) & 0x8000) { + if (RGFW_isPressedI(win, VK_SHIFT)) { ToAscii((UINT) msg.wParam, MapVirtualKey((UINT) msg.wParam, MAPVK_VK_TO_CHAR), keyboardState, (LPWORD) win->event.keyName, 0); } win->event.type = RGFW_keyReleased; + GetKeyboardState(RGFW_keyBoard); break; case WM_KEYDOWN: win->event.keyCode = (u32) msg.wParam; + + if (RGFW_isPressedI(win, win->event.keyCode)) + RGFW_keyBoard_prev[win->event.keyCode] |= 0x80; + else + RGFW_keyBoard_prev[win->event.keyCode] = 0; + strncpy(win->event.keyName, RGFW_keyCodeTokeyStr(msg.lParam), 16); - if (GetKeyState(VK_SHIFT) & 0x8000) { + if (RGFW_isPressedI(win, VK_SHIFT) & 0x8000) { ToAscii((UINT) msg.wParam, MapVirtualKey((UINT) msg.wParam, MAPVK_VK_TO_CHAR), keyboardState, (LPWORD) win->event.keyName, 0); } win->event.type = RGFW_keyPressed; + GetKeyboardState(RGFW_keyBoard); break; case WM_MOUSEMOVE: @@ -4413,6 +4525,15 @@ static HMODULE wglinstance = NULL; much of this event is source from glfw */ case WM_DROPFILES: { + + if (win->event.droppedFilesCount) { + u32 i; + for (i = 0; i < win->event.droppedFilesCount; i++) + win->event.droppedFiles[i][0] = '\0'; + } + + win->event.droppedFilesCount = 0; + win->event.type = RGFW_dnd; HDROP drop = (HDROP) msg.wParam; @@ -4522,6 +4643,9 @@ static HMODULE wglinstance = NULL; typedef struct { int iIndex; HMONITOR hMonitor; } RGFW_mInfo; BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { + RGFW_UNUSED(hdcMonitor) + RGFW_UNUSED(lprcMonitor) + RGFW_mInfo* info = (RGFW_mInfo*) dwData; if (info->hMonitor == hMonitor) return FALSE; @@ -4586,6 +4710,9 @@ static HMODULE wglinstance = NULL; RGFW_monitor RGFW_monitors[6]; BOOL CALLBACK GetMonitorHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { + RGFW_UNUSED(hdcMonitor) + RGFW_UNUSED(lprcMonitor) + RGFW_mInfo* info = (RGFW_mInfo*) dwData; if (info->iIndex >= 6) @@ -4618,9 +4745,20 @@ static HMODULE wglinstance = NULL; if (win != NULL && !win->event.inFocus) return 0; - if (GetAsyncKeyState(key) & 0x8000) + if (RGFW_keyBoard[key] & 0x80) return 1; - else return 0; + + return 0; + } + + u8 RGFW_wasPressedI(RGFW_window* win, u32 key) { + if (win != NULL && !win->event.inFocus) + return 0; + + if (RGFW_keyBoard_prev[key] & 0x80) + return 1; + + return 0; } HICON RGFW_loadHandleImage(RGFW_window* win, u8* src, RGFW_area a, BOOL icon) { @@ -4684,6 +4822,7 @@ static HMODULE wglinstance = NULL; void RGFW_window_setMouse(RGFW_window* win, u8* image, RGFW_area a, i32 channels) { assert(win != NULL); + RGFW_UNUSED(channels) HCURSOR cursor = (HCURSOR) RGFW_loadHandleImage(win, image, a, FALSE); SetClassLongPtrA(win->src.window, GCLP_HCURSOR, (LPARAM) cursor); @@ -4824,6 +4963,7 @@ static HMODULE wglinstance = NULL; /* much of this function is sourced from GLFW */ void RGFW_window_setIcon(RGFW_window* win, u8* src, RGFW_area a, i32 channels) { assert(win != NULL); + RGFW_UNUSED(channels) HICON handle = RGFW_loadHandleImage(win, src, a, TRUE); @@ -4900,12 +5040,14 @@ static HMODULE wglinstance = NULL; u16 RGFW_registerJoystick(RGFW_window* win, i32 jsNumber) { assert(win != NULL); + RGFW_UNUSED(jsNumber) + return RGFW_registerJoystickF(win, (char*) ""); } u16 RGFW_registerJoystickF(RGFW_window* win, char* file) { assert(win != NULL); - + RGFW_UNUSED(file) return win->src.joystickCount - 1; } @@ -4936,7 +5078,7 @@ static HMODULE wglinstance = NULL; } #ifndef RGFW_NO_THREADS - RGFW_thread RGFW_createThread(void* (*function_ptr)(void*), void* args) { return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) *function_ptr, args, 0, NULL); } + RGFW_thread RGFW_createThread(RGFW_threadFunc_ptr ptr, void* args) { return CreateThread(NULL, 0, ptr, args, 0, NULL); } void RGFW_cancelThread(RGFW_thread thread) { CloseHandle((HANDLE) thread); } void RGFW_joinThread(RGFW_thread thread) { WaitForSingleObject((HANDLE) thread, INFINITE); } void RGFW_setThreadPriority(RGFW_thread thread, u8 priority) { SetThreadPriority((HANDLE) thread, priority); } @@ -4962,7 +5104,10 @@ static HMODULE wglinstance = NULL; } #endif - CVReturn displayCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* inNow, const CVTimeStamp* inOutputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) { return kCVReturnSuccess; } + CVReturn displayCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* inNow, const CVTimeStamp* inOutputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) { + RGFW_UNUSED(displayLink) RGFW_UNUSED(inNow) RGFW_UNUSED(inOutputTime) RGFW_UNUSED(flagsIn) RGFW_UNUSED(flagsOut) RGFW_UNUSED(displayLinkContext) + return kCVReturnSuccess; + } RGFW_window* RGFW_windows[10]; u32 RGFW_windows_size = 0; @@ -4983,17 +5128,25 @@ static HMODULE wglinstance = NULL; } /* NOTE(EimaMei): Fixes the constant clicking when the app is running under a terminal. */ - bool acceptsFirstResponder() { return true; } - bool performKeyEquivalent(NSEvent* event) { return true; } + bool acceptsFirstResponder(void) { return true; } + bool performKeyEquivalent(NSEvent* event) { RGFW_UNUSED(event); return true; } - NSDragOperation draggingEntered(id self, SEL sel, id sender) { return NSDragOperationCopy; } - NSDragOperation draggingUpdated(id self, SEL sel, id sender) { return NSDragOperationCopy; } + NSDragOperation draggingEntered(id self, SEL sel, id sender) { + RGFW_UNUSED(sender); RGFW_UNUSED(self); RGFW_UNUSED(sel); + return NSDragOperationCopy; + } + NSDragOperation draggingUpdated(id self, SEL sel, id sender) { + RGFW_UNUSED(sender); RGFW_UNUSED(self); RGFW_UNUSED(sel); + return NSDragOperationCopy; + } bool prepareForDragOperation(void) { return true; } - void RGFW__osxDraggingEnded(id self, SEL sel, id sender) { return; } + void RGFW__osxDraggingEnded(id self, SEL sel, id sender) { RGFW_UNUSED(sender); RGFW_UNUSED(self); RGFW_UNUSED(sel); return; } /* NOTE(EimaMei): Usually, you never need 'id self, SEL cmd' for C -> Obj-C methods. This isn't the case. */ bool performDragOperation(id self, SEL sel, id sender) { + RGFW_UNUSED(sender); RGFW_UNUSED(self); RGFW_UNUSED(sel); + NSWindow* window = objc_msgSend_id(sender, sel_registerName("draggingDestinationWindow")); u32 i; bool found = false; @@ -5007,10 +5160,10 @@ static HMODULE wglinstance = NULL; if (!found) i = 0; - Class* array[] = { objc_getClass("NSURL"), NULL }; - char** droppedFiles = (char**) NSPasteboard_readObjectsForClasses( - (NSPasteboard*) objc_msgSend_id(sender, sel_registerName("draggingPasteboard")), - array, 1, NULL); + Class array[] = { objc_getClass("NSURL"), NULL }; + NSPasteboard* pasteBoard = objc_msgSend_id(sender, sel_registerName("draggingPasteboard")); + + char** droppedFiles = (char**) NSPasteboard_readObjectsForClasses(pasteBoard, array, 1, NULL); RGFW_windows[i]->event.droppedFilesCount = si_array_len(droppedFiles); @@ -5020,11 +5173,12 @@ static HMODULE wglinstance = NULL; strcpy(RGFW_windows[i]->event.droppedFiles[y], droppedFiles[y]); RGFW_windows[i]->event.type = RGFW_dnd; + RGFW_windows[i]->src.dndPassed = false; - NSPoint p = *(NSPoint*) objc_msgSend_id(sender, sel_registerName("draggingLocation")); - RGFW_windows[i]->event.point.x = p.x; - RGFW_windows[i]->event.point.x = p.y; + NSPoint p = ((NSPoint(*)(id, SEL)) objc_msgSend)(sender, sel_registerName("draggingLocation")); + RGFW_windows[i]->event.point.x = (i32)p.x; + RGFW_windows[i]->event.point.x = (i32)p.y; return true; } @@ -5059,6 +5213,8 @@ static HMODULE wglinstance = NULL; NSSize RGFW__osxWindowResize(void* self, SEL sel, NSSize frameSize) { + RGFW_UNUSED(sel); + u32 i; for (i = 0; i < RGFW_windows_size; i++) { if (RGFW_windows[i] && NSWindow_delegate(RGFW_windows[i]) == self) { @@ -5074,6 +5230,8 @@ static HMODULE wglinstance = NULL; } void RGFW__osxWindowMove(void* self, SEL sel) { + RGFW_UNUSED(sel); + u32 i; for (i = 0; i < RGFW_windows_size; i++) { if (RGFW_windows[i] && NSWindow_delegate(RGFW_windows[i]) == self) { @@ -5093,8 +5251,8 @@ static HMODULE wglinstance = NULL; #define APPKIT_EXTERN extern #endif - APPKIT_EXTERN NSPasteboardType const NSPasteboardTypeURL = "public.url"; API_AVAILABLE(macos(10.13)); // Equivalent to kUTTypeURL - APPKIT_EXTERN NSPasteboardType const NSPasteboardTypeFileURL = "public.file-url"; API_AVAILABLE(macos(10.13)); // Equivalent to kUTTypeFileURL + NSPasteboardType const NSPasteboardTypeURL = "public.url"; + NSPasteboardType const NSPasteboardTypeFileURL = "public.file-url"; RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) { static u8 RGFW_loaded = 0; @@ -5109,7 +5267,7 @@ static HMODULE wglinstance = NULL; si_func_to_SEL("NSWindow", performKeyEquivalent); if (NSApp == NULL) { - NSApp = objc_msgSend_id(objc_getClass("NSApplication"), sel_registerName("sharedApplication")); + NSApp = objc_msgSend_id((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication")); ((void (*)(id, SEL, NSUInteger))objc_msgSend) (NSApp, sel_registerName("setActivationPolicy:"), NSApplicationActivationPolicyRegular); @@ -5164,7 +5322,7 @@ static HMODULE wglinstance = NULL; #else NSRect contentRect = NSMakeRect(0, 0, win->r.w, win->r.h); win->src.view = ((id(*)(id, SEL, NSRect))objc_msgSend) - (NSAlloc(objc_getClass("NSView")), sel_registerName("initWithFrame:"), + (NSAlloc((id)objc_getClass("NSView")), sel_registerName("initWithFrame:"), contentRect); #endif @@ -5193,7 +5351,7 @@ static HMODULE wglinstance = NULL; } win->src.display = CGMainDisplayID(); - CVDisplayLinkCreateWithCGDisplay(win->src.display, &win->src.displayLink); + CVDisplayLinkCreateWithCGDisplay(win->src.display, (CVDisplayLinkRef*)&win->src.displayLink); CVDisplayLinkSetOutputCallback(win->src.displayLink, displayCallback, win); CVDisplayLinkStart(win->src.displayLink); @@ -5213,35 +5371,17 @@ static HMODULE wglinstance = NULL; NSMoveToResourceDir(); Class delegateClass = objc_allocateClassPair(objc_getClass("NSObject"), "WindowDelegate", 0); + + class_addMethod(delegateClass, sel_registerName("windowWillResize:toSize:"), (IMP) RGFW__osxWindowResize, "{NSSize=ff}@:{NSSize=ff}"); class_addMethod(delegateClass, sel_registerName("windowWillMove:"), (IMP) RGFW__osxWindowMove, ""); class_addMethod(delegateClass, sel_registerName("windowDidMove:"), (IMP) RGFW__osxWindowMove, ""); - - - if (args & RGFW_ALLOW_DND) { - win->src.winArgs |= RGFW_ALLOW_DND; - -/* - NSPasteboardType types[] = {NSPasteboardTypeURL, NSPasteboardTypeFileURL, NSPasteboardTypeString}; - - siArray(NSPasteboardType) array = sic_arrayInit(types, sizeof(id), countof(types)); - NSWindow_registerForDraggedTypes(win->hwnd, array); - - win->dndHead = win->dndPrev = out; -*/ - - NSPasteboardType array[] = {NSPasteboardTypeURL, NSPasteboardTypeFileURL, NSPasteboardTypeString}; - NSregisterForDraggedTypes(win->src.window, array, 3); - - /* NOTE(EimaMei): Drag 'n Drop requires too many damn functions for just a Drag 'n Drop event. */ - class_addMethod(delegateClass, "draggingEntered:", draggingEntered, "l@:@"); - class_addMethod(delegateClass, "draggingUpdated:", draggingUpdated, "l@:@"); - class_addMethod(delegateClass, "draggingExited:", RGFW__osxDraggingEnded, "v@:@"); - class_addMethod(delegateClass, "draggingEnded:", RGFW__osxDraggingEnded, "v@:@"); - class_addMethod(delegateClass, "prepareForDragOperation:", prepareForDragOperation, "B@:@"); - class_addMethod(delegateClass, "performDragOperation:", performDragOperation, "B@:@"); - - } + class_addMethod(delegateClass, sel_registerName("draggingEntered:"), (IMP)draggingEntered, "l@:@"); + class_addMethod(delegateClass, sel_registerName("draggingUpdated:"), (IMP)draggingUpdated, "l@:@"); + class_addMethod(delegateClass, sel_registerName("draggingExited:"), (IMP)RGFW__osxDraggingEnded, "v@:@"); + class_addMethod(delegateClass, sel_registerName("draggingEnded:"), (IMP)RGFW__osxDraggingEnded, "v@:@"); + class_addMethod(delegateClass, sel_registerName("prepareForDragOperation:"), (IMP)prepareForDragOperation, "B@:@"); + class_addMethod(delegateClass, sel_registerName("performDragOperation:"), (IMP)performDragOperation, "B@:@"); id delegate = objc_msgSend_id(NSAlloc(delegateClass), sel_registerName("init")); @@ -5249,6 +5389,13 @@ static HMODULE wglinstance = NULL; objc_msgSend_void_id(win->src.window, sel_registerName("setDelegate:"), delegate); + if (args & RGFW_ALLOW_DND) { + win->src.winArgs |= RGFW_ALLOW_DND; + + NSPasteboardType types[] = {NSPasteboardTypeURL, NSPasteboardTypeFileURL, NSPasteboardTypeString}; + NSregisterForDraggedTypes(win->src.window, types, 3); + } + // Show the window ((id(*)(id, SEL, SEL))objc_msgSend)(win->src.window, sel_registerName("makeKeyAndOrderFront:"), NULL); objc_msgSend_void_bool(win->src.window, sel_registerName("setIsVisible:"), true); @@ -5259,6 +5406,8 @@ static HMODULE wglinstance = NULL; RGFW_loaded = 1; } + objc_msgSend_void(win->src.window, sel_registerName("makeKeyWindow")); + NSApplication_finishLaunching(NSApp); RGFW_windows_size++; @@ -5385,12 +5534,25 @@ static HMODULE wglinstance = NULL; }; + typedef enum NSEventModifierFlags { + NSEventModifierFlagCapsLock = 1 << 16, + NSEventModifierFlagShift = 1 << 17, + NSEventModifierFlagControl = 1 << 18, + NSEventModifierFlagOption = 1 << 19, + NSEventModifierFlagCommand = 1 << 20 + } NSEventModifierFlags; + RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) { assert(win != NULL); if (win->event.type == RGFW_quit) return &win->event; + if (win->event.type == RGFW_dnd && win->src.dndPassed == 0) { + win->src.dndPassed = 1; + return &win->event; + } + static void* eventFunc = NULL; if (eventFunc == NULL) eventFunc = sel_registerName("nextEventMatchingMask:untilDate:inMode:dequeue:"); @@ -5426,85 +5588,156 @@ static HMODULE wglinstance = NULL; win->event.inFocus = (bool) objc_msgSend_bool(win->src.window, sel_registerName("isKeyWindow")); switch (objc_msgSend_uint(e, sel_registerName("type"))) { - case NSEventTypeKeyDown: - win->event.type = RGFW_keyPressed; - win->event.keyCode = (u16) objc_msgSend_uint(e, sel_registerName("keyCode")); - win->event.keyName = (const char*) NSString_to_char(objc_msgSend_id(e, sel_registerName("characters"))); + case NSEventTypeKeyDown: + win->event.keyCode = (u16) objc_msgSend_uint(e, sel_registerName("keyCode")); + RGFW_keyBoard_prev[win->event.keyCode] = RGFW_keyBoard[win->event.keyCode]; - RGFW_keyMap[win->event.keyCode] = 1; - break; + win->event.type = RGFW_keyPressed; + win->event.keyName = (char*)(const char*) NSString_to_char(objc_msgSend_id(e, sel_registerName("characters"))); - case NSEventTypeKeyUp: - win->event.type = RGFW_keyReleased; - win->event.keyCode = (u16) objc_msgSend_uint(e, sel_registerName("keyCode")); - win->event.keyName = (const char*) NSString_to_char(objc_msgSend_id(e, sel_registerName("characters"))); + RGFW_keyBoard[win->event.keyCode] = 1; + break; - RGFW_keyMap[win->event.keyCode] = 0; - break; + case NSEventTypeKeyUp: + win->event.keyCode = (u16) objc_msgSend_uint(e, sel_registerName("keyCode")); - case NSEventTypeLeftMouseDragged: - case NSEventTypeOtherMouseDragged: - case NSEventTypeRightMouseDragged: - case NSEventTypeMouseMoved: - win->event.type = RGFW_mousePosChanged; - NSPoint p = ((NSPoint(*)(id, SEL)) objc_msgSend)(e, sel_registerName("locationInWindow")); + RGFW_keyBoard_prev[win->event.keyCode] = RGFW_keyBoard[win->event.keyCode]; - win->event.point = RGFW_VECTOR((u32) p.x, (u32) (win->r.h - p.y)); + win->event.type = RGFW_keyReleased; + win->event.keyName = (char*)(const char*) NSString_to_char(objc_msgSend_id(e, sel_registerName("characters"))); - if (win->src.winArgs & RGFW_HOLD_MOUSE) { - RGFW_vector mouse = RGFW_getGlobalMousePoint(); - if ((mouse.x != win->r.x + (win->r.w / 2) || mouse.y != win->r.y + (win->r.h / 2))) { - RGFW_window_moveMouse(win, RGFW_VECTOR(win->r.x + (win->r.w / 2), win->r.y + (win->r.h / 2))); + RGFW_keyBoard[win->event.keyCode] = 0; + break; + + case NSEventTypeFlagsChanged: { + u32 flags = objc_msgSend_uint(e, sel_registerName("modifierFlags")); + memcpy(RGFW_keyBoard_prev + 55, RGFW_keyBoard + 55, 5); + + if ((flags & NSEventModifierFlagCapsLock) && !RGFW_wasPressedI(win, 57)) { + RGFW_keyBoard[57] = 1; + win->event.type = RGFW_keyPressed; + win->event.keyCode = 57; + break; + } if (!(flags & NSEventModifierFlagCapsLock) && RGFW_wasPressedI(win, 57)) { + RGFW_keyBoard[57] = 0; + win->event.type = RGFW_keyReleased; + win->event.keyCode = 57; + break; } + + if ((flags & NSEventModifierFlagOption) && !RGFW_wasPressedI(win, 58)) { + RGFW_keyBoard[58] = 1; + win->event.type = RGFW_keyPressed; + win->event.keyCode = 58; + break; + } if (!(flags & NSEventModifierFlagOption) && RGFW_wasPressedI(win, 58)) { + RGFW_keyBoard[58] = 0; + win->event.type = RGFW_keyReleased; + win->event.keyCode = 58; + break; + } + + if ((flags & NSEventModifierFlagControl) && !RGFW_wasPressedI(win, 59)) { + RGFW_keyBoard[59] = 1; + win->event.type = RGFW_keyPressed; + win->event.keyCode = 59; + break; + } if (!(flags & NSEventModifierFlagControl) && RGFW_wasPressedI(win, 59)) { + RGFW_keyBoard[59] = 0; + win->event.type = RGFW_keyReleased; + win->event.keyCode = 59; + break; + } + + if ((flags & NSEventModifierFlagCommand) && !RGFW_wasPressedI(win, 55)) { + RGFW_keyBoard[55] = 1; + win->event.type = RGFW_keyPressed; + win->event.keyCode = 55; + break; + } if (!(flags & NSEventModifierFlagCommand) && RGFW_wasPressedI(win, 55)) { + RGFW_keyBoard[55] = 0; + win->event.type = RGFW_keyReleased; + win->event.keyCode = 55; + break; + } + + if ((flags & NSEventModifierFlagShift) && !RGFW_wasPressedI(win, 56)) { + RGFW_keyBoard[56] = 1; + win->event.type = RGFW_keyPressed; + win->event.keyCode = 56; + break; + } if (!(flags & NSEventModifierFlagShift) && RGFW_wasPressedI(win, 56)) { + RGFW_keyBoard[56] = 0; + win->event.type = RGFW_keyReleased; + win->event.keyCode = 56; + break; + } + + break; } - break; + case NSEventTypeLeftMouseDragged: + case NSEventTypeOtherMouseDragged: + case NSEventTypeRightMouseDragged: + case NSEventTypeMouseMoved: + win->event.type = RGFW_mousePosChanged; + NSPoint p = ((NSPoint(*)(id, SEL)) objc_msgSend)(e, sel_registerName("locationInWindow")); - case NSEventTypeLeftMouseDown: - win->event.button = RGFW_mouseLeft; - win->event.type = RGFW_mouseButtonPressed; - break; + win->event.point = RGFW_VECTOR((u32) p.x, (u32) (win->r.h - p.y)); - case NSEventTypeOtherMouseDown: - win->event.button = RGFW_mouseMiddle; - win->event.type = RGFW_mouseButtonPressed; - break; + if (win->src.winArgs & RGFW_HOLD_MOUSE) { + RGFW_vector mouse = RGFW_getGlobalMousePoint(); + if ((mouse.x != win->r.x + (win->r.w / 2) || mouse.y != win->r.y + (win->r.h / 2))) { + RGFW_window_moveMouse(win, RGFW_VECTOR(win->r.x + (win->r.w / 2), win->r.y + (win->r.h / 2))); + } + } + break; - case NSEventTypeRightMouseDown: - win->event.button = RGFW_mouseRight; - win->event.type = RGFW_mouseButtonPressed; - break; + case NSEventTypeLeftMouseDown: + win->event.button = RGFW_mouseLeft; + win->event.type = RGFW_mouseButtonPressed; + break; - case NSEventTypeLeftMouseUp: - win->event.button = RGFW_mouseLeft; - win->event.type = RGFW_mouseButtonReleased; - break; + case NSEventTypeOtherMouseDown: + win->event.button = RGFW_mouseMiddle; + win->event.type = RGFW_mouseButtonPressed; + break; - case NSEventTypeOtherMouseUp: - win->event.button = RGFW_mouseMiddle; - win->event.type = RGFW_mouseButtonReleased; - break; + case NSEventTypeRightMouseDown: + win->event.button = RGFW_mouseRight; + win->event.type = RGFW_mouseButtonPressed; + break; - case NSEventTypeScrollWheel: { - double deltaY = ((CGFloat(*)(id, SEL))abi_objc_msgSend_fpret)(e, sel_registerName("deltaY")); + case NSEventTypeLeftMouseUp: + win->event.button = RGFW_mouseLeft; + win->event.type = RGFW_mouseButtonReleased; + break; - if (deltaY > 0) - win->event.button = RGFW_mouseScrollUp; + case NSEventTypeOtherMouseUp: + win->event.button = RGFW_mouseMiddle; + win->event.type = RGFW_mouseButtonReleased; + break; - else if (deltaY < 0) - win->event.button = RGFW_mouseScrollDown; + case NSEventTypeScrollWheel: { + double deltaY = ((CGFloat(*)(id, SEL))abi_objc_msgSend_fpret)(e, sel_registerName("deltaY")); - win->event.scroll = deltaY; + if (deltaY > 0) + win->event.button = RGFW_mouseScrollUp; - win->event.type = RGFW_mouseButtonReleased; - break; - } - case NSEventTypeRightMouseUp: - win->event.button = RGFW_mouseRight; - win->event.type = RGFW_mouseButtonReleased; - break; + else if (deltaY < 0) + win->event.button = RGFW_mouseScrollDown; - default: - break; + win->event.scroll = deltaY; + + win->event.type = RGFW_mouseButtonReleased; + break; + } + case NSEventTypeRightMouseUp: + win->event.button = RGFW_mouseRight; + win->event.type = RGFW_mouseButtonReleased; + break; + + default: + break; } objc_msgSend_void_id(NSApp, sel_registerName("sendEvent:"), e); @@ -5617,6 +5850,8 @@ static HMODULE wglinstance = NULL; } void RGFW_window_showMouse(RGFW_window* win, i8 show) { + RGFW_UNUSED(win); + if (show) { CGDisplayShowCursor(kCGDirectMainDisplay); } @@ -5626,11 +5861,13 @@ static HMODULE wglinstance = NULL; } void RGFW_window_setMouseStandard(RGFW_window* win, void* mouse) { + RGFW_UNUSED(win); CGDisplayShowCursor(kCGDirectMainDisplay); objc_msgSend_void(mouse, sel_registerName("set")); } void RGFW_window_moveMouse(RGFW_window* win, RGFW_vector v) { + RGFW_UNUSED(win); assert(win != NULL); CGWarpMouseCursorPosition(CGPointMake(v.x, v.y)); @@ -5716,6 +5953,7 @@ static HMODULE wglinstance = NULL; } u8 RGFW_isPressedI(RGFW_window* win, u32 key) { + RGFW_UNUSED(win); if (key >= 128) { #ifdef RGFW_PRINT_ERRORS fprintf(stderr, "RGFW_isPressedI : invalid keycode\n"); @@ -5723,7 +5961,19 @@ static HMODULE wglinstance = NULL; RGFW_error = 1; } - return RGFW_keyMap[key]; + return RGFW_keyBoard[key]; + } + + u8 RGFW_wasPressedI(RGFW_window* win, u32 key) { + RGFW_UNUSED(win); + if (key >= 128) { +#ifdef RGFW_PRINT_ERRORS + fprintf(stderr, "RGFW_wasPressedI : invalid keycode\n"); +#endif + RGFW_error = 1; + } + + return RGFW_keyBoard_prev[key]; } #ifdef __cplusplus @@ -5740,6 +5990,8 @@ static HMODULE wglinstance = NULL; } void RGFW_writeClipboard(const char* text, u32 textLen) { + RGFW_UNUSED(textLen); + NSPasteboardType array[] = { NSPasteboardTypeString, NULL }; NSPasteBoard_declareTypes(NSPasteboard_generalPasteboard(), array, 1, NULL); @@ -5747,12 +5999,16 @@ static HMODULE wglinstance = NULL; } u16 RGFW_registerJoystick(RGFW_window* win, i32 jsNumber) { + RGFW_UNUSED(jsNumber); + assert(win != NULL); return RGFW_registerJoystickF(win, (char*) ""); } u16 RGFW_registerJoystickF(RGFW_window* win, char* file) { + RGFW_UNUSED(file); + assert(win != NULL); return win->src.joystickCount - 1; @@ -5820,9 +6076,11 @@ static HMODULE wglinstance = NULL; #ifndef RGFW_NO_THREADS #include - RGFW_thread RGFW_createThread(void* (*function_ptr)(void*), void* args) { + RGFW_thread RGFW_createThread(RGFW_threadFunc_ptr ptr, void* args) { + RGFW_UNUSED(args); + RGFW_thread t; - pthread_create((pthread_t*) &t, NULL, *function_ptr, NULL); + pthread_create((pthread_t*) &t, NULL, *ptr, NULL); return t; } void RGFW_cancelThread(RGFW_thread thread) { pthread_cancel((pthread_t) thread); } @@ -5848,7 +6106,7 @@ static HMODULE wglinstance = NULL; #endif #else #ifdef RGFW_EGL - eglMakeCurrent(win->src.EGL_display, win->src.EGL_surface, win->src.EGL_surface, win->src.rSurf); + eglMakeCurrent(win->src.EGL_display, win->src.EGL_surface, win->src.EGL_surface, win->src.EGL_context); #endif #endif @@ -5927,9 +6185,6 @@ static HMODULE wglinstance = NULL; void RGFW_window_swapBuffers(RGFW_window* win) { assert(win != NULL); - win->event.frames++; - RGFW_window_checkFPS(win); - RGFW_window_makeCurrent(win); /* clear the window*/ @@ -5958,10 +6213,10 @@ static HMODULE wglinstance = NULL; RGFW_area area = RGFW_getScreenSize(); #ifndef RGFW_X11_DONT_CONVERT_BGR - win->src.bitmap->data = (const char*) win->buffer; + win->src.bitmap->data = (char*) win->buffer; u32 x, y; - for (y = 0; y < win->r.h; y++) { - for (x = 0; x < win->r.w; x++) { + for (y = 0; y < (u32)win->r.h; y++) { + for (x = 0; x < (u32)win->r.w; x++) { u32 index = (y * 4 * area.w) + x * 4; u8 red = win->src.bitmap->data[index]; @@ -5992,7 +6247,7 @@ static HMODULE wglinstance = NULL; "NSDeviceRGBColorSpace", 0, area.w * 4, 32 ); - id image = NSAlloc(objc_getClass("NSImage")); + id image = NSAlloc((id)objc_getClass("NSImage")); NSImage_addRepresentation(image, rep); objc_msgSend_void_id(layer, sel_registerName("setContents:"), (id) image); @@ -6009,28 +6264,25 @@ static HMODULE wglinstance = NULL; #endif } - if (win->src.winArgs & RGFW_NO_GPU_RENDER) - return; + if (!(win->src.winArgs & RGFW_NO_GPU_RENDER)) { + #ifdef RGFW_EGL + eglSwapBuffers(win->src.EGL_display, win->src.EGL_surface); + #elif defined(RGFW_OPENGL) + #if defined(RGFW_X11) && defined(RGFW_OPENGL) + glXSwapBuffers((Display*) win->src.display, (Window) win->src.window); + #elif defined(RGFW_WINDOWS) + SwapBuffers(win->src.hdc); + #elif defined(RGFW_MACOS) + NSOpenGLContext_flushBuffer(win->src.rSurf); + #endif + #endif -#ifdef RGFW_OPENGL -#ifdef RGFW_EGL - eglSwapBuffers(win->src.EGL_display, win->src.EGL_surface); -#else -#if defined(RGFW_X11) && defined(RGFW_OPENGL) - glXSwapBuffers((Display*) win->src.display, (Window) win->src.window); -#endif -#ifdef RGFW_WINDOWS - SwapBuffers(win->src.hdc); -#endif -#if defined(RGFW_MACOS) - NSOpenGLContext_flushBuffer(win->src.rSurf); -#endif -#endif -#endif + #if defined(RGFW_WINDOWS) && defined(RGFW_DIRECTX) + win->src.swapchain->lpVtbl->Present(win->src.swapchain, 0, 0); + #endif + } -#if defined(RGFW_WINDOWS) && defined(RGFW_DIRECTX) - win->src.swapchain->lpVtbl->Present(win->src.swapchain, 0, 0); -#endif + RGFW_window_checkFPS(win); } void RGFW_window_maximize(RGFW_window* win) { @@ -6072,11 +6324,11 @@ static HMODULE wglinstance = NULL; #endif } - void RGFW_sleep(u32 ms) { + void RGFW_sleep(u64 ms) { #ifndef RGFW_WINDOWS struct timespec time; time.tv_sec = 0; - time.tv_nsec = ms * 1000; + time.tv_nsec = ms * 1e+6; nanosleep(&time, NULL); #else @@ -6084,32 +6336,31 @@ static HMODULE wglinstance = NULL; #endif } - static float currentFrameTime = 0; - void RGFW_window_checkFPS(RGFW_window* win) { - assert(win != NULL); + u64 deltaTime = RGFW_getTimeNS() - win->event.frameTime; - win->event.fps = RGFW_getFPS(); + u64 fps = round(1e+9 / deltaTime); + win->event.fps = fps; - if (win->fpsCap == 0) - return; + if (win->fpsCap && fps > win->fpsCap) { + u64 frameTimeNS = 1e+9 / win->fpsCap; + u64 sleepTimeMS = (frameTimeNS - deltaTime) / 1e6; - double targetFrameTime = 1.0 / win->fpsCap; - double elapsedTime = RGFW_getTime() - currentFrameTime; - - if (elapsedTime < targetFrameTime) { - u32 sleepTime = (u32) ((targetFrameTime - elapsedTime) * 1e3); - RGFW_sleep(sleepTime); + if (sleepTimeMS > 0) { + RGFW_sleep(sleepTimeMS); + win->event.frameTime = 0; + } } - currentFrameTime = (float) RGFW_getTime(); + win->event.frameTime = RGFW_getTimeNS(); + + if (win->fpsCap) { + u64 deltaTime = RGFW_getTimeNS() - win->event.frameTime2; - if (elapsedTime < targetFrameTime) { - u32 sleepTime = (u32) ((targetFrameTime - elapsedTime) * 1e3); - RGFW_sleep(sleepTime); + win->event.fps = round(1e+9 / deltaTime); + + win->event.frameTime2 = RGFW_getTimeNS(); } - - currentFrameTime = (float) RGFW_getTime(); } #ifdef __APPLE__ @@ -6127,7 +6378,7 @@ static HMODULE wglinstance = NULL; return (u64) (counter.QuadPart * 1e9 / frequency.QuadPart); #elif defined(__unix__) struct timespec ts = { 0 }; - clock_gettime(CLOCK_MONOTONIC, &ts); + clock_gettime(1, &ts); unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; return nanoSeconds; @@ -6151,7 +6402,7 @@ static HMODULE wglinstance = NULL; return (u64) (counter.QuadPart / (double) frequency.QuadPart); #elif defined(__unix__) struct timespec ts = { 0 }; - clock_gettime(CLOCK_MONOTONIC, &ts); + clock_gettime(1, &ts); unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; return (double)(nanoSeconds) * 1e-9; @@ -6165,27 +6416,6 @@ static HMODULE wglinstance = NULL; return 0; } - u32 RGFW_getFPS(void) { - static double previousSeconds = 0.0; - if (previousSeconds == 0.0) - previousSeconds = (double) RGFW_getTime();//glfwGetTime(); - - static i16 frameCount; - double currentSeconds = (double) RGFW_getTime();//glfwGetTime(); - double elapsedSeconds = currentSeconds - previousSeconds; - static double fps = 0; - - if (elapsedSeconds > 0.25) { - previousSeconds = currentSeconds; - fps = (double) frameCount / elapsedSeconds; - frameCount = 0; - } - - frameCount++; - - return (u32) fps; - } - #endif /*RGFW_IMPLEMENTATION*/ #define RGFW_Escape RGFW_OS_BASED_VALUE(0xff1b, 0x1B, 53) diff --git a/src/platforms/rcore_desktop_rgfw.c b/src/platforms/rcore_desktop_rgfw.c index 623dc0e7..f4e31749 100644 --- a/src/platforms/rcore_desktop_rgfw.c +++ b/src/platforms/rcore_desktop_rgfw.c @@ -431,7 +431,11 @@ void SetWindowFocused(void) // Get native window handle void *GetWindowHandle(void) { - return platform.window->src.window; + #ifndef RGFW_WINDOWS + return (void*)platform.window->src.window; + #else + return platform.window->src.hwnd; + #endif } // Get number of monitors @@ -890,8 +894,6 @@ void PollInputEvents(void) CORE.Input.Mouse.currentPosition.x = (event->point.x - (platform.window->r.w / 2)) * 2; CORE.Input.Mouse.currentPosition.y = (event->point.y - (platform.window->r.h / 2)) * 2; - - RGFW_window_showMouse(platform.window, 1); } else { CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;