From fae6c725ec5a29b5c07195caea0f2763b3943f96 Mon Sep 17 00:00:00 2001 From: Sean Baggaley Date: Wed, 30 Jul 2025 21:38:05 +0100 Subject: [PATCH] SDL: Set GL attributes before creating the window This is required as on some platforms the attributes are needed at window creation time. For example, EGL uses some attributes like bit depths as part of the surface (which SDL creates alongside the window), and not the context. --- src/Window_SDL2.c | 11 +++++++++-- src/Window_SDL3.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Window_SDL2.c b/src/Window_SDL2.c index 9f8d101c9..4c0b5dcbb 100644 --- a/src/Window_SDL2.c +++ b/src/Window_SDL2.c @@ -38,6 +38,8 @@ typedef struct _WINDATA { #endif +static void SetGLAttributes(); + static void RefreshWindowBounds(void) { SDL_GetWindowSize(win_handle, &Window_Main.Width, &Window_Main.Height); } @@ -99,7 +101,10 @@ static void DoCreateWindow(int width, int height, int flags) { void Window_Create2D(int width, int height) { DoCreateWindow(width, height, 0); } #if CC_GFX_BACKEND_IS_GL() -void Window_Create3D(int width, int height) { DoCreateWindow(width, height, SDL_WINDOW_OPENGL); } +void Window_Create3D(int width, int height) { + SetGLAttributes(); + DoCreateWindow(width, height, SDL_WINDOW_OPENGL); +} #else void Window_Create3D(int width, int height) { DoCreateWindow(width, height, 0); } #endif @@ -537,7 +542,7 @@ void Gamepads_Process(float delta) { #if CC_GFX_BACKEND_IS_GL() static SDL_GLContext win_ctx; -void GLContext_Create(void) { +void SetGLAttributes(void) { struct GraphicsMode mode; InitGraphicsMode(&mode); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, mode.R); @@ -553,7 +558,9 @@ void GLContext_Create(void) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif +} +void GLContext_Create(void) { win_ctx = SDL_GL_CreateContext(win_handle); if (!win_ctx) Window_SDLFail("creating OpenGL context"); } diff --git a/src/Window_SDL3.c b/src/Window_SDL3.c index 351c72610..0fd8edce9 100644 --- a/src/Window_SDL3.c +++ b/src/Window_SDL3.c @@ -11,6 +11,8 @@ static SDL_Window* win_handle; static Uint32 dlg_event; +static void SetGLAttributes(); + static void RefreshWindowBounds(void) { SDL_GetWindowSize(win_handle, &Window_Main.Width, &Window_Main.Height); } @@ -84,7 +86,10 @@ static void DoCreateWindow(int width, int height, int flags) { void Window_Create2D(int width, int height) { DoCreateWindow(width, height, 0); } #if CC_GFX_BACKEND_IS_GL() -void Window_Create3D(int width, int height) { DoCreateWindow(width, height, SDL_WINDOW_OPENGL); } +void Window_Create3D(int width, int height) { + SetGLAttributes(); + DoCreateWindow(width, height, SDL_WINDOW_OPENGL); +} #else void Window_Create3D(int width, int height) { DoCreateWindow(width, height, 0); } #endif @@ -567,7 +572,7 @@ void Gamepads_Process(float delta) { #if CC_GFX_BACKEND_IS_GL() static SDL_GLContext win_ctx; -void GLContext_Create(void) { +void SetGLAttributes(void) { struct GraphicsMode mode; InitGraphicsMode(&mode); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, mode.R); @@ -583,7 +588,9 @@ void GLContext_Create(void) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif +} +void GLContext_Create(void) { win_ctx = SDL_GL_CreateContext(win_handle); if (!win_ctx) Window_SDLFail("creating OpenGL context"); }