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

working volume control

This commit is contained in:
ineslelin 2024-05-27 12:40:51 +02:00
parent 70c4d3c030
commit 6373afe3a2
5 changed files with 32 additions and 12 deletions

View File

@ -31,3 +31,5 @@ constexpr int BUFF_VALUE = 1;
constexpr int PLAY_LOOPED = -1; constexpr int PLAY_LOOPED = -1;
constexpr int PLAY_ONCE = 0; constexpr int PLAY_ONCE = 0;
constexpr int MAX_VOLUME = 128;

View File

@ -37,7 +37,11 @@ class SoundManager
Mix_Music* loadMusic(const char* fileName); Mix_Music* loadMusic(const char* fileName);
Mix_Chunk* loadSound(const char* fileName); Mix_Chunk* loadSound(const char* fileName);
static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops); static void playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume);
static void playMusic(Game* game, std::string sound, int loops); static void playMusic(Game* game, std::string sound, int loops, int volume);
// set general volume
static void setVolume(int volume);
private: private:
}; };

View File

@ -22,9 +22,9 @@ 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); 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); 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); 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); 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); 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); SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE, MAX_VOLUME);
} }
void ProjectileComponent::update() void ProjectileComponent::update()

View File

@ -48,7 +48,7 @@ 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, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops) void SoundManager::playSound(Game* game, std::string sound, bool canOverlap, int loops, int volume)
{ {
if(!canOverlap) if(!canOverlap)
{ {
@ -56,26 +56,40 @@ void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound,
return; return;
} }
if(Mix_VolumeChunk(game->assets->getSound(sound), volume) == -1)
{
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
}
if (Mix_PlayChannel(-1, 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) void SoundManager::playMusic(Game* game, std::string music, int loops, int volume)
{ {
if (Mix_PlayingMusic() != 0) if (Mix_PlayingMusic() != 0)
return; return;
if(Mix_VolumeMusic(volume) == -1)
{
std::cerr << "Error adjusting volume: " << Mix_GetError() << std::endl;
}
if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1) if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1)
{ {
std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl; std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl;
} }
} }
void SoundManager::setVolume(int volume)
{
Mix_Volume(-1, volume);
}
// TODO: // TODO:
// functions to // functions to
// 1. free music and sound // 1. free music and sound
// 2. pause/halt music and sound // 2. pause/halt music and sound
// 3. restart music and sound // 3. restart music and sound
// 4. control volume