From aa6b9f6fc34921dac02b68d035477247315dbc9b Mon Sep 17 00:00:00 2001 From: Dennis Meinen Date: Tue, 23 Jul 2024 22:36:37 +0200 Subject: [PATCH] Updated SDL3, SDL_Image, SDL_TTF, freetype and ImGUI/ --- CMakeLists.txt | 12 +++++------ assethandler/assethandler.cpp | 21 +++++++++++++------ input/inputdevice.h | 10 ++++----- input/inputhandler.cpp | 6 +++--- input/inputhandler.h | 2 +- render2d/SDL2_gfxPrimitives.h | 2 +- render2d/SDL2_rotozoom.c | 39 +++++++++++++---------------------- render2d/SDL2_rotozoom.h | 2 +- window/windowhandler.cpp | 17 ++++++++++----- 9 files changed, 58 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cfbcbe..b6e67a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,7 +105,7 @@ set(INTERFACE_SDL3_SHARED OFF) set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS ON) CPMAddPackage( NAME SDL3 - GIT_TAG e3395a7 + GIT_TAG f5f44de GITHUB_REPOSITORY libsdl-org/SDL ) @@ -117,7 +117,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF) CPMAddPackage( NAME SDL3_image - GIT_TAG 9add12c + GIT_TAG 8abc07d GITHUB_REPOSITORY libsdl-org/SDL_image ) @@ -129,8 +129,8 @@ if(NOT MSVC) CPMAddPackage( NAME freetype GIT_REPOSITORY https://github.com/aseprite/freetype2.git - GIT_TAG e8ebfe9 -) + GIT_TAG 9a2d6d9 + ) if (freetype_ADDED) add_library(Freetype::Freetype ALIAS freetype) @@ -140,7 +140,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF) CPMAddPackage( NAME SDL3_ttf - GIT_TAG 6e260a2 + GIT_TAG 5b6171c GITHUB_REPOSITORY libsdl-org/SDL_ttf ) @@ -157,7 +157,7 @@ target_link_libraries("luminoveau" PUBLIC if (ADD_IMGUI) CPMAddPackage( NAME Imgui - GIT_TAG 4f9ba19 + GIT_TAG fe09ebb GITHUB_REPOSITORY ocornut/imgui ) diff --git a/assethandler/assethandler.cpp b/assethandler/assethandler.cpp index 62399a8..6738aaa 100644 --- a/assethandler/assethandler.cpp +++ b/assethandler/assethandler.cpp @@ -39,6 +39,10 @@ TextureAsset AssetHandler::_loadTexture(const std::string &fileName) { SDL_Texture *tex = SDL_CreateTextureFromSurface(Window::GetRenderer(), surface); texture.texture = tex; + SDL_PropertiesID props = SDL_GetTextureProperties(tex); + + texture.id = props[SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER]; + SDL_SetTextureScaleMode(tex, (SDL_ScaleMode)defaultMode); @@ -67,6 +71,9 @@ TextureAsset AssetHandler::_createEmptyTexture(const vf2d &size) { texture.texture = SDL_CreateTexture(Window::GetRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, (int) size.x, (int) size.y); + SDL_PropertiesID props = SDL_GetTextureProperties(texture.texture); + texture.id = props[SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER]; + SDL_SetTextureScaleMode(texture.texture, (SDL_ScaleMode)defaultMode); return texture; @@ -77,9 +84,11 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) { SDL_Surface *surface = texture.surface; if (!surface) { - int texWidth, texHeight; - SDL_QueryTexture(texture.texture, NULL, NULL, &texWidth, &texHeight); - surface = SDL_CreateSurface(texWidth, texHeight,SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32); + float texWidth, texHeight; + + SDL_PropertiesID props = SDL_GetTextureProperties(texture.texture); + SDL_GetTextureSize(texture.texture, &texWidth, &texHeight); + surface = SDL_CreateSurface((int)texWidth, (int)texHeight, SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32); if (!surface) { SDL_Log("%s", SDL_GetError()); @@ -91,8 +100,8 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) { SDL_Surface *rgbaSurface = surface; - if (surface->format->format != SDL_PIXELFORMAT_RGBA32) { - rgbaSurface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32); + if (surface->format != SDL_PIXELFORMAT_RGBA32) { + rgbaSurface = SDL_ConvertSurface(surface, SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32); } unsigned char *pixels = new unsigned char[rgbaSurface->w * rgbaSurface->h * 4]; @@ -105,7 +114,7 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) { delete[] pixels; - if (surface->format->format != SDL_PIXELFORMAT_RGBA32) { + if (surface->format != SDL_PixelFormat::SDL_PIXELFORMAT_RGBA32) { SDL_DestroySurface(rgbaSurface); } } diff --git a/input/inputdevice.h b/input/inputdevice.h index 8b061ea..6b05815 100644 --- a/input/inputdevice.h +++ b/input/inputdevice.h @@ -60,16 +60,16 @@ class InputDevice { int gamepadID; std::map> mappingKB = { - {Buttons::LEFT, {SDLK_a, SDLK_LEFT}}, - {Buttons::RIGHT, {SDLK_d, SDLK_RIGHT}}, - {Buttons::UP, {SDLK_w, SDLK_UP}}, - {Buttons::DOWN, {SDLK_s, SDLK_DOWN}}, + {Buttons::LEFT, {SDLK_A, SDLK_LEFT}}, + {Buttons::RIGHT, {SDLK_D, SDLK_RIGHT}}, + {Buttons::UP, {SDLK_W, SDLK_UP}}, + {Buttons::DOWN, {SDLK_S, SDLK_DOWN}}, {Buttons::ACCEPT, {SDLK_SPACE, SDLK_KP_ENTER, SDLK_RETURN}}, {Buttons::BACK, {SDLK_ESCAPE, SDLK_BACKSPACE}}, {Buttons::SWITCH_NEXT, {SDLK_TAB}}, - {Buttons::SWITCH_PREV, {SDLK_BACKQUOTE}}, + {Buttons::SWITCH_PREV, {SDLK_GRAVE}}, {Buttons::RUN, {SDL_SCANCODE_LSHIFT}}, diff --git a/input/inputhandler.cpp b/input/inputhandler.cpp index 9830304..59f3f4b 100644 --- a/input/inputhandler.cpp +++ b/input/inputhandler.cpp @@ -87,7 +87,7 @@ bool Input::_keyPressed(int key) { auto curState = currentKeyboardState; auto prevState = previousKeyboardState; - auto scancode = SDL_GetScancodeFromKey(key); + auto scancode = SDL_GetScancodeFromKey(key, nullptr); return curState[scancode] == 1 && prevState[scancode] == 0; } @@ -97,14 +97,14 @@ bool Input::_keyReleased(int key) { auto curState = currentKeyboardState; auto prevState = previousKeyboardState; - auto scancode = SDL_GetScancodeFromKey(key); + auto scancode = SDL_GetScancodeFromKey(key, nullptr); return curState[scancode] == 0 && prevState[scancode] == 1; } bool Input::_keyDown(int key) { - auto scancode = SDL_GetScancodeFromKey(key); + auto scancode = SDL_GetScancodeFromKey(key, nullptr); return currentKeyboardState[scancode] == 1; } diff --git a/input/inputhandler.h b/input/inputhandler.h index 3aa014e..7ae3b9a 100644 --- a/input/inputhandler.h +++ b/input/inputhandler.h @@ -191,7 +191,7 @@ class Input { Uint32 previousMouseButtons = 0; - SDL_JoystickID *joystickIds = nullptr; + const SDL_JoystickID *joystickIds = nullptr; struct gamepadInfo { SDL_JoystickID joystickId; diff --git a/render2d/SDL2_gfxPrimitives.h b/render2d/SDL2_gfxPrimitives.h index 3a85cfd..df55422 100644 --- a/render2d/SDL2_gfxPrimitives.h +++ b/render2d/SDL2_gfxPrimitives.h @@ -35,7 +35,7 @@ Andreas Schiffler -- aschiffler at ferzkopp dot net #define M_PI 3.1415926535897932384626433832795 #endif -#include "SDL.h" +#include "SDL3/SDL.h" /* Set up for C function definitions, even when using C++ */ #ifdef __cplusplus diff --git a/render2d/SDL2_rotozoom.c b/render2d/SDL2_rotozoom.c index a3648f0..df7e0a1 100644 --- a/render2d/SDL2_rotozoom.c +++ b/render2d/SDL2_rotozoom.c @@ -816,7 +816,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) return NULL; } - if ((src->format->bits_per_pixel % 8) != 0) { + if (src->format != 0) { SDL_SetError("Invalid source surface bit depth"); return NULL; } @@ -836,7 +836,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) newHeight = src->h; } - dst = SDL_CreateSurface(newWidth, newHeight, src->format->bits_per_pixel); + dst = SDL_CreateSurface(newWidth, newHeight, src->format); if(!dst) { SDL_SetError("Could not create destination surface"); return NULL; @@ -850,7 +850,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) } /* Calculate byte-per-pixel */ - bpp = src->format->bits_per_pixel / 8; + bpp = SDL_GetPixelFormatDetails(src->format)->bits_per_pixel; switch(normalizedClockwiseTurns) { case 0: /* Make a copy of the surface */ @@ -1070,8 +1070,8 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->bits_per_pixel == 32); - if ((is32bit) || (src->format->bits_per_pixel == 8)) { + is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32); + if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ @@ -1174,10 +1174,7 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do /* * Copy palette and colorkey info */ - for (i = 0; i < rz_src->format->palette->ncolors; i++) { - rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; - } - rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* * Call the 8bit transformation routine to do the rotation */ @@ -1250,10 +1247,7 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do /* * Copy palette and colorkey info */ - for (i = 0; i < rz_src->format->palette->ncolors; i++) { - rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; - } - rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* * Call the 8bit transformation routine to do the zooming @@ -1362,8 +1356,8 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->bits_per_pixel == 32); - if ((is32bit) || (src->format->bits_per_pixel == 8)) { + is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32); + if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ @@ -1441,10 +1435,7 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo /* * Copy palette and colorkey info */ - for (i = 0; i < rz_src->format->palette->ncolors; i++) { - rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; - } - rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + /* * Call the 8bit transformation routine to do the zooming */ @@ -1507,8 +1498,8 @@ SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory) /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->bits_per_pixel == 32); - if ((is32bit) || (src->format->bits_per_pixel == 8)) { + is32bit = (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 32); + if ((is32bit) || (SDL_GetPixelFormatDetails(src->format)->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ @@ -1587,10 +1578,8 @@ SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory) /* * Copy palette and colorkey info */ - for (i = 0; i < rz_src->format->palette->ncolors; i++) { - rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i]; - } - rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors; + + /* * Call the 8bit transformation routine to do the shrinking */ diff --git a/render2d/SDL2_rotozoom.h b/render2d/SDL2_rotozoom.h index d9fa577..d7b6141 100644 --- a/render2d/SDL2_rotozoom.h +++ b/render2d/SDL2_rotozoom.h @@ -41,7 +41,7 @@ extern "C" { #define M_PI 3.1415926535897932384626433832795 #endif -#include "SDL.h" +#include "SDL3/SDL.h" /* ---- Defines */ diff --git a/window/windowhandler.cpp b/window/windowhandler.cpp index 56945fb..a8c4e8e 100644 --- a/window/windowhandler.cpp +++ b/window/windowhandler.cpp @@ -13,7 +13,7 @@ void Window::_initWindow(const std::string &title, int width, int height, int sc height *= scale; } - SDL_InitSubSystem(SDL_InitFlags::SDL_INIT_VIDEO); + SDL_InitSubSystem(SDL_INIT_VIDEO); auto window = SDL_CreateWindow(title.c_str(), width, height, flags); if (window) { m_window.reset(window); @@ -21,12 +21,19 @@ void Window::_initWindow(const std::string &title, int width, int height, int sc throw std::runtime_error(SDL_GetError()); } + SDL_PropertiesID props; + + props = SDL_CreateProperties(); + + SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window); #ifdef __EMSCRIPTEN__ - auto renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + SDL_SetStringProperty(props, SDL_PROP_RENDERER_VSYNC_NUMBER, 1); #else - auto renderer = SDL_CreateRenderer(window, "opengl", flags); + SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, "opengl"); #endif + auto renderer = SDL_CreateRendererWithProperties(props); + if (!renderer) { SDL_Log("SDL renderer failed: %s", SDL_GetError()); SDL_Log("Showing available SDL Renderers:"); @@ -153,10 +160,10 @@ void Window::_handleInput() { _shouldQuit = true; break; case SDL_EventType::SDL_EVENT_KEY_DOWN: - newKeysDown.push_back(event.key.keysym.scancode); + newKeysDown.push_back(event.key.scancode); break; case SDL_EventType::SDL_EVENT_KEY_UP: - newKeysUp.push_back(event.key.keysym.scancode); + newKeysUp.push_back(event.key.scancode); break; case SDL_EventType::SDL_EVENT_GAMEPAD_ADDED: Input::AddGamepadDevice(event.gdevice.which);