diff --git a/include/Constants.h b/include/Constants.h index a09cd3c..8a89933 100644 --- a/include/Constants.h +++ b/include/Constants.h @@ -31,3 +31,5 @@ constexpr int BUFF_VALUE = 1; constexpr int PLAY_LOOPED = -1; constexpr int PLAY_ONCE = 0; +constexpr int MAX_VOLUME = 128; + diff --git a/include/SoundManager.h b/include/SoundManager.h index f6a498b..84d175d 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -37,7 +37,11 @@ class SoundManager Mix_Music* loadMusic(const char* fileName); Mix_Chunk* loadSound(const char* fileName); - static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops); - static void playMusic(Game* game, std::string sound, int loops); + static void playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume); + static void playMusic(Game* game, std::string sound, int loops, int volume); + + // set general volume + static void setVolume(int volume); + private: }; \ No newline at end of file diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 198f920..ac17d4b 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -22,9 +22,9 @@ void KeyboardController::init() } void KeyboardController::update() -{# +{ // TODO: move this, this is definitely the wrong place to put this but i wanted to put it somewhere to test it - SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED); + SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED, 25); transform->direction.x = 0; transform->direction.y = 0; @@ -33,24 +33,24 @@ void KeyboardController::update() if (keystates[this->up]) { transform->direction.y = -1; sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME); } if (keystates[this->left]) { transform->direction.x = -1; sprite->playAnimation(WALK); sprite->setDirection(Direction::LEFT); - SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME); } if (keystates[this->down]) { transform->direction.y = 1; sprite->playAnimation(WALK); - SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME); } if (keystates[this->right]) { transform->direction.x = 1; sprite->playAnimation(WALK); sprite->setDirection(Direction::RIGHT); - SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE); + SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME); } if (keystates[this->fire]) { diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index 1890f8d..af0eb13 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", true, PLAY_ONCE); + SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME); } void ProjectileComponent::update() diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 3f6f24c..4769912 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -48,7 +48,7 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName) } // 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) +void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume) { if(!canOverlap) { @@ -56,26 +56,40 @@ void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound, return; } + if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1) + { + std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl; + } + if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1) { std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl; } } -void SoundManager::playMusic(Game* game, std::string music, int loops) +void SoundManager::playMusic(Game* game, std::string music, int loops, int volume) { if (Mix_PlayingMusic() != 0) return; + if(Mix_VolumeMusic(volume) == -1) + { + std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl; + } + if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1) { std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl; } } +void SoundManager::setVolume(int volume) +{ + Mix_Volume(-1, volume); +} + // TODO: // functions to // 1. free music and sound // 2. pause/halt music and sound // 3. restart music and sound -// 4. control volume