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

magic_enum library stuff and refactored TextureEnum to Textures due to bad naming

This commit is contained in:
freezarite 2024-11-17 16:00:47 +01:00
parent 65e00c2314
commit 494ff8aa0a
14 changed files with 54 additions and 42 deletions

3
.gitmodules vendored
View File

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

View File

@ -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")

1
extern/magic_enum vendored Submodule

@ -0,0 +1 @@
Subproject commit a72a0536c716fdef4f029fb43e1fd7e7b3d9ac9b

View File

@ -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<void (Entity*)> 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<void (Entity*)> 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);

View File

@ -2,6 +2,7 @@
#include <map>
#include <string>
#include <TextureEnumBase.h>
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<int, std::pair<std::string, bool>>* textureDict /* backreference */);
static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<Textures, bool>>* textureDict /* backreference */);
static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<Textures, bool>>* textureDict);
};

View File

@ -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<std::string, std::unique_ptr<Animation>>* animationList,
std::string defaultAnimation);
~SpriteComponent();
void setTexture(TexturesEnum texture);
void setTexture(Textures texture);
void init() override;
void update() override;

View File

@ -1,3 +1,3 @@
#pragma once
enum class TexturesEnum;
enum class Textures;

View File

@ -18,12 +18,12 @@ class TextureManager
}
}
void addSingleTexture(TexturesEnum texture, const char* filePath);
void addTextures(const std::map<TexturesEnum, const char*>& textures);
SDL_Texture* loadTexture(TexturesEnum texture);
void addSingleTexture(Textures texture, const char* filePath);
void addTextures(const std::map<Textures, const char*>& textures);
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);
private:
Manager* manager;
std::map<TexturesEnum, SDL_Texture*> texture_cache;
std::map<Textures, SDL_Texture*> texture_cache;
};

View File

@ -3,8 +3,10 @@
#include <SDL.h>
#include <string>
#include <map>
#include <magic_enum/magic_enum.hpp>
#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<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;
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;
};

View File

@ -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<TransformComponent>(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<void (Entity*)> pickupFunc, std::string texturePath) {
void AssetManager::createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, Textures texture) {
auto& powerups(man->addEntity());
powerups.addComponent<TransformComponent>(pos.x, pos.y, 32, 32, 1); //32x32 is standard size for objects
try {
powerups.addComponent<SpriteComponent>(texturePath.c_str());
powerups.addComponent<SpriteComponent>(texture);
}
catch (std::runtime_error e) {
std::cout << e.what() << std::endl;

View File

@ -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<int, std::pair<std::string, bool>>* textureDict /* backreference */)
void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<Textures, bool>>* 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<int, std::pair<std::string, bool>>* textureDict) // tile entity
void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<Textures, bool>>* textureDict) // tile entity
{
auto& tile(game->manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict);
if(tile.getComponent<TileComponent>().hasCollision()) tile.addComponent<ColliderComponent>(tile.getComponent<TileComponent>().getName().data());
if(tile.getComponent<TileComponent>().hasCollision())
tile.addComponent<ColliderComponent>(tile.getComponent<TileComponent>().getName().data());
tile.addGroup((size_t)Entity::GroupLabel::MAPTILES);
}

View File

@ -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<std::string, std::unique_ptr<Animation>>* 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);
}

View File

@ -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<TexturesEnum, const char*> &textures) {
void TextureManager::addTextures(const std::map<Textures, const char*> &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())

View File

@ -9,7 +9,7 @@
#include "TextureEnumBase.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.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<TransformComponent>(this->tileRect.x, this->tileRect.y, this->tileRect.w, this->tileRect.h, 1);
this->transform = &entity->getComponent<TransformComponent>();
this->entity->addComponent<SpriteComponent>(this->path);
this->entity->addComponent<SpriteComponent>(this->texture);
this->sprite = &entity->getComponent<SpriteComponent>();
}