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

fixed memory leaks

managers are no singletons anymore
This commit is contained in:
Benedikt Galbavy 2024-01-30 15:17:58 +01:00
parent 807ff4b0d0
commit 6a2e8289f6
11 changed files with 24 additions and 38 deletions

View File

@ -9,6 +9,8 @@
class AssetManager; class AssetManager;
class CollisionHandler; class CollisionHandler;
class TextureManager;
class SoundManager;
enum class TeamLabel; enum class TeamLabel;
class Game class Game
@ -31,6 +33,8 @@ public:
static SDL_Event event; static SDL_Event event;
static CollisionHandler* collisionHandler; static CollisionHandler* collisionHandler;
static AssetManager* assets; static AssetManager* assets;
static TextureManager* textureManager;
static SoundManager* soundManager;
private: private:
void setWinner(TeamLabel winningTeam); void setWinner(TeamLabel winningTeam);

View File

@ -22,7 +22,7 @@ public:
SpriteComponent* sprite; SpriteComponent* sprite;
//for attack cooldown in between shots //for attack cooldown in between shots
uint32_t lastFireTime; uint32_t lastFireTime = 0;
uint32_t fireCooldown = 800; //in ms can be adjusted to change possible attack-speed uint32_t fireCooldown = 800; //in ms can be adjusted to change possible attack-speed
KeyboardController() = default; KeyboardController() = default;

View File

@ -15,21 +15,13 @@ enum SoundTypes
class SoundManager class SoundManager
{ {
public: public:
static SoundManager& get()
{
static SoundManager instance;
return instance;
}
SoundManager() {} SoundManager() {}
private:
~SoundManager() { ~SoundManager() {
for (auto& it : this->sound_cache) { for (auto& it : this->sound_cache) {
Mix_FreeChunk(it.second); Mix_FreeChunk(it.second);
} }
} }
public:
SoundManager(SoundManager const&) = delete; SoundManager(SoundManager const&) = delete;
void operator=(SoundManager const&) = delete; void operator=(SoundManager const&) = delete;

View File

@ -25,7 +25,7 @@ private:
bool animated = false; bool animated = false;
uint8_t frames = 0; uint8_t frames = 0;
uint8_t speed = 100; uint8_t speed = 100;
bool flipped; bool flipped = false;
public: public:
SpriteComponent() = default; SpriteComponent() = default;

View File

@ -14,13 +14,6 @@ struct cmp_str
class TextureManager class TextureManager
{ {
public: public:
static TextureManager& get()
{
static TextureManager instance;
return instance;
}
private:
TextureManager() {} TextureManager() {}
~TextureManager() { ~TextureManager() {
for (auto& it : this->texture_cache) { for (auto& it : this->texture_cache) {
@ -28,14 +21,9 @@ class TextureManager
} }
} }
public:
TextureManager(TextureManager const&) = delete;
void operator=(TextureManager const&) = delete;
std::map<const char*, SDL_Texture*, cmp_str> texture_cache; std::map<const char*, SDL_Texture*, cmp_str> texture_cache;
SDL_Texture* loadTexture(const char* fileName); SDL_Texture* loadTexture(const char* fileName);
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_Texture* texture, SDL_Rect src, SDL_Rect dest); // defaults to flipped false -> legacy static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped = false);
static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped);
}; };

View File

