0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 12:33:43 +00:00

Compare commits

...

13 Commits

Author SHA1 Message Date
Freezarite
ff27a0e55c
Merge pull request #86 from VEGO-Engine/textureManagerChanges
Texture manager changes
2024-12-01 21:09:09 +01:00
freezarite
58be6b05f0 more documentation changes 2024-12-01 21:04:45 +01:00
freezarite
b490e2dc17 removed magic enum and documentation improvements
since getName is no longer used from the TileComponent.h we no longer need magic_enum for the enum to string conversion
also some minor documentation changes
2024-12-01 20:25:31 +01:00
freezarite
dadf846470 fixed \todo in TextureManager.h 2024-12-01 14:51:10 +01:00
freezarite
4ead20ecb7 Cleanup of documentation and refactoring
renamed TextureEnumBase.h to Textures.h
improved some of the doxygen documentation
2024-12-01 14:39:45 +01:00
freezarite
7dbcfe876c Added some doxygen documentation to TextureEnumBase.h and TextureManager.h 2024-11-30 15:43:15 +01:00
freezarite
cbd1993c20 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
2024-11-30 15:42:56 +01:00
freezarite
27a80d9766 added new Constructor for SpriteComponent as Tiles wont work with enums 2024-11-17 17:05:26 +01:00
freezarite
ac217e931b added new Map for tile-textures as they wont work with our current enum-class maps. 2024-11-17 16:59:46 +01:00
freezarite
1a8a196e95 merged dev into textureManagerChanges 2024-11-17 16:26:05 +01:00
freezarite
494ff8aa0a magic_enum library stuff and refactored TextureEnum to Textures due to bad naming 2024-11-17 16:00:47 +01:00
freezarite
65e00c2314 changed texture_cache map to use enums instead of string for keys and functions using said map 2024-10-15 13:24:56 +02:00
freezarite
68079d0279 made texture_cache map private 2024-10-13 13:29:32 +02:00
13 changed files with 185 additions and 58 deletions

1
.gitmodules vendored
View File

@ -20,3 +20,4 @@
[submodule "docs/doxygen-awesome-css"] [submodule "docs/doxygen-awesome-css"]
path = docs/doxygen-awesome-css path = docs/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css.git url = https://github.com/jothepro/doxygen-awesome-css.git

View File

@ -25,6 +25,7 @@ add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL) add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
add_library(${PROJECT_NAME} ${SOURCES}) add_library(${PROJECT_NAME} ${SOURCES})

View File

@ -24,28 +24,24 @@ public:
AssetManager(Manager* manager); AssetManager(Manager* manager);
~AssetManager(); ~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, Textures textureEnum, Entity* owner);
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath); void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture);
Vector2D calculateSpawnPosition(); Vector2D calculateSpawnPosition();
PowerupType calculateType(); PowerupType calculateType();
//texture management
void addTexture(std::string id, const char* path);
// sound management // sound management
void addSoundEffect(std::string id, const char* path); void addSoundEffect(std::string id, const char* path);
void addMusic(std::string id, const char* path); void addMusic(std::string id, const char* path);
SDL_Texture* getTexture(std::string id);
Mix_Chunk* getSound(std::string id); Mix_Chunk* getSound(std::string id);
Mix_Music* getMusic(std::string id); Mix_Music* getMusic(std::string id);
private: private:
Manager* man; Manager* man;
std::map<std::string, SDL_Texture*> textures;
std::map<std::string, Mix_Chunk*> soundEffects; std::map<std::string, Mix_Chunk*> soundEffects;
std::map<std::string, Mix_Music*> music; std::map<std::string, Mix_Music*> music;
}; };

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "Textures.h"
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Component.h" #include "Component.h"
#include "Direction.h" #include "Direction.h"
@ -24,7 +25,7 @@ private:
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
const char* texturePath; Textures textureEnum;
bool animated = false; bool animated = false;
uint8_t frames = 0; uint8_t frames = 0;
@ -34,18 +35,24 @@ private:
int textureXOffset; int textureXOffset;
int textureYOffset; int textureYOffset;
//there should be a better solution as this variable is only used for the loading of the tmx map
//TODO: improve this in the future and also remove it from the scope of the developer
const char* path; //!< empty string if texture has a texture enum value, otherwise the path of the texture
public: public:
SpriteComponent(const char* path, int zIndex); 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(const char* path, int xOffset, int yOffset, int zIndex);
SpriteComponent( SpriteComponent(
const char* path, Textures texture,
bool isAnimated, bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationList, std::map<std::string, std::unique_ptr<Animation>>* animationList,
std::string defaultAnimation, std::string defaultAnimation,
int zIndex); int zIndex);
~SpriteComponent(); ~SpriteComponent();
void setTexture(const char* path); void setTexture(Textures texture);
void setMapTileTexture(const char* path);
void init() override; void init() override;
void update() override; void update() override;

View File

