0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 10:13: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();
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);
Vector2D calculateSpawnPosition();

View File

@ -4,10 +4,12 @@
#include <SDL_render.h>
#include <memory>
#include <string>
#include <TextureEnumBase.h>
#include "AnimationHandler.h"
#include "Component.h"
#include "Direction.h"
#include "TextureEnumBase.h"
class TransformComponent;
@ -23,7 +25,7 @@ private:
SDL_Texture* texture;
SDL_Rect srcRect, destRect;
const char* texturePath;
TexturesEnum textureEnum;
bool animated = false;
uint8_t frames = 0;
@ -32,15 +34,15 @@ private:
public:
SpriteComponent() = default;
SpriteComponent(const char* path);
SpriteComponent(TexturesEnum textureEnum);
SpriteComponent(
const char* path,
TexturesEnum textureEnum,
bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationList,
std::string defaultAnimation);
~SpriteComponent();
void setTexture(const char* path);
void setTexture(TexturesEnum texture);
void init() override;
void update() override;

View File

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

View File

@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <vector>
#include "TextureEnumBase.h"
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 void draw(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped = false);
private:
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 <iostream>
#include "TextureEnumBase.h"
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));
//textures.emplace(id, this->man->getGame()->textureManager->loadTexture(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);
}
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());
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<ColliderComponent>("projectile", 0.6f);
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_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png");
//SDL_Texture* backgroundTexture = GameInternal::textureManager->loadTexture("assets/startscreen.png");
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
//SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
SDL_RenderPresent(renderer);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)

View File

@ -12,13 +12,13 @@
#include "GameInternal.h"
#include "Manager.h"
SpriteComponent::SpriteComponent(const char* path)
SpriteComponent::SpriteComponent(TexturesEnum textureEnum)
{
this->texturePath = path;
this->textureEnum = textureEnum;
}
SpriteComponent::SpriteComponent(
const char* path,
TexturesEnum textureEnum,
bool isAnimated,
std::map<std::string, std::unique_ptr<Animation>>* animationMap,
std::string defaultAnimation)
@ -29,19 +29,19 @@ SpriteComponent::SpriteComponent(
playAnimation(defaultAnimation);
this->texturePath = path;
this->textureEnum = textureEnum;
}
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()
{
setTexture(this->texturePath);
setTexture(this->textureEnum);
this->transform = &entity->getComponent<TransformComponent>();

View File

@ -6,17 +6,32 @@
#include "GameInternal.h"
SDL_Texture* TextureManager::loadTexture(const char* fileName)
{
auto it = this->texture_cache.find(fileName);
if (it != this->texture_cache.end()) {
return it->second;
void TextureManager::addSingleTexture(TexturesEnum texture, const char* filePath) {
auto sdlTexture = IMG_LoadTexture(this->manager->getGame()->renderer, filePath);
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);
return texture;
}
SDL_Texture* TextureManager::loadTexture(TexturesEnum 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)

View File

@ -7,6 +7,8 @@
#include "SpriteComponent.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)
{
this->tileRect.x = x;