0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 12:33:43 +00:00
SDL_Minigame/include/TextureManager.h
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

86 lines
3.0 KiB
C++

#pragma once
#include "ECS.h"
#include <SDL_render.h>
#include <map>
#include <memory>
#include <string>
#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 "Textures" are used to identify textures within the engine.
* It is expected that they are implemented within the games scope.
*/
class TextureManager
{
public:
TextureManager(Manager* manager) : manager(manager) {}
~TextureManager() {
for (auto& it : this->texture_cache) {
SDL_DestroyTexture(it.second);
}
for (auto& it : this->mapTile_texture_cache) {
SDL_DestroyTexture(it.second);
}
}
/*!
* \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
* a 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 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 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.
*
* 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:
Manager* manager;
std::map<Textures, SDL_Texture*> texture_cache;
std::map<std::string, SDL_Texture*> mapTile_texture_cache;
};