From db44d4d619a4219a553a911ef30e371249dd1e24 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Mon, 27 May 2024 11:36:02 +0200 Subject: [PATCH] updated SoundManager - created constexpr for the options to play a sound once or loop it infinitely - removed SoundTypes enum, changed playSound() to accept a string and a bool => allows for playSound() to be used more universally --- include/Constants.h | 3 +++ include/SoundManager.h | 14 +++++++------- src/KeyboardController.cpp | 8 ++++---- src/ProjectileComponent.cpp | 2 +- src/SoundManager.cpp | 34 ++++++++++++++++++---------------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/include/Constants.h b/include/Constants.h index 20ccf44..a09cd3c 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -28,3 +28,6 @@ constexpr int BUFF_DURATION = 240; constexpr int BUFF_VALUE = 1; +constexpr int PLAY_LOOPED = -1; +constexpr int PLAY_ONCE = 0; + diff --git a/include/SoundManager.h b/include/SoundManager.h index b75bada..24ad768 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -6,12 +6,12 @@ #include "ECS.h" #include "TextureManager.h" - -enum SoundTypes -{ - STEPS, - THROW_EGG, -}; + +// enum SoundTypes +// { +// STEPS, +// THROW_EGG, +// }; class Game; class SoundManager @@ -37,7 +37,7 @@ class SoundManager Mix_Music* loadMusic(const char* fileName); Mix_Chunk* loadSound(const char* fileName); - static void playSound(Game* game, SoundTypes sound, int loops); + static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops); // static void playMusic(...); private: }; \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 7abb899..dd39416 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, 0); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); } if (keystates[this->left]) { transform->direction.x = -1; sprite->playAnimation(WALK); sprite->setDirection(Direction::LEFT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); } if (keystates[this->down]) { transform->direction.y = 1; sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); } if (keystates[this->right]) { transform->direction.x = 1; sprite->playAnimation(WALK); sprite->setDirection(Direction::RIGHT); - SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); } if (keystates[this->fire]) { diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index 1923fb8..1890f8d 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, 0); + SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE); } void ProjectileComponent::update() diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 84e3b5e..fe9947e 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -47,24 +47,26 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName) return sound; } -void SoundManager::playSound(Game* game, SoundTypes sound, int loops) +// void SoundManager::playMusic(...) + +// TODO: using a string here is probably... a less than stellar method, figure out how to change this +void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops) { - switch (sound) + if(!canOverlap) { - case SoundTypes::STEPS: - if (Mix_Playing(-1) != 0) - break; + if (Mix_Playing(-1) != 0) + return; + } - 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"), loops) == -1) { - std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl; - } - break; + if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1) + { + std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl; } } + +// TODO: +// functions to +// 1. free music and sound +// 2. pause/halt music and sound +// 3. restart music and sound +// 4. control volume