summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 02:27:30 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-09-24 02:27:30 +0200
commitb5c7b4a162a780980b3ecacf0edd49541888a0a8 (patch)
treea401b50e2e8071fc6aec6b5392997b902e1b721d /src
parentea8840291cdf66784c6a2cb465b63ccfb5483c38 (diff)
downloadrpgedit-b5c7b4a162a780980b3ecacf0edd49541888a0a8.tar
rpgedit-b5c7b4a162a780980b3ecacf0edd49541888a0a8.zip
Move more SDL-specific things to the view
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/control/MapContext.cpp15
-rw-r--r--src/control/MapContext.hpp9
-rw-r--r--src/control/RPGEdit.cpp2
-rw-r--r--src/control/RPGEdit.hpp1
-rw-r--r--src/view/MapView.cpp31
-rw-r--r--src/view/MapView.hpp10
-rw-r--r--src/view/SpriteCache.cpp (renamed from src/control/ImageLoader.cpp)18
-rw-r--r--src/view/SpriteCache.hpp (renamed from src/control/ImageLoader.hpp)10
-rw-r--r--src/view/Window.hpp8
10 files changed, 51 insertions, 55 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 71fc6e2..54836d0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -3,11 +3,11 @@ link_directories(${SDL2_LIBRARY_DIRS})
add_executable(rpgedit
rpgedit.cpp
- control/ImageLoader.cpp
control/MapContext.cpp
control/RPGEdit.cpp
model/Map.cpp
view/MapView.cpp
+ view/SpriteCache.cpp
)
set_target_properties(rpgedit PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall ${SDL2_CFLAGS_OTHER}")
set_target_properties(rpgedit PROPERTIES LINK_FLAGS "${SDL2_LDFLAGS_OTHER}")
diff --git a/src/control/MapContext.cpp b/src/control/MapContext.cpp
index a981e8a..5c715db 100644
--- a/src/control/MapContext.cpp
+++ b/src/control/MapContext.cpp
@@ -31,19 +31,8 @@ namespace RPGEdit {
namespace Control {
-MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, ImageLoader *imageLoader0, const std::shared_ptr<const Model::Map> &map0)
- : eventBus(eventBus0), inputHandler(inputHandler0), imageLoader(imageLoader0), map(map0) {
- const std::vector<std::string> &tileset = map->getTileset();
-
- tiles.resize(tileset.size());
- for (size_t i = 0; i < tileset.size(); i++)
- tiles[i] = imageLoader->get("tile/" + tileset[i]);
-
- std::deque<std::shared_ptr<Model::Entity>> &mapEntities = map->getEntities();
-
- for (const std::shared_ptr<Model::Entity> &entity : mapEntities)
- entities[entity->getName()] = imageLoader->get("entity/" + entity->getName());
-
+MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<const Model::Map> &map0)
+ : eventBus(eventBus0), inputHandler(inputHandler0), map(map0) {
inputHandler->registerListener(
[this] (uint16_t key, bool pressed, uint64_t time) {
if (pressed)
diff --git a/src/control/MapContext.hpp b/src/control/MapContext.hpp
index 446ca10..0612dd3 100644
--- a/src/control/MapContext.hpp
+++ b/src/control/MapContext.hpp
@@ -27,7 +27,6 @@
#pragma once
#include "EventBus.hpp"
-#include "ImageLoader.hpp"
#include "InputHandler.hpp"
#include "../model/Map.hpp"
#include "../view/MapView.hpp"
@@ -44,13 +43,9 @@ class MapContext {
private:
EventBus *const eventBus;
InputHandler *const inputHandler;
- ImageLoader *const imageLoader;
std::shared_ptr<const Model::Map> map;
- std::vector<SDL_Surface *> tiles;
- std::map<std::string, SDL_Surface *> entities;
-
uint64_t totalTicks = 0;
void movePlayer(Model::Direction dir, uint64_t time);
@@ -58,14 +53,14 @@ private:
void keyPressed(uint16_t key, uint64_t time);
public:
- MapContext(EventBus *eventBus0, InputHandler *inputHandler0, ImageLoader *imageLoader0, const std::shared_ptr<const Model::Map> &map0);
+ MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<const Model::Map> &map0);
Model::Position getViewPosition(uint64_t time) {
return map->getPlayerEntity()->getPosition(time);
}
std::shared_ptr<View::MapView> initView(const std::shared_ptr<View::Window> &window) {
- return std::make_shared<View::MapView>(window, map, tiles, entities);
+ return std::make_shared<View::MapView>(window, map);
}
};
diff --git a/src/control/RPGEdit.cpp b/src/control/RPGEdit.cpp
index 0c55a6b..0f31468 100644
--- a/src/control/RPGEdit.cpp
+++ b/src/control/RPGEdit.cpp
@@ -110,7 +110,7 @@ void RPGEdit::eventLoop() {
void RPGEdit::run() {
std::shared_ptr<Model::Map> map = Model::Map::load("test");
- ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, &tileLoader, map);
+ ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, map);
window = std::make_shared<View::Window>();
mapView = ctx->initView(window);
diff --git a/src/control/RPGEdit.hpp b/src/control/RPGEdit.hpp
index 8b5e697..4279f10 100644
--- a/src/control/RPGEdit.hpp
+++ b/src/control/RPGEdit.hpp
@@ -46,7 +46,6 @@ private:
EventBus eventBus;
InputHandler inputHandler;
- ImageLoader tileLoader;
std::shared_ptr<MapContext> ctx;
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/control/ImageLoader.cpp b/src/view/SpriteCache.cpp
index 2e78087..82f669f 100644
--- a/src/control/ImageLoader.cpp
+++ b/src/view/SpriteCache.cpp
@@ -24,31 +24,33 @@
*/
-#include "ImageLoader.hpp"
+#include "SpriteCache.hpp"
#include <SDL_image.h>
namespace RPGEdit {
-namespace Control {
+namespace View {
-std::unique_ptr<SDL_Surface, ImageLoader::SDL_Surface_deleter> ImageLoader::load(const std::string &name) {
- std::string filename = "../resources/" + name + ".png";
+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 * ImageLoader::get(const std::string &name) {
- std::unique_ptr<SDL_Surface, SDL_Surface_deleter> &surface = tiles[name];
+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(name);
+ surface = load(id);
if (!surface)
- tiles.erase(tiles.find(name));
+ sprites.erase(sprites.find(id));
return surface.get();
}
diff --git a/src/control/ImageLoader.hpp b/src/view/SpriteCache.hpp
index 36ae7cd..c08ba6f 100644
--- a/src/control/ImageLoader.hpp
+++ b/src/view/SpriteCache.hpp
@@ -34,9 +34,9 @@
namespace RPGEdit {
-namespace Control {
+namespace View {
-class ImageLoader {
+class SpriteCache {
private:
class SDL_Surface_deleter {
public:
@@ -45,15 +45,15 @@ private:
}
};
- std::unordered_map<std::string, std::unique_ptr<SDL_Surface, SDL_Surface_deleter>> tiles;
+ 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 &name);
+ SDL_Surface * get(const std::string &type, const std::string &name);
void clear() {
- tiles.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;
}