diff --git a/include/SoundManager.h b/include/SoundManager.h index 6fa2d73..b75bada 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -22,14 +22,22 @@ class SoundManager for (auto& it : this->sound_cache) { Mix_FreeChunk(it.second); } + + for (auto& it : this->music_cache) { + Mix_FreeMusic(it.second); + } } SoundManager(SoundManager const&) = delete; void operator=(SoundManager const&) = delete; + std::map music_cache; std::map sound_cache; + Mix_Music* loadMusic(const char* fileName); Mix_Chunk* loadSound(const char* fileName); - static void playSound(Game* game, SoundTypes sound); + + static void playSound(Game* game, SoundTypes sound, int loops); + // static void playMusic(...); private: }; \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index df1d193..7abb899 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -30,24 +30,24 @@ void KeyboardController::update() if (keystates[this->up]) { transform->direction.y = -1; sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); } if (keystates[this->left]) { transform->direction.x = -1; sprite->playAnimation(WALK); sprite->setDirection(Direction::LEFT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); } if (keystates[this->down]) { transform->direction.y = 1; sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); } if (keystates[this->right]) { transform->direction.x = 1; sprite->playAnimation(WALK); sprite->setDirection(Direction::RIGHT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS); + SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); } if (keystates[this->fire]) { diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index 957fc66..1923fb8 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -13,7 +13,7 @@ void ProjectileComponent::init() { transformComponent = &entity->getComponent(); transformComponent->direction = direction; - SoundManager::playSound(this->entity->getManager().getGame(), THROW_EGG); + SoundManager::playSound(this->entity->getManager().getGame(), THROW_EGG, 0); } void ProjectileComponent::update() diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 2a890d7..84e3b5e 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -7,6 +7,26 @@ #include "Game.h" #include "AssetManager.h" +Mix_Music* SoundManager::loadMusic(const char* fileName) +{ + auto it = this->music_cache.find(fileName); + + if (it != this->music_cache.end()) { + return it->second; + } + + auto music = Mix_LoadMUS(fileName); + + if (music == NULL) + throw std::runtime_error(std::string("Couldn't load music '") + fileName + "'"); + + this->music_cache.emplace(fileName, music); + + printf("Loaded music at '%s'\n", fileName); + + return music; +} + Mix_Chunk* SoundManager::loadSound(const char* fileName) { auto it = this->sound_cache.find(fileName); @@ -27,7 +47,7 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName) return sound; } -void SoundManager::playSound(Game* game, SoundTypes sound) +void SoundManager::playSound(Game* game, SoundTypes sound, int loops) { switch (sound) { @@ -35,14 +55,14 @@ void SoundManager::playSound(Game* game, SoundTypes sound) if (Mix_Playing(-1) != 0) break; - if (Mix_PlayChannel(-1, game->assets->getSound("steps"), 0) == -1) { + if (Mix_PlayChannel(-1, game->assets->getSound("steps"), loops) == -1) { std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl; } break; case SoundTypes::THROW_EGG: - if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), 0) == -1) { + if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), loops) == -1) { std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl; } break;