0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

finished sound

- implemented fade in + fade out
- implemented pause for sound + music
- implemented unpausing/restarting sound + music
This commit is contained in:
ineslelin 2024-06-16 19:20:39 +02:00
parent 6373afe3a2
commit cb1b5af358
3 changed files with 62 additions and 14 deletions

View File

@ -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<const char*, Mix_Music*> music_cache;
std::map<const char*, Mix_Chunk*> 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); //<! Plays music
//! handles how often the track should loop, as well as the volume at which the specified track should play
// set general volume
static void setVolume(int volume);
static void setVolume(int volume); //!< Volume handling for the entire program
static void pauseSound();
static void pauseMusic();
static void restartSound();
static void restartMusic();
static void fadeOutMusic(int ms);
private:
};

View File

@ -24,7 +24,7 @@ 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, 25);
SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED, 10, 15000);
transform->direction.x = 0;
transform->direction.y = 0;

View File

@ -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);
}