summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-05 21:14:41 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-05 21:14:41 -0500
commit64ad64ae11a9497f36258e1c83d23f7964bbf885 (patch)
tree9d96e640fbc0982d3faafddee8e8ffec3a44f3a0 /src
parent4d26b7cfd73cef00a7295e3ec9bf021506f0838c (diff)
downloadtanetane-64ad64ae11a9497f36258e1c83d23f7964bbf885.tar.gz
tanetane-64ad64ae11a9497f36258e1c83d23f7964bbf885.tar.bz2
tanetane-64ad64ae11a9497f36258e1c83d23f7964bbf885.zip
Prevented multiple loading of the same image files
Diffstat (limited to 'src')
-rw-r--r--src/renderer.cpp26
-rw-r--r--src/renderer.h5
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
231int Renderer::loadImageFromFile(std::string_view filename) { 231int 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
247void Renderer::renderMessageLine(MessageCache& line, const std::string& text, Game& game) { 251void 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
124private: 124private:
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);