summaryrefslogtreecommitdiff
path: root/src/app.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.cpp')
-rw-r--r--src/app.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/app.cpp b/src/app.cpp
new file mode 100644
index 0000000..30bccdf
--- /dev/null
+++ b/src/app.cpp
@@ -0,0 +1,71 @@
+#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_} )
+ //, 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<TitleState>(States::Id::Title);
+}