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
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,38 @@
|
||||||
#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) {
|
|
||||||
if(event->key.code == sf::Keyboard::H) {
|
void Tri::State::handle_events() {
|
||||||
flags.reset(0);
|
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) {
|
} else if(event.type == sf::Event::MouseButtonPressed) {
|
||||||
switch(currentTri_state) {
|
switch(currentTri_state) {
|
||||||
case CurrentState::NONE:
|
case CurrentState::NONE:
|
||||||
break;
|
break;
|
||||||
|
@ -34,11 +45,20 @@ void Tri::State::handle_event(sf::Event *event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue