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:
parent
34d1955bd1
commit
2fe86ad368
3 changed files with 46 additions and 52 deletions
34
src/main.cpp
34
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;
|
||||
}
|
||||
|
|
|
@ -1,44 +1,64 @@
|
|||
#include "state.hpp"
|
||||
|
||||
#include <imgui-SFML.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
|
|
@ -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<sf::ConvexShape> 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();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue