From aadcecf2022ec13d15da5d816567779740a37da7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2014 15:22:09 +0200 Subject: Keep mutable entity list in the MapContext only --- src/control/MapContext.cpp | 21 ++++++++++++++++----- src/control/MapContext.hpp | 15 +++++++++++---- src/control/RPGEdit.cpp | 9 +++------ src/model/Map.cpp | 11 +++-------- src/model/Map.hpp | 17 ++++++++--------- src/view/MapView.cpp | 36 ++++++++++++++++++++++++------------ src/view/MapView.hpp | 7 +++++-- 7 files changed, 70 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/control/MapContext.cpp b/src/control/MapContext.cpp index 5c715db..e3f9a49 100644 --- a/src/control/MapContext.cpp +++ b/src/control/MapContext.cpp @@ -31,8 +31,19 @@ namespace RPGEdit { namespace Control { -MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr &map0) - : eventBus(eventBus0), inputHandler(inputHandler0), map(map0) { +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)); + + playerEntity = new Model::Entity("square"); + playerEntity->moveTo(Model::Position{8, 8}); + + entities.emplace_back(playerEntity); + + view->updateEntities(entities); + + inputHandler->registerListener( [this] (uint16_t key, bool pressed, uint64_t time) { if (pressed) @@ -42,14 +53,14 @@ MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const s } void MapContext::movePlayer(Model::Direction dir, uint64_t time) { - if (map->getPlayerEntity()->hasTransition()) + if (playerEntity->hasTransition()) return; - map->getPlayerEntity()->move(dir, time, time+250); + playerEntity->move(dir, time, time+250); eventBus->enqueue( [=] { - map->getPlayerEntity()->finishTransition(); + playerEntity->finishTransition(); movePlayerContinue(time+250); }, time+250 diff --git a/src/control/MapContext.hpp b/src/control/MapContext.hpp index 37a38ea..a9481a6 100644 --- a/src/control/MapContext.hpp +++ b/src/control/MapContext.hpp @@ -44,19 +44,26 @@ private: EventBus *const eventBus; InputHandler *const inputHandler; + View::MapView *const view; + std::shared_ptr map; - uint64_t totalTicks = 0; + std::vector> entities; + Model::Entity *playerEntity; void movePlayer(Model::Direction dir, uint64_t time); void movePlayerContinue(uint64_t time); void keyPressed(uint16_t key, uint64_t time); + Model::Position getViewPosition(uint64_t time) { + return playerEntity->getPosition(time); + } + public: - MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr &map0); + MapContext(EventBus *eventBus0, InputHandler *inputHandler0, View::MapView *view0, const std::shared_ptr &map0); - Model::Position getViewPosition(uint64_t time) { - return map->getPlayerEntity()->getPosition(time); + void render(uint64_t time) { + view->render(entities, getViewPosition(time), time); } }; diff --git a/src/control/RPGEdit.cpp b/src/control/RPGEdit.cpp index 31998b2..e22d8fa 100644 --- a/src/control/RPGEdit.cpp +++ b/src/control/RPGEdit.cpp @@ -74,10 +74,7 @@ void RPGEdit::systemLoop() { { std::unique_lock lock(modelMutex); - uint64_t time = std::min(timeProvider.now(), handledTime); - - Model::Position pos = ctx->getViewPosition(time); - mapView->render(pos.x, pos.y, time); + ctx->render(std::min(timeProvider.now(), handledTime)); } SDL_RenderPresent(window->getRenderer()); @@ -104,11 +101,11 @@ void RPGEdit::eventLoop() { void RPGEdit::run() { std::shared_ptr map = Model::Map::load("test"); - ctx = std::make_shared(&eventBus, &inputHandler, map); - window = std::make_shared(); mapView = std::make_shared(window, map); + ctx = std::make_shared(&eventBus, &inputHandler, mapView.get(), map); + eventThread = std::thread([this] { eventLoop(); }); systemLoop(); diff --git a/src/model/Map.cpp b/src/model/Map.cpp index a770ee4..aff05d1 100644 --- a/src/model/Map.cpp +++ b/src/model/Map.cpp @@ -32,14 +32,11 @@ namespace RPGEdit { namespace Model { std::shared_ptr Map::load(__attribute__((unused)) const std::string &name) { - std::shared_ptr map(new Map(16, 16)); + std::shared_ptr map(new Map(16, 16, 2)); map->tileset.push_back("dirt"); map->tileset.push_back("horizontal_bar"); - for (int i = 0; i < 2; i++) - map->tiles.emplace_back(new uint32_t[16*16]); - for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { if (4 <= i && i < 12 && 4 <= j && j < 12) @@ -54,10 +51,8 @@ std::shared_ptr Map::load(__attribute__((unused)) const std::string &name) } } - map->playerEntity = std::make_shared("square"); - map->playerEntity->moveTo(Model::Position{6, 6}); - - map->entities.push_back(map->playerEntity); + map->entities.emplace_back("square"); + map->entities.back().moveTo(Model::Position{6, 6}); return map; } diff --git a/src/model/Map.hpp b/src/model/Map.hpp index 423c91d..77a67be 100644 --- a/src/model/Map.hpp +++ b/src/model/Map.hpp @@ -27,7 +27,6 @@ #pragma once #include -#include #include #include #include @@ -44,14 +43,14 @@ private: std::vector tileset; size_t width, height; - std::vector> tiles; + std::vector> tiles; + std::vector entities; - std::shared_ptr playerEntity; - mutable std::deque> entities; - - Map(size_t width0, size_t height0) + Map(size_t width0, size_t height0, size_t layers) : width(width0), height(height0) { + for (size_t i = 0; i < layers; i++) + tiles.emplace_back(width*height); } public: @@ -63,12 +62,12 @@ public: return tileset; } - std::deque> & getEntities() const { + std::vector & getEntities() { return entities; } - const std::shared_ptr & getPlayerEntity() const { - return playerEntity; + const std::vector & getEntities() const { + return entities; } size_t getWidth() const { diff --git a/src/view/MapView.cpp b/src/view/MapView.cpp index d511c31..c8abab5 100644 --- a/src/view/MapView.cpp +++ b/src/view/MapView.cpp @@ -71,21 +71,33 @@ MapView::MapView(const std::shared_ptr &window0, tiles = SDL_CreateTextureFromSurface(window->getRenderer(), surface); SDL_FreeSurface(surface); - - for (auto &entity : map->getEntities()) { - const std::string &name = entity->getName(); - entities[name] = SDL_CreateTextureFromSurface(window->getRenderer(), spriteCache->get("entity", name)); - } } MapView::~MapView() { SDL_DestroyTexture(tiles); - for (const std::pair &entity : entities) + clearEntities(); +} + +void MapView::updateEntities(const std::vector> &entities) { + SpriteCache *spriteCache = window->getSpriteCache(); + + for (auto &entity : entities) { + const std::string &name = entity->getName(); + + if (!entitySprites[name]) + entitySprites[name] = SDL_CreateTextureFromSurface(window->getRenderer(), spriteCache->get("entity", name)); + } +} + +void MapView::clearEntities() { + for (const std::pair &entity : entitySprites) SDL_DestroyTexture(entity.second); + + entitySprites.clear(); } -void MapView::render(float centerX, float centerY, uint64_t time) { +void MapView::render(const std::vector> &entities, Model::Position center, uint64_t time) { SDL_RenderClear(window->getRenderer()); std::pair viewport = window->getViewport(); @@ -96,10 +108,10 @@ void MapView::render(float centerX, float centerY, uint64_t time) { float tilesW = viewport.first / tilePixels; float tilesH = viewport.second / tilePixels; - int minX = std::floor(centerX - tilesW/2 - 0.5f), maxX = std::ceil(centerX + tilesW/2 + 0.5f); - int minY = std::floor(centerY - tilesH/2 - 0.5f), maxY = std::ceil(centerY + tilesH/2 + 0.5f); + int minX = std::floor(center.x - tilesW/2 - 0.5f), maxX = std::ceil(center.x + tilesW/2 + 0.5f); + int minY = std::floor(center.y - tilesH/2 - 0.5f), maxY = std::ceil(center.y + tilesH/2 + 0.5f); - int baseX = viewport.first/2 - int(centerX * tilePixels) - tilePixels/2, baseY = viewport.second/2 - int(centerY * tilePixels) - tilePixels/2; + int baseX = viewport.first/2 - int(center.x * tilePixels) - tilePixels/2, baseY = viewport.second/2 - int(center.y * tilePixels) - tilePixels/2; for (size_t layer = 0; layer < map->getLayerCount(); layer++) { for (int x = minX; x <= maxX; x++) { @@ -127,7 +139,7 @@ void MapView::render(float centerX, float centerY, uint64_t time) { } } - for (const std::shared_ptr &entity : map->getEntities()) { + for (const std::unique_ptr &entity : entities) { Model::Position pos = entity->getPosition(time); Model::Direction dir = entity->getDirection(); @@ -145,7 +157,7 @@ void MapView::render(float centerX, float centerY, uint64_t time) { .h = tilePixels, }; - SDL_RenderCopy(window->getRenderer(), entities[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 e764297..e128a93 100644 --- a/src/view/MapView.hpp +++ b/src/view/MapView.hpp @@ -44,7 +44,7 @@ private: std::shared_ptr map; SDL_Texture *tiles; - std::map entities; + std::map entitySprites; int getTileSize() { return 32; @@ -55,7 +55,10 @@ public: const std::shared_ptr &map0); ~MapView(); - void render(float centerX, float centerY, uint64_t time); + void updateEntities(const std::vector> &entities); + void clearEntities(); + + void render(const std::vector> &entities, Model::Position center, uint64_t time); }; } -- cgit v1.2.3