From 6a2e8289f64d67721b3e40a067814e0645a20947 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Tue, 30 Jan 2024 15:17:58 +0100 Subject: [PATCH] fixed memory leaks managers are no singletons anymore --- include/Game.h | 4 ++++ include/KeyboardController.h | 2 +- include/SoundManager.h | 10 +--------- include/SpriteComponent.h | 2 +- include/TextureManager.h | 14 +------------- src/AssetManager.cpp | 5 +++-- src/Game.cpp | 9 ++++++--- src/GameObject.cpp | 2 +- src/Map.cpp | 2 +- src/SpriteComponent.cpp | 7 +++++-- src/TextureManager.cpp | 5 ----- 11 files changed, 24 insertions(+), 38 deletions(-) diff --git a/include/Game.h b/include/Game.h index 9a23997..bfef43a 100644 --- a/include/Game.h +++ b/include/Game.h @@ -9,6 +9,8 @@ class AssetManager; class CollisionHandler; +class TextureManager; +class SoundManager; enum class TeamLabel; class Game @@ -31,6 +33,8 @@ public: static SDL_Event event; static CollisionHandler* collisionHandler; static AssetManager* assets; + static TextureManager* textureManager; + static SoundManager* soundManager; private: void setWinner(TeamLabel winningTeam); diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 38f3e08..c5bc756 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -22,7 +22,7 @@ public: SpriteComponent* sprite; //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 KeyboardController() = default; diff --git a/include/SoundManager.h b/include/SoundManager.h index 0a7d588..2b73298 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -15,21 +15,13 @@ enum SoundTypes class SoundManager { public: - static SoundManager& get() - { - static SoundManager instance; - return instance; - } - SoundManager() {} - private: ~SoundManager() { for (auto& it : this->sound_cache) { Mix_FreeChunk(it.second); } } - - public: + SoundManager(SoundManager const&) = delete; void operator=(SoundManager const&) = delete; diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index ef6786f..fc310aa 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -25,7 +25,7 @@ private: bool animated = false; uint8_t frames = 0; uint8_t speed = 100; - bool flipped; + bool flipped = false; public: SpriteComponent() = default; diff --git a/include/TextureManager.h b/include/TextureManager.h index 5721f67..a65415c 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -14,13 +14,6 @@ struct cmp_str class TextureManager { public: - static TextureManager& get() - { - static TextureManager instance; - return instance; - } - - private: TextureManager() {} ~TextureManager() { for (auto& it : this->texture_cache) { @@ -28,14 +21,9 @@ class TextureManager } } - public: - TextureManager(TextureManager const&) = delete; - void operator=(TextureManager const&) = delete; - std::map texture_cache; SDL_Texture* loadTexture(const char* fileName); static std::vector 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); + static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped = false); }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index b6988dd..b5bda9d 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -4,18 +4,19 @@ #include "TextureManager.h" #include "SoundManager.h" #include "Components.h" +#include "Game.h" AssetManager::AssetManager(Manager* manager) : man(manager) {} AssetManager::~AssetManager() {} 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) { - soundEffects.emplace(id, SoundManager::get().loadSound(path)); + soundEffects.emplace(id, Game::soundManager->loadSound(path)); } SDL_Texture* AssetManager::getTexture(std::string id) { diff --git a/src/Game.cpp b/src/Game.cpp index 0c481a4..e4ec8f7 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -16,6 +16,8 @@ Map* map; Manager manager; AssetManager* Game::assets = new AssetManager(&manager); +TextureManager* Game::textureManager = new TextureManager(); +SoundManager* Game::soundManager = new SoundManager(); 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_Texture* backgroundTexture = TextureManager::get().loadTexture("assets/startscreen.png"); + SDL_Texture* backgroundTexture = Game::textureManager->loadTexture("assets/startscreen.png"); SDL_RenderClear(renderer); 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_RenderCopy(renderer, backgroundTexture, NULL, NULL); @@ -320,8 +322,9 @@ void Game::render() void Game::clean() { - SDL_DestroyWindow(window); + delete(textureManager); SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); SDL_Quit(); std::cout << "Game Cleaned!" << std::endl; } diff --git a/src/GameObject.cpp b/src/GameObject.cpp index af866d1..fafa17b 100644 --- a/src/GameObject.cpp +++ b/src/GameObject.cpp @@ -5,7 +5,7 @@ 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->yPos = y; } diff --git a/src/Map.cpp b/src/Map.cpp index f5e0e5f..30222f5 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -12,7 +12,7 @@ bool Map::loadMap(const char* path, int sizeX, int sizeY) { std::string tileIDstr; - char singleChar; + char singleChar = 0; std::ifstream mapFile; mapFile.open(path); diff --git a/src/SpriteComponent.cpp b/src/SpriteComponent.cpp index 6673843..52c6f44 100644 --- a/src/SpriteComponent.cpp +++ b/src/SpriteComponent.cpp @@ -8,6 +8,7 @@ #include "TextureManager.h" #include "Entity.h" #include "TransformComponent.h" +#include "Game.h" SpriteComponent::SpriteComponent(const char* path) { @@ -33,7 +34,7 @@ SpriteComponent::~SpriteComponent() void SpriteComponent::setTexture(const char* path) { - this->texture = TextureManager::get().loadTexture(path); + this->texture = Game::textureManager->loadTexture(path); } void SpriteComponent::init() @@ -43,6 +44,8 @@ void SpriteComponent::init() this->srcRect.x = this->srcRect.y = 0; this->srcRect.w = transform->width; this->srcRect.h = transform->height; + + this->update(); } void SpriteComponent::update() @@ -61,7 +64,7 @@ void SpriteComponent::update() 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) diff --git a/src/TextureManager.cpp b/src/TextureManager.cpp index 13814bf..c3d0ab0 100644 --- a/src/TextureManager.cpp +++ b/src/TextureManager.cpp @@ -18,11 +18,6 @@ SDL_Texture* TextureManager::loadTexture(const char* fileName) 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) { SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;