diff --git a/include/SoundManager.h b/include/SoundManager.h index 84d175d..acd42e5 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -14,6 +14,13 @@ // }; class Game; + +/*! + * + * \brief Handles music and sound. + * \details SoundManager handles loading in music and sound effects from files, playing music and sound effects and toggling the audio volume. + * + */ class SoundManager { public: @@ -34,14 +41,27 @@ class SoundManager std::map music_cache; std::map sound_cache; - Mix_Music* loadMusic(const char* fileName); - Mix_Chunk* loadSound(const char* fileName); + Mix_Music* loadMusic(const char* fileName); //!< Loads music from a file (mp3) + //! \returns a pointer to Mix_Music, which is added to a map in the AssetManager + //! \sa AssetManager::AddMusic(std::string id, const char* path) + Mix_Chunk* loadSound(const char* fileName); //!< Loads sound effects from a file (wav) + //! \returns a pointer to Mix_Chunk, which is added to a map in the AssetManager + //! \sa AssetManager::AddSound(std::string id, const char* path) - 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); + static void playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume); //!< Plays sound effects + //! handles if sounds can overlap, how often they can loop, as well as the volume at which the specified sound effect should play + static void playMusic(Game* game, std::string sound, int loops, int volume, int ms); //entity->getManager().getGame(), "background_music", PLAY_LOOPED, 25); + SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED, 10, 15000); transform->direction.x = 0; transform->direction.y = 0; diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index 4769912..2daa198 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -67,11 +67,17 @@ void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int } } -void SoundManager::playMusic(Game* game, std::string music, int loops, int volume) +void SoundManager::playMusic(Game* game, std::string music, int loops, int volume, int ms) { - if (Mix_PlayingMusic() != 0) + if (Mix_PlayingMusic() != 0 || Mix_Fading() == Mix_Fading::MIX_FADING_IN) return; + if(ms > 0) + { + Mix_FadeInMusic(game->assets->getMusic(music), loops, ms); + return; + } + if(Mix_VolumeMusic(volume) == -1) { std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl; @@ -88,8 +94,30 @@ 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 +void SoundManager::pauseSound() +{ + Mix_Pause(-1); +} + +void SoundManager::pauseMusic() +{ + Mix_PauseMusic(); +} + +void SoundManager::restartSound() +{ + Mix_Resume(-1); +} + +void SoundManager::restartMusic() +{ + Mix_ResumeMusic(); +} + +void SoundManager::fadeOutMusic(int ms) +{ + if(Mix_Fading() == Mix_Fading::MIX_FADING_OUT) + return; + + Mix_FadeOutMusic(ms); +}