From 494ff8aa0a84557703976e618ed3fc6e7d7f4fd6 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 16:00:47 +0100 Subject: [PATCH] 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(); }