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

Compare commits

..

1 Commits

3 changed files with 33 additions and 47 deletions

View File

@ -7,6 +7,14 @@
#include "ECS.h" #include "ECS.h"
#include "TextureManager.h" #include "TextureManager.h"
// enum SoundTypes
// {
// STEPS,
// THROW_EGG,
// };
// class Game;
class GameInternal; class GameInternal;
/*! /*!
@ -35,43 +43,28 @@ 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)
* \brief Loads music from a file (mp3) //! \returns a pointer to Mix_Music, which is added to a map in the AssetManager
* \returns a pointer to Mix_Music //! \sa AssetManager::AddMusic(std::string id, const char* path)
* \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
Mix_Music* loadMusic(const char* fileName); //! \sa AssetManager::AddSound(std::string id, const char* path)
/*!
* \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
* \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
* static void playMusic(GameInternal* game, std::string sound, int loops, int volume, int ms); //<! Plays music
* Handles if sounds can overlap, how often they can loop, as well as the volume at which the specified sound effect should play //! handles how often the track should loop, as well as the volume at which the specified track 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 sound effects (either all or on a specific channel) static void setSoundVolume(int volume, int channel); //!< Volume handling for the entire program
static void setMusicVolume(int volume); //!< Volume handling for music track static void setMusicVolume(int volume);
static void pauseSound(int channel); //!< Handles pausing sound effects (either all or on a specific channel) static void pauseSound(int channel);
static void pauseMusic(); //!< Handles pausing music track static void pauseMusic();
static void restartSound(int channel); //!< Handles resuming sound effects (either all or on a specific channel) static void restartSound(int channel);
static void restartMusic(); //!< Handles resuming music track static void restartMusic();
static void fadeOutMusic(int ms); //!< Handles fading out a music track static void fadeOutMusic(int ms);
private: 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"); assets->addTexture("egg", "assets/egg.png");
*/ */
// loading sounds // loading sounds
// assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
// assets->addSoundEffect("steps", "assets/sound/steps.wav"); assets->addSoundEffect("steps", "assets/sound/steps.wav");
// loading music // loading music
// assets->addMusic("background_music", "assets/sound/background_music.mp3"); // 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); auto music = Mix_LoadMUS(fileName);
if (music == NULL) if (music == NULL)
std::cerr << "Couldn't load music '" << fileName << "'" << std::endl; throw std::runtime_error(std::string("Couldn't load music '") + fileName + "'");
this->music_cache.emplace(fileName, music); this->music_cache.emplace(fileName, music);
std::cout << "Loaded music at " << fileName << std::endl; printf("Loaded music at '%s'\n", fileName);
return music; return music;
} }
@ -38,29 +38,22 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
auto sound = Mix_LoadWAV(fileName); auto sound = Mix_LoadWAV(fileName);
if (sound == NULL) if (sound == NULL)
std::cerr << "Couldn't load sound '" << fileName << "'" << std::endl; throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'");
this->sound_cache.emplace(fileName, sound); this->sound_cache.emplace(fileName, sound);
std::cout << "Loaded sound at " << fileName << std::endl; printf("Loaded sound at '%s'\n", fileName);
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)
{ {
// dev needs to specify a channel for this check to work, if they set it to -1 and let sdl pick the first available if (Mix_Playing(channel) != 0)
// channel mix_getchunk() won't work
if (Mix_Playing(channel) != 0 &&
Mix_GetChunk(channel) == game->assets->getSound(sound) &&
channel != -1)
{
return; return;
}
Mix_HaltChannel(channel);
} }
if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1) if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1)