@ -6,6 +6,19 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Textures.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.
*
* \sa Textures are used to identify textures within the engine.
* It is expected that they are implemented within the games scope.
*/
class TextureManager class TextureManager
{ {
@ -15,13 +28,60 @@ class TextureManager
for (auto& it : this->texture_cache) { for (auto& it : this->texture_cache) {
SDL_DestroyTexture(it.second); SDL_DestroyTexture(it.second);
} }
for (auto& it : this->mapTile_texture_cache) {
SDL_DestroyTexture(it.second);
}
} }
std::map<std::string, SDL_Texture*> texture_cache; /*!
* \brief Adds a single texture to the cache.
* \param texture The texture identifier.
* \param filePath The file path to the texture file.
* \throws std::runtime_error Is thrown if the texture could not be loaded correctly
*
* This function loads the texture from the specified file and stores it in
* a cache.
*/
void addSingleTexture(Textures texture, const char* filePath);
SDL_Texture* loadTexture(const char* fileName); /*!
* \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 for several
* textures to be added at once.
*/
void addTextures(const std::map<Textures, const char*>& 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 within the 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<SDL_Rect> splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet); static std::vector<SDL_Rect> 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); 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 `SDL_Texture*` representing the map tile.
* \throws std::runtime_error Is thrown if the texture could not be loaded correctly
*
* 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.
*
* \todo should not be usable for the developer and only be accessed by the map class
*/
SDL_Texture* loadMapTileTexture(const char* path);
private: private:
Manager* manager; Manager* manager;
std::map<Textures, SDL_Texture*> texture_cache;
std::map<std::string, SDL_Texture*> mapTile_texture_cache;
}; };

14
include/Textures.h Normal file
View File

@ -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;

View File

@ -5,6 +5,7 @@
#include <map> #include <map>
#include "Component.h" #include "Component.h"
#include "Textures.h"
class SpriteComponent; class SpriteComponent;
class TransformComponent; class TransformComponent;
@ -17,17 +18,19 @@ public:
SDL_Rect tileRect; SDL_Rect tileRect;
int tileID; int tileID;
const char* path; Textures texture;
TileComponent() = default; TileComponent() = default;
TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<std::string, bool>>* textureDict); TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<Textures, bool>>* textureDict);
~TileComponent() = default; ~TileComponent() = default;
void init() override; void init() override;
bool hasCollision(){return this->collision;} bool hasCollision() {
std::string getName(){return this->tileName;} return this->collision;
}
private: private:
bool collision; bool collision;
std::string tileName;
}; };

View File

