Skip to content

Commit

Permalink
Upgrade to SFML 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Sep 3, 2023
1 parent 4f02e81 commit a90ac73
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 52 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down Expand Up @@ -55,7 +60,7 @@ jobs:
with:
repository: SFML/SFML
path: sfml
ref: 2.6.0
ref: master

- name: Configure SFML
run: |
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <SFML/Window/Event.hpp>

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;

Expand Down
35 changes: 3 additions & 32 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<WindowContext>& 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?");
Expand Down Expand Up @@ -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<WindowContext>& 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)?");
Expand Down Expand Up @@ -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<unsigned>(width), static_cast<unsigned>(height)))) {
return false;
}
#else
if (!texture.create(static_cast<unsigned>(width), static_cast<unsigned>(height))) {
return false;
}
#endif

texture.update(pixels);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 6 additions & 13 deletions imgui-SFML.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit a90ac73

Please sign in to comment.