Some restructuring of State

This commit is contained in:
Stephen Seo 2020-07-22 15:19:37 +09:00
parent 2fe86ad368
commit b9d7a0496d
4 changed files with 39 additions and 11 deletions

View file

@ -6,12 +6,14 @@
#include "state.hpp" #include "state.hpp"
namespace Tri { 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) { 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::SetNextWindowPos(sf::Vector2f(10.0f, 10.0f));
ImGui::SetNextWindowSize(sf::Vector2f( ImGui::SetNextWindowSize(sf::Vector2f(
state->width - 20.0f, state->get_width() - 20.0f,
state->height - 20.0f)); state->get_height() - 20.0f));
ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration); ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration);
ImGui::Text("This is the help window."); ImGui::Text("This is the help window.");
ImGui::End(); ImGui::End();

View file

@ -10,10 +10,10 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
// init // init
Tri::State state{}; Tri::State state(argc, argv);
// main loop // main loop
while(state.window.isOpen()) { while(state.get_flags().test(1)) {
state.handle_events(); state.handle_events();
state.update(); state.update();
state.draw(); state.draw();

View file

@ -1,16 +1,19 @@
#include "state.hpp" #include "state.hpp"
#include <cstring>
#include <imgui-SFML.h> #include <imgui-SFML.h>
#include "imgui_helper.hpp" #include "imgui_helper.hpp"
Tri::State::State() : Tri::State::State(int argc, char **argv) :
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), window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close),
currentTri_state(CurrentState::NONE) currentTri_state(CurrentState::NONE)
{ {
flags.set(1); // is running
ImGui::SFML::Init(window); ImGui::SFML::Init(window);
window.setFramerateLimit(60); window.setFramerateLimit(60);
@ -28,6 +31,7 @@ void Tri::State::handle_events() {
ImGui::SFML::ProcessEvent(event); ImGui::SFML::ProcessEvent(event);
if(event.type == sf::Event::Closed) { if(event.type == sf::Event::Closed) {
window.close(); window.close();
flags.reset(1);
} else if(event.type == sf::Event::KeyPressed) { } else if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::H) { if(event.key.code == sf::Keyboard::H) {
flags.flip(0); flags.flip(0);
@ -62,3 +66,15 @@ void Tri::State::draw() {
window.display(); 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;
}

View file

@ -8,15 +8,19 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
namespace Tri { namespace Tri {
struct State { class State {
State(); public:
State(int argc, char **argv);
~State(); ~State();
/* /*
* 0 - display help * 0 - display help
* 1 - is running
*/ */
std::bitset<64> flags; private:
const unsigned int width; typedef std::bitset<64> BitsetType;
const unsigned int height; BitsetType flags;
unsigned int width;
unsigned int height;
const sf::Time dt; const sf::Time dt;
sf::RenderWindow window; sf::RenderWindow window;
@ -26,9 +30,15 @@ namespace Tri {
sf::Event event; sf::Event event;
public:
void handle_events(); void handle_events();
void update(); void update();
void draw(); void draw();
unsigned int get_width();
unsigned int get_height();
const BitsetType get_flags() const;
}; };
} }