|
| 1 | +#include "imgui.h" // necessary for ImGui::*, imgui-SFML.h doesn't include imgui.h |
| 2 | + |
| 3 | +#include "imgui-SFML.h" // for ImGui::SFML::* functions and SFML-specific overloads |
| 4 | + |
| 5 | +#include <SFML/Graphics/CircleShape.hpp> |
| 6 | +#include <SFML/Graphics/RenderWindow.hpp> |
| 7 | +#include <SFML/System/Clock.hpp> |
| 8 | +#include <SFML/Window/Event.hpp> |
| 9 | + |
| 10 | +int main() { |
| 11 | + sf::RenderWindow window(sf::VideoMode(1280, 720), "ImGui + SFML = <3"); |
| 12 | + window.setFramerateLimit(60); |
| 13 | + ImGui::SFML::Init(window); |
| 14 | + |
| 15 | + sf::RenderWindow childWindow(sf::VideoMode(640, 480), "ImGui-SFML Child window"); |
| 16 | + childWindow.setFramerateLimit(60); |
| 17 | + ImGui::SFML::Init(childWindow); |
| 18 | + |
| 19 | + sf::Clock deltaClock; |
| 20 | + while (window.isOpen()) { |
| 21 | + // Main window event processing |
| 22 | + sf::Event event; |
| 23 | + while (window.pollEvent(event)) { |
| 24 | + ImGui::SFML::ProcessEvent(window, event); |
| 25 | + if (event.type == sf::Event::Closed) { |
| 26 | + if (childWindow.isOpen()) { |
| 27 | + childWindow.close(); |
| 28 | + } |
| 29 | + window.close(); |
| 30 | + ImGui::SFML::Shutdown(); // will shutdown all windows |
| 31 | + return 0; // return here so that we don't call Update/Render |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Child window event processing |
| 36 | + if (childWindow.isOpen()) { |
| 37 | + while (childWindow.pollEvent(event)) { |
| 38 | + ImGui::SFML::ProcessEvent(childWindow, event); |
| 39 | + if (event.type == sf::Event::Closed) { |
| 40 | + childWindow.close(); |
| 41 | + ImGui::SFML::Shutdown(childWindow); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Update |
| 47 | + const sf::Time dt = deltaClock.restart(); |
| 48 | + ImGui::SFML::Update(window, dt); |
| 49 | + if (childWindow.isOpen()) { |
| 50 | + ImGui::SFML::Update(childWindow, dt); |
| 51 | + } |
| 52 | + |
| 53 | + // Add ImGui widgets in the first window |
| 54 | + ImGui::SFML::SetCurrentWindow(window); |
| 55 | + ImGui::Begin("Hello, world!"); |
| 56 | + ImGui::Button("Look at this pretty button"); |
| 57 | + ImGui::End(); |
| 58 | + ImGui::ShowDemoWindow(); |
| 59 | + // Add ImGui widgets in the child window |
| 60 | + if (childWindow.isOpen()) { |
| 61 | + ImGui::SFML::SetCurrentWindow(childWindow); |
| 62 | + ImGui::Begin("Works in a second window!"); |
| 63 | + ImGui::Button("Example button"); |
| 64 | + ImGui::End(); |
| 65 | + } |
| 66 | + |
| 67 | + // Main window drawing |
| 68 | + sf::CircleShape shape(100.f); |
| 69 | + shape.setFillColor(sf::Color::Green); |
| 70 | + |
| 71 | + window.clear(); |
| 72 | + window.draw(shape); |
| 73 | + ImGui::SFML::Render(window); |
| 74 | + window.display(); |
| 75 | + |
| 76 | + // Child window drawing |
| 77 | + if (childWindow.isOpen()) { |
| 78 | + sf::CircleShape shape2(50.f); |
| 79 | + shape2.setFillColor(sf::Color::Red); |
| 80 | + |
| 81 | + childWindow.clear(); |
| 82 | + childWindow.draw(shape2); |
| 83 | + ImGui::SFML::Render(childWindow); |
| 84 | + childWindow.display(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
0 commit comments