From 96d9169a641fb7e116f0a478e2da17ec52568e58 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 | 15 ++++++++++++++- src/main.cpp | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8398e55..e178f13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,13 +5,26 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) option(BUILD_SHARED_LIBS "Build shared libraries" OFF) include(FetchContent) + FetchContent_Declare(SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 2.6.x) FetchContent_MakeAvailable(SFML) +FetchContent_Declare(ImGui + GIT_REPOSITORY https://github.com/ocornut/imgui + GIT_TAG v1.89.9) +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 2.6.x) +FetchContent_MakeAvailable(ImGui-SFML) + add_executable(CMakeSFMLProject src/main.cpp) -target_link_libraries(CMakeSFMLProject PRIVATE sfml-graphics) +target_link_libraries(CMakeSFMLProject PRIVATE sfml-graphics ImGui-SFML::ImGui-SFML) target_compile_features(CMakeSFMLProject PRIVATE cxx_std_17) if(WIN32) diff --git a/src/main.cpp b/src/main.cpp index cb00d46..4c6fa15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,36 @@ #include +#include +#include int main() { auto window = sf::RenderWindow{ { 1920u, 1080u }, "CMake SFML Project" }; window.setFramerateLimit(144); + ImGui::SFML::Init(window); + sf::Clock clock; while (window.isOpen()) { for (auto event = sf::Event{}; window.pollEvent(event);) { + ImGui::SFML::ProcessEvent(window, event); + if (event.type == sf::Event::Closed) { 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(); }