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

updated SoundManager

- created constexpr for the options to play a sound once or loop it infinitely
- removed SoundTypes enum, changed playSound() to accept a string and a bool => allows for playSound() to be used more universally
This commit is contained in:
ineslelin 2024-05-27 11:36:02 +02:00
parent cb01c54968
commit db44d4d619
5 changed files with 33 additions and 28 deletions

View File

@ -28,3 +28,6 @@ constexpr int BUFF_DURATION = 240;
constexpr int BUFF_VALUE = 1;
constexpr int PLAY_LOOPED = -1;
constexpr int PLAY_ONCE = 0;

View File

@ -6,12 +6,12 @@
#include "ECS.h"
#include "TextureManager.h"
enum SoundTypes
{
STEPS,
THROW_EGG,
};
// enum SoundTypes
// {
// STEPS,
// THROW_EGG,
// };
class Game;
class SoundManager
@ -37,7 +37,7 @@ class SoundManager
Mix_Music* loadMusic(const char* fileName);
Mix_Chunk* loadSound(const char* fileName);
static void playSound(Game* game, SoundTypes sound, int loops);
static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops);
// static void playMusic(...);
private:
};

View File

@ -30,24 +30,24 @@ void KeyboardController::update()
if (keystates[this->up]) {
transform->direction.y = -1;
sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
}
if (keystates[this->left]) {
transform->direction.x = -1;
sprite->playAnimation(WALK);
sprite->setDirection(Direction::LEFT);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
}
if (keystates[this->down]) {
transform->direction.y = 1;
sprite->playAnimation(WALK);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
}
if (keystates[this->right]) {
transform->direction.x = 1;
sprite->playAnimation(WALK);
sprite->setDirection(Direction::RIGHT);
SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
}
if (keystates[this->fire]) {

View File

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

View File

@ -47,24 +47,26 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
return sound;
}
void SoundManager::playSound(Game* game, SoundTypes sound, int loops)
// void SoundManager::playMusic(...)
// 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)
{
switch (sound)
if(!canOverlap)
{
case SoundTypes::STEPS:
if (Mix_Playing(-1) != 0)
break;
if (Mix_Playing(-1) != 0)
return;
}
if (Mix_PlayChannel(-1, game->assets->getSound("steps"), loops) == -1) {
std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl;
}
break;
case SoundTypes::THROW_EGG:
if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), loops) == -1) {
std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl;
}
break;
if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1)
{
std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl;
}
}
// TODO:
// functions to
// 1. free music and sound
// 2. pause/halt music and sound
// 3. restart music and sound
// 4. control volume