From cd5b59a17639a453a8fd73f932107f102cac6529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Wandrol?= <80282710+YewIsHot@users.noreply.github.com> Date: Wed, 5 Nov 2025 20:58:00 +0100 Subject: [PATCH] Update for SDL3 --- src/cppgraphics.cpp | 52 ++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/cppgraphics.cpp b/src/cppgraphics.cpp index f2fd094..f54ab09 100644 --- a/src/cppgraphics.cpp +++ b/src/cppgraphics.cpp @@ -24,7 +24,7 @@ #include #include -#include "SDL2/SDL.h" +#include "SDL3/SDL.h" #ifdef CPPGRAPHICS_SUPPORT_IMGUI @@ -13594,10 +13594,10 @@ void create_window(const std::string& title, double width, double height, bool f // Create window and context. g_state.window = SDL_CreateWindow( - "", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, // initial pos + "", 20, 20, // will be changed later SDL_WINDOW_OPENGL - | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) + | (fullscreen ? SDL_EVENT_WINDOW_LEAVE_FULLSCREEN : 0) | SDL_WINDOW_RESIZABLE); // Following functions may fail. Throwing is not an option (WASM). @@ -13707,7 +13707,7 @@ void close_window() imgui_cleanup(); #endif if (g_state.context) - SDL_GL_DeleteContext( g_state.context ); + SDL_GL_DestroyContext( g_state.context ); g_state.context = nullptr; SDL_DestroyWindow(g_state.window); g_state.window = nullptr; @@ -13895,17 +13895,17 @@ static int process_events(double timeout, bool wait_for_key, bool wait_for_mouse ImGui_ImplSDL2_ProcessEvent(&event); #endif - if (event.type == SDL_QUIT) { + if (event.type == SDL_EVENT_QUIT) { close_window(); return WindowClosed; } - if (event.type == SDL_KEYDOWN && wait_for_key) - return event.key.keysym.scancode + 1; + if (event.type == SDL_EVENT_KEY_DOWN && wait_for_key) + return event.key.key + 1; - if (event.type == SDL_MOUSEBUTTONDOWN && wait_for_mouse) + if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && wait_for_mouse) return MouseLeft + event.button.button - SDL_BUTTON_LEFT; - else if (event.type == SDL_MOUSEWHEEL) { + else if (event.type == SDL_EVENT_MOUSE_WHEEL) { g_state.mouse_wheel_pos += event.wheel.y; if (wait_for_mouse) return event.wheel.y > 0 @@ -13913,18 +13913,18 @@ static int process_events(double timeout, bool wait_for_key, bool wait_for_mouse : MouseWheelDown; } - else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) + else if (event.type == 0x200 && event.window.type == SDL_EVENT_WINDOW_RESIZED) update_view(); - else if (event.type == SDL_MOUSEMOTION) { + else if (event.type == SDL_EVENT_MOUSE_MOTION) { const Rect& vp = g_state.viewport; if (event.motion.x > vp.x && event.motion.x < vp.x+vp.width && event.motion.y > vp.y && event.motion.y < vp.y+vp.height) - g_state.mouse_pos_window = {event.motion.x, event.motion.y}; + g_state.mouse_pos_window = {(int)event.motion.x, (int)event.motion.y}; } - else if (event.type == SDL_KEYDOWN) { - if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) { + else if (event.type == SDL_EVENT_KEY_DOWN) { + if (event.key.key == SDL_SCANCODE_BACKSPACE) { std::string& s = g_state.entered_text; while (! s.empty()) { // pop-back UTF-8 @@ -13934,11 +13934,11 @@ static int process_events(double timeout, bool wait_for_key, bool wait_for_mouse break; } } - else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN) + else if (event.key.key == SDL_SCANCODE_RETURN) g_state.entered_text += "\n"; } - else if (event.type == SDL_TEXTINPUT) { + else if (event.type == SDL_EVENT_TEXT_INPUT) { const char* c = event.text.text; // null-terminated C-string std::string& str = g_state.entered_text; while (*c != 0) @@ -14065,13 +14065,13 @@ bool is_input(int input) { if (input < cg::MouseLeft) { int len = 0; - const Uint8 *state = SDL_GetKeyboardState(&len); + const bool *state = SDL_GetKeyboardState(&len); int idx = input-KeyUnknown; return idx < len ? state[idx] : false; } else if (input >= cg::MouseLeft && input <= cg::MouseRight) { Uint32 buttons = SDL_GetMouseState(nullptr, nullptr); - return buttons & SDL_BUTTON(input - cg::MouseLeft + SDL_BUTTON_LEFT); + return buttons & SDL_BUTTON_MASK(input - cg::MouseLeft + SDL_BUTTON_LEFT); } else return false; @@ -14094,7 +14094,8 @@ static double get_mouse_pos(int axis) // The window did not get a MouseMoved event yet. Get the position // ourselves now. We don't want to do it every time, it would be hard // to return anything meaningful when the mouse is outside the window. - SDL_GetMouseState(&pos[0], &pos[1]); + float fpos[] = { pos[0] , pos[1] }; + SDL_GetMouseState(&fpos[0], &fpos[1]); bool inside = pos[0] > 0 && pos[0] < win_size_x && pos[1] > 0 && pos[1] < win_size_y; if (! inside) @@ -14258,9 +14259,8 @@ static SDL_Surface* draw_text(const stbtt_fontinfo* info, const std::string& tex } // Convert the bitmap to SDL_Surface: - SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, b_w, b_h, 32, - 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); - SDL_FillRect(surface, NULL, 0); // Init as transparent + SDL_Surface* surface = SDL_CreateSurface(b_w, b_h, SDL_PIXELFORMAT_ARGB8888); + SDL_FillSurfaceRect(surface, NULL, 0); // Init as transparent double a = color.a / 255.; double r = color.r / 255.; @@ -14867,13 +14867,13 @@ bool TextureCache::add(const std::string& filename, const unsigned char* data, i width = surface->w; height = surface->h; - surface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32, 0); + surface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); - SDL_FreeSurface(surface); + SDL_DestroySurface(surface); } // The texture is created. Save the id into our map so we can find it @@ -15275,7 +15275,7 @@ std::string read_line(double x, double y, double height, bool persist, int max_c new_text.clear(); std::string temp = "init"; // So it renders the prompt before first keypress. bool done = false; - SDL_StartTextInput(); // SDL_TEXTINPUT event will start coming + SDL_StartTextInput(g_state.window); // SDL_TEXTINPUT event will start coming // In case the user set 'steps_per_second' too low, the input would be laggy. // Override the value. We will restore it before this function returns. @@ -15320,7 +15320,7 @@ std::string read_line(double x, double y, double height, bool persist, int max_c temp = new_text; } g_state.inv_steps_per_second = steps_per_sec_inv_stash; - SDL_StopTextInput(); // unsubscribe to the SDL_TEXTINPUT event + SDL_StopTextInput(g_state.window); // unsubscribe to the SDL_TEXTINPUT event return new_text; }