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

updated SoundManager

- loading music from sound file now possible
- looping sound effects is possible now
This commit is contained in:
ineslelin 2024-05-26 19:06:52 +02:00
parent 737fd70851
commit cb01c54968
4 changed files with 37 additions and 9 deletions

View File

@ -22,14 +22,22 @@ class SoundManager
for (auto& it : this->sound_cache) { for (auto& it : this->sound_cache) {
Mix_FreeChunk(it.second); Mix_FreeChunk(it.second);
} }
for (auto& it : this->music_cache) {
Mix_FreeMusic(it.second);
}
} }
SoundManager(SoundManager const&) = delete; SoundManager(SoundManager const&) = delete;
void operator=(SoundManager const&) = delete; void operator=(SoundManager const&) = delete;
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);
Mix_Chunk* loadSound(const char* fileName); Mix_Chunk* loadSound(const char* fileName);
static void playSound(Game* game, SoundTypes sound);
static void playSound(Game* game, SoundTypes sound, int loops);
// static void playMusic(...);
private: private:
}; };

View File

@ -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); SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
} }
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); SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
} }
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); SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
} }
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); SoundManager::playSound(this->entity->getManager().getGame(), STEPS, 0);
} }
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); SoundManager::playSound(this->entity->getManager().getGame(), THROW_EGG, 0);
} }
void ProjectileComponent::update() void ProjectileComponent::update()

View File

@ -7,6 +7,26 @@
#include "Game.h" #include "Game.h"
#include "AssetManager.h" #include "AssetManager.h"
Mix_Music* SoundManager::loadMusic(const char* fileName)
{
auto it = this->music_cache.find(fileName);
if (it != this->music_cache.end()) {
return it->second;
}
auto music = Mix_LoadMUS(fileName);
if (music == NULL)
throw std::runtime_error(std::string("Couldn't load music '") + fileName + "'");
this->music_cache.emplace(fileName, music);
printf("Loaded music at '%s'\n", fileName);
return music;
}
Mix_Chunk* SoundManager::loadSound(const char* fileName) Mix_Chunk* SoundManager::loadSound(const char* fileName)
{ {
auto it = this->sound_cache.find(fileName); auto it = this->sound_cache.find(fileName);
@ -27,7 +47,7 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
return sound; return sound;
} }
void SoundManager::playSound(Game* game, SoundTypes sound) void SoundManager::playSound(Game* game, SoundTypes sound, int loops)
{ {
switch (sound) switch (sound)
{ {
@ -35,14 +55,14 @@ void SoundManager::playSound(Game* game, SoundTypes sound)
if (Mix_Playing(-1) != 0) if (Mix_Playing(-1) != 0)
break; break;
if (Mix_PlayChannel(-1, game->assets->getSound("steps"), 0) == -1) { if (Mix_PlayChannel(-1, game->assets->getSound("steps"), loops) == -1) {
std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl; std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl;
} }
break; break;
case SoundTypes::THROW_EGG: case SoundTypes::THROW_EGG:
if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), 0) == -1) { if (Mix_PlayChannel(-1, game->assets->getSound("throw_egg"), loops) == -1) {
std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl; std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl;
} }
break; break;