From b9d7a0496dcca1c232f8c91d7a80925a136083c2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 22 Jul 2020 15:19:37 +0900 Subject: [PATCH] Some restructuring of State --- src/imgui_helper.hpp | 8 +++++--- src/main.cpp | 4 ++-- src/state.cpp | 18 +++++++++++++++++- src/state.hpp | 20 +++++++++++++++----- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/imgui_helper.hpp b/src/imgui_helper.hpp index 0108e1b..2315c70 100644 --- a/src/imgui_helper.hpp +++ b/src/imgui_helper.hpp @@ -6,12 +6,14 @@ #include "state.hpp" namespace Tri { + // Seems misleading, but imgui handles setting up the window during update + // so this should be called during update, not draw inline void draw_help(Tri::State *state) { - if(state->flags.test(0)) { + if(state->get_flags().test(0)) { ImGui::SetNextWindowPos(sf::Vector2f(10.0f, 10.0f)); ImGui::SetNextWindowSize(sf::Vector2f( - state->width - 20.0f, - state->height - 20.0f)); + state->get_width() - 20.0f, + state->get_height() - 20.0f)); ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration); ImGui::Text("This is the help window."); ImGui::End(); diff --git a/src/main.cpp b/src/main.cpp index 5ec6832..16b035e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,10 +10,10 @@ int main(int argc, char **argv) { // init - Tri::State state{}; + Tri::State state(argc, argv); // main loop - while(state.window.isOpen()) { + while(state.get_flags().test(1)) { state.handle_events(); state.update(); state.draw(); diff --git a/src/state.cpp b/src/state.cpp index 2a45515..1356073 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -1,16 +1,19 @@ #include "state.hpp" +#include + #include #include "imgui_helper.hpp" -Tri::State::State() : +Tri::State::State(int argc, char **argv) : width(800), height(600), dt(sf::microseconds(16666)), window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close), currentTri_state(CurrentState::NONE) { + flags.set(1); // is running ImGui::SFML::Init(window); window.setFramerateLimit(60); @@ -28,6 +31,7 @@ void Tri::State::handle_events() { ImGui::SFML::ProcessEvent(event); if(event.type == sf::Event::Closed) { window.close(); + flags.reset(1); } else if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::H) { flags.flip(0); @@ -62,3 +66,15 @@ void Tri::State::draw() { window.display(); } + +unsigned int Tri::State::get_width() { + return width; +} + +unsigned int Tri::State::get_height() { + return height; +} + +const Tri::State::BitsetType Tri::State::get_flags() const { + return flags; +} diff --git a/src/state.hpp b/src/state.hpp index 58108c3..a48328e 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -8,15 +8,19 @@ #include namespace Tri { - struct State { - State(); + class State { + public: + State(int argc, char **argv); ~State(); /* * 0 - display help + * 1 - is running */ - std::bitset<64> flags; - const unsigned int width; - const unsigned int height; + private: + typedef std::bitset<64> BitsetType; + BitsetType flags; + unsigned int width; + unsigned int height; const sf::Time dt; sf::RenderWindow window; @@ -26,9 +30,15 @@ namespace Tri { sf::Event event; + public: void handle_events(); void update(); void draw(); + + unsigned int get_width(); + unsigned int get_height(); + + const BitsetType get_flags() const; }; }