From 64ad64ae11a9497f36258e1c83d23f7964bbf885 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 5 Feb 2021 21:14:41 -0500 Subject: Prevented multiple loading of the same image files --- src/renderer.cpp | 26 +++++++++++++++----------- src/renderer.h | 5 +++-- 2 files changed, 18 insertions(+), 13 deletions(-) (limited to 'src') 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) { SDL_RenderPresent(ren_.get()); } -int Renderer::loadImageFromFile(std::string_view filename) { - surface_ptr pfs(IMG_Load(filename.data())); - if (!pfs) - { - throw img_error(); - } +int Renderer::loadImageFromFile(std::string filename) { + if (!filenameToTexId_.count(filename)) { + surface_ptr pfs(IMG_Load(filename.data())); + if (!pfs) + { + throw img_error(); + } - texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); - //SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); + texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); + //SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); - int texId = textures_.size(); - textures_.push_back(std::move(tex)); + int texId = textures_.size(); + textures_.push_back(std::move(tex)); + + filenameToTexId_[filename] = texId; + } - return texId; + return filenameToTexId_[filename]; } 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 @@ #include #include #include -#include +#include #include #include @@ -119,7 +119,7 @@ public: void render(Game& game); - int loadImageFromFile(std::string_view filename); + int loadImageFromFile(std::string filename); private: @@ -131,6 +131,7 @@ private: // Textures loaded from files std::vector textures_; + std::map filenameToTexId_; // Map rendering texture_ptr renderMapLayer(const Map& map, int layer); -- cgit 1.4.1