diff options
-rw-r--r-- | src/renderer.cpp | 26 | ||||
-rw-r--r-- | src/renderer.h | 5 |
2 files changed, 18 insertions, 13 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index 4f5b4ff..0035ce2 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -228,20 +228,24 @@ void Renderer::render(Game& game) { | |||
228 | SDL_RenderPresent(ren_.get()); | 228 | SDL_RenderPresent(ren_.get()); |
229 | } | 229 | } |
230 | 230 | ||
231 | int Renderer::loadImageFromFile(std::string_view filename) { | 231 | int Renderer::loadImageFromFile(std::string filename) { |
232 | surface_ptr pfs(IMG_Load(filename.data())); | 232 | if (!filenameToTexId_.count(filename)) { |
233 | if (!pfs) | 233 | surface_ptr pfs(IMG_Load(filename.data())); |
234 | { | 234 | if (!pfs) |
235 | throw img_error(); | 235 | { |
236 | } | 236 | throw img_error(); |
237 | } | ||
237 | 238 | ||
238 | texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | 239 | texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); |
239 | //SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); | 240 | //SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); |
240 | 241 | ||
241 | int texId = textures_.size(); | 242 | int texId = textures_.size(); |
242 | textures_.push_back(std::move(tex)); | 243 | textures_.push_back(std::move(tex)); |
244 | |||
245 | filenameToTexId_[filename] = texId; | ||
246 | } | ||
243 | 247 | ||
244 | return texId; | 248 | return filenameToTexId_[filename]; |
245 | } | 249 | } |
246 | 250 | ||
247 | void Renderer::renderMessageLine(MessageCache& line, const std::string& text, Game& game) { | 251 | void Renderer::renderMessageLine(MessageCache& line, const std::string& text, Game& game) { |
diff --git a/src/renderer.h b/src/renderer.h index b812429..2d7f93b 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -5,7 +5,7 @@ | |||
5 | #include <SDL_image.h> | 5 | #include <SDL_image.h> |
6 | #include <stdexcept> | 6 | #include <stdexcept> |
7 | #include <memory> | 7 | #include <memory> |
8 | #include <string_view> | 8 | #include <map> |
9 | #include <string> | 9 | #include <string> |
10 | #include <vector> | 10 | #include <vector> |
11 | 11 | ||
@@ -119,7 +119,7 @@ public: | |||
119 | 119 | ||
120 | void render(Game& game); | 120 | void render(Game& game); |
121 | 121 | ||
122 | int loadImageFromFile(std::string_view filename); | 122 | int loadImageFromFile(std::string filename); |
123 | 123 | ||
124 | private: | 124 | private: |
125 | 125 | ||
@@ -131,6 +131,7 @@ private: | |||
131 | 131 | ||
132 | // Textures loaded from files | 132 | // Textures loaded from files |
133 | std::vector<texture_ptr> textures_; | 133 | std::vector<texture_ptr> textures_; |
134 | std::map<std::string, int> filenameToTexId_; | ||
134 | 135 | ||
135 | // Map rendering | 136 | // Map rendering |
136 | texture_ptr renderMapLayer(const Map& map, int layer); | 137 | texture_ptr renderMapLayer(const Map& map, int layer); |