diff --git a/include/AssetManager.h b/include/AssetManager.h index 61b94d2..94c0b83 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -1,4 +1,5 @@ #include +#include #include #include @@ -17,10 +18,16 @@ public: //texture management void addTexture(std::string id, const char* path); + // sound management + void addSoundEffect(std::string id, const char* path); + SDL_Texture* getTexture(std::string id); + Mix_Chunk* getSound(std::string id); + private: Manager* man; std::map textures; + std::map soundEffects; }; diff --git a/include/SoundManager.h b/include/SoundManager.h new file mode 100644 index 0000000..f8ebd18 --- /dev/null +++ b/include/SoundManager.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include + +#include "TextureManager.h" + +class SoundManager +{ + public: + static SoundManager& get() + { + static SoundManager instance; + return instance; + } + + private: + SoundManager() {} + ~SoundManager() { + for (auto& it : this->sound_cache) { + Mix_FreeChunk(it.second); + } + } + + public: + SoundManager(SoundManager const&) = delete; + void operator=(SoundManager const&) = delete; + + std::map sound_cache; + + Mix_Chunk* loadSound(const char* fileName); +}; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 766fe27..ebe5bcf 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -1,6 +1,7 @@ #include "AssetManager.h" #include "TextureManager.h" +#include "SoundManager.h" #include "Components.h" AssetManager::AssetManager(Manager* manager) : man(manager) {} @@ -11,10 +12,19 @@ void AssetManager::addTexture(std::string id, const char* path) { textures.emplace(id, TextureManager::get().loadTexture(path)); } +void AssetManager::addSoundEffect(std::string id, const char* path) +{ + soundEffects.emplace(id, SoundManager::get().loadSound(path)); +} + SDL_Texture* AssetManager::getTexture(std::string id) { return textures.at(id); } +Mix_Chunk* AssetManager::getSound(std::string id) { + return soundEffects.at(id); +} + void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) { auto& projectile(man->addEntity()); diff --git a/src/Game.cpp b/src/Game.cpp index faa6b57..802b19f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -62,6 +62,12 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderPresent(renderer); + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) + { + std::cout << "ERROR: Mixer couldnt be initialized!" << std::endl; + return; + } + //SDL_Event event; bool hasQuit = false; @@ -113,6 +119,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo assets->addTexture("player2", "assets/chicken_neutral.png"); assets->addTexture("egg", "assets/egg.png"); + // loading sounds + assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); + assets->addSoundEffect("steps", "assets/sound/steps.wav"); //ecs implementation diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp new file mode 100644 index 0000000..ad3c217 --- /dev/null +++ b/src/SoundManager.cpp @@ -0,0 +1,19 @@ +#include "SoundManager.h" + +#include +#include + +#include "Game.h" + +Mix_Chunk* SoundManager::loadSound(const char* fileName) +{ + auto it = this->sound_cache.find(fileName); + if (it != this->sound_cache.end()) { + return it->second; + } + auto sound = Mix_LoadWAV(fileName); + if (sound == NULL) throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'"); + this->sound_cache.emplace(fileName, sound); + printf("Loaded sound at '%s'\n", fileName); + return sound; +} \ No newline at end of file