diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-09-24 02:27:30 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2014-09-24 02:27:30 +0200 |
commit | b5c7b4a162a780980b3ecacf0edd49541888a0a8 (patch) | |
tree | a401b50e2e8071fc6aec6b5392997b902e1b721d /src/view | |
parent | ea8840291cdf66784c6a2cb465b63ccfb5483c38 (diff) | |
download | rpgedit-b5c7b4a162a780980b3ecacf0edd49541888a0a8.tar rpgedit-b5c7b4a162a780980b3ecacf0edd49541888a0a8.zip |
Move more SDL-specific things to the view
Diffstat (limited to 'src/view')
-rw-r--r-- | src/view/MapView.cpp | 31 | ||||
-rw-r--r-- | src/view/MapView.hpp | 10 | ||||
-rw-r--r-- | src/view/SpriteCache.cpp | 61 | ||||
-rw-r--r-- | src/view/SpriteCache.hpp | 62 | ||||
-rw-r--r-- | src/view/Window.hpp | 8 |
5 files changed, 153 insertions, 19 deletions
diff --git a/src/view/MapView.cpp b/src/view/MapView.cpp index 5e55290..4938c5d 100644 --- a/src/view/MapView.cpp +++ b/src/view/MapView.cpp @@ -32,9 +32,7 @@ namespace RPGEdit { namespace View { MapView::MapView(const std::shared_ptr<Window> &window0, - const std::shared_ptr<const Model::Map> &map0, - const std::vector<SDL_Surface *> &tiles, - const std::map<std::string, SDL_Surface *> &entities) + const std::shared_ptr<const Model::Map> &map0) : window(window0), map(map0) { uint32_t rmask, gmask, bmask, amask; @@ -50,10 +48,14 @@ MapView::MapView(const std::shared_ptr<Window> &window0, amask = 0xff000000; #endif - SDL_Surface *surface = SDL_CreateRGBSurface(0, getTileSize()*tiles.size(), getTileSize(), 32, rmask, gmask, bmask, amask); + const std::vector<std::string> &tileset = map->getTileset(); + + SDL_Surface *surface = SDL_CreateRGBSurface(0, getTileSize()*tileset.size(), getTileSize(), 32, rmask, gmask, bmask, amask); SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); - for (size_t i = 0; i < tiles.size(); i++) { + SpriteCache *spriteCache = window->getSpriteCache(); + + for (size_t i = 0; i < tileset.size(); i++) { SDL_Rect rect = { .x = int(getTileSize()*i), .y = 0, @@ -61,21 +63,22 @@ MapView::MapView(const std::shared_ptr<Window> &window0, .h = 0, }; - SDL_BlitSurface(tiles[i], nullptr, surface, &rect); - } + SDL_BlitSurface(spriteCache->get("tile", tileset[i]), nullptr, surface, &rect); + } - tileTexture = SDL_CreateTextureFromSurface(window->getRenderer(), surface); + tiles = SDL_CreateTextureFromSurface(window->getRenderer(), surface); SDL_FreeSurface(surface); - for (const std::pair<std::string, SDL_Surface *> &entity : entities) { - entityTextures[entity.first] = SDL_CreateTextureFromSurface(window->getRenderer(), entity.second); + 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(tileTexture); + SDL_DestroyTexture(tiles); - for (const std::pair<std::string, SDL_Texture *> &entity : entityTextures) + for (const std::pair<std::string, SDL_Texture *> &entity : entities) SDL_DestroyTexture(entity.second); } @@ -115,7 +118,7 @@ void MapView::render(float centerX, float centerY, uint64_t time) { .h = tilePixels, }; - SDL_RenderCopy(window->getRenderer(), tileTexture, &src, &dst); + SDL_RenderCopy(window->getRenderer(), tiles, &src, &dst); } } @@ -137,7 +140,7 @@ void MapView::render(float centerX, float centerY, uint64_t time) { .h = tilePixels, }; - SDL_RenderCopy(window->getRenderer(), entityTextures[entity->getName()], &src, &dst); + SDL_RenderCopy(window->getRenderer(), entities[entity->getName()], &src, &dst); } } diff --git a/src/view/MapView.hpp b/src/view/MapView.hpp index 7f83006..e764297 100644 --- a/src/view/MapView.hpp +++ b/src/view/MapView.hpp @@ -31,6 +31,7 @@ #include <cmath> #include <map> +#include <unordered_set> namespace RPGEdit { @@ -41,8 +42,9 @@ class MapView { private: std::shared_ptr<Window> window; std::shared_ptr<const Model::Map> map; - SDL_Texture *tileTexture; - std::map<std::string, SDL_Texture *> entityTextures; + + SDL_Texture *tiles; + std::map<std::string, SDL_Texture *> entities; int getTileSize() { return 32; @@ -50,9 +52,7 @@ private: public: MapView(const std::shared_ptr<Window> &window0, - const std::shared_ptr<const Model::Map> &map0, - const std::vector<SDL_Surface *> &tiles, - const std::map<std::string, SDL_Surface *> &entities); + const std::shared_ptr<const Model::Map> &map0); ~MapView(); void render(float centerX, float centerY, uint64_t time); diff --git a/src/view/SpriteCache.cpp b/src/view/SpriteCache.cpp new file mode 100644 index 0000000..82f669f --- /dev/null +++ b/src/view/SpriteCache.cpp @@ -0,0 +1,61 @@ +/* + Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net> + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "SpriteCache.hpp" + +#include <SDL_image.h> + + +namespace RPGEdit { + +namespace View { + +std::unique_ptr<SDL_Surface, SpriteCache::SDL_Surface_deleter> SpriteCache::load(const std::string &name) { + std::string filename = "../resources/sprite/" + name + ".png"; + + SDL_Surface *surface = IMG_Load(filename.c_str()); + + return std::unique_ptr<SDL_Surface, SDL_Surface_deleter>(surface, SDL_Surface_deleter()); +} + +SDL_Surface * SpriteCache::get(const std::string &type, const std::string &name) { + std::string id = type + "/" + name; + + std::unique_ptr<SDL_Surface, SDL_Surface_deleter> &surface = sprites[id]; + + if (!surface) + surface = load(id); + + if (!surface) + sprites.erase(sprites.find(id)); + + return surface.get(); +} + + +} + +} diff --git a/src/view/SpriteCache.hpp b/src/view/SpriteCache.hpp new file mode 100644 index 0000000..c08ba6f --- /dev/null +++ b/src/view/SpriteCache.hpp @@ -0,0 +1,62 @@ +/* + Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net> + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#pragma once + +#include <memory> +#include <unordered_map> + +#include <SDL.h> + + +namespace RPGEdit { + +namespace View { + +class SpriteCache { +private: + class SDL_Surface_deleter { + public: + void operator()(SDL_Surface *surface) { + SDL_FreeSurface(surface); + } + }; + + std::unordered_map<std::string, std::unique_ptr<SDL_Surface, SDL_Surface_deleter>> sprites; + + std::unique_ptr<SDL_Surface, SDL_Surface_deleter> load(const std::string &name); + +public: + SDL_Surface * get(const std::string &type, const std::string &name); + + void clear() { + sprites.clear(); + } +}; + +} + +} diff --git a/src/view/Window.hpp b/src/view/Window.hpp index f53a9a0..8c4c7a7 100644 --- a/src/view/Window.hpp +++ b/src/view/Window.hpp @@ -26,6 +26,8 @@ #pragma once +#include "SpriteCache.hpp" + #include <utility> #include <SDL.h> @@ -37,6 +39,8 @@ namespace View { class Window { private: + SpriteCache spriteCache; + SDL_Window *window; SDL_Renderer *renderer; @@ -51,6 +55,10 @@ public: SDL_DestroyWindow(window); } + SpriteCache * getSpriteCache() { + return &spriteCache; + } + SDL_Renderer * getRenderer() { return renderer; } |