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

updated SoundManager to incorporate requested changes

This commit is contained in:
ineslelin 2024-06-24 22:21:18 +02:00
parent 247d6cc173
commit 24e69b73a2
2 changed files with 37 additions and 31 deletions

View File

@ -7,14 +7,6 @@
#include "ECS.h" #include "ECS.h"
#include "TextureManager.h" #include "TextureManager.h"
// enum SoundTypes
// {
// STEPS,
// THROW_EGG,
// };
// class Game;
class GameInternal; class GameInternal;
/*! /*!
@ -43,28 +35,43 @@ class SoundManager
std::map<const char*, Mix_Music*> music_cache; std::map<const char*, Mix_Music*> music_cache;
std::map<const char*, Mix_Chunk*> sound_cache; std::map<const char*, Mix_Chunk*> sound_cache;
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 * \brief Loads music from a file (mp3)
//! \sa AssetManager::AddMusic(std::string id, const char* path) * \returns a pointer to Mix_Music
Mix_Chunk* loadSound(const char* fileName); //!< Loads sound effects from a file (wav) * \sa AssetManager::AddMusic(std::string id, const char* path)
//! \returns a pointer to Mix_Chunk, which is added to a map in the AssetManager */
//! \sa AssetManager::AddSound(std::string id, const char* path) Mix_Music* loadMusic(const char* fileName);
/*!
* \brief Loads sound effects from a file (wav)
* \returns a pointer to Mix_Chunk
* \sa AssetManager::AddSound(std::string id, const char* path)
*/
Mix_Chunk* loadSound(const char* fileName);
static void playSound(GameInternal* game, std::string sound, bool canOverlap, int loops, int volume, int channel); //!< 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 * \brief Handles playing of sound effects
static void playMusic(GameInternal* 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 * Handles if sounds can overlap, how often they can loop, as well as the volume at which the specified sound effect should play
* and on which channel the soundeffect should play.
*/
static void playSound(GameInternal* game, std::string sound, bool canOverlap, int loops, int volume, int channel);
/*!
* \brief Handles playing of music
*
* Handles how often track can loop, as well as the volume at which the specified track should play and if it fades in.
*/
static void playMusic(GameInternal* game, std::string sound, int loops, int volume, int ms);
static void setSoundVolume(int volume, int channel); //!< Volume handling for the entire program static void setSoundVolume(int volume, int channel); //!< Volume handling for sound effects (either all or on a specific channel)
static void setMusicVolume(int volume); static void setMusicVolume(int volume); //!< Volume handling for music track
static void pauseSound(int channel); static void pauseSound(int channel); //!< Handles pausing sound effects (either all or on a specific channel)
static void pauseMusic(); static void pauseMusic(); //!< Handles pausing music track
static void restartSound(int channel); static void restartSound(int channel); //!< Handles resuming sound effects (either all or on a specific channel)
static void restartMusic(); static void restartMusic(); //!< Handles resuming music track
static void fadeOutMusic(int ms); static void fadeOutMusic(int ms); //!< Handles fading out a music track
private: private:
}; };

View File

@ -18,11 +18,11 @@ Mix_Music* SoundManager::loadMusic(const char* fileName)
auto music = Mix_LoadMUS(fileName); auto music = Mix_LoadMUS(fileName);
if (music == NULL) if (music == NULL)
throw std::runtime_error(std::string("Couldn't load music '") + fileName + "'"); std::cerr << "Couldn't load music '" << fileName << "'" << std::endl;
this->music_cache.emplace(fileName, music); this->music_cache.emplace(fileName, music);
printf("Loaded music at '%s'\n", fileName); std::cout << "Loaded music at " << fileName << std::endl;
return music; return music;
} }
@ -38,22 +38,21 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
auto sound = Mix_LoadWAV(fileName); auto sound = Mix_LoadWAV(fileName);
if (sound == NULL) if (sound == NULL)
throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'"); std::cerr << "Couldn't load sound '" << fileName << "'" << std::endl;
this->sound_cache.emplace(fileName, sound); this->sound_cache.emplace(fileName, sound);
printf("Loaded sound at '%s'\n", fileName); std::cout << "Loaded sound at " << fileName << std::endl;
return sound; return sound;
} }
// TODO: using a string here is probably... a less than stellar method, figure out how to change this
void SoundManager::playSound(GameInternal* game, std::string sound, bool canOverlap, int loops, int volume, int channel) void SoundManager::playSound(GameInternal* game, std::string sound, bool canOverlap, int loops, int volume, int channel)
{ {
if(!canOverlap) if(!canOverlap)
{ {
if (Mix_Playing(channel) != 0) if (Mix_Playing(channel) != 0)
return; Mix_HaltChannel(channel);
} }
if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1) if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1)