diff options
Diffstat (limited to 'src/view/MapView.cpp')
-rw-r--r-- | src/view/MapView.cpp | 36 |
1 files changed, 24 insertions, 12 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); } } |