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

changed texture_cache map to use enums instead of string for keys and functions using said map

This commit is contained in:
freezarite 2024-10-15 13:24:56 +02:00
parent 68079d0279
commit 65e00c2314
9 changed files with 58 additions and 29 deletions

View File

@ -24,7 +24,7 @@ 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, TexturesEnum textureEnum, Entity* owner);
void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath); void createPowerup(Vector2D pos, std::function<void (Entity*)> pickupFunc, std::string texturePath);
Vector2D calculateSpawnPosition(); Vector2D calculateSpawnPosition();

View File

@ -4,10 +4,12 @@
#include <SDL_render.h> #include <SDL_render.h>
#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"
#include "TextureEnumBase.h"
class TransformComponent; class TransformComponent;
@ -23,7 +25,7 @@ private:
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect srcRect, destRect; SDL_Rect srcRect, destRect;
const char* texturePath; TexturesEnum textureEnum;
bool animated = false; bool animated = false;
uint8_t frames = 0; uint8_t frames = 0;
@ -32,15 +34,15 @@ private:
public: public:
SpriteComponent() = default; SpriteComponent() = default;
SpriteComponent(const char* path); SpriteComponent(TexturesEnum textureEnum);
SpriteComponent( SpriteComponent(
const char* path, TexturesEnum textureEnum,
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);
~SpriteComponent(); ~SpriteComponent();
void setTexture(const char* path); void setTexture(TexturesEnum texture);
void init() override; void init() override;
void update() override; void update() override;

View File

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

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

View File

@ -15,12 +15,15 @@
#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() {}
//seems to not be used anymore
void AssetManager::addTexture(std::string id, const char* path) { void AssetManager::addTexture(std::string id, const char* path) {
textures.emplace(id, this->man->getGame()->textureManager->loadTexture(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)
@ -46,11 +49,11 @@ 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, TexturesEnum 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); projectile.addComponent<SpriteComponent>(textureEnum);
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);

View File

@ -78,10 +78,11 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
} }
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png");
//SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png");
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); //SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)

View File

@ -12,13 +12,13 @@
#include "GameInternal.h" #include "GameInternal.h"
#include "Manager.h" #include "Manager.h"
SpriteComponent::SpriteComponent(const char* path) SpriteComponent::SpriteComponent(TexturesEnum textureEnum)
{ {
this->texturePath = path; this->textureEnum = textureEnum;
} }
SpriteComponent::SpriteComponent( SpriteComponent::SpriteComponent(
const char* path, TexturesEnum textureEnum,
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)
@ -29,19 +29,19 @@ SpriteComponent::SpriteComponent(
playAnimation(defaultAnimation); playAnimation(defaultAnimation);
this->texturePath = path; this->textureEnum = textureEnum;
} }
SpriteComponent::~SpriteComponent() {} SpriteComponent::~SpriteComponent() {}
void SpriteComponent::setTexture(const char* path) void SpriteComponent::setTexture(TexturesEnum texture)
{ {
this->texture = this->entity->getManager().getGame()->textureManager->loadTexture(path); this->texture = this->entity->getManager().getGame()->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

@ -6,17 +6,32 @@
#include "GameInternal.h" #include "GameInternal.h"
SDL_Texture* TextureManager::loadTexture(const char* fileName)
{ void TextureManager::addSingleTexture(TexturesEnum texture, const char* filePath) {
auto it = this->texture_cache.find(fileName); auto sdlTexture = IMG_LoadTexture(this->manager->getGame()->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;
}
void TextureManager::addTextures(const std::map<TexturesEnum, const char*> &textures) {
for (auto texture : textures) {
addSingleTexture(texture.first, texture.second);
} }
auto texture = IMG_LoadTexture(this->manager->getGame()->renderer, fileName); }
if (texture == NULL) throw std::runtime_error(std::string("Couldn't load texture '") + fileName + "'");
this->texture_cache.emplace(std::string(fileName), texture);
printf("Loaded texture at '%s'\n", fileName); SDL_Texture* TextureManager::loadTexture(TexturesEnum texture) {
return 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)

View File

@ -7,6 +7,8 @@
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "TileComponent.h" #include "TileComponent.h"
#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<std::string, bool>>* textureDict)
{ {
this->tileRect.x = x; this->tileRect.x = x;