@ -4,18 +4,19 @@
#include "TextureManager.h" #include "TextureManager.h"
#include "SoundManager.h" #include "SoundManager.h"
#include "Components.h" #include "Components.h"
#include "Game.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) { void AssetManager::addTexture(std::string id, const char* path) {
textures.emplace(id, TextureManager::get().loadTexture(path)); textures.emplace(id, Game::textureManager->loadTexture(path));
} }
void AssetManager::addSoundEffect(std::string id, const char* path) void AssetManager::addSoundEffect(std::string id, const char* path)
{ {
soundEffects.emplace(id, SoundManager::get().loadSound(path)); soundEffects.emplace(id, Game::soundManager->loadSound(path));
} }
SDL_Texture* AssetManager::getTexture(std::string id) { SDL_Texture* AssetManager::getTexture(std::string id) {

View File

@ -16,6 +16,8 @@ Map* map;
Manager manager; Manager manager;
AssetManager* Game::assets = new AssetManager(&manager); AssetManager* Game::assets = new AssetManager(&manager);
TextureManager* Game::textureManager = new TextureManager();
SoundManager* Game::soundManager = new SoundManager();
CollisionHandler* Game::collisionHandler = new CollisionHandler(manager); CollisionHandler* Game::collisionHandler = new CollisionHandler(manager);
@ -70,7 +72,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
} }
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Texture* backgroundTexture = TextureManager::get().loadTexture("assets/startscreen.png"); SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/startscreen.png");
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
@ -235,7 +237,7 @@ void Game::selectCharacters(const char* &playerSprite, const char* &enemySprite)
} }
} }
SDL_Texture* backgroundTexture = TextureManager::get().loadTexture("assets/characterSelection.png"); SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/characterSelection.png");
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
@ -320,8 +322,9 @@ void Game::render()
void Game::clean() void Game::clean()
{ {
SDL_DestroyWindow(window); delete(textureManager);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
std::cout << "Game Cleaned!" << std::endl; std::cout << "Game Cleaned!" << std::endl;
} }

View File

@ -5,7 +5,7 @@
GameObject::GameObject(const char* texturesheet, int x, int y) GameObject::GameObject(const char* texturesheet, int x, int y)
{ {
this->objTexture = TextureManager::get().loadTexture(texturesheet); this->objTexture = Game::textureManager->loadTexture(texturesheet);
this->xPos = x; this->xPos = x;
this->yPos = y; this->yPos = y;
} }

View File

@ -12,7 +12,7 @@
bool Map::loadMap(const char* path, int sizeX, int sizeY) bool Map::loadMap(const char* path, int sizeX, int sizeY)
{ {
std::string tileIDstr; std::string tileIDstr;
char singleChar; char singleChar = 0;
std::ifstream mapFile; std::ifstream mapFile;
mapFile.open(path); mapFile.open(path);

View File

@ -8,6 +8,7 @@
#include "TextureManager.h" #include "TextureManager.h"
#include "Entity.h" #include "Entity.h"
#include "TransformComponent.h" #include "TransformComponent.h"
#include "Game.h"
SpriteComponent::SpriteComponent(const char* path) SpriteComponent::SpriteComponent(const char* path)
{ {
@ -33,7 +34,7 @@ SpriteComponent::~SpriteComponent()
void SpriteComponent::setTexture(const char* path) void SpriteComponent::setTexture(const char* path)
{ {
this->texture = TextureManager::get().loadTexture(path); this->texture = Game::textureManager->loadTexture(path);
} }
void SpriteComponent::init() void SpriteComponent::init()
@ -43,6 +44,8 @@ void SpriteComponent::init()
this->srcRect.x = this->srcRect.y = 0; this->srcRect.x = this->srcRect.y = 0;
this->srcRect.w = transform->width; this->srcRect.w = transform->width;
this->srcRect.h = transform->height; this->srcRect.h = transform->height;
this->update();
} }
void SpriteComponent::update() void SpriteComponent::update()
@ -61,7 +64,7 @@ void SpriteComponent::update()
void SpriteComponent::draw() void SpriteComponent::draw()
{ {
TextureManager::get().draw(this->texture, this->srcRect, this->destRect, this->animated && this->flipped); Game::textureManager->draw(this->texture, this->srcRect, this->destRect, this->animated && this->flipped);
} }
void SpriteComponent::playAnimation(AnimationType type) void SpriteComponent::playAnimation(AnimationType type)

View File

@ -18,11 +18,6 @@ SDL_Texture* TextureManager::loadTexture(const char* fileName)
return texture; return texture;
} }
void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest)
{
draw(texture, src, dest, false);
}
void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped) void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped)
{ {
SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;