From 0c34af0e63d31c908826131475dc7632ff8dec3f Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Wed, 10 May 2023 12:41:32 -0600 Subject: [PATCH] Demonstrate how to acquire and use ImGui-SFML --- CMakeLists.txt | 21 ++++++++++++++++++++- src/main.cpp | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 096def9..711c490 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(CMakeSFMLProject LANGUAGES CXX) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) include(FetchContent) + FetchContent_Declare(SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 3.0.0 @@ -12,5 +13,23 @@ FetchContent_Declare(SFML SYSTEM) FetchContent_MakeAvailable(SFML) +FetchContent_Declare(ImGui + GIT_REPOSITORY https://github.com/ocornut/imgui + GIT_TAG v1.91.1 + GIT_SHALLOW ON + EXCLUDE_FROM_ALL + SYSTEM) +FetchContent_MakeAvailable(ImGui) +FetchContent_GetProperties(ImGui SOURCE_DIR IMGUI_DIR) + +set(IMGUI_SFML_FIND_SFML OFF) +FetchContent_Declare(ImGui-SFML + GIT_REPOSITORY https://github.com/SFML/imgui-sfml + GIT_TAG master + GIT_SHALLOW ON + EXCLUDE_FROM_ALL + SYSTEM) +FetchContent_MakeAvailable(ImGui-SFML) + add_executable(main src/main.cpp) -target_link_libraries(main PRIVATE SFML::Graphics) +target_link_libraries(main PRIVATE SFML::Graphics ImGui-SFML::ImGui-SFML) diff --git a/src/main.cpp b/src/main.cpp index e1aec12..474c388 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,37 @@ #include +#include +#include int main() { auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "CMake SFML Project"); window.setFramerateLimit(144); + if (!ImGui::SFML::Init(window)) + return -1; + sf::Clock clock; while (window.isOpen()) { while (const std::optional event = window.pollEvent()) { + ImGui::SFML::ProcessEvent(window, *event); + if (event->is()) { window.close(); } } + ImGui::SFML::Update(window, clock.restart()); + + ImGui::Begin("Hello, world!"); + ImGui::Button("Look at this pretty button"); + ImGui::End(); + window.clear(); + ImGui::SFML::Render(window); window.display(); } + + ImGui::SFML::Shutdown(); }