From 24ae84861379f2f878804b53a457153ad1ad6c1e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 23 Sep 2014 20:58:49 +0200 Subject: Rewrite main loop --- src/control/MapContext.hpp | 4 +++ src/control/RPGEdit.cpp | 85 +++++++++++++++++++--------------------------- src/control/RPGEdit.hpp | 23 ++++++++++--- src/model/Map.cpp | 2 +- 4 files changed, 58 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/control/MapContext.hpp b/src/control/MapContext.hpp index 504afc2..26f7e34 100644 --- a/src/control/MapContext.hpp +++ b/src/control/MapContext.hpp @@ -55,6 +55,10 @@ public: void advance(InputHandler *inputHandler, unsigned ticks); + Model::Position getViewPosition() { + return map->getPlayerEntity().getPosition(); + } + std::shared_ptr initView(const std::shared_ptr &window) { return std::make_shared(window, map, tiles, entities); } diff --git a/src/control/RPGEdit.cpp b/src/control/RPGEdit.cpp index cb34812..679796c 100644 --- a/src/control/RPGEdit.cpp +++ b/src/control/RPGEdit.cpp @@ -25,8 +25,6 @@ #include "RPGEdit.hpp" -#include "EventBus.hpp" -#include "InputHandler.hpp" #include "MapContext.hpp" #include "../view/MapView.hpp" @@ -40,67 +38,52 @@ namespace RPGEdit { namespace Control { -RPGEdit::RPGEdit() { -} - -RPGEdit::~RPGEdit() { -} - -void RPGEdit::run() { - EventBus eventBus; - - InputHandler inputHandler; +bool RPGEdit::systemIter(unsigned ticks) { + int timeout = 0; - ImageLoader tileLoader; - std::shared_ptr map = Model::Map::load("test"); - - std::shared_ptr ctx(new MapContext(&tileLoader, map)); - - std::shared_ptr window(new View::Window); - std::shared_ptr mapView = ctx->initView(window); + SDL_Event event; + if (SDL_WaitEventTimeout(&event, timeout)) { + switch (event.type) { + case SDL_KEYDOWN: + inputHandler.keyPressed(event.key.keysym.scancode); + break; - bool running = true; - uint32_t ticks = SDL_GetTicks(); - uint32_t lastFrameTicks = ticks; + case SDL_KEYUP: + inputHandler.keyReleased(event.key.keysym.scancode); + break; - while (true) { - int timeout = lastFrameTicks + MIN_FRAME_DELAY - SDL_GetTicks(); - if (timeout < 0) - timeout = 0; + case SDL_QUIT: + return false; + } + } - SDL_Event event; - if (SDL_WaitEventTimeout(&event, timeout)) { - switch (event.type) { - case SDL_KEYDOWN: - inputHandler.keyPressed(event.key.keysym.scancode); - break; + ctx->advance(&inputHandler, ticks); - case SDL_KEYUP: - inputHandler.keyReleased(event.key.keysym.scancode); - break; + Model::Position pos = ctx->getViewPosition(); + mapView->render(pos.x, pos.y); - case SDL_QUIT: - running = false; - break; - } - } + return true; +} - if (!running) - break; +void RPGEdit::systemLoop() { + uint32_t ticks1 = SDL_GetTicks(); + uint32_t ticks2 = ticks1; - uint32_t newTicks = SDL_GetTicks(); - ctx->advance(&inputHandler, newTicks - ticks); + while (systemIter(ticks2 - ticks1)) { + ticks1 = ticks2; + ticks2 = SDL_GetTicks(); + } +} - ticks = newTicks; +void RPGEdit::run() { + std::shared_ptr map = Model::Map::load("test"); - if (!SDL_TICKS_PASSED(SDL_GetTicks(), lastFrameTicks + MIN_FRAME_DELAY)) - continue; + ctx = std::make_shared(&tileLoader, map); - Model::Position pos = map->getPlayerEntity().getPosition(); - mapView->render(pos.x, pos.y); + window = std::make_shared(); + mapView = ctx->initView(window); - lastFrameTicks = ticks; - } + systemLoop(); } } diff --git a/src/control/RPGEdit.hpp b/src/control/RPGEdit.hpp index edab13c..f045265 100644 --- a/src/control/RPGEdit.hpp +++ b/src/control/RPGEdit.hpp @@ -26,7 +26,12 @@ #pragma once -#include +#include "EventBus.hpp" +#include "InputHandler.hpp" +#include "MapContext.hpp" + +#include +#include namespace RPGEdit { @@ -35,11 +40,21 @@ namespace Control { class RPGEdit { private: + EventBus eventBus; + InputHandler inputHandler; + ImageLoader tileLoader; -public: - RPGEdit(); - ~RPGEdit(); + std::shared_ptr ctx; + + std::shared_ptr window; + std::shared_ptr mapView; + std::mutex modelLock; + + bool systemIter(unsigned ticks); + void systemLoop(); + +public: void run(); }; diff --git a/src/model/Map.cpp b/src/model/Map.cpp index 48f5669..8081255 100644 --- a/src/model/Map.cpp +++ b/src/model/Map.cpp @@ -45,7 +45,7 @@ std::shared_ptr Map::load(__attribute__((unused)) const std::string &name) } } - map->playerEntity.reset(new Entity("square")); + map->playerEntity = std::make_shared("square"); map->playerEntity->moveTo(6, 6); map->entities.push_back(map->playerEntity); -- cgit v1.2.3