0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 15:53:42 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
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
7 changed files with 93 additions and 9 deletions

View File

@ -36,14 +36,12 @@ public:
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

@ -51,6 +51,7 @@ public:
~SpriteComponent(); ~SpriteComponent();
void setTexture(Textures texture); void setTexture(Textures texture);
void setMapTileTexture(const char* path);
void init() override; void init() override;
void update() override; void update() override;

View File

@ -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 #pragma once
enum class Textures; enum class Textures;

View File

@ -8,6 +8,22 @@
#include <vector> #include <vector>
#include "TextureEnumBase.h" #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 class TextureManager
{ {
public: public:
@ -16,14 +32,55 @@ 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);
}
} }
/*!
* \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); 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, const char*>& textures); 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 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); 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 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); SDL_Texture* loadMapTileTexture(const char* path);
private: private:

View File

@ -31,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);

View File

@ -3,6 +3,7 @@
#include <SDL_timer.h> #include <SDL_timer.h>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <magic_enum/magic_enum.hpp>
#include "AnimationHandler.h" #include "AnimationHandler.h"
#include "Direction.h" #include "Direction.h"
@ -18,11 +19,13 @@
SpriteComponent::SpriteComponent(Textures texture, 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->textureEnum = texture; this->textureEnum = texture;
this->path = "";
} }
SpriteComponent::SpriteComponent(Textures texture, 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->textureEnum = texture; 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(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); playAnimation(defaultAnimation);
this->textureEnum = texture; this->textureEnum = texture;
this->path = "";
} }
SpriteComponent::~SpriteComponent() {} SpriteComponent::~SpriteComponent() {}
@ -55,7 +60,12 @@ void SpriteComponent::setTexture(Textures texture)
void SpriteComponent::init() void SpriteComponent::init()
{ {
if (this->path == "") {
setTexture(this->textureEnum); setTexture(this->textureEnum);
}
else {
setMapTileTexture(this->path);
}
this->transform = &entity->getComponent<TransformComponent>(); this->transform = &entity->getComponent<TransformComponent>();
@ -98,3 +108,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

@ -4,7 +4,6 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <VEGO.h> #include <VEGO.h>
#include <linux/soundcard.h>
#include "GameInternal.h" #include "GameInternal.h"
@ -46,10 +45,13 @@ SDL_Texture* TextureManager::loadMapTileTexture(const char *path) {
//returns tile if it exists already //returns tile if it exists already
if(mapTile_texture_cache.contains(std::string(path))) 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); 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); this->mapTile_texture_cache.emplace(std::string(path), newTexture);
return newTexture; return newTexture;