From 63bba196c5a39ddeab139a4527318f267b84d8de Mon Sep 17 00:00:00 2001 From: majvax Date: Thu, 11 Sep 2025 10:47:32 +0200 Subject: [PATCH 1/2] feat: support for 1.92.0 --- imgui-SFML.cpp | 87 ++++++++++++++++++++++++++++++++------------------ imgui-SFML.h | 8 ++++- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/imgui-SFML.cpp b/imgui-SFML.cpp index 70c9b18..de5cb5f 100644 --- a/imgui-SFML.cpp +++ b/imgui-SFML.cpp @@ -1,4 +1,5 @@ #include "imgui-SFML.h" +#include #include #include @@ -34,6 +35,8 @@ #include #include + + int openKeyboardIME() { ANativeActivity* activity = sf::getNativeActivity(); @@ -313,6 +316,7 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF io.BackendFlags |= ImGuiBackendFlags_HasGamepad; io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; + io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; io.BackendPlatformName = "imgui_impl_sfml"; s_currWindowCtx->joystickId = getConnectedJoystickId(); @@ -338,13 +342,6 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF loadMouseCursor(ImGuiMouseCursor_ResizeNWSE, sf::Cursor::Type::SizeTopLeftBottomRight); loadMouseCursor(ImGuiMouseCursor_Hand, sf::Cursor::Type::Hand); - if (loadDefaultFont) - { - // this will load default font automatically - // No need to call AddDefaultFont - return UpdateFontTexture(); - } - return true; } @@ -589,6 +586,15 @@ void Shutdown(const sf::Window& window) { const bool needReplacement = (s_currWindowCtx->window->getNativeHandle() == window.getNativeHandle()); + for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) + { + if (tex->RefCount == 1) + { + tex->SetStatus(ImTextureStatus_WantDestroy); + UpdateFontTexture(tex); + } + } + // remove window's context auto found = std::find_if(s_windowContexts.begin(), s_windowContexts.end(), @@ -619,37 +625,58 @@ void Shutdown(const sf::Window& window) void Shutdown() { + for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) + { + if (tex->RefCount == 1) + { + tex->SetStatus(ImTextureStatus_WantDestroy); + UpdateFontTexture(tex); + } + } s_currWindowCtx = nullptr; ImGui::SetCurrentContext(nullptr); s_windowContexts.clear(); } -bool UpdateFontTexture() +void UpdateFontTexture(ImTextureData* tex) { assert(s_currWindowCtx); - ImGuiIO& io = ImGui::GetIO(); - unsigned char* pixels = nullptr; - int width = 0; - int height = 0; - - io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); - - sf::Texture newTexture; - if (!newTexture.resize(sf::Vector2u(sf::Vector2(width, height)))) + if (tex->Status = ImTextureStatus_WantCreate) { - return false; - } + auto sfml_texture = std::make_unique(); - newTexture.update(pixels); + [[maybe_unused]] auto res = sfml_texture->resize(sf::Vector2u(sf::Vector2(tex->Width, tex->Height))); - ImTextureID texID = convertGLTextureHandleToImTextureID(newTexture.getNativeHandle()); - io.Fonts->SetTexID(texID); + sfml_texture->update(reinterpret_cast(tex->GetPixels())); - s_currWindowCtx->fontTexture = std::move(newTexture); + ImTextureID id = convertGLTextureHandleToImTextureID(sfml_texture->getNativeHandle()); + tex->SetTexID(id); + textureMap[id] = std::move(sfml_texture); + tex->SetStatus(ImTextureStatus_OK); + } + else if (tex->Status = ImTextureStatus_WantUpdates) + { + auto it = textureMap.find(tex->GetTexID()); + if (it != textureMap.end()) { + for (ImTextureRect& r : tex->Updates) + { + it->second->update( + reinterpret_cast(tex->GetPixelsAt(r.x, r.y)), + sf::Vector2u(sf::Vector2(r.w, r.h)), + sf::Vector2u(sf::Vector2(r.x, r.y))); + tex->SetStatus(ImTextureStatus_OK); + } - return true; + } + } + else + { + tex->SetTexID(ImTextureID_Invalid); + tex->SetStatus(ImTextureStatus_Destroyed); + textureMap.erase(tex->GetTexID()); + } } std::optional& GetFontTexture() @@ -946,14 +973,12 @@ void SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height) // Rendering callback void RenderDrawLists(ImDrawData* draw_data) { - ImGui::GetDrawData(); - if (draw_data->CmdListsCount == 0) - { - return; - } + auto& io = ImGui::GetIO(); - const ImGuiIO& io = ImGui::GetIO(); - assert(io.Fonts->TexID != (ImTextureID) nullptr); // You forgot to create and set font texture + if (draw_data->Textures != nullptr) + for (ImTextureData* tex : *draw_data->Textures) + if (tex->Status != ImTextureStatus_OK) + ImGui::SFML::UpdateFontTexture(tex); // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != // framebuffer coordinates) diff --git a/imgui-SFML.h b/imgui-SFML.h index ee873f6..e0d15aa 100644 --- a/imgui-SFML.h +++ b/imgui-SFML.h @@ -6,10 +6,14 @@ #include #include #include +#include #include #include "imgui-SFML_export.h" +#include "imgui.h" // IMGUI_IMPL_API +#include +#include namespace sf { @@ -26,6 +30,8 @@ namespace ImGui { namespace SFML { + inline std::unordered_map> textureMap; + [[nodiscard]] IMGUI_SFML_API bool Init(sf::RenderWindow& window, bool loadDefaultFont = true); [[nodiscard]] IMGUI_SFML_API bool Init(sf::Window& window, sf::RenderTarget& target, bool loadDefaultFont = true); [[nodiscard]] IMGUI_SFML_API bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultFont = true); @@ -45,7 +51,7 @@ IMGUI_SFML_API void Shutdown(const sf::Window& window); // Shuts down all ImGui contexts IMGUI_SFML_API void Shutdown(); -[[nodiscard]] IMGUI_SFML_API bool UpdateFontTexture(); +IMGUI_SFML_API void UpdateFontTexture(ImTextureData* tex); IMGUI_SFML_API std::optional& GetFontTexture(); // joystick functions From 44ac8a212e7f818a19e3b7323428af452408fab5 Mon Sep 17 00:00:00 2001 From: majvax Date: Thu, 11 Sep 2025 16:43:07 +0200 Subject: [PATCH 2/2] fix: removed deprecated function --- imgui-SFML.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/imgui-SFML.cpp b/imgui-SFML.cpp index de5cb5f..8017ef9 100644 --- a/imgui-SFML.cpp +++ b/imgui-SFML.cpp @@ -803,7 +803,7 @@ void Image(const sf::Texture& texture, const sf::Vector2f& size, const sf::Color { ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle()); - ImGui::Image(textureID, toImVec2(size), ImVec2(0, 0), ImVec2(1, 1), toImColor(tintColor), toImColor(borderColor)); + ImGui::ImageWithBg(textureID, toImVec2(size), ImVec2(0, 0), ImVec2(1, 1), toImColor(tintColor), toImColor(borderColor)); } /////////////// Image Overloads for sf::RenderTexture @@ -816,7 +816,7 @@ void Image(const sf::RenderTexture& texture, const sf::Vector2f& size, const sf: { ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getTexture().getNativeHandle()); - ImGui::Image(textureID, + ImGui::ImageWithBg(textureID, toImVec2(size), ImVec2(0, 1), ImVec2(1, 0), // flipped vertically, @@ -837,7 +837,7 @@ void Image(const sf::Sprite& sprite, const sf::Color& tintColor, const sf::Color void Image(const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color& tintColor, const sf::Color& borderColor) { auto [uv0, uv1, textureID] = getSpriteTextureData(sprite); - ImGui::Image(textureID, toImVec2(size), uv0, uv1, toImColor(tintColor), toImColor(borderColor)); + ImGui::ImageWithBg(textureID, toImVec2(size), uv0, uv1, toImColor(tintColor), toImColor(borderColor)); } /////////////// Image Button Overloads for sf::Texture