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"
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) {
if(state->flags.test(0)) {
if(state->get_flags().test(0)) {
ImGui::SetNextWindowPos(sf::Vector2f(10.0f, 10.0f));
ImGui::SetNextWindowSize(sf::Vector2f(
state->width - 20.0f,
state->height - 20.0f));
state->get_width() - 20.0f,
state->get_height() - 20.0f));
ImGui::Begin("Help Window", nullptr, ImGuiWindowFlags_NoDecoration);
ImGui::Text("This is the help window.");
ImGui::End();

View file

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

View file

@ -1,16 +1,19 @@
#include "state.hpp"
#include <cstring>
#include <imgui-SFML.h>
#include "imgui_helper.hpp"
Tri::State::State() :
Tri::State::State(int argc, char **argv) :
width(800),
height(600),
dt(sf::microseconds(16666)),
window(sf::VideoMode(800, 600), "Triangles", sf::Style::Titlebar | sf::Style::Close),
currentTri_state(CurrentState::NONE)
{
flags.set(1); // is running
ImGui::SFML::Init(window);
window.setFramerateLimit(60);
@ -28,6 +31,7 @@ void Tri::State::handle_events() {
ImGui::SFML::ProcessEvent(event);
if(event.type == sf::Event::Closed) {
window.close();
flags.reset(1);
} else if(event.type == sf::Event::KeyPressed) {
if(event.key.code == sf::Keyboard::H) {
flags.flip(0);
@ -62,3 +66,15 @@ void Tri::State::draw() {
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>
namespace Tri {
struct State {
State();
class State {
public:
State(int argc, char **argv);
~State();
/*
* 0 - display help
* 1 - is running
*/
std::bitset<64> flags;
const unsigned int width;
const unsigned int height;
private:
typedef std::bitset<64> BitsetType;
BitsetType flags;
unsigned int width;
unsigned int height;
const sf::Time dt;
sf::RenderWindow window;
@ -26,9 +30,15 @@ namespace Tri {
sf::Event event;
public:
void handle_events();
void update();
void draw();
unsigned int get_width();
unsigned int get_height();
const BitsetType get_flags() const;
};
}