From ee0c73bf25a403d498a072cd4cb1335ddbb0df81 Mon Sep 17 00:00:00 2001 From: Marcos Slomp Date: Sun, 14 Jun 2026 11:24:14 -0700 Subject: [PATCH] switch to SDL2 (no cmake fetch, just find_package) --- examples/opengl/triangle/CMakeLists.txt | 28 ++---- .../triangle/platform/platform_rgfw.cpp | 97 ------------------- .../triangle/platform/platform_sdl2.cpp | 89 +++++++++++++++++ 3 files changed, 97 insertions(+), 117 deletions(-) delete mode 100644 examples/opengl/triangle/platform/platform_rgfw.cpp create mode 100644 examples/opengl/triangle/platform/platform_sdl2.cpp diff --git a/examples/opengl/triangle/CMakeLists.txt b/examples/opengl/triangle/CMakeLists.txt index 342c6075..47f224e2 100644 --- a/examples/opengl/triangle/CMakeLists.txt +++ b/examples/opengl/triangle/CMakeLists.txt @@ -4,7 +4,7 @@ # cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -B build/ninja . # cmake --build build/ninja # -# Linux (requires libx11-dev libgl1-mesa-dev): +# Linux (requires libsdl2-dev libgl1-mesa-dev): # cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -B build/ninja . # cmake --build build/ninja # @@ -22,20 +22,15 @@ set(TRACY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..") option(TRACY_ENABLE "Enable Tracy profiling" ON) # --------------------------------------------------------------------------- -# Platform — RGFW (cross-platform windowing, fetched automatically) +# Platform — SDL2 (cross-platform windowing, must be installed on the system) # --------------------------------------------------------------------------- -include(FetchContent) -FetchContent_Declare(rgfw - GIT_REPOSITORY https://github.com/ColleagueRiley/RGFW.git - GIT_TAG main # pin to a specific commit for reproducible builds - GIT_SHALLOW TRUE -) -FetchContent_MakeAvailable(rgfw) +find_package(SDL2 REQUIRED) # --------------------------------------------------------------------------- # GL extension loader — GLEW (Windows + Linux, fetched automatically) # --------------------------------------------------------------------------- if(NOT APPLE) + include(FetchContent) set(glew-cmake_BUILD_SHARED OFF CACHE BOOL "" FORCE) set(ONLY_LIBS ON CACHE BOOL "" FORCE) FetchContent_Declare(glew @@ -46,20 +41,14 @@ if(NOT APPLE) FetchContent_MakeAvailable(glew) endif() -set(PLATFORM_SOURCES platform/platform_rgfw.cpp) -set(PLATFORM_INCLUDES ${rgfw_SOURCE_DIR}) +set(PLATFORM_SOURCES platform/platform_sdl2.cpp) if(APPLE) - set(PLATFORM_LIBS "-framework Cocoa" "-framework OpenGL" - "-framework CoreVideo" "-framework IOKit") + set(PLATFORM_LIBS SDL2::SDL2 "-framework OpenGL") elseif(WIN32) - set(PLATFORM_LIBS opengl32 user32 gdi32 libglew_static) + set(PLATFORM_LIBS SDL2::SDL2 opengl32 libglew_static) else() - find_package(X11 REQUIRED) - if(NOT X11_Xrandr_FOUND) - message(FATAL_ERROR "Xrandr not found — install libxrandr-dev") - endif() - set(PLATFORM_LIBS X11::X11 X11::Xrandr GL libglew_static) + set(PLATFORM_LIBS SDL2::SDL2 GL libglew_static) endif() # --------------------------------------------------------------------------- @@ -90,6 +79,5 @@ endif() target_include_directories(gl_spinning_triangle PRIVATE "${TRACY_DIR}/public" - ${PLATFORM_INCLUDES} ) target_link_libraries(gl_spinning_triangle PRIVATE ${PLATFORM_LIBS}) diff --git a/examples/opengl/triangle/platform/platform_rgfw.cpp b/examples/opengl/triangle/platform/platform_rgfw.cpp deleted file mode 100644 index 4275bd2f..00000000 --- a/examples/opengl/triangle/platform/platform_rgfw.cpp +++ /dev/null @@ -1,97 +0,0 @@ -// platform_rgfw.cpp — RGFW windowing backend (cross-platform) -// https://github.com/ColleagueRiley/RGFW - -#include "platform.h" // GL headers first (gl3.h / glew.h) so RGFW sees guards set - -#define RGFW_OPENGL -#define RGFW_IMPLEMENTATION -#include - -#include -#include - -#if defined(__linux__) -#include -static bool platformHasDisplay() { - // RGFW workaround: RGFW indiscriminately passes XOpenDisplay(0) unchecked - // to X11 functions like XCreateWindow(), which will lead to SIGSEGV. - Display* display = XOpenDisplay(0); - if (display == nullptr) { - fprintf(stderr, "ERROR: failed to open X11 display (is $DISPLAY set?)\n"); - return false; - } - XCloseDisplay(display); - return true; -} -#else -static bool platformHasDisplay() { - return true; -} -#endif - -static RGFW_window* sWin = nullptr; -static std::chrono::steady_clock::time_point sStartTime; - -bool platformInit(int width, int height, const char* title) { - if (!platformHasDisplay()) { - fprintf(stderr, "ERROR: no display found\n"); - return false; - } - - RGFW_glHints* hints = RGFW_getGlobalHints_OpenGL(); - hints->major = 3; - hints->minor = 3; - RGFW_setGlobalHints_OpenGL(hints); - - sWin = RGFW_createWindow(title, 0, 0, width, height, - RGFW_windowCenter | RGFW_windowOpenGL); - if (!sWin) { - fprintf(stderr, "ERROR: failed to create window\n"); - return false; - } - RGFW_window_makeCurrentContext_OpenGL(sWin); - RGFW_window_swapInterval_OpenGL(sWin, 1); - RGFW_window_setExitKey(sWin, RGFW_keyEscape); - - sStartTime = std::chrono::steady_clock::now(); - return true; -} - -bool platformInitGL() { -#ifndef __APPLE__ - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) { - fprintf(stderr, "Failed to initialize GLEW\n"); - return false; - } -#endif - return true; -} - -double platformGetTime() { - return std::chrono::duration( - std::chrono::steady_clock::now() - sStartTime).count(); -} - -void platformSwapBuffers() { RGFW_window_swapBuffers_OpenGL(sWin); } - -void platformGetPixelDensityScale(float* x, float* y) { - i32 pw, ph; - RGFW_window_getSizeInPixels(sWin, &pw, &ph); - *x = (float)pw / (float)sWin->w; - *y = (float)ph / (float)sWin->h; -} - -void platformRunLoop(void (*render)(), void (*shutdown)()) { - while (RGFW_window_shouldClose(sWin) == RGFW_FALSE) { - RGFW_event event; - while (RGFW_window_checkEvent(sWin, &event)) { - if (event.type == RGFW_windowClose) goto done; - } - render(); - } -done: - shutdown(); - RGFW_window_close(sWin); - sWin = nullptr; -} diff --git a/examples/opengl/triangle/platform/platform_sdl2.cpp b/examples/opengl/triangle/platform/platform_sdl2.cpp new file mode 100644 index 00000000..f6e95dd6 --- /dev/null +++ b/examples/opengl/triangle/platform/platform_sdl2.cpp @@ -0,0 +1,89 @@ +// platform_sdl2.cpp — SDL2 windowing backend (cross-platform) + +#include "platform.h" // GL headers first (gl3.h / glew.h) so SDL sees guards set + +#define SDL_MAIN_HANDLED // we don't want SDL_main +#include + +#include +#include + +static SDL_Window* sWin = nullptr; +static SDL_GLContext sCtx = nullptr; +static std::chrono::steady_clock::time_point sStartTime; + +bool platformInit(int width, int height, const char* title) { + SDL_SetMainReady(); + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "ERROR: SDL_Init failed: %s\n", SDL_GetError()); + return false; + } + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + sWin = SDL_CreateWindow(title, + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + width, height, + SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI); + if (!sWin) { + fprintf(stderr, "ERROR: SDL_CreateWindow failed: %s\n", SDL_GetError()); + SDL_Quit(); + return false; + } + + sCtx = SDL_GL_CreateContext(sWin); + if (!sCtx) { + fprintf(stderr, "ERROR: SDL_GL_CreateContext failed: %s\n", SDL_GetError()); + SDL_DestroyWindow(sWin); + SDL_Quit(); + return false; + } + + SDL_GL_SetSwapInterval(1); + sStartTime = std::chrono::steady_clock::now(); + return true; +} + +bool platformInitGL() { +#ifndef __APPLE__ + glewExperimental = GL_TRUE; + if (glewInit() != GLEW_OK) { + fprintf(stderr, "Failed to initialize GLEW\n"); + return false; + } +#endif + return true; +} + +double platformGetTime() { + return std::chrono::duration( + std::chrono::steady_clock::now() - sStartTime).count(); +} + +void platformSwapBuffers() { SDL_GL_SwapWindow(sWin); } + +void platformGetPixelDensityScale(float* x, float* y) { + int pw, ph, ww, wh; + SDL_GL_GetDrawableSize(sWin, &pw, &ph); + SDL_GetWindowSize(sWin, &ww, &wh); + *x = (ww > 0) ? (float)pw / (float)ww : 1.0f; + *y = (wh > 0) ? (float)ph / (float)wh : 1.0f; +} + +void platformRunLoop(void (*render)(), void (*shutdown)()) { + bool running = true; + while (running) { + SDL_Event e; + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) running = false; + if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) running = false; + } + if (running) render(); + } + shutdown(); + SDL_GL_DeleteContext(sCtx); + SDL_DestroyWindow(sWin); + SDL_Quit(); +}