diff --git a/source/code/sago/SagoDataHolder.cpp b/source/code/sago/SagoDataHolder.cpp index 926a048..d06d04b 100644 --- a/source/code/sago/SagoDataHolder.cpp +++ b/source/code/sago/SagoDataHolder.cpp @@ -42,6 +42,7 @@ struct SagoDataHolder::SagoDataHolderData { std::vector rwOpsToFree; std::vector> dataToFree; bool verbose = false; + Uint64 version = 0; SDL_Renderer* renderer = nullptr; }; @@ -64,6 +65,7 @@ void SagoDataHolder::invalidateAll(SDL_Renderer* renderer) { } void SagoDataHolder::invalidateAll() { + data->version++; for (auto& item : data->textures) { SDL_DestroyTexture(item.second); } @@ -265,4 +267,27 @@ void SagoDataHolder::setVerbose(bool value) { data->verbose = value; } +Uint64 SagoDataHolder::getVersion() const { + return data->version; +} + +TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string &textureName) { + this->holder = holder; + this->version = this->holder->getVersion(); + this->textureName = textureName; + this->data = this->holder->getTexturePtr(this->textureName); +} + +SDL_Texture* TextureHandler::get() { + if (version != holder->getVersion()) { + //The holder has been invalidated + this->data = this->holder->getTexturePtr(textureName); + } + return data; +} + +TextureHandler SagoDataHolder::getTextureHolder(const std::string &textureName) const { + return TextureHandler(this, textureName); +} + } //name space sago diff --git a/source/code/sago/SagoDataHolder.hpp b/source/code/sago/SagoDataHolder.hpp index 389d098..05d0308 100644 --- a/source/code/sago/SagoDataHolder.hpp +++ b/source/code/sago/SagoDataHolder.hpp @@ -34,6 +34,20 @@ SOFTWARE. namespace sago { +class SagoDataHolder; + +class TextureHandler { +public: + TextureHandler() {}; + TextureHandler(const SagoDataHolder* holder, const std::string &textureName); + SDL_Texture* get(); +private: + std::string textureName; + const SagoDataHolder* holder = nullptr; + SDL_Texture* data = nullptr; + Uint64 version = 0; +}; + class SagoDataHolder { public: /** @@ -48,6 +62,7 @@ public: * @return Pointer to the loaded texture */ SDL_Texture* getTexturePtr(const std::string &textureName) const; + TextureHandler getTextureHolder(const std::string &textureName) const; TTF_Font* getFontPtr(const std::string &fontName, int ptsize) const; Mix_Music* getMusicPtr(const std::string &musicName) const; Mix_Chunk* getSoundPtr(const std::string &soundName) const; @@ -63,6 +78,7 @@ public: * Setting a new renderer might cause all old textures to no longer match the renderer format. */ void invalidateAll(SDL_Renderer* renderer); + Uint64 getVersion() const; virtual ~SagoDataHolder(); private: SagoDataHolder(const SagoDataHolder& base) = delete; diff --git a/source/code/sago/SagoSprite.cpp b/source/code/sago/SagoSprite.cpp index 2068196..4c0f57e 100644 --- a/source/code/sago/SagoSprite.cpp +++ b/source/code/sago/SagoSprite.cpp @@ -28,7 +28,7 @@ SOFTWARE. namespace sago { struct SagoSprite::SagoSpriteData { - SDL_Texture* tex = nullptr; + TextureHandler tex; SDL_Rect imgCord{}; SDL_Rect origin{}; int aniFrames = 0; @@ -41,7 +41,7 @@ SagoSprite::SagoSprite() { SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) { data = new SagoSpriteData(); - data->tex = texHolder.getTexturePtr(texture); + data->tex = texHolder.getTextureHolder(texture); data->imgCord = initImage; data->aniFrames = animationFrames; data->aniFrameTime = animationFrameLength; @@ -65,7 +65,7 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) cons } void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const { - if (!data->tex) { + if (!data->tex.get()) { std::cerr << "Texture is null!\n"; } SDL_Rect rect = data->imgCord; @@ -79,7 +79,7 @@ void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y if (h > 0) { pos.h = h; } - SDL_RenderCopy(target, data->tex, &rect, &pos); + SDL_RenderCopy(target, data->tex.get(), &rect, &pos); } void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const { @@ -92,7 +92,7 @@ void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, cons SDL_Rect pos = rect; pos.x = x; pos.y = y; - SDL_RenderCopy(target, data->tex, &rect, &pos); + SDL_RenderCopy(target, data->tex.get(), &rect, &pos); } void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const { @@ -138,7 +138,7 @@ void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int rect.h -= absDiff; } - SDL_RenderCopy(target, data->tex, &rect, &pos); + SDL_RenderCopy(target, data->tex.get(), &rect, &pos); } void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {