summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/control/MapContext.hpp4
-rw-r--r--src/control/RPGEdit.cpp85
-rw-r--r--src/control/RPGEdit.hpp23
-rw-r--r--src/model/Map.cpp2
4 files changed, 58 insertions, 56 deletions
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<View::MapView> initView(const std::shared_ptr<View::Window> &window) {
return std::make_shared<View::MapView>(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<Model::Map> map = Model::Map::load("test");
-
- std::shared_ptr<MapContext> ctx(new MapContext(&tileLoader, map));
-
- std::shared_ptr<View::Window> window(new View::Window);
- std::shared_ptr<View::MapView> 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<Model::Map> map = Model::Map::load("test");
- if (!SDL_TICKS_PASSED(SDL_GetTicks(), lastFrameTicks + MIN_FRAME_DELAY))
- continue;
+ ctx = std::make_shared<MapContext>(&tileLoader, map);
- Model::Position pos = map->getPlayerEntity().getPosition();
- mapView->render(pos.x, pos.y);
+ window = std::make_shared<View::Window>();
+ 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 <SDL.h>
+#include "EventBus.hpp"
+#include "InputHandler.hpp"
+#include "MapContext.hpp"
+
+#include <memory>
+#include <mutex>
namespace RPGEdit {
@@ -35,11 +40,21 @@ namespace Control {
class RPGEdit {
private:
+ EventBus eventBus;
+ InputHandler inputHandler;
+ ImageLoader tileLoader;
-public:
- RPGEdit();
- ~RPGEdit();
+ std::shared_ptr<MapContext> ctx;
+
+ std::shared_ptr<View::Window> window;
+ std::shared_ptr<View::MapView> 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> Map::load(__attribute__((unused)) const std::string &name)
}
}
- map->playerEntity.reset(new Entity("square"));
+ map->playerEntity = std::make_shared<Entity>("square");
map->playerEntity->moveTo(6, 6);
map->entities.push_back(map->playerEntity);