From a90ac73b5fc01d9f28a1488fae06e381fb68836d Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 3 Sep 2023 11:37:54 -0600 Subject: [PATCH] Upgrade to SFML 3 --- .github/workflows/ci.yml | 9 +++++++-- CMakeLists.txt | 4 ++-- README.md | 4 ++-- examples/minimal/main.cpp | 2 +- imgui-SFML.cpp | 35 +++-------------------------------- imgui-SFML.h | 19 ++++++------------- 6 files changed, 21 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 05ef117..18850ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,11 @@ name: CI -on: [push, pull_request, workflow_dispatch] +on: + push: + pull_request: + workflow_dispatch: + schedule: + - cron: '15 9 * * 2' concurrency: group: environment-${{github.ref}} @@ -55,7 +60,7 @@ jobs: with: repository: SFML/SFML path: sfml - ref: 2.6.0 + ref: master - name: Configure SFML run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index c0bed7e..bbfce5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,7 @@ if(IMGUI_SFML_FIND_SFML) if(NOT BUILD_SHARED_LIBS) set(SFML_STATIC_LIBRARIES ON) endif() - find_package(SFML 2.5 COMPONENTS graphics) + find_package(SFML 3 COMPONENTS Graphics) if(NOT SFML_FOUND) message(FATAL_ERROR "SFML 2 directory not found. Set SFML_DIR to directory where SFML was built (or one which contains SFMLConfig.cmake)") @@ -94,7 +94,7 @@ add_library(ImGui-SFML ${IMGUI_SFML_LIBRARY_TYPE} add_library(ImGui-SFML::ImGui-SFML ALIAS ImGui-SFML) target_link_libraries(ImGui-SFML PUBLIC - sfml-graphics + SFML::Graphics OpenGL::GL ) diff --git a/README.md b/README.md index 945352d..4718668 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ State of Development Dependencies ----- -* [SFML](https://github.com/SFML/SFML) >= 2.5.0 +* [SFML](https://github.com/SFML/SFML) >= 3.0.0 * [Dear ImGui](https://github.com/ocornut/imgui) >= 1.80 Contributing ----- -* The code is written in C++11 (stable SFML is still C++03, Dear ImGui has started using C++11 since 2022) +* The code is written in C++17 (SFML 3 uses C++17, Dear ImGui has started using C++11 since 2022) * The code should be formatted via [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) using `.clang-format` provided in the root of this repository How-to diff --git a/examples/minimal/main.cpp b/examples/minimal/main.cpp index 32ed772..2b5b274 100644 --- a/examples/minimal/main.cpp +++ b/examples/minimal/main.cpp @@ -8,7 +8,7 @@ #include int main() { - sf::RenderWindow window(sf::VideoMode(640, 480), "ImGui + SFML = <3"); + sf::RenderWindow window(sf::VideoMode({640, 480}), "ImGui + SFML = <3"); window.setFramerateLimit(60); if (!ImGui::SFML::Init(window)) return -1; diff --git a/imgui-SFML.cpp b/imgui-SFML.cpp index 408a7b6..dd82afa 100644 --- a/imgui-SFML.cpp +++ b/imgui-SFML.cpp @@ -29,13 +29,8 @@ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif -#if SFML_VERSION_MAJOR >= 3 #define IMGUI_SFML_KEY_APOSTROPHE sf::Keyboard::Apostrophe #define IMGUI_SFML_KEY_GRAVE sf::Keyboard::Grave -#else -#define IMGUI_SFML_KEY_APOSTROPHE sf::Keyboard::Quote -#define IMGUI_SFML_KEY_GRAVE sf::Keyboard::Tilde -#endif #ifdef ANDROID #ifdef USE_JNI @@ -313,7 +308,7 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF void SetCurrentWindow(const sf::Window& window) { auto found = std::find_if(s_windowContexts.begin(), s_windowContexts.end(), [&](std::unique_ptr& ctx) { - return ctx->window->getSystemHandle() == window.getSystemHandle(); + return ctx->window->getNativeHandle() == window.getNativeHandle(); }); assert(found != s_windowContexts.end() && "Failed to find the window. Forgot to call ImGui::SFML::Init for the window?"); @@ -754,12 +749,12 @@ void Render() { } void Shutdown(const sf::Window& window) { - bool needReplacement = (s_currWindowCtx->window->getSystemHandle() == window.getSystemHandle()); + bool needReplacement = (s_currWindowCtx->window->getNativeHandle() == window.getNativeHandle()); // remove window's context auto found = std::find_if(s_windowContexts.begin(), s_windowContexts.end(), [&](std::unique_ptr& ctx) { - return ctx->window->getSystemHandle() == window.getSystemHandle(); + return ctx->window->getNativeHandle() == window.getNativeHandle(); }); assert(found != s_windowContexts.end() && "Window wasn't inited properly: forgot to call ImGui::SFML::Init(window)?"); @@ -797,16 +792,10 @@ bool UpdateFontTexture() { io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); sf::Texture& texture = s_currWindowCtx->fontTexture; -#if SFML_VERSION_MAJOR >= 3 if (!texture.create( sf::Vector2u(static_cast(width), static_cast(height)))) { return false; } -#else - if (!texture.create(static_cast(width), static_cast(height))) { - return false; - } -#endif texture.update(pixels); @@ -979,16 +968,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) { -#if SFML_VERSION_MAJOR >= 3 const sf::Texture& texture = sprite.getTexture(); -#else - const sf::Texture* texturePtr = sprite.getTexture(); - // sprite without texture cannot be drawn - if (!texturePtr) { - return; - } - const sf::Texture& texture = *texturePtr; -#endif const sf::Vector2f textureSize(texture.getSize()); const sf::FloatRect textureRect(sprite.getTextureRect()); ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y); @@ -1045,16 +1025,7 @@ bool ImageButton(const sf::Sprite& sprite, const int framePadding, const sf::Col bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) { -#if SFML_VERSION_MAJOR >= 3 const sf::Texture& texture = sprite.getTexture(); -#else - const sf::Texture* texturePtr = sprite.getTexture(); - // sprite without texture cannot be drawn - if (!texturePtr) { - return false; - } - const sf::Texture& texture = *texturePtr; -#endif const sf::Vector2f textureSize(texture.getSize()); const sf::FloatRect textureRect(sprite.getTextureRect()); ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y); diff --git a/imgui-SFML.h b/imgui-SFML.h index e431e99..3e3204a 100644 --- a/imgui-SFML.h +++ b/imgui-SFML.h @@ -9,12 +9,6 @@ #include "imgui-SFML_export.h" -#if __cplusplus >= 201703L // C++17 and above -#define IMGUI_SFML_NODISCARD [[nodiscard]] -#else -#define IMGUI_SFML_NODISCARD -#endif - namespace sf { class Event; class RenderTarget; @@ -27,12 +21,11 @@ class Window; namespace ImGui { namespace SFML { -IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::RenderWindow& window, - bool loadDefaultFont = true); -IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::Window& window, sf::RenderTarget& target, - bool loadDefaultFont = true); -IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::Window& window, const sf::Vector2f& displaySize, - bool loadDefaultFont = true); +[[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); IMGUI_SFML_API void SetCurrentWindow(const sf::Window& window); IMGUI_SFML_API void ProcessEvent(const sf::Event& event); // DEPRECATED: use (window, @@ -52,7 +45,7 @@ IMGUI_SFML_API void Shutdown(const sf::Window& window); // Shuts down all ImGui contexts IMGUI_SFML_API void Shutdown(); -IMGUI_SFML_NODISCARD IMGUI_SFML_API bool UpdateFontTexture(); +[[nodiscard]] IMGUI_SFML_API bool UpdateFontTexture(); IMGUI_SFML_API sf::Texture& GetFontTexture(); // joystick functions