Move logic/data to state

Also set "H" key to toggle help window instead of only showing when "H"
is pressed down.
This commit is contained in:
Stephen Seo 2020-07-22 15:11:10 +09:00
parent 34d1955bd1
commit 2fe86ad368
3 changed files with 46 additions and 52 deletions

View file

@ -12,42 +12,12 @@ int main(int argc, char **argv) {
// init // init
Tri::State state{}; 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 // main loop
sf::Event event; while(state.window.isOpen()) {
while(window.isOpen()) { state.handle_events();
// 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);
state.update(); state.update();
ImGui::EndFrame();
// update end
// draw
window.clear();
ImGui::SFML::Render(window);
state.draw(); state.draw();
window.display();
} }
// cleanup
window.close();
ImGui::SFML::Shutdown();
return 0; return 0;
} }

View file

@ -1,44 +1,64 @@
#include "state.hpp" #include "state.hpp"
#include <imgui-SFML.h>
#include "imgui_helper.hpp" #include "imgui_helper.hpp"
Tri::State::State() : Tri::State::State() :
width(800), width(800),
height(600), height(600),
dt(sf::microseconds(16666)), dt(sf::microseconds(16666)),
window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close),
currentTri_state(CurrentState::NONE) currentTri_state(CurrentState::NONE)
{ {
ImGui::SFML::Init(window);
window.setFramerateLimit(60);
currentTri.setPointCount(3); currentTri.setPointCount(3);
currentTri.setFillColor(sf::Color::White); currentTri.setFillColor(sf::Color::White);
} }
void Tri::State::handle_event(sf::Event *event) { Tri::State::~State() {
if(event->type == sf::Event::KeyPressed) { window.close();
if(event->key.code == sf::Keyboard::H) { ImGui::SFML::Shutdown();
flags.set(0); }
}
} else if(event->type == sf::Event::KeyReleased) { void Tri::State::handle_events() {
if(event->key.code == sf::Keyboard::H) { while(window.pollEvent(event)) {
flags.reset(0); ImGui::SFML::ProcessEvent(event);
} if(event.type == sf::Event::Closed) {
} else if(event->type == sf::Event::MouseButtonPressed) { window.close();
switch(currentTri_state) { } else if(event.type == sf::Event::KeyPressed) {
case CurrentState::NONE: if(event.key.code == sf::Keyboard::H) {
break; flags.flip(0);
case CurrentState::FIRST: }
break; } else if(event.type == sf::Event::MouseButtonPressed) {
case CurrentState::SECOND: switch(currentTri_state) {
break; case CurrentState::NONE:
case CurrentState::THIRD: break;
break; case CurrentState::FIRST:
break;
case CurrentState::SECOND:
break;
case CurrentState::THIRD:
break;
}
} }
} }
} }
void Tri::State::update() { void Tri::State::update() {
ImGui::SFML::Update(window, dt);
// Seems misleading, but imgui handles setting up the window during update // Seems misleading, but imgui handles setting up the window during update
Tri::draw_help(this); Tri::draw_help(this);
ImGui::EndFrame();
} }
void Tri::State::draw() { void Tri::State::draw() {
window.clear();
ImGui::SFML::Render(window);
window.display();
} }

View file

@ -10,6 +10,7 @@
namespace Tri { namespace Tri {
struct State { struct State {
State(); State();
~State();
/* /*
* 0 - display help * 0 - display help
*/ */
@ -18,11 +19,14 @@ namespace Tri {
const unsigned int height; const unsigned int height;
const sf::Time dt; const sf::Time dt;
sf::RenderWindow window;
std::vector<sf::ConvexShape> tris; std::vector<sf::ConvexShape> tris;
sf::ConvexShape currentTri; sf::ConvexShape currentTri;
enum CurrentState { NONE, FIRST, SECOND, THIRD } currentTri_state; enum CurrentState { NONE, FIRST, SECOND, THIRD } currentTri_state;
void handle_event(sf::Event *event); sf::Event event;
void handle_events();
void update(); void update();
void draw(); void draw();
}; };