From 68079d027960bbe84efdee3625be7a7edd9aa5bc Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 13 Oct 2024 13:29:32 +0200 Subject: [PATCH 1/8] made texture_cache map private --- include/TextureManager.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/TextureManager.h b/include/TextureManager.h index 3e4f1c4..dcf88d2 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -17,11 +17,10 @@ class TextureManager } } - std::map texture_cache; - SDL_Texture* loadTexture(const char* fileName); 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); private: Manager* manager; + std::map texture_cache; }; \ No newline at end of file From 65e00c231477cd7422222fb8d9cb0ed9433a0655 Mon Sep 17 00:00:00 2001 From: freezarite Date: Tue, 15 Oct 2024 13:24:56 +0200 Subject: [PATCH 2/8] changed texture_cache map to use enums instead of string for keys and functions using said map --- include/AssetManager.h | 2 +- include/SpriteComponent.h | 10 ++++++---- include/TextureEnumBase.h | 3 +++ include/TextureManager.h | 7 +++++-- src/AssetManager.cpp | 9 ++++++--- src/GameInternal.cpp | 5 +++-- src/SpriteComponent.cpp | 14 +++++++------- src/TextureManager.cpp | 35 +++++++++++++++++++++++++---------- src/TileComponent.cpp | 2 ++ 9 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 include/TextureEnumBase.h diff --git a/include/AssetManager.h b/include/AssetManager.h index 1d3c97a..ee4a447 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -24,7 +24,7 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner); + void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, TexturesEnum textureEnum, Entity* owner); void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); Vector2D calculateSpawnPosition(); diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index ed584a2..bbeac64 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -4,10 +4,12 @@ #include #include #include +#include #include "AnimationHandler.h" #include "Component.h" #include "Direction.h" +#include "TextureEnumBase.h" class TransformComponent; @@ -23,7 +25,7 @@ private: SDL_Texture* texture; SDL_Rect srcRect, destRect; - const char* texturePath; + TexturesEnum textureEnum; bool animated = false; uint8_t frames = 0; @@ -32,15 +34,15 @@ private: public: SpriteComponent() = default; - SpriteComponent(const char* path); + SpriteComponent(TexturesEnum textureEnum); SpriteComponent( - const char* path, + TexturesEnum textureEnum, bool isAnimated, std::map>* animationList, std::string defaultAnimation); ~SpriteComponent(); - void setTexture(const char* path); + void setTexture(TexturesEnum texture); void init() override; void update() override; diff --git a/include/TextureEnumBase.h b/include/TextureEnumBase.h new file mode 100644 index 0000000..0857f12 --- /dev/null +++ b/include/TextureEnumBase.h @@ -0,0 +1,3 @@ +#pragma once + +enum class TexturesEnum; \ No newline at end of file diff --git a/include/TextureManager.h b/include/TextureManager.h index dcf88d2..2cc9469 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -6,6 +6,7 @@ #include #include #include +#include "TextureEnumBase.h" class TextureManager { @@ -17,10 +18,12 @@ class TextureManager } } - SDL_Texture* loadTexture(const char* fileName); + void addSingleTexture(TexturesEnum texture, const char* filePath); + void addTextures(const std::map& textures); + SDL_Texture* loadTexture(TexturesEnum 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); private: Manager* manager; - std::map texture_cache; + std::map texture_cache; }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index a772fe6..b2d960c 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -15,12 +15,15 @@ #include "PowerupComponent.h" #include +#include "TextureEnumBase.h" + AssetManager::AssetManager(Manager* manager) : man(manager) {} AssetManager::~AssetManager() {} +//seems to not be used anymore void AssetManager::addTexture(std::string id, const char* path) { - textures.emplace(id, this->man->getGame()->textureManager->loadTexture(path)); + //textures.emplace(id, this->man->getGame()->textureManager->loadTexture(path)); } void AssetManager::addSoundEffect(std::string id, const char* path) @@ -46,11 +49,11 @@ Mix_Music* AssetManager::getMusic(std::string id) return music.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity* owner) { +void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, TexturesEnum textureEnum, Entity* owner) { auto& projectile(man->addEntity()); projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects - projectile.addComponent(texturePath); + projectile.addComponent(textureEnum); projectile.addComponent(range, speed, velocity, owner); projectile.addComponent("projectile", 0.6f); projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 0e7587b..29d1622 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -78,10 +78,11 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he } SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png"); + + //SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png"); SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); + //SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderPresent(renderer); if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 2c730cf..8ec85e9 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -12,13 +12,13 @@ #include "GameInternal.h" #include "Manager.h" -SpriteComponent::SpriteComponent(const char* path) +SpriteComponent::SpriteComponent(TexturesEnum textureEnum) { - this->texturePath = path; + this->textureEnum = textureEnum; } SpriteComponent::SpriteComponent( - const char* path, + TexturesEnum textureEnum, bool isAnimated, std::map>* animationMap, std::string defaultAnimation) @@ -29,19 +29,19 @@ SpriteComponent::SpriteComponent( playAnimation(defaultAnimation); - this->texturePath = path; + this->textureEnum = textureEnum; } SpriteComponent::~SpriteComponent() {} -void SpriteComponent::setTexture(const char* path) +void SpriteComponent::setTexture(TexturesEnum texture) { - this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(path); + this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(texture); } void SpriteComponent::init() { - setTexture(this->texturePath); + setTexture(this->textureEnum); this->transform = &entity->getComponent(); diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 3e46d46..03852ad 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -6,17 +6,32 @@ #include "GameInternal.h" -SDL_Texture* TextureManager::loadTexture(const char* fileName) -{ - auto it = this->texture_cache.find(fileName); - if (it != this->texture_cache.end()) { - return it->second; + +void TextureManager::addSingleTexture(TexturesEnum texture, const char* filePath) { + auto sdlTexture = IMG_LoadTexture(this->manager->getGame()->renderer, filePath); + + if (sdlTexture == nullptr) + throw std::runtime_error(std::string("Couldn't load texture '") + filePath + "'"); + + this->texture_cache.emplace(texture, sdlTexture); + std::cout << "Loaded texture at " << filePath << std::endl; +} + +void TextureManager::addTextures(const std::map &textures) { + for (auto texture : textures) { + addSingleTexture(texture.first, texture.second); } - auto texture = IMG_LoadTexture(this->manager->getGame()->renderer, fileName); - if (texture == NULL) throw std::runtime_error(std::string("Couldn't load texture '") + fileName + "'"); - this->texture_cache.emplace(std::string(fileName), texture); - printf("Loaded texture at '%s'\n", fileName); - return texture; +} + + +SDL_Texture* TextureManager::loadTexture(TexturesEnum texture) { + auto it = this->texture_cache.find(texture); + + if (it != this->texture_cache.end()) + return it->second; + + std::cout << "ERROR: Couldn't load texture!" << std::endl; + return nullptr; } void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped) diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp index eeebb92..8658e93 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -7,6 +7,8 @@ #include "SpriteComponent.h" #include "TileComponent.h" +#include "TextureEnumBase.h" + TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict) { this->tileRect.x = x; From 494ff8aa0a84557703976e618ed3fc6e7d7f4fd6 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 16:00:47 +0100 Subject: [PATCH 3/8] magic_enum library stuff and refactored TextureEnum to Textures due to bad naming --- .gitmodules | 3 +++ CMakeLists.txt | 2 ++ extern/magic_enum | 1 + include/AssetManager.h | 6 ++---- include/Map.h | 5 +++-- include/SpriteComponent.h | 9 ++++----- include/TextureEnumBase.h | 2 +- include/TextureManager.h | 8 ++++---- include/TileComponent.h | 22 +++++++++++++++++----- src/AssetManager.cpp | 11 +++-------- src/Map.cpp | 8 +++++--- src/SpriteComponent.cpp | 6 +++--- src/TextureManager.cpp | 6 +++--- src/TileComponent.cpp | 7 +++---- 14 files changed, 54 insertions(+), 42 deletions(-) create mode 160000 extern/magic_enum diff --git a/.gitmodules b/.gitmodules index bfa520c..3ae9513 100644 --- a/.gitmodules +++ b/.gitmodules @@ -14,3 +14,6 @@ path = extern/SDL_ttf url = https://github.com/libsdl-org/SDL_ttf.git branch = release-2.22.x +[submodule "extern/magic_enum"] + path = extern/magic_enum + url = https://github.com/Neargye/magic_enum.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 5149005..24a3f77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) +add_subdirectory(extern/magic_enum EXCLUDE_FROM_ALL) file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) add_library(${PROJECT_NAME} ${SOURCES}) @@ -33,6 +34,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL fu SDL2_image::SDL2_image-static SDL2_mixer::SDL2_mixer-static SDL2_ttf::SDL2_ttf-static + magic_enum::magic_enum ) if(CMAKE_BUILD_TYPE MATCHES "Debug") diff --git a/extern/magic_enum b/extern/magic_enum new file mode 160000 index 0000000..a72a053 --- /dev/null +++ b/extern/magic_enum @@ -0,0 +1 @@ +Subproject commit a72a0536c716fdef4f029fb43e1fd7e7b3d9ac9b diff --git a/include/AssetManager.h b/include/AssetManager.h index ee4a447..6dd5233 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -24,14 +24,12 @@ public: AssetManager(Manager* manager); ~AssetManager(); - void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, TexturesEnum textureEnum, Entity* owner); - void createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath); + void createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, Textures textureEnum, Entity* owner); + void createPowerup(Vector2D pos, std::function pickupFunc, Textures texture); Vector2D calculateSpawnPosition(); PowerupType calculateType(); - //texture management - void addTexture(std::string id, const char* path); // sound management void addSoundEffect(std::string id, const char* path); diff --git a/include/Map.h b/include/Map.h index 4c1bc3b..32184be 100644 --- a/include/Map.h +++ b/include/Map.h @@ -2,6 +2,7 @@ #include #include +#include class GameInternal; class Map @@ -21,6 +22,6 @@ public: * \return Boolean for success * */ - static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */); - static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict); + static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */); + static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict); }; diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index bbeac64..f887097 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -9,7 +9,6 @@ #include "AnimationHandler.h" #include "Component.h" #include "Direction.h" -#include "TextureEnumBase.h" class TransformComponent; @@ -25,7 +24,7 @@ private: SDL_Texture* texture; SDL_Rect srcRect, destRect; - TexturesEnum textureEnum; + Textures textureEnum; bool animated = false; uint8_t frames = 0; @@ -34,15 +33,15 @@ private: public: SpriteComponent() = default; - SpriteComponent(TexturesEnum textureEnum); + SpriteComponent(Textures textureEnum); SpriteComponent( - TexturesEnum textureEnum, + Textures textureEnum, bool isAnimated, std::map>* animationList, std::string defaultAnimation); ~SpriteComponent(); - void setTexture(TexturesEnum texture); + void setTexture(Textures texture); void init() override; void update() override; diff --git a/include/TextureEnumBase.h b/include/TextureEnumBase.h index 0857f12..75c3573 100644 --- a/include/TextureEnumBase.h +++ b/include/TextureEnumBase.h @@ -1,3 +1,3 @@ #pragma once -enum class TexturesEnum; \ No newline at end of file +enum class Textures; \ No newline at end of file diff --git a/include/TextureManager.h b/include/TextureManager.h index 2cc9469..cf6056c 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -18,12 +18,12 @@ class TextureManager } } - void addSingleTexture(TexturesEnum texture, const char* filePath); - void addTextures(const std::map& textures); - SDL_Texture* loadTexture(TexturesEnum texture); + void addSingleTexture(Textures texture, const char* filePath); + void addTextures(const std::map& textures); + 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); private: Manager* manager; - std::map texture_cache; + std::map texture_cache; }; \ No newline at end of file diff --git a/include/TileComponent.h b/include/TileComponent.h index 8844daa..627bb93 100644 --- a/include/TileComponent.h +++ b/include/TileComponent.h @@ -3,8 +3,10 @@ #include #include #include +#include #include "Component.h" +#include "TextureEnumBase.h" class SpriteComponent; class TransformComponent; @@ -17,17 +19,27 @@ public: SDL_Rect tileRect; int tileID; - const char* path; + Textures texture; TileComponent() = default; - TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict); + TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict); ~TileComponent() = default; void init() override; - bool hasCollision(){return this->collision;} - std::string getName(){return this->tileName;} + bool hasCollision() { + return this->collision; + } + + std::string getName() { +#ifdef TEXTURE_ENUM_DEFINED + return std::string(magic_enum::enum_name(this->texture)); +#else + return "Undefined Enum"; +#endif + } + + private: bool collision; - std::string tileName; }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index b2d960c..41c3889 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -21,11 +21,6 @@ AssetManager::AssetManager(Manager* manager) : man(manager) {} AssetManager::~AssetManager() {} -//seems to not be used anymore -void AssetManager::addTexture(std::string id, const char* path) { - //textures.emplace(id, this->man->getGame()->textureManager->loadTexture(path)); -} - void AssetManager::addSoundEffect(std::string id, const char* path) { soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path)); @@ -49,7 +44,7 @@ Mix_Music* AssetManager::getMusic(std::string id) return music.at(id); } -void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, TexturesEnum textureEnum, Entity* owner) { +void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, Textures textureEnum, Entity* owner) { auto& projectile(man->addEntity()); projectile.addComponent(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects @@ -59,13 +54,13 @@ void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); } -void AssetManager::createPowerup(Vector2D pos, std::function pickupFunc, std::string texturePath) { +void AssetManager::createPowerup(Vector2D pos, std::function pickupFunc, Textures texture) { auto& powerups(man->addEntity()); powerups.addComponent(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects try { - powerups.addComponent(texturePath.c_str()); + powerups.addComponent(texture); } catch (std::runtime_error e) { std::cout << e.what() << std::endl; diff --git a/src/Map.cpp b/src/Map.cpp index 5689d9e..796002d 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -10,7 +10,7 @@ #include "SDL_error.h" #include "TileComponent.h" -void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */) +void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map>* textureDict /* backreference */) { std::string tileIDstr; char singleChar = 0; @@ -56,11 +56,13 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, co mapFile.close(); } -void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict) // tile entity +void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map>* textureDict) // tile entity { auto& tile(game->manager.addEntity()); tile.addComponent(x, y, TILE_SIZE, TILE_SIZE, id, textureDict); - if(tile.getComponent().hasCollision()) tile.addComponent(tile.getComponent().getName().data()); + if(tile.getComponent().hasCollision()) + tile.addComponent(tile.getComponent().getName().data()); + tile.addGroup((size_t)Entity::GroupLabel::MAPTILES); } \ No newline at end of file diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 8ec85e9..d7728ab 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -12,13 +12,13 @@ #include "GameInternal.h" #include "Manager.h" -SpriteComponent::SpriteComponent(TexturesEnum textureEnum) +SpriteComponent::SpriteComponent(Textures textureEnum) { this->textureEnum = textureEnum; } SpriteComponent::SpriteComponent( - TexturesEnum textureEnum, + Textures textureEnum, bool isAnimated, std::map>* animationMap, std::string defaultAnimation) @@ -34,7 +34,7 @@ SpriteComponent::SpriteComponent( SpriteComponent::~SpriteComponent() {} -void SpriteComponent::setTexture(TexturesEnum texture) +void SpriteComponent::setTexture(Textures texture) { this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(texture); } diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 03852ad..ff2b697 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -7,7 +7,7 @@ #include "GameInternal.h" -void TextureManager::addSingleTexture(TexturesEnum texture, const char* filePath) { +void TextureManager::addSingleTexture(Textures texture, const char* filePath) { auto sdlTexture = IMG_LoadTexture(this->manager->getGame()->renderer, filePath); if (sdlTexture == nullptr) @@ -17,14 +17,14 @@ void TextureManager::addSingleTexture(TexturesEnum texture, const char* filePath std::cout << "Loaded texture at " << filePath << std::endl; } -void TextureManager::addTextures(const std::map &textures) { +void TextureManager::addTextures(const std::map &textures) { for (auto texture : textures) { addSingleTexture(texture.first, texture.second); } } -SDL_Texture* TextureManager::loadTexture(TexturesEnum texture) { +SDL_Texture* TextureManager::loadTexture(Textures texture) { auto it = this->texture_cache.find(texture); if (it != this->texture_cache.end()) diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp index 8658e93..3664e19 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -9,7 +9,7 @@ #include "TextureEnumBase.h" -TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict) +TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict) { this->tileRect.x = x; this->tileRect.y = y; @@ -24,8 +24,7 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map< } this->collision = it->second.second; - this->tileName = it->second.first; - this->path = it->second.first.data(); + this->texture = it->second.first; } void TileComponent::init() @@ -33,7 +32,7 @@ void TileComponent::init() this->entity->addComponent(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1); this->transform = &entity->getComponent(); - this->entity->addComponent(this->path); + this->entity->addComponent(this->texture); this->sprite = &entity->getComponent(); } From ac217e931baf79f33bcc177563293775fa6f7ae0 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 16:59:46 +0100 Subject: [PATCH 4/8] 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; +} From 27a80d9766f71328244e69d46ba26c85e47eeddb Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 17:05:26 +0100 Subject: [PATCH 5/8] added new Constructor for SpriteComponent as Tiles wont work with enums --- include/SpriteComponent.h | 4 ++++ src/SpriteComponent.cpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 5cc3b53..4b9f8e2 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -35,9 +35,13 @@ private: int textureXOffset; int textureYOffset; + //should be changed in the future as this is only for the tiles + const char* path; + public: SpriteComponent(Textures texture, int zIndex); SpriteComponent(Textures texture, int xOffset, int yOffset, int zIndex); + SpriteComponent(const char* path, int xOffset, int yOffset, int zIndex); SpriteComponent( Textures texture, bool isAnimated, diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 36f6766..77ad9f1 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -25,6 +25,11 @@ SpriteComponent::SpriteComponent(Textures texture, int xOffset, int yOffset, int this->textureEnum = texture; } +SpriteComponent::SpriteComponent(const char* path, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset) { + + this->path = path; +} + SpriteComponent::SpriteComponent( Textures texture, bool isAnimated, From cbd1993c20e8be78f447151067ebd7b1eb610f6d Mon Sep 17 00:00:00 2001 From: freezarite Date: Sat, 30 Nov 2024 14:55:40 +0100 Subject: [PATCH 6/8] 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; From 7dbcfe876cdd9a084d55be0c687e0642a55a8839 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sat, 30 Nov 2024 15:35:03 +0100 Subject: [PATCH 7/8] Added some doxygen documentation to TextureEnumBase.h and TextureManager.h --- include/TextureEnumBase.h | 15 +++++++++++ include/TextureManager.h | 54 +++++++++++++++++++++++++++++++++++++++ src/TextureManager.cpp | 1 - 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/include/TextureEnumBase.h b/include/TextureEnumBase.h index 75c3573..e9e5d9a 100644 --- a/include/TextureEnumBase.h +++ b/include/TextureEnumBase.h @@ -1,3 +1,18 @@ +/*! + * \file Textures.h + * \brief Forward declaration of the \c Textures enum class. + * + * This file contains a forward declaration of the \c Textures enum class, which is used as a base + * class for texture identifiers in the engine. It allows developers to define their own set of texture + * types in their own implementation, providing flexibility in texture management within the engine. + * + * \details + * The \c Textures enum class is intended to be implemented by the developer within their own scope. + * This allows for customized texture entries to be defined based on the specific needs of the project. + * The base declaration ensures that the enum class can be referenced and used consistently throughout + * the engine while leaving the details of the texture identifiers up to the implementation. + */ + #pragma once enum class Textures; \ No newline at end of file diff --git a/include/TextureManager.h b/include/TextureManager.h index 5e5cd02..8789a2c 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -8,6 +8,22 @@ #include #include "TextureEnumBase.h" +/*! + * \class TextureManager + * \brief A manager for loading, caching, and drawing textures. + * + * The `TextureManager` class is responsible for handling texture loading, caching, + * and rendering in the engine. It provides functions to add, load, and draw textures + * from files, as well as manage sprite sheets. + * + * It uses two caches: one for regular textures (`texture_cache`) and one for + * map tile textures (`mapTile_texture_cache`). This allows for efficient texture reuse + * and management across different parts of the game. + * + * \note Textures are identified by an enum class `Textures` which is user-defined. + * Developers are expected to define the texture types in their own implementation. + */ + class TextureManager { public: @@ -21,12 +37,50 @@ class TextureManager } } + /*! + * \brief Adds a single texture to the cache. + * \param texture The texture identifier. + * \param filePath The file path to the texture file. + * + * This function loads the texture from the specified file and stores it in + * the `texture_cache`. If loading the texture fails, an exception is thrown. + */ void addSingleTexture(Textures texture, const char* filePath); + + /*! + * \brief Adds multiple textures to the cache. + * \param textures A map of texture identifiers and corresponding file paths. + * + * This function iterates over the provided map of textures and loads each + * texture using `addSingleTexture`. It allows developers to add several + * textures at once. + */ void addTextures(const std::map& textures); + + /*! + * \brief Loads a texture from the cache. + * \param texture The texture identifier. + * \return A pointer to the `SDL_Texture` if found, or `nullptr` if not found. + * + * This function looks up a texture in the `texture_cache` and returns the + * corresponding `SDL_Texture*`. If the texture is not found, it logs an error + * message and returns `nullptr`. + */ 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); + /*! + * \brief Loads a map tile texture from the file system and caches it. + * \param path The file path to the texture. + * \return A pointer to the `SDL_Texture` representing the map tile. + * + * This function checks if the map tile texture is already cached. If not, it + * loads the texture from the file system and stores it in the cache. + * + * If loading fails, an exception is thrown with a descriptive error message. + * /todo should not be usable for the developer and only be accessed by the map class + */ SDL_Texture* loadMapTileTexture(const char* path); private: diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index ff8e2fd..3b81ee3 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "GameInternal.h" From 4ead20ecb7a9fb760a928480bff6137f8aaaa89e Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 1 Dec 2024 14:39:45 +0100 Subject: [PATCH 8/8] Cleanup of documentation and refactoring renamed TextureEnumBase.h to Textures.h improved some of the doxygen documentation --- include/SpriteComponent.h | 7 ++++--- include/TextureEnumBase.h | 18 ------------------ include/TextureManager.h | 20 ++++++++------------ include/Textures.h | 14 ++++++++++++++ include/TileComponent.h | 2 +- src/AssetManager.cpp | 2 +- src/TileComponent.cpp | 2 -- 7 files changed, 28 insertions(+), 37 deletions(-) delete mode 100644 include/TextureEnumBase.h create mode 100644 include/Textures.h diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 444663d..fae052f 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -5,7 +5,7 @@ #include #include -#include "TextureEnumBase.h" +#include "Textures.h" #include "AnimationHandler.h" #include "Component.h" #include "Direction.h" @@ -35,8 +35,9 @@ private: int textureXOffset; int textureYOffset; - //should be changed in the future as this is only for the tiles - const char* path; + //might be a better solution as this variable is only used for the loading of the tmx map + //TODO: improve this in the future + const char* path; //empty string if texture has a texture enum value, otherwise the path of the texture public: SpriteComponent(Textures texture, int zIndex); diff --git a/include/TextureEnumBase.h b/include/TextureEnumBase.h deleted file mode 100644 index e9e5d9a..0000000 --- a/include/TextureEnumBase.h +++ /dev/null @@ -1,18 +0,0 @@ -/*! - * \file Textures.h - * \brief Forward declaration of the \c Textures enum class. - * - * This file contains a forward declaration of the \c Textures enum class, which is used as a base - * class for texture identifiers in the engine. It allows developers to define their own set of texture - * types in their own implementation, providing flexibility in texture management within the engine. - * - * \details - * The \c Textures enum class is intended to be implemented by the developer within their own scope. - * This allows for customized texture entries to be defined based on the specific needs of the project. - * The base declaration ensures that the enum class can be referenced and used consistently throughout - * the engine while leaving the details of the texture identifiers up to the implementation. - */ - -#pragma once - -enum class Textures; \ No newline at end of file diff --git a/include/TextureManager.h b/include/TextureManager.h index 8789a2c..24614e5 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -6,7 +6,7 @@ #include #include #include -#include "TextureEnumBase.h" +#include "Textures.h" /*! * \class TextureManager @@ -16,12 +16,8 @@ * and rendering in the engine. It provides functions to add, load, and draw textures * from files, as well as manage sprite sheets. * - * It uses two caches: one for regular textures (`texture_cache`) and one for - * map tile textures (`mapTile_texture_cache`). This allows for efficient texture reuse - * and management across different parts of the game. - * - * \note Textures are identified by an enum class `Textures` which is user-defined. - * Developers are expected to define the texture types in their own implementation. + * \sa \ref Textures "Textures" are used to identify textures within the engine. + * It is expected that they are implemented within the games scope. */ class TextureManager @@ -43,7 +39,7 @@ class TextureManager * \param filePath The file path to the texture file. * * This function loads the texture from the specified file and stores it in - * the `texture_cache`. If loading the texture fails, an exception is thrown. + * a cache. If loading the texture fails, an exception is thrown. */ void addSingleTexture(Textures texture, const char* filePath); @@ -52,8 +48,8 @@ class TextureManager * \param textures A map of texture identifiers and corresponding file paths. * * This function iterates over the provided map of textures and loads each - * texture using `addSingleTexture`. It allows developers to add several - * textures at once. + * texture using `addSingleTexture`. It allows for several + * textures to be added at once. */ void addTextures(const std::map& textures); @@ -62,7 +58,7 @@ class TextureManager * \param texture The texture identifier. * \return A pointer to the `SDL_Texture` if found, or `nullptr` if not found. * - * This function looks up a texture in the `texture_cache` and returns the + * This function looks up a texture within the cache and returns the * corresponding `SDL_Texture*`. If the texture is not found, it logs an error * message and returns `nullptr`. */ @@ -73,7 +69,7 @@ class TextureManager /*! * \brief Loads a map tile texture from the file system and caches it. * \param path The file path to the texture. - * \return A pointer to the `SDL_Texture` representing the map tile. + * \return `SDL_Texture*` representing the map tile. * * This function checks if the map tile texture is already cached. If not, it * loads the texture from the file system and stores it in the cache. diff --git a/include/Textures.h b/include/Textures.h new file mode 100644 index 0000000..eb63dfd --- /dev/null +++ b/include/Textures.h @@ -0,0 +1,14 @@ +#pragma once + +/*! + * \class Textures + * \brief Forward declaration of the \c Textures enum class. + * + * The \c Textures enum class is intended to be implemented within the game scope. + * This allows for customized texture entries to be defined based on the specific needs of the project. + * The base declaration ensures that the enum class can be referenced and used consistently throughout + * the engine while leaving the details of the texture identifiers up to the implementation. + * \sa \ref TextureManager "TextureManager" for how the enum is used. + */ + +enum class Textures; \ No newline at end of file diff --git a/include/TileComponent.h b/include/TileComponent.h index 627bb93..0cff1b8 100644 --- a/include/TileComponent.h +++ b/include/TileComponent.h @@ -6,7 +6,7 @@ #include #include "Component.h" -#include "TextureEnumBase.h" +#include "Textures.h" class SpriteComponent; class TransformComponent; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 9ac14c1..273e345 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -15,7 +15,7 @@ #include "PowerupComponent.h" #include -#include "TextureEnumBase.h" +#include "Textures.h" AssetManager::AssetManager(Manager* manager) : man(manager) {} diff --git a/src/TileComponent.cpp b/src/TileComponent.cpp index 5f36e2f..324c621 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -5,9 +5,7 @@ #include "Entity.h" #include "TransformComponent.h" #include "SpriteComponent.h" -#include "TileComponent.h" -#include "TextureEnumBase.h" TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map>* textureDict) {