#include "app.h" #include "states/ids.h" #include "states/titleState.h" const sf::Time App::TIME_PER_FRAME = sf::seconds(1.f/60.f); App::App() : textures_() , window_(sf::VideoMode(640, 480), "Demo game" ) , stateStack_( State::Context{&window_, &textures_, &textureFonts_} ) //, player_() { window_.setFramerateLimit(60); registerStates(); stateStack_.push(States::Id::Title); } void App::run(){ // Make the app render the current state, so it needs a state stack too sf::Clock clock; sf::Time timeSinceLastUpdate = sf::Time::Zero; while(window_.isOpen()){ // TODO Maybe move to a fixed time game loop (I prefer that) sf::Time dt = clock.restart(); timeSinceLastUpdate += dt; while (timeSinceLastUpdate > TIME_PER_FRAME) { timeSinceLastUpdate -= TIME_PER_FRAME; processInput(); update(TIME_PER_FRAME); // Check inside this loop, because stack might be empty before update() call if (stateStack_.isEmpty()) window_.close(); } render(); } } void App::render(){ window_.clear(); stateStack_.render(); window_.display(); } void App::update(sf::Time dt){ stateStack_.update(dt); } void App::processInput(){ sf::Event event; while (window_.pollEvent(event)) { stateStack_.handleEvent(event); if (event.type == sf::Event::Closed) window_.close(); } } void App::registerStates(){ stateStack_.registerState(States::Id::Title); }