Skip to content

Commit

Permalink
Fix MSVC errors for PLATFORM_DESKTOP_RGFW (#4441)
Browse files Browse the repository at this point in the history
* (rcore_desktop_rgfw.c) fix errors when compiling with mingw

* define WideCharToMultiByte

* update RGFW

* move stdcall def to windows only

* fix raw cursor input

* Fix warnings, update RGFW, fix msvc errors (make sure windows macro _WIN32 is correct)
  • Loading branch information
ColleagueRiley authored Oct 26, 2024
1 parent 38cf9f3 commit 7fe5f71
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
50 changes: 36 additions & 14 deletions src/external/RGFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define RGFW_OPENGL_ES2 - (optional) use OpenGL ES (version 2)
#define RGFW_OPENGL_ES3 - (optional) use OpenGL ES (version 3)
#define RGFW_DIRECTX - (optional) use directX for the rendering backend (rather than opengl) (windows only, defaults to opengl for unix)
#define RGFW_WEBGPU - (optional) use webGPU for rendering (Web ONLY)
#define RGFW_NO_API - (optional) don't use any rendering API (no opengl, no vulkan, no directX)
#define RGFW_LINK_EGL (optional) (windows only) if EGL is being used, if EGL functions should be defined dymanically (using GetProcAddress)
Expand Down Expand Up @@ -84,6 +85,9 @@
Rob Rohan -> X11 bugs and missing features, MacOS/Cocoa fixing memory issues/bugs
AICDG (@THISISAGOODNAME) -> vulkan support (example)
@Easymode -> support, testing/debugging, bug fixes and reviews
Joshua Rowe (omnisci3nce) - bug fix, review (macOS)
@lesleyrs -> bug fix, review (OpenGL)
Nick Porcino (meshula) - testing, organization, review (MacOS, examples)
*/

#if _MSC_VER
Expand Down Expand Up @@ -201,7 +205,7 @@
#ifdef __EMSCRIPTEN__
#define RGFW_WEBASM

#ifndef RGFW_NO_API
#if !defined(RGFW_NO_API) && !defined(RGFW_WEBGPU)
#define RGFW_OPENGL
#endif

Expand All @@ -211,6 +215,10 @@

#include <emscripten/html5.h>
#include <emscripten/key_codes.h>

#ifdef RGFW_WEBGPU
#include <emscripten/html5_webgpu.h>
#endif
#endif

#if defined(RGFW_X11) && defined(__APPLE__)
Expand Down Expand Up @@ -573,7 +581,13 @@ typedef struct RGFW_window_src {
void* image;
#endif
#elif defined(RGFW_WEBASM)
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;
#ifdef RGFW_WEBGPU
WGPUInstance ctx;
WGPUDevice device;
WGPUQueue queue;
#else
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx;
#endif
#endif
} RGFW_window_src;

Expand Down Expand Up @@ -1180,9 +1194,9 @@ int main() {
static : ar rcs RGFW.a RGFW.o
shared :
windows:
gcc -shared RGFW.o -lopengl32 -lshell32 -lgdi32 -o RGFW.dll
gcc -shared RGFW.o -lwinmm -lopengl32 -lshell32 -lgdi32 -o RGFW.dll
linux:
gcc -shared RGFW.o -lX11 -lXcursor -lGL -o RGFW.so
gcc -shared RGFW.o -lX11 -lXcursor -lGL -lXrandr -o RGFW.so
macos:
gcc -shared RGFW.o -framework Foundation -framework AppKit -framework OpenGL -framework CoreVideo
*/
Expand Down Expand Up @@ -1406,7 +1420,7 @@ char RGFW_keyCodeToChar(u32 keycode, b8 shift) {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '`', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '-', '=', 0, '\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', ',', '/', '[', ']', ';', '\n', '\'', '\\',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '/', '*', '-', '1', '2', '3', '3', '5', '6', '7', '8', '9', '0', '\n'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '/', '*', '-', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\n'
};

static const char mapCaps[] = {
Expand Down Expand Up @@ -1969,7 +1983,7 @@ void RGFW_updateLockState(RGFW_window* win, b8 capital, b8 numlock) {

size_t index = (sizeof(attribs) / sizeof(attribs[0])) - 13;

#define RGFW_GL_ADD_ATTRIB(attrib, attVal) \
#define RGFW_GL_ADD_ATTRIB(attrib, attVal) \
if (attVal) { \
attribs[index] = attrib;\
attribs[index + 1] = attVal;\
Expand Down Expand Up @@ -2522,7 +2536,8 @@ Start of Linux / Unix defines
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 || best_fbc == -1)) {
if ((!(args & RGFW_TRANSPARENT_WINDOW) || vi->depth == 32) &&
(best_fbc < 0 || samp_buf) && (samples == RGFW_SAMPLES || best_fbc == -1)) {
best_fbc = i;
}
}
Expand All @@ -2538,11 +2553,6 @@ Start of Linux / Unix defines
XVisualInfo* vi = glXGetVisualFromFBConfig((Display*) win->src.display, bestFbc);

XFree(fbc);

if (args & RGFW_TRANSPARENT_WINDOW) {
XMatchVisualInfo((Display*) win->src.display, DefaultScreen((Display*) win->src.display), 32, TrueColor, vi); /*!< for RGBA backgrounds*/
}

#else
XVisualInfo viNorm;

Expand Down Expand Up @@ -8547,7 +8557,8 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
RGFW_UNUSED(RGFW_initFormatAttribs);

RGFW_window* win = RGFW_window_basic_init(rect, args);


#ifndef RGFW_WEBGPU
EmscriptenWebGLContextAttributes attrs;
attrs.alpha = EM_TRUE;
attrs.depth = EM_TRUE;
Expand Down Expand Up @@ -8576,6 +8587,11 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
#ifdef LEGACY_GL_EMULATION
EM_ASM("Module.useWebGL = true; GLImmediate.init();");
#endif
#else
win->src.ctx = wgpuCreateInstance(NULL);
win->src.device = emscripten_webgpu_get_device();
win->src.queue = wgpuDeviceGetQueue(win->src.device);
#endif

emscripten_set_canvas_element_size("#canvas", rect.w, rect.h);
emscripten_set_window_title(name);
Expand Down Expand Up @@ -8871,24 +8887,30 @@ void RGFW_window_swapBuffers(RGFW_window* win) {
}
#endif

#ifndef RGFW_WEBGPU
emscripten_webgl_commit_frame();
#endif
emscripten_sleep(0);
}


void RGFW_window_makeCurrent_OpenGL(RGFW_window* win) {
#ifndef RGFW_WEBGPU
if (win == NULL)
emscripten_webgl_make_context_current(0);
else
emscripten_webgl_make_context_current(win->src.ctx);
#endif
}

#ifndef RGFW_EGL
void RGFW_window_swapInterval(RGFW_window* win, i32 swapInterval) { RGFW_UNUSED(win); RGFW_UNUSED(swapInterval); }
#endif

void RGFW_window_close(RGFW_window* win) {
emscripten_webgl_destroy_context(win->src.ctx);
#ifndef RGFW_WEBGPU
emscripten_webgl_destroy_context(win->src.ctx);
#endif

free(win);
}
Expand Down
16 changes: 11 additions & 5 deletions src/platforms/rcore_desktop_rgfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ void CloseWindow(void);

#define RGFW_IMPLEMENTATION

#if defined(__WIN32) || defined(__WIN64)
#if defined(_WIN32) || defined(_WIN64)
#define WIN32_LEAN_AND_MEAN
#define Rectangle rectangle_win32
#define Rectangle rectangle_win32
#define CloseWindow CloseWindow_win32
#define ShowCursor __imp_ShowCursor
#define _APISETSTRING_
__declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char *lpMultiByteStr, int cbMultiByte, wchar_t *lpWideCharStr, int cchWideChar);
#define _APISETSTRING_

#undef MAX_PATH

__declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char *lpMultiByteStr, int cbMultiByte, wchar_t *lpWideCharStr, int cchWideChar);
#endif

