From cbd1993c20e8be78f447151067ebd7b1eb610f6d Mon Sep 17 00:00:00 2001 From: freezarite Date: Sat, 30 Nov 2024 14:55:40 +0100 Subject: [PATCH] Extra map for MapTiles generated by TMX file now works like intended. removed textures map from AssetManager as it is no longer used. Updated SpriteComponent to now check if it is a normal Texture or a MapTileTexture. Added if condition in TextureManager::LoadMapTileTexture to check if the texture was able to be loaded by SDL --- include/AssetManager.h | 2 -- include/SpriteComponent.h | 1 + include/TextureManager.h | 3 +++ src/AssetManager.cpp | 3 --- src/SpriteComponent.cpp | 18 ++++++++++++++++-- src/TextureManager.cpp | 5 ++++- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 6dd5233..154b7ea 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -36,14 +36,12 @@ public: void addMusic(std::string id, const char* path); - SDL_Texture* getTexture(std::string id); Mix_Chunk* getSound(std::string id); Mix_Music* getMusic(std::string id); private: Manager* man; - std::map textures; std::map soundEffects; std::map music; }; diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 4b9f8e2..444663d 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -51,6 +51,7 @@ public: ~SpriteComponent(); void setTexture(Textures texture); + void setMapTileTexture(const char* path); void init() override; void update() override; diff --git a/include/TextureManager.h b/include/TextureManager.h index 9925705..5e5cd02 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -16,6 +16,9 @@ class TextureManager for (auto& it : this->texture_cache) { SDL_DestroyTexture(it.second); } + for (auto& it : this->mapTile_texture_cache) { + SDL_DestroyTexture(it.second); + } } void addSingleTexture(Textures texture, const char* filePath); diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f90b6d1..9ac14c1 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -31,9 +31,6 @@ void AssetManager::addMusic(std::string id, const char* path) music.emplace(id, this->man->getGame()->soundManager->loadMusic(path)); } -SDL_Texture* AssetManager::getTexture(std::string id) { - return textures.at(id); -} Mix_Chunk* AssetManager::getSound(std::string id) { return soundEffects.at(id); diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 77ad9f1..99c7690 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "AnimationHandler.h" #include "Direction.h" @@ -18,11 +19,13 @@ SpriteComponent::SpriteComponent(Textures texture, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(0), textureYOffset(0) { this->textureEnum = texture; + this->path = ""; } SpriteComponent::SpriteComponent(Textures texture, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset) { this->textureEnum = texture; + this->path = ""; } SpriteComponent::SpriteComponent(const char* path, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset) { @@ -44,6 +47,8 @@ SpriteComponent::SpriteComponent( playAnimation(defaultAnimation); this->textureEnum = texture; + + this->path = ""; } SpriteComponent::~SpriteComponent() {} @@ -55,7 +60,12 @@ void SpriteComponent::setTexture(Textures texture) void SpriteComponent::init() { - setTexture(this->textureEnum); + if (this->path == "") { + setTexture(this->textureEnum); + } + else { + setMapTileTexture(this->path); + } this->transform = &entity->getComponent(); @@ -97,4 +107,8 @@ void SpriteComponent::playAnimation(std::string type) void SpriteComponent::setDirection(Direction direction) { this->flipped = direction == Direction::RIGHT; -} \ No newline at end of file +} + +void SpriteComponent::setMapTileTexture(const char *path) { + this->texture = VEGO_Game().textureManager->loadMapTileTexture(path); +} diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 1a85fd1..ff8e2fd 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -46,10 +46,13 @@ SDL_Texture* TextureManager::loadMapTileTexture(const char *path) { //returns tile if it exists already if(mapTile_texture_cache.contains(std::string(path))) - return mapTile_texture_cache.at(std::string(path)); + return mapTile_texture_cache.find(std::string(path))->second; auto newTexture = IMG_LoadTexture(VEGO_Game().renderer, path); + if (newTexture == nullptr) + throw std::runtime_error(std::string("Couldn't load texture '") + path + "'"); + this->mapTile_texture_cache.emplace(std::string(path), newTexture); return newTexture;