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

Merge 27a80d9766f71328244e69d46ba26c85e47eeddb into a8052b4bbbbaa72b5832bdd4e689bdeb14c41edf

This commit is contained in:
Freezarite 2024-11-25 19:04:59 +00:00 committed by GitHub
commit 98f5d65c7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 117 additions and 52 deletions

3
.gitmodules vendored
View File

@ -20,3 +20,6 @@
[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
[submodule "extern/magic_enum"]
path = extern/magic_enum
url = https://github.com/Neargye/magic_enum.git

View File

@ -24,6 +24,7 @@ add_subdirectory(extern/SDL_image EXCLUDE_FROM_ALL)
add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL) 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)
add_subdirectory(extern/magic_enum 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})
@ -36,6 +37,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL fu
SDL2_image::SDL2_image-static SDL2_image::SDL2_image-static
SDL2_mixer::SDL2_mixer-static SDL2_mixer::SDL2_mixer-static
SDL2_ttf::SDL2_ttf-static SDL2_ttf::SDL2_ttf-static
magic_enum::magic_enum
tmxlite tmxlite
) )

1
extern/magic_enum vendored Submodule

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

View File

@ -24,14 +24,12 @@ 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);

View File

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "TextureEnumBase.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,22 @@ private:
int textureXOffset; int textureXOffset;
int textureYOffset; int textureYOffset;
//should be changed in the future as this is only for the tiles
const char* path;
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 init() override; void init() override;
void update() override; void update() override;

View File

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

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "TextureEnumBase.h"
class TextureManager class TextureManager
{ {
@ -17,11 +18,16 @@ class TextureManager
} }
} }
std::map<std::string, SDL_Texture*> texture_cache; void addSingleTexture(Textures texture, const char* filePath);
void addTextures(const std::map<Textures, const char*>& textures);
SDL_Texture* loadTexture(const char* fileName); 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);
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;
}; };

View File

@ -3,8 +3,10 @@
#include <SDL.h> #include <SDL.h>
#include <string> #include <string>
#include <map> #include <map>
#include <magic_enum/magic_enum.hpp>
#include "Component.h" #include "Component.h"
#include "TextureEnumBase.h"
class SpriteComponent; class SpriteComponent;
class TransformComponent; class TransformComponent;
@ -17,17 +19,27 @@ 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;
}
std::string getName() {
#ifdef TEXTURE_ENUM_DEFINED
return std::string(magic_enum::enum_name(this->texture));
#else
return "Undefined Enum";
#endif
}
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 "TextureEnumBase.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));
@ -46,23 +44,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,23 @@
#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;
} }
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;
}
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 +43,19 @@ SpriteComponent::SpriteComponent(
playAnimation(defaultAnimation); playAnimation(defaultAnimation);
this->texturePath = path; this->textureEnum = texture;
} }
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); setTexture(this->textureEnum);
this->transform = &entity->getComponent<TransformComponent>(); this->transform = &entity->getComponent<TransformComponent>();

View File

@ -3,20 +3,37 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <VEGO.h>
#include <linux/soundcard.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 +41,16 @@ 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.at(std::string(path));
auto newTexture = IMG_LoadTexture(VEGO_Game().renderer, path);
this->mapTile_texture_cache.emplace(std::string(path), newTexture);
return newTexture;
}

View File

@ -7,7 +7,9 @@
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "TileComponent.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) #include "TextureEnumBase.h"
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 +24,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 +32,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>();
} }