From efa8640aabb3b4df31531ecd41ed1073dda63ed2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2014 17:39:36 +0200 Subject: Make MapContext keep a complete copy of the map --- src/control/MapContext.cpp | 15 ++++++--------- src/control/MapContext.hpp | 10 ++++------ src/control/RPGEdit.cpp | 5 ++--- src/control/RPGEdit.hpp | 5 ++--- src/model/Map.cpp | 4 ++-- src/model/Map.hpp | 9 +++++---- src/view/MapView.cpp | 23 ++++++++++------------- src/view/MapView.hpp | 10 +++++----- 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 &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 &window, const Model::Map &map0) + : eventBus(eventBus0), inputHandler(inputHandler0), map(map0) { + view = std::unique_ptr(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; - std::shared_ptr map; - - std::vector> 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 &map0); + MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr &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 map = Model::Map::load("test"); + std::unique_ptr map = Model::Map::load("test"); window = std::make_shared(); - mapView = std::make_shared(window, map); - ctx = std::make_shared(&eventBus, &inputHandler, mapView.get(), map); + ctx = std::make_shared(&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 ctx; - std::shared_ptr window; - std::shared_ptr mapView; + + std::shared_ptr 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::load(__attribute__((unused)) const std::string &name) { - std::shared_ptr map(new Map(16, 16, 2)); +std::unique_ptr Map::load(__attribute__((unused)) const std::string &name) { + std::unique_ptr 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 +#include #include #include #include @@ -44,7 +45,7 @@ private: size_t width, height; std::vector> tiles; - std::vector entities; + std::deque entities; Map(size_t width0, size_t height0, size_t layers) @@ -62,11 +63,11 @@ public: return tileset; } - std::vector & getEntities() { + std::deque & getEntities() { return entities; } - const std::vector & getEntities() const { + const std::deque & getEntities() const { return entities; } @@ -96,7 +97,7 @@ public: return tiles[layer][y*width + x]; } - static std::shared_ptr load(const std::string &name); + static std::unique_ptr 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 &window0, - const std::shared_ptr &map0) - : window(window0), map(map0) { +MapView::MapView(const std::shared_ptr &window0, const std::vector &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 &window0, SpriteCache *spriteCache = window->getSpriteCache(); - const std::vector &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> &entities) { +void MapView::updateEntities(const std::deque &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> & } void MapView::clearEntities() { - for (const std::pair &entity : entitySprites) + for (auto &entity : entitySprites) SDL_DestroyTexture(entity.second); entitySprites.clear(); } -void MapView::render(const std::vector> &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 viewport = window->getViewport(); @@ -139,9 +136,9 @@ void MapView::render(const std::vector> &entities } } - for (const std::unique_ptr &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> &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; - std::shared_ptr map; SDL_Texture *tiles; std::map entitySprites; @@ -51,14 +50,15 @@ private: } public: - MapView(const std::shared_ptr &window0, - const std::shared_ptr &map0); + MapView(const std::shared_ptr &window0, const std::vector &tileset); ~MapView(); - void updateEntities(const std::vector> &entities); + void updateEntities(const std::deque &entities); void clearEntities(); - void render(const std::vector> &entities, Model::Position center, uint64_t time); + void clear(); + + void render(const Model::Map *map, Model::Position center, uint64_t time); }; } -- cgit v1.2.3