@ -15,14 +15,12 @@
#include "PowerupComponent.h" #include "PowerupComponent.h"
#include <iostream> #include <iostream>
#include "Textures.h"
AssetManager::AssetManager(Manager* manager) : man(manager) {} AssetManager::AssetManager(Manager* manager) : man(manager) {}
AssetManager::~AssetManager() {} AssetManager::~AssetManager() {}
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) void AssetManager::addSoundEffect(std::string id, const char* path)
{ {
soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path)); soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path));
@ -33,9 +31,6 @@ void AssetManager::addMusic(std::string id, const char* path)
music.emplace(id, this->man->getGame()->soundManager->loadMusic(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) { Mix_Chunk* AssetManager::getSound(std::string id) {
return soundEffects.at(id); return soundEffects.at(id);
@ -46,23 +41,23 @@ Mix_Music* AssetManager::getMusic(std::string id)
return music.at(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, Textures textureEnum, Entity* owner) {
auto& projectile(man->addEntity()); auto& projectile(man->addEntity());
projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects projectile.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, scale); //32x32 is standard size for objects
projectile.addComponent<SpriteComponent>(texturePath, 4); projectile.addComponent<SpriteComponent>(textureEnum, 4);
projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner); projectile.addComponent<ProjectileComponent>(range, speed, velocity, owner);
projectile.addComponent<ColliderComponent>("projectile", 0.6f); projectile.addComponent<ColliderComponent>("projectile", 0.6f);
projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE); projectile.addGroup((size_t)Entity::GroupLabel::PROJECTILE);
} }
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath) { void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) {
auto& powerups(man->addEntity()); auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
try { try {
powerups.addComponent<SpriteComponent>(texturePath.c_str(), 3); powerups.addComponent<SpriteComponent>(texture, 3);
} }
catch (std::runtime_error e) { catch (std::runtime_error e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;

View File

@ -104,7 +104,7 @@ void Map::loadTileLayer(const tmx::TileLayer& layer)
tmx::Vector2i textureSize; tmx::Vector2i textureSize;
SDL_QueryTexture( SDL_QueryTexture(
VEGO_Game().textureManager->loadTexture(texturePath), VEGO_Game().textureManager->loadMapTileTexture(texturePath),
nullptr, nullptr,
nullptr, nullptr,
&(textureSize.x), &(textureSize.x),
@ -170,6 +170,7 @@ void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int
tile.addComponent<TransformComponent>(x, y, mapTileSize.x, mapTileSize.y, 1); tile.addComponent<TransformComponent>(x, y, mapTileSize.x, mapTileSize.y, 1);
tile.addComponent<SpriteComponent>(texturePath.c_str(), v, u, zIndex); // why does uv need to be reversed? tile.addComponent<SpriteComponent>(texturePath.c_str(), v, u, zIndex); // why does uv need to be reversed?
//TODO: also implement updated map stuff for this
if (hasCollision) { if (hasCollision) {
// tag currently does not have a clear purposes, TODO: figure out appropriate tag name // tag currently does not have a clear purposes, TODO: figure out appropriate tag name

View File

@ -15,18 +15,25 @@
#include "Manager.h" #include "Manager.h"
#include "VEGO.h" #include "VEGO.h"
SpriteComponent::SpriteComponent(const char* path, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(0), textureYOffset(0) SpriteComponent::SpriteComponent(Textures texture, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(0), textureYOffset(0)
{ {
this->texturePath = path; 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) SpriteComponent::SpriteComponent(Textures texture, int xOffset, int yOffset, int zIndex) : RenderObject(zIndex, VEGO_Game().renderManager), textureXOffset(xOffset), textureYOffset(yOffset)
{ {
this->texturePath = path; 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) {
this->path = path;
} }
SpriteComponent::SpriteComponent( SpriteComponent::SpriteComponent(
const char* path, Textures texture,
bool isAnimated, bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationMap, std::map<std::string, std::unique_ptr<Animation>>* animationMap,
std::string defaultAnimation, std::string defaultAnimation,
@ -38,19 +45,26 @@ SpriteComponent::SpriteComponent(
playAnimation(defaultAnimation); playAnimation(defaultAnimation);
this->texturePath = path; this->textureEnum = texture;
this->path = "";
} }
SpriteComponent::~SpriteComponent() {} SpriteComponent::~SpriteComponent() {}
void SpriteComponent::setTexture(const char* path) void SpriteComponent::setTexture(Textures texture)
{ {
this->texture = VEGO_Game().textureManager->loadTexture(path); this->texture = VEGO_Game().textureManager->loadTexture(texture);
} }
void SpriteComponent::init() void SpriteComponent::init()
{ {
setTexture(this->texturePath); if (this->path == "") {
setTexture(this->textureEnum);
}
else {
setMapTileTexture(this->path);
}
this->transform = &entity->getComponent<TransformComponent>(); this->transform = &entity->getComponent<TransformComponent>();
@ -93,3 +107,7 @@ void SpriteComponent::setDirection(Direction direction)
{ {
this->flipped = direction == Direction::RIGHT; this->flipped = direction == Direction::RIGHT;
} }
void SpriteComponent::setMapTileTexture(const char *path) {
this->texture = VEGO_Game().textureManager->loadMapTileTexture(path);
}

View File

@ -3,20 +3,36 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <VEGO.h>
#include "GameInternal.h" #include "GameInternal.h"
SDL_Texture* TextureManager::loadTexture(const char* fileName)
{ void TextureManager::addSingleTexture(Textures texture, const char* filePath) {
auto it = this->texture_cache.find(fileName); auto sdlTexture = IMG_LoadTexture(VEGO_Game().renderer, filePath);
if (it != this->texture_cache.end()) {
return it->second; 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;
} }
auto texture = IMG_LoadTexture(this->manager->getGame()->renderer, fileName);
if (texture == NULL) throw std::runtime_error(std::string("Couldn't load texture '") + fileName + "'"); void TextureManager::addTextures(const std::map<Textures, const char*> &textures) {
this->texture_cache.emplace(std::string(fileName), texture); for (auto texture : textures) {
printf("Loaded texture at '%s'\n", fileName); addSingleTexture(texture.first, texture.second);
return texture; }
}
SDL_Texture* TextureManager::loadTexture(Textures 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) void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped)
@ -24,3 +40,19 @@ void TextureManager::draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect
SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
SDL_RenderCopyEx(renderer, texture, &src, &dest, 0, NULL, flip); SDL_RenderCopyEx(renderer, texture, &src, &dest, 0, NULL, flip);
} }
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.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;
}

View File

@ -5,9 +5,9 @@
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "TileComponent.h"
TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<std::string, bool>>* textureDict)
TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map<int, std::pair<Textures, bool>>* textureDict)
{ {
this->tileRect.x = x; this->tileRect.x = x;
this->tileRect.y = y; this->tileRect.y = y;
@ -22,8 +22,7 @@ TileComponent::TileComponent(int x, int y, int w, int h, int id, const std::map<
} }
this->collision = it->second.second; this->collision = it->second.second;
this->tileName = it->second.first; this->texture = it->second.first;
this->path = it->second.first.data();
} }
void TileComponent::init() void TileComponent::init()
@ -31,7 +30,7 @@ void TileComponent::init()
this->entity->addComponent<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1); this->entity->addComponent<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1);
this->transform = &entity->getComponent<TransformComponent>(); this->transform = &entity->getComponent<TransformComponent>();
this->entity->addComponent<SpriteComponent>(this->path, 0); this->entity->addComponent<SpriteComponent>(this->texture, 0);
this->sprite = &entity->getComponent<SpriteComponent>(); this->sprite = &entity->getComponent<SpriteComponent>();
} }