summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 17:39:36 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 17:39:36 +0200
commitefa8640aabb3b4df31531ecd41ed1073dda63ed2 (patch)
tree151ddb75a5866784ce6b65757e0eea68aee6e05b
parentaadcecf2022ec13d15da5d816567779740a37da7 (diff)
downloadrpgedit-efa8640aabb3b4df31531ecd41ed1073dda63ed2.tar
rpgedit-efa8640aabb3b4df31531ecd41ed1073dda63ed2.zip
Make MapContext keep a complete copy of the map
-rw-r--r--src/control/MapContext.cpp15
-rw-r--r--src/control/MapContext.hpp10
-rw-r--r--src/control/RPGEdit.cpp5
-rw-r--r--src/control/RPGEdit.hpp5
-rw-r--r--src/model/Map.cpp4
-rw-r--r--src/model/Map.hpp9
-rw-r--r--src/view/MapView.cpp23
-rw-r--r--src/view/MapView.hpp10
8 files changed, 36 insertions, 45 deletions
diff --git a/src/control/MapContext.cpp b/src/control/MapContext.cpp
index e3f9a49..1904db4 100644
--- a/src/control/MapContext.cpp
+++ b/src/control/MapContext.cpp
@@ -31,18 +31,15 @@ namespace RPGEdit {
namespace Control {
-MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, View::MapView *view0, const std::shared_ptr<const Model::Map> &map0)
- : eventBus(eventBus0), inputHandler(inputHandler0), view(view0), map(map0) {
- for (const Model::Entity &entity : map->getEntities())
- entities.emplace_back(new Model::Entity(entity));
+MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<View::Window> &window, const Model::Map &map0)
+ : eventBus(eventBus0), inputHandler(inputHandler0), map(map0) {
+ view = std::unique_ptr<View::MapView>(new View::MapView(window, map.getTileset()));
- playerEntity = new Model::Entity("square");
+ map.getEntities().emplace_back("square");
+ playerEntity = &map.getEntities().back();
playerEntity->moveTo(Model::Position{8, 8});
- entities.emplace_back(playerEntity);
-
- view->updateEntities(entities);
-
+ view->updateEntities(map.getEntities());
inputHandler->registerListener(
[this] (uint16_t key, bool pressed, uint64_t time) {
diff --git a/src/control/MapContext.hpp b/src/control/MapContext.hpp
index a9481a6..a60bdfc 100644
--- a/src/control/MapContext.hpp
+++ b/src/control/MapContext.hpp
@@ -44,11 +44,9 @@ private:
EventBus *const eventBus;
InputHandler *const inputHandler;
- View::MapView *const view;
+ std::unique_ptr<View::MapView> view;
- std::shared_ptr<const Model::Map> map;
-
- std::vector<std::unique_ptr<Model::Entity>> entities;
+ Model::Map map;
Model::Entity *playerEntity;
void movePlayer(Model::Direction dir, uint64_t time);
@@ -60,10 +58,10 @@ private:
}
public:
- MapContext(EventBus *eventBus0, InputHandler *inputHandler0, View::MapView *view0, const std::shared_ptr<const Model::Map> &map0);
+ MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<View::Window> &window, const Model::Map &map0);
void render(uint64_t time) {
- view->render(entities, getViewPosition(time), time);
+ view->render(&map, getViewPosition(time), time);
}
};
diff --git a/src/control/RPGEdit.cpp b/src/control/RPGEdit.cpp
index e22d8fa..257a801 100644
--- a/src/control/RPGEdit.cpp
+++ b/src/control/RPGEdit.cpp
@@ -99,12 +99,11 @@ void RPGEdit::eventLoop() {
}
void RPGEdit::run() {
- std::shared_ptr<Model::Map> map = Model::Map::load("test");
+ std::unique_ptr<Model::Map> map = Model::Map::load("test");
window = std::make_shared<View::Window>();
- mapView = std::make_shared<View::MapView>(window, map);
- ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, mapView.get(), map);
+ ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, window, *map);
eventThread = std::thread([this] { eventLoop(); });
diff --git a/src/control/RPGEdit.hpp b/src/control/RPGEdit.hpp
index d6f3aee..e505597 100644
--- a/src/control/RPGEdit.hpp
+++ b/src/control/RPGEdit.hpp
@@ -46,10 +46,9 @@ private:
EventBus eventBus;
InputHandler inputHandler;
- std::shared_ptr<MapContext> ctx;
-
std::shared_ptr<View::Window> window;
- std::shared_ptr<View::MapView> mapView;
+
+ std::shared_ptr<MapContext> ctx;
std::thread eventThread;
std::mutex modelMutex;
diff --git a/src/model/Map.cpp b/src/model/Map.cpp
index aff05d1..21fbe00 100644
--- a/src/model/Map.cpp
+++ b/src/model/Map.cpp
@@ -31,8 +31,8 @@ namespace RPGEdit {
namespace Model {
-std::shared_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
- std::shared_ptr<Map> map(new Map(16, 16, 2));
+std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
+ std::unique_ptr<Map> map(new Map(16, 16, 2));
map->tileset.push_back("dirt");
map->tileset.push_back("horizontal_bar");
diff --git a/src/model/Map.hpp b/src/model/Map.hpp
index 77a67be..421b487 100644
--- a/src/model/Map.hpp
+++ b/src/model/Map.hpp
@@ -27,6 +27,7 @@
#pragma once
#include <cstdint>
+#include <deque>
#include <memory>
#include <stdexcept>
#include <vector>
@@ -44,7 +45,7 @@ private:
size_t width, height;
std::vector<std::vector<uint32_t>> tiles;
- std::vector<Entity> entities;
+ std::deque<Entity> entities;
Map(size_t width0, size_t height0, size_t layers)
@@ -62,11 +63,11 @@ public:
return tileset;
}
- std::vector<Entity> & getEntities() {
+ std::deque<Entity> & getEntities() {
return entities;
}
- const std::vector<Entity> & getEntities() const {
+ const std::deque<Entity> & getEntities() const {
return entities;
}
@@ -96,7 +97,7 @@ public:
return tiles[layer][y*width + x];
}
- static std::shared_ptr<Map> load(const std::string &name);
+ static std::unique_ptr<Map> load(const std::string &name);
};
}
diff --git a/src/view/MapView.cpp b/src/view/MapView.cpp
index c8abab5..dbce5e4 100644
--- a/src/view/MapView.cpp
+++ b/src/view/MapView.cpp
@@ -31,9 +31,8 @@ namespace RPGEdit {
namespace View {
-MapView::MapView(const std::shared_ptr<Window> &window0,
- const std::shared_ptr<const Model::Map> &map0)
- : window(window0), map(map0) {
+MapView::MapView(const std::shared_ptr<Window> &window0, const std::vector<std::string> &tileset)
+ : window(window0) {
uint32_t rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
@@ -50,8 +49,6 @@ MapView::MapView(const std::shared_ptr<Window> &window0,
SpriteCache *spriteCache = window->getSpriteCache();
- const std::vector<std::string> &tileset = map->getTileset();
-
SDL_Surface *surface = SDL_CreateRGBSurface(0, getTileSize()*tileset.size(), getTileSize(), 32, rmask, gmask, bmask, amask);
for (size_t i = 0; i < tileset.size(); i++) {
@@ -79,11 +76,11 @@ MapView::~MapView() {
clearEntities();
}
-void MapView::updateEntities(const std::vector<std::unique_ptr<Model::Entity>> &entities) {
+void MapView::updateEntities(const std::deque<Model::Entity> &entities) {
SpriteCache *spriteCache = window->getSpriteCache();
for (auto &entity : entities) {
- const std::string &name = entity->getName();
+ const std::string &name = entity.getName();
if (!entitySprites[name])
entitySprites[name] = SDL_CreateTextureFromSurface(window->getRenderer(), spriteCache->get("entity", name));
@@ -91,13 +88,13 @@ void MapView::updateEntities(const std::vector<std::unique_ptr<Model::Entity>> &
}
void MapView::clearEntities() {
- for (const std::pair<std::string, SDL_Texture *> &entity : entitySprites)
+ for (auto &entity : entitySprites)
SDL_DestroyTexture(entity.second);
entitySprites.clear();
}
-void MapView::render(const std::vector<std::unique_ptr<Model::Entity>> &entities, Model::Position center, uint64_t time) {
+void MapView::render(const Model::Map *map, Model::Position center, uint64_t time) {
SDL_RenderClear(window->getRenderer());
std::pair<int, int> viewport = window->getViewport();
@@ -139,9 +136,9 @@ void MapView::render(const std::vector<std::unique_ptr<Model::Entity>> &entities
}
}
- for (const std::unique_ptr<Model::Entity> &entity : entities) {
- Model::Position pos = entity->getPosition(time);
- Model::Direction dir = entity->getDirection();
+ for (auto &entity : map->getEntities()) {
+ Model::Position pos = entity.getPosition(time);
+ Model::Direction dir = entity.getDirection();
SDL_Rect src = {
.x = getTileSize()*dir,
@@ -157,7 +154,7 @@ void MapView::render(const std::vector<std::unique_ptr<Model::Entity>> &entities
.h = tilePixels,
};
- SDL_RenderCopy(window->getRenderer(), entitySprites[entity->getName()], &src, &dst);
+ SDL_RenderCopy(window->getRenderer(), entitySprites[entity.getName()], &src, &dst);
}
}
diff --git a/src/view/MapView.hpp b/src/view/MapView.hpp
index e128a93..f101c24 100644
--- a/src/view/MapView.hpp
+++ b/src/view/MapView.hpp
@@ -41,7 +41,6 @@ namespace View {
class MapView {
private:
std::shared_ptr<Window> window;
- std::shared_ptr<const Model::Map> map;
SDL_Texture *tiles;
std::map<std::string, SDL_Texture *> entitySprites;
@@ -51,14 +50,15 @@ private:
}
public:
- MapView(const std::shared_ptr<Window> &window0,
- const std::shared_ptr<const Model::Map> &map0);
+ MapView(const std::shared_ptr<Window> &window0, const std::vector<std::string> &tileset);
~MapView();
- void updateEntities(const std::vector<std::unique_ptr<Model::Entity>> &entities);
+ void updateEntities(const std::deque<Model::Entity> &entities);
void clearEntities();
- void render(const std::vector<std::unique_ptr<Model::Entity>> &entities, Model::Position center, uint64_t time);
+ void clear();
+
+ void render(const Model::Map *map, Model::Position center, uint64_t time);
};
}