mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
Compare commits
2 Commits
6373afe3a2
...
b321051ac5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b321051ac5 | ||
|
|
cb1b5af358 |
@ -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,28 @@ 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, 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(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 setSoundVolume(int volume, int channel); //!< Volume handling for the entire program
|
||||
static void setMusicVolume(int volume);
|
||||
|
||||
static void pauseSound(int channel);
|
||||
static void pauseMusic();
|
||||
|
||||
static void restartSound(int channel);
|
||||
static void restartMusic();
|
||||
|
||||
static void fadeOutMusic(int ms);
|
||||
|
||||
private:
|
||||
};
|
||||
@ -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;
|
||||
@ -33,24 +33,24 @@ void KeyboardController::update()
|
||||
if (keystates[this->up]) {
|
||||
transform->direction.y = -1;
|
||||
sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
if (keystates[this->left]) {
|
||||
transform->direction.x = -1;
|
||||
sprite->playAnimation(WALK);
|
||||
sprite->setDirection(Direction::LEFT);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
if (keystates[this->down]) {
|
||||
transform->direction.y = 1;
|
||||
sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
if (keystates[this->right]) {
|
||||
transform->direction.x = 1;
|
||||
sprite->playAnimation(WALK);
|
||||
sprite->setDirection(Direction::RIGHT);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
|
||||
if (keystates[this->fire]) {
|
||||
|
||||
@ -13,7 +13,7 @@ void ProjectileComponent::init()
|
||||
{
|
||||
transformComponent = &entity->getComponent<TransformComponent>();
|
||||
transformComponent->direction = direction;
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME);
|
||||
SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME, -1);
|
||||
}
|
||||
|
||||
void ProjectileComponent::update()
|
||||
|
||||
@ -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
|
||||
void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume)
|
||||
void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume, int channel)
|
||||
{
|
||||
if(!canOverlap)
|
||||
{
|
||||
if (Mix_Playing(-1) != 0)
|
||||
if (Mix_Playing(channel) != 0)
|
||||
return;
|
||||
}
|
||||
|
||||
@ -61,17 +61,23 @@ void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int
|
||||
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
|
||||
}
|
||||
|
||||
if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1)
|
||||
if (Mix_PlayChannel(channel, game->assets->getSound(sound), loops) == -1)
|
||||
{
|
||||
std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@ -83,13 +89,40 @@ void SoundManager::playMusic(Game* game, std::string music, int loops, int volum
|
||||
}
|
||||
}
|
||||
|
||||
void SoundManager::setVolume(int volume)
|
||||
void SoundManager::setSoundVolume(int volume, int channel)
|
||||
{
|
||||
Mix_Volume(-1, volume);
|
||||
Mix_Volume(channel, volume);
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// functions to
|
||||
// 1. free music and sound
|
||||
// 2. pause/halt music and sound
|
||||
// 3. restart music and sound
|
||||
void SoundManager::setMusicVolume(int volume)
|
||||
{
|
||||
Mix_VolumeMusic(volume);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user