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

Compare commits

..

No commits in common. "b321051ac5f8892379dcb228aa80d3e8a871f67d" and "6373afe3a2fa0a25121e8d0c71792b29462044b8" have entirely different histories.

4 changed files with 24 additions and 78 deletions

View File

@ -14,13 +14,6 @@
// }; // };
class Game; 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 class SoundManager
{ {
public: public:
@ -41,28 +34,14 @@ 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) Mix_Music* loadMusic(const char* fileName);
//! \returns a pointer to Mix_Music, which is added to a map in the AssetManager Mix_Chunk* loadSound(const char* fileName);
//! \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, int channel); //!< Plays sound effects static void playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume);
//! 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);
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
static void setSoundVolume(int volume, int channel); //!< Volume handling for the entire program // set general volume
static void setMusicVolume(int volume); static void setVolume(int volume);
static void pauseSound(int channel);
static void pauseMusic();
static void restartSound(int channel);
static void restartMusic();
static void fadeOutMusic(int ms);
private: private:
}; };

View File

@ -24,7 +24,7 @@ void KeyboardController::init()
void KeyboardController::update() 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 // 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, 10, 15000); SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED, 25);
transform->direction.x = 0; transform->direction.x = 0;
transform->direction.y = 0; transform->direction.y = 0;
@ -33,24 +33,24 @@ void KeyboardController::update()
if (keystates[this->up]) { if (keystates[this->up]) {
transform->direction.y = -1; transform->direction.y = -1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1); SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
} }
if (keystates[this->left]) { if (keystates[this->left]) {
transform->direction.x = -1; transform->direction.x = -1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
sprite->setDirection(Direction::LEFT); sprite->setDirection(Direction::LEFT);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1); SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
} }
if (keystates[this->down]) { if (keystates[this->down]) {
transform->direction.y = 1; transform->direction.y = 1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1); SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
} }
if (keystates[this->right]) { if (keystates[this->right]) {
transform->direction.x = 1; transform->direction.x = 1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
sprite->setDirection(Direction::RIGHT); sprite->setDirection(Direction::RIGHT);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1); SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
} }
if (keystates[this->fire]) { if (keystates[this->fire]) {

View File

@ -13,7 +13,7 @@ void ProjectileComponent::init()
{ {
transformComponent = &entity->getComponent<TransformComponent>(); transformComponent = &entity->getComponent<TransformComponent>();
transformComponent->direction = direction; transformComponent->direction = direction;
SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME, -1); SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME);
} }
void ProjectileComponent::update() void ProjectileComponent::update()

View File

@ -48,11 +48,11 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
} }
// TODO: using a string here is probably... a less than stellar method, figure out how to change this // TODO: using a string here is probably... a less than stellar method, figure out how to change this
void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume, int channel) void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume)
{ {
if(!canOverlap) if(!canOverlap)
{ {
if (Mix_Playing(channel) != 0) if (Mix_Playing(-1) != 0)
return; return;
} }
@ -61,23 +61,17 @@ void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl; std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
} }
if (Mix_PlayChannel(channel, game->assets->getSound(sound), loops) == -1) if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1)
{ {
std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl; std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl;
} }
} }
void SoundManager::playMusic(Game* game, std::string music, int loops, int volume, int ms) void SoundManager::playMusic(Game* game, std::string music, int loops, int volume)
{ {
if (Mix_PlayingMusic() != 0 || Mix_Fading() == Mix_Fading::MIX_FADING_IN) if (Mix_PlayingMusic() != 0)
return; return;
if(ms > 0)
{
Mix_FadeInMusic(game->assets->getMusic(music), loops, ms);
return;
}
if(Mix_VolumeMusic(volume) == -1) if(Mix_VolumeMusic(volume) == -1)
{ {
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl; std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
@ -89,40 +83,13 @@ void SoundManager::playMusic(Game* game, std::string music, int loops, int volum
} }
} }
void SoundManager::setSoundVolume(int volume, int channel) void SoundManager::setVolume(int volume)
{ {
Mix_Volume(channel, volume); Mix_Volume(-1, volume);
} }
void SoundManager::setMusicVolume(int volume) // TODO:
{ // functions to
Mix_VolumeMusic(volume); // 1. free music and sound
} // 2. pause/halt music and sound
// 3. restart music and sound
void SoundManager::pauseSound(int channel)
{
Mix_Pause(channel);
}
void SoundManager::pauseMusic()
{
Mix_PauseMusic();
}
void SoundManager::restartSound(int channel)
{
Mix_Resume(channel);
}
void SoundManager::restartMusic()
{
Mix_ResumeMusic();
}
void SoundManager::fadeOutMusic(int ms)
{
if(Mix_Fading() == Mix_Fading::MIX_FADING_OUT)
return;
Mix_FadeOutMusic(ms);
}