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

Compare commits

..

3 Commits

3 changed files with 47 additions and 33 deletions

View File

@ -7,14 +7,6 @@
#include "ECS.h"
#include "TextureManager.h"
// enum SoundTypes
// {
// STEPS,
// THROW_EGG,
// };
// class Game;
class GameInternal;
/*!
@ -43,28 +35,43 @@ class SoundManager
std::map<const char*, Mix_Music*> music_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
//! \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)
/*!
* \brief Loads music from a file (mp3)
* \returns a pointer to Mix_Music
* \sa AssetManager::AddMusic(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
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
/*!
* \brief Handles playing of 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
* 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 setMusicVolume(int volume);
static void setSoundVolume(int volume, int channel); //!< Volume handling for sound effects (either all or on a specific channel)
static void setMusicVolume(int volume); //!< Volume handling for music track
static void pauseSound(int channel);
static void pauseMusic();
static void pauseSound(int channel); //!< Handles pausing sound effects (either all or on a specific channel)
static void pauseMusic(); //!< Handles pausing music track
static void restartSound(int channel);
static void restartMusic();
static void restartSound(int channel); //!< Handles resuming sound effects (either all or on a specific channel)
static void restartMusic(); //!< Handles resuming music track
static void fadeOutMusic(int ms);
static void fadeOutMusic(int ms); //!< Handles fading out a music track
private:
};

View File

@ -152,8 +152,8 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
assets->addTexture("egg", "assets/egg.png");
*/
// loading sounds
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
assets->addSoundEffect("steps", "assets/sound/steps.wav");
// assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
// assets->addSoundEffect("steps", "assets/sound/steps.wav");
// loading music
// assets->addMusic("background_music", "assets/sound/background_music.mp3");

View File

@ -18,11 +18,11 @@ Mix_Music* SoundManager::loadMusic(const char* fileName)
auto music = Mix_LoadMUS(fileName);
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);
printf("Loaded music at '%s'\n", fileName);
std::cout << "Loaded music at " << fileName << std::endl;
return music;
}
@ -38,22 +38,29 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
auto sound = Mix_LoadWAV(fileName);
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);
printf("Loaded sound at '%s'\n", fileName);
std::cout << "Loaded sound at " << fileName << std::endl;
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)
{
if(!canOverlap)
{
if (Mix_Playing(channel) != 0)
// dev needs to specify a channel for this check to work, if they set it to -1 and let sdl pick the first available
// channel mix_getchunk() won't work
if (Mix_Playing(channel) != 0 &&
Mix_GetChunk(channel) == game->assets->getSound(sound) &&
channel != -1)
{
return;
}
Mix_HaltChannel(channel);
}
if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1)