From b733d320e6179a6f69ae79a42bd7b813e2f9e42d Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Wed, 12 Jun 2024 22:39:18 +0200 Subject: [PATCH] Fixes for latest SDL3 version - SDL_QueryTexture was replaced by SDL_GetTextureSize - SDL_Rect was changed to SDL_FRect for viewport and clipping rect - Rendering has been broken with SDL3 for a while due to incorrect parameters being passed to SDL_RenderGeometryRaw --- .../SDL_Renderer/BackendRenderTargetSDL.cpp | 39 +++++++++++++++---- .../SDL_Renderer/BackendTextureSDL.cpp | 7 ++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Backend/Renderer/SDL_Renderer/BackendRenderTargetSDL.cpp b/src/Backend/Renderer/SDL_Renderer/BackendRenderTargetSDL.cpp index 4cd4e1d31..355470766 100644 --- a/src/Backend/Renderer/SDL_Renderer/BackendRenderTargetSDL.cpp +++ b/src/Backend/Renderer/SDL_Renderer/BackendRenderTargetSDL.cpp @@ -77,6 +77,23 @@ namespace tgui if (!m_renderer || (m_targetSize.x == 0) || (m_targetSize.y == 0) || (m_viewRect.width <= 0) || (m_viewRect.height <= 0)) return; + // Store the current blend mode, in case we need to change it + SDL_BlendMode oldBlendMode = SDL_BLENDMODE_BLEND; + SDL_GetRenderDrawBlendMode(m_renderer, &oldBlendMode); + +#if SDL_MAJOR_VERSION >= 3 + // Store the current clipping settings, in case we need to change it + SDL_FRect oldClipRect; + const SDL_bool oldClipEnabled = SDL_RenderClipEnabled(m_renderer); + if (oldClipEnabled) + SDL_GetRenderClipRect(m_renderer, &oldClipRect); + + // Store the current viewport, in case we need to change it + SDL_FRect oldViewport; + SDL_GetRenderViewport(m_renderer, &oldViewport); + + const SDL_FRect newViewport = {m_viewport.left, m_viewport.top, m_viewport.width, m_viewport.height}; +#else // Store the current clipping settings, in case we need to change it SDL_Rect oldClipRect; const SDL_bool oldClipEnabled = SDL_RenderClipEnabled(m_renderer); @@ -87,16 +104,13 @@ namespace tgui SDL_Rect oldViewport; SDL_GetRenderViewport(m_renderer, &oldViewport); - // Store the current blend mode, in case we need to change it - SDL_BlendMode oldBlendMode = SDL_BLENDMODE_BLEND; - SDL_GetRenderDrawBlendMode(m_renderer, &oldBlendMode); - - // Change the viewport if needed SDL_Rect newViewport; newViewport.x = static_cast(m_viewport.left); newViewport.y = static_cast(m_viewport.top); newViewport.w = static_cast(m_viewport.width); newViewport.h = static_cast(m_viewport.height); +#endif + // Change the viewport if needed const bool viewportNeedsUpdate = (oldViewport.x != newViewport.x) || (oldViewport.y != newViewport.y) || (oldViewport.w != newViewport.w) || (oldViewport.h != newViewport.h); if (viewportNeedsUpdate) @@ -153,9 +167,9 @@ namespace tgui #if SDL_MAJOR_VERSION >= 3 static_assert(sizeof(Vertex::Color) == sizeof(SDL_Color), "SDL_Color requires same memory layout as tgui::Vertex::Color for cast to work"); SDL_RenderGeometryRaw(m_renderer, textureSDL, - &vertices->position.x, sizeof(Vertex), - reinterpret_cast(&vertices->color), sizeof(Vertex), - &vertices->texCoords.x, sizeof(Vertex), + &verticesSDL[0].position.x, sizeof(Vertex), + reinterpret_cast(&verticesSDL[0].color), sizeof(Vertex), + &verticesSDL[0].texCoords.x, sizeof(Vertex), static_cast(vertexCount), indices, static_cast(indexCount), sizeof(unsigned int)); #else // We use SDL_RenderGeometry instead of SDL_RenderGeometryRaw because it's easier and because the signature of @@ -176,11 +190,20 @@ namespace tgui { m_pixelsPerPoint = {clipViewport.width / clipRect.width, clipViewport.height / clipRect.height}; +#if SDL_MAJOR_VERSION >= 3 + const SDL_FRect clipRectSDL = { + clipViewport.left - m_viewport.left, + clipViewport.top - m_viewport.top, + clipViewport.width, + clipViewport.height + }; +#else SDL_Rect clipRectSDL; clipRectSDL.x = static_cast(clipViewport.left - m_viewport.left); clipRectSDL.y = static_cast(clipViewport.top - m_viewport.top); clipRectSDL.w = static_cast(clipViewport.width); clipRectSDL.h = static_cast(clipViewport.height); +#endif SDL_SetRenderClipRect(m_renderer, &clipRectSDL); } else // Clip the entire window diff --git a/src/Backend/Renderer/SDL_Renderer/BackendTextureSDL.cpp b/src/Backend/Renderer/SDL_Renderer/BackendTextureSDL.cpp index 575962a90..96e1017d9 100644 --- a/src/Backend/Renderer/SDL_Renderer/BackendTextureSDL.cpp +++ b/src/Backend/Renderer/SDL_Renderer/BackendTextureSDL.cpp @@ -166,10 +166,17 @@ namespace tgui m_texture = texture; m_pixels = nullptr; +#if SDL_MAJOR_VERSION >= 3 + float width; + float height; + if (SDL_GetTextureSize(texture, &width, &height) == 0) + m_imageSize = {static_cast(width), static_cast(height)}; +#else int width; int height; if (SDL_QueryTexture(texture, nullptr, nullptr, &width, &height) == 0) m_imageSize = {static_cast(width), static_cast(height)}; +#endif #if (SDL_MAJOR_VERSION > 2) \ || ((SDL_MAJOR_VERSION == 2) && (SDL_MINOR_VERSION > 0)) \