From 2fe86ad3685319e2f5e9b54affd26b401eb8d4d9 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 22 Jul 2020 15:11:10 +0900 Subject: [PATCH] Move logic/data to state Also set "H" key to toggle help window instead of only showing when "H" is pressed down. --- src/main.cpp | 34 ++---------------------------- src/state.cpp | 58 ++++++++++++++++++++++++++++++++++----------------- src/state.hpp | 6 +++++- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fefe163..5ec6832 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,42 +12,12 @@ int main(int argc, char **argv) { // init Tri::State state{}; - sf::RenderWindow window( - sf::VideoMode(state.width, state.height), - "Triangles", - sf::Style::Titlebar | sf::Style::Close); - ImGui::SFML::Init(window); - window.setFramerateLimit(60); - // main loop - sf::Event event; - while(window.isOpen()) { - // events - window.pollEvent(event); - ImGui::SFML::ProcessEvent(event); - if(event.type == sf::Event::Closed) { - window.close(); - } - state.handle_event(&event); - - // update - ImGui::SFML::Update(window, state.dt); - + while(state.window.isOpen()) { + state.handle_events(); state.update(); - - ImGui::EndFrame(); - // update end - - // draw - window.clear(); - ImGui::SFML::Render(window); state.draw(); - window.display(); } - // cleanup - window.close(); - ImGui::SFML::Shutdown(); - return 0; } diff --git a/src/state.cpp b/src/state.cpp index 8825e33..2a45515 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -1,44 +1,64 @@ #include "state.hpp" +#include + #include "imgui_helper.hpp" Tri::State::State() : width(800), height(600), dt(sf::microseconds(16666)), +window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close), currentTri_state(CurrentState::NONE) { + ImGui::SFML::Init(window); + window.setFramerateLimit(60); + currentTri.setPointCount(3); currentTri.setFillColor(sf::Color::White); } -void Tri::State::handle_event(sf::Event *event) { - if(event->type == sf::Event::KeyPressed) { - if(event->key.code == sf::Keyboard::H) { - flags.set(0); - } - } else if(event->type == sf::Event::KeyReleased) { - if(event->key.code == sf::Keyboard::H) { - flags.reset(0); - } - } else if(event->type == sf::Event::MouseButtonPressed) { - switch(currentTri_state) { - case CurrentState::NONE: - break; - case CurrentState::FIRST: - break; - case CurrentState::SECOND: - break; - case CurrentState::THIRD: - break; +Tri::State::~State() { + window.close(); + ImGui::SFML::Shutdown(); +} + +void Tri::State::handle_events() { + while(window.pollEvent(event)) { + ImGui::SFML::ProcessEvent(event); + if(event.type == sf::Event::Closed) { + window.close(); + } else if(event.type == sf::Event::KeyPressed) { + if(event.key.code == sf::Keyboard::H) { + flags.flip(0); + } + } else if(event.type == sf::Event::MouseButtonPressed) { + switch(currentTri_state) { + case CurrentState::NONE: + break; + case CurrentState::FIRST: + break; + case CurrentState::SECOND: + break; + case CurrentState::THIRD: + break; + } } } } void Tri::State::update() { + ImGui::SFML::Update(window, dt); + // Seems misleading, but imgui handles setting up the window during update Tri::draw_help(this); + + ImGui::EndFrame(); } void Tri::State::draw() { + window.clear(); + ImGui::SFML::Render(window); + + window.display(); } diff --git a/src/state.hpp b/src/state.hpp index f3707e1..58108c3 100644 --- a/src/state.hpp +++ b/src/state.hpp @@ -10,6 +10,7 @@ namespace Tri { struct State { State(); + ~State(); /* * 0 - display help */ @@ -18,11 +19,14 @@ namespace Tri { const unsigned int height; const sf::Time dt; + sf::RenderWindow window; std::vector tris; sf::ConvexShape currentTri; enum CurrentState { NONE, FIRST, SECOND, THIRD } currentTri_state; - void handle_event(sf::Event *event); + sf::Event event; + + void handle_events(); void update(); void draw(); };