From ac217e931baf79f33bcc177563293775fa6f7ae0 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 16:59:46 +0100 Subject: [PATCH] added new Map for tile-textures as they wont work with our current enum-class maps. --- include/TextureManager.h | 4 ++++ src/Map.cpp | 3 ++- src/TextureManager.cpp | 19 +++++++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/TextureManager.h b/include/TextureManager.h index cf6056c..9925705 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -23,7 +23,11 @@ class TextureManager SDL_Texture* loadTexture(Textures texture); static std::vector splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet); static void draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped = false); + + SDL_Texture* loadMapTileTexture(const char* path); + private: Manager* manager; std::map texture_cache; + std::map mapTile_texture_cache; }; \ No newline at end of file diff --git a/src/Map.cpp b/src/Map.cpp index 6b72837..9eb9212 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -104,7 +104,7 @@ void Map::loadTileLayer(const tmx::TileLayer& layer) tmx::Vector2i textureSize; SDL_QueryTexture( - VEGO_Game().textureManager->loadTexture(texturePath), + VEGO_Game().textureManager->loadMapTileTexture(texturePath), nullptr, nullptr, &(textureSize.x), @@ -170,6 +170,7 @@ void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int tile.addComponent(x, y, mapTileSize.x, mapTileSize.y, 1); tile.addComponent(texturePath.c_str(), v, u, zIndex); // why does uv need to be reversed? + //TODO: also implement updated map stuff for this if (hasCollision) { // tag currently does not have a clear purposes, TODO: figure out appropriate tag name diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index ff2b697..1a85fd1 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -3,12 +3,14 @@ #include #include #include +#include +#include #include "GameInternal.h" void TextureManager::addSingleTexture(Textures texture, const char* filePath) { - auto sdlTexture = IMG_LoadTexture(this->manager->getGame()->renderer, filePath); + auto sdlTexture = IMG_LoadTexture(VEGO_Game().renderer, filePath); if (sdlTexture == nullptr) throw std::runtime_error(std::string("Couldn't load texture '") + filePath + "'"); @@ -38,4 +40,17 @@ void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect { SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; SDL_RenderCopyEx(renderer, texture, &src, &dest, 0, NULL, flip); -} \ No newline at end of file +} + +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)); + + auto newTexture = IMG_LoadTexture(VEGO_Game().renderer, path); + + this->mapTile_texture_cache.emplace(std::string(path), newTexture); + + return newTexture; +}