mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-13 14:53:41 +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:
parent
cb01c54968
commit
db44d4d619
@ -28,3 +28,6 @@ constexpr int BUFF_DURATION = 240;
|
|||||||
|
|
||||||
constexpr int BUFF_VALUE = 1;
|
constexpr int BUFF_VALUE = 1;
|
||||||
|
|
||||||
|
constexpr int PLAY_LOOPED = -1;
|
||||||
|
constexpr int PLAY_ONCE = 0;
|
||||||
|
|
||||||
|
|||||||
@ -7,11 +7,11 @@
|
|||||||
#include "ECS.h"
|
#include "ECS.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
|
|
||||||
enum SoundTypes
|
// enum SoundTypes
|
||||||
{
|
// {
|
||||||
STEPS,
|
// STEPS,
|
||||||
THROW_EGG,
|
// THROW_EGG,
|
||||||
};
|
// };
|
||||||
|
|
||||||
class Game;
|
class Game;
|
||||||
class SoundManager
|
class SoundManager
|
||||||
@ -37,7 +37,7 @@ 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, int loops);
|
static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops);
|
||||||
// static void playMusic(...);
|
// static void playMusic(...);
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
@ -30,24 +30,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, 0);
|
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
|
||||||
}
|
}
|
||||||
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, 0);
|
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
|
||||||
}
|
}
|
||||||
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, 0);
|
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
|
||||||
}
|
}
|
||||||
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, 0);
|
SoundManager::playSound(this->entity->getManager().getGame(), "steps", false, PLAY_ONCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keystates[this->fire]) {
|
if (keystates[this->fire]) {
|
||||||
|
|||||||
@ -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, 0);
|
SoundManager::playSound(this->entity->getManager().getGame(), "throw_egg", true, PLAY_ONCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectileComponent::update()
|
void ProjectileComponent::update()
|
||||||
|
|||||||
@ -47,24 +47,26 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
|
|||||||
return sound;
|
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)
|
if (Mix_Playing(-1) != 0)
|
||||||
break;
|
return;
|
||||||
|
|
||||||
if (Mix_PlayChannel(-1, game->assets->getSound("steps"), loops) == -1) {
|
|
||||||
std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
if (Mix_PlayChannel(-1, game->assets->getSound(sound), loops) == -1)
|
||||||
|
{
|
||||||
case SoundTypes::THROW_EGG:
|
std::cerr << "Error playing sound '" << sound << "': " << Mix_GetError() << std::endl;
|
||||||
if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), loops) == -1) {
|
|
||||||
std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// functions to
|
||||||
|
// 1. free music and sound
|
||||||
|
// 2. pause/halt music and sound
|
||||||
|
// 3. restart music and sound
|
||||||
|
// 4. control volume
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user