From fe0c3b733abf6eac5f4b5bc18f24f8a5cda1bfc4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 22 Sep 2014 21:54:01 +0200 Subject: Render entities --- src/view/MapView.hpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src/view') diff --git a/src/view/MapView.hpp b/src/view/MapView.hpp index 9150753..e3fccc6 100644 --- a/src/view/MapView.hpp +++ b/src/view/MapView.hpp @@ -30,6 +30,7 @@ #include "../model/Map.hpp" #include +#include namespace RPGEdit { @@ -41,11 +42,13 @@ private: std::shared_ptr window; std::shared_ptr map; SDL_Texture *tileTexture; + std::map entityTextures; public: MapView(const std::shared_ptr &window0, const std::shared_ptr &map0, - const std::vector tiles) + const std::vector &tiles, + const std::map &entities) : window(window0), map(map0) { uint32_t rmask, gmask, bmask, amask; @@ -77,10 +80,17 @@ public: tileTexture = SDL_CreateTextureFromSurface(window->getRenderer(), surface); SDL_FreeSurface(surface); + + for (const std::pair &entity : entities) { + entityTextures[entity.first] = SDL_CreateTextureFromSurface(window->getRenderer(), entity.second); + } } ~MapView() { SDL_DestroyTexture(tileTexture); + + for (const std::pair &entity : entityTextures) + SDL_DestroyTexture(entity.second); } void render(float centerX, float centerY) { @@ -123,6 +133,27 @@ public: } } + for (const std::shared_ptr &entity : map->getEntities()) { + std::pair pos = entity->getPosition(); + Direction dir = entity->getDirection(); + + SDL_Rect src = { + .x = 16*dir, + .y = 0, + .w = 16, + .h = 16, + }; + + SDL_Rect dst = { + .x = baseX + int(pos.first*tilePixels), + .y = baseY + int(pos.second*tilePixels), + .w = tilePixels, + .h = tilePixels, + }; + + SDL_RenderCopy(window->getRenderer(), entityTextures[entity->getName()], &src, &dst); + } + SDL_RenderPresent(window->getRenderer()); } }; -- cgit v1.2.3