#if defined(__APPLE__)
Expand All @@ -79,11 +82,14 @@ void CloseWindow(void);

#include "../external/RGFW.h"

#if defined(__WIN32) || defined(__WIN64)
#if defined(_WIN32) || defined(_WIN64)
#undef DrawText
#undef ShowCursor
#undef CloseWindow
#undef Rectangle

#undef MAX_PATH
#define MAX_PATH 1025
#endif

#if defined(__APPLE__)
Expand Down
4 changes: 2 additions & 2 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@
__declspec(dllimport) unsigned long __stdcall GetModuleFileNameA(void *hModule, void *lpFilename, unsigned long nSize);
__declspec(dllimport) unsigned long __stdcall GetModuleFileNameW(void *hModule, void *lpFilename, unsigned long nSize);
__declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, void *widestr, int cchwide, void *str, int cbmb, void *defchar, int *used_default);
unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod);
unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
__declspec(dllimport) unsigned int __stdcall timeBeginPeriod(unsigned int uPeriod);
__declspec(dllimport) unsigned int __stdcall timeEndPeriod(unsigned int uPeriod);
#elif defined(__linux__)
#include <unistd.h>
#elif defined(__FreeBSD__)
Expand Down

0 comments on commit 7fe5f71

Please sign in to comment.