summaryrefslogtreecommitdiffstats
path: root/src/view
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 15:22:09 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 15:22:09 +0200
commitaadcecf2022ec13d15da5d816567779740a37da7 (patch)
tree1d1c5c448c69deea35d689d89cfd7abdd754e649 /src/view
parentccf859fe805afe8db3aa694df5d26da7778dfc68 (diff)
downloadrpgedit-aadcecf2022ec13d15da5d816567779740a37da7.tar
rpgedit-aadcecf2022ec13d15da5d816567779740a37da7.zip
Keep mutable entity list in the MapContext only
Diffstat (limited to 'src/view')
-rw-r--r--src/view/MapView.cpp36
-rw-r--r--src/view/MapView.hpp7
2 files changed, 29 insertions, 14 deletions
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<Window> &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<std::string, SDL_Texture *> &entity : entities)
+ clearEntities();
+}
+
+void MapView::updateEntities(const std::vector<std::unique_ptr<Model::Entity>> &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<std::string, SDL_Texture *> &entity : entitySprites)
SDL_DestroyTexture(entity.second);
+
+ entitySprites.clear();
}
-void MapView::render(float centerX, float centerY, uint64_t time) {
+void MapView::render(const std::vector<std::unique_ptr<Model::Entity>> &entities, Model::Position center, uint64_t time) {
SDL_RenderClear(window->getRenderer());
std::pair<int, int> 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<Model::Entity> &entity : map->getEntities()) {
+ for (const std::unique_ptr<Model::Entity> &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<const Model::Map> map;
SDL_Texture *tiles;
- std::map<std::string, SDL_Texture *> entities;
+ std::map<std::string, SDL_Texture *> entitySprites;
int getTileSize() {
return 32;
@@ -55,7 +55,10 @@ public:
const std::shared_ptr<const Model::Map> &map0);
~MapView();
- void render(float centerX, float centerY, uint64_t time);
+ void updateEntities(const std::vector<std::unique_ptr<Model::Entity>> &entities);
+ void clearEntities();
+
+ void render(const std::vector<std::unique_ptr<Model::Entity>> &entities, Model::Position center, uint64_t time);
};
}