From 98d0350edb295ace107b22787e511f766e55fe04 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Thu, 11 Jan 2024 17:08:35 +0100 Subject: [PATCH] Implemented texture caching --- include/SpriteComponent.h | 4 ++-- include/TextureManager.h | 33 +++++++++++++++++++++++++++++++-- src/GameObject.cpp | 2 +- src/TextureManager.cpp | 9 ++++++++- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 7e538bf..9a5667c 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -19,7 +19,7 @@ class SpriteComponent : public Component void setTexture(const char* path) { - this->texture = TextureManager::loadTexture(path); + this->texture = TextureManager::get().loadTexture(path); } void init() override @@ -43,7 +43,7 @@ class SpriteComponent : public Component void draw() override { - TextureManager::draw(this->texture, this->srcRect, this->destRect); + TextureManager::get().draw(this->texture, this->srcRect, this->destRect); } private: diff --git a/include/TextureManager.h b/include/TextureManager.h index 83fbd63..85e6bbb 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -1,10 +1,39 @@ #pragma once #include "Game.h" +#include "SDL_render.h" +#include + +struct cmp_str +{ + bool operator()(char const *a, char const *b) const { + return strcmp(a, b) < 0; + } +}; class TextureManager { - public: - static SDL_Texture* loadTexture(const char* fileName); + public: + static TextureManager& get() + { + static TextureManager instance; + return instance; + } + + private: + TextureManager() {} + ~TextureManager() { + for (auto& it : this->texture_cache) { + SDL_DestroyTexture(it.second); + } + } + + public: + TextureManager(TextureManager const&) = delete; + void operator=(TextureManager const&) = delete; + + std::map texture_cache; + + SDL_Texture* loadTexture(const char* fileName); static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); }; \ No newline at end of file diff --git a/src/GameObject.cpp b/src/GameObject.cpp index 84df9c1..8d61ac7 100644 --- a/src/GameObject.cpp +++ b/src/GameObject.cpp @@ -3,7 +3,7 @@ GameObject::GameObject(const char* texturesheet, int x, int y) { - this->objTexture = TextureManager::loadTexture(texturesheet); + this->objTexture = TextureManager::get().loadTexture(texturesheet); this->xPos = x; this->yPos = y; } diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 93fc3e3..a3e0d85 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -1,8 +1,15 @@ #include "TextureManager.h" +#include SDL_Texture* TextureManager::loadTexture(const char* fileName) { - return IMG_LoadTexture(Game::renderer, fileName); + auto it = this->texture_cache.find(fileName); + if (it != this->texture_cache.end()) { + return it->second; + } + auto texture = IMG_LoadTexture(Game::renderer, fileName); + this->texture_cache.emplace(fileName, texture); + return texture; } void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest)