From 68079d027960bbe84efdee3625be7a7edd9aa5bc Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 13 Oct 2024 13:29:32 +0200 Subject: [PATCH 1/9] 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/9] 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 6de979d7948c9cc053ca904f1d6839193a3c6e85 Mon Sep 17 00:00:00 2001 From: freezarite Date: Tue, 5 Nov 2024 12:45:26 +0100 Subject: [PATCH 3/9] so much happened idk what to write here :c --- .gitmodules | 3 +++ CMakeLists.txt | 2 ++ docs/doxygen-awesome-css | 1 + extern/magic_enum | 1 + include/AssetManager.h | 2 +- include/Map.h | 5 +++-- include/TileComponent.h | 22 +++++++++++++++++----- src/AssetManager.cpp | 4 ++-- src/Map.cpp | 8 +++++--- src/TileComponent.cpp | 8 ++++---- 10 files changed, 39 insertions(+), 17 deletions(-) create mode 160000 docs/doxygen-awesome-css 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/docs/doxygen-awesome-css b/docs/doxygen-awesome-css new file mode 160000 index 0000000..df88fe4 --- /dev/null +++ b/docs/doxygen-awesome-css @@ -0,0 +1 @@ +Subproject commit df88fe4fdd97714fadfd3ef17de0b4401f804052 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..ebec1bd 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -25,7 +25,7 @@ public: ~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 createPowerup(Vector2D pos, std::function pickupFunc, TexturesEnum texture); Vector2D calculateSpawnPosition(); PowerupType calculateType(); diff --git a/include/Map.h b/include/Map.h index 4c1bc3b..c18a121 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/TileComponent.h b/include/TileComponent.h index 8844daa..7bb005e 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; + TexturesEnum 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..1c7622b 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -59,13 +59,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, TexturesEnum 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..cb762bc 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/TileComponent.cpp b/src/TileComponent.cpp index 8658e93..2799dd6 100644 --- a/src/TileComponent.cpp +++ b/src/TileComponent.cpp @@ -9,7 +9,8 @@ #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 +25,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 +33,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 6bedb08d9b767dcd2f464f5b605191f52d467355 Mon Sep 17 00:00:00 2001 From: freezarite Date: Tue, 5 Nov 2024 18:38:53 +0100 Subject: [PATCH 4/9] changes to make chickengame work with updated textureManager future changes regarding the removal of the character selection needed --- .gitignore | 2 ++ src/GameInternal.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 31d3def..50144ad 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ Makefile install_manifest.txt .cache/ build/ +cmake-build-debug/ +.idea/ # Compiled files *.suo diff --git a/src/GameInternal.cpp b/src/GameInternal.cpp index 29d1622..82aa16c 100644 --- a/src/GameInternal.cpp +++ b/src/GameInternal.cpp @@ -141,6 +141,8 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he // loading music // assets->addMusic("background_music", "assets/sound/background_music.mp3"); + + this->gameInstance = GameFactory::instance().create(this); this->gameInstance->init(); } From 549178fc7ccb35bfdd4bcfbb8ec1c41ec8cb93ce Mon Sep 17 00:00:00 2001 From: freezarite Date: Tue, 5 Nov 2024 18:45:34 +0100 Subject: [PATCH 5/9] removed doxigen (idk how it got in here) --- docs/doxygen-awesome-css | 1 - 1 file changed, 1 deletion(-) delete mode 160000 docs/doxygen-awesome-css diff --git a/docs/doxygen-awesome-css b/docs/doxygen-awesome-css deleted file mode 160000 index df88fe4..0000000 --- a/docs/doxygen-awesome-css +++ /dev/null @@ -1 +0,0 @@ -Subproject commit df88fe4fdd97714fadfd3ef17de0b4401f804052 From 494ff8aa0a84557703976e618ed3fc6e7d7f4fd6 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sun, 17 Nov 2024 16:00:47 +0100 Subject: [PATCH 6/9] 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 7/9] 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 8/9] 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 b5cae00daebf33b602c8d7e96301e60f98e1eec1 Mon Sep 17 00:00:00 2001 From: freezarite Date: Sat, 30 Nov 2024 14:55:40 +0100 Subject: [PATCH 9/9] 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;