From 06a603b9d5354c687c0433dcada599b78500010a Mon Sep 17 00:00:00 2001 From: ineslelin Date: Mon, 29 Jan 2024 20:53:35 +0100 Subject: [PATCH] throwing projectile makes sound --- include/AssetManager.h | 1 - include/KeyboardController.h | 3 +++ include/SoundManager.h | 11 +++++++++-- src/Game.cpp | 8 ++++++++ src/KeyboardController.cpp | 1 - src/ProjectileComponent.cpp | 1 + src/SoundManager.cpp | 31 +++++++++++++++++++++++++++++-- src/TransformComponent.cpp | 2 ++ 8 files changed, 52 insertions(+), 6 deletions(-) diff --git a/include/AssetManager.h b/include/AssetManager.h index 94c0b83..02761cc 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -24,7 +24,6 @@ public: SDL_Texture* getTexture(std::string id); Mix_Chunk* getSound(std::string id); - private: Manager* man; diff --git a/include/KeyboardController.h b/include/KeyboardController.h index 1466719..38f3e08 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -3,6 +3,7 @@ #include "Component.h" #include "Vector2D.h" +#include "SoundManager.h" class TransformComponent; class SpriteComponent; @@ -35,4 +36,6 @@ private: //for creation of projectiles TransformComponent* player; //for starting position of projectile Vector2D fireVelocity; //decide source of projectile and flying direction + // SoundManager* soundEffect = Game::assets->getSound; + //SoundManager* soundEffect = new SoundManager(); }; diff --git a/include/SoundManager.h b/include/SoundManager.h index f8ebd18..d2370da 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -6,6 +6,12 @@ #include "TextureManager.h" +enum SoundTypes +{ + STEPS, + THROW_EGG, +}; + class SoundManager { public: @@ -15,8 +21,8 @@ class SoundManager return instance; } - private: SoundManager() {} + private: ~SoundManager() { for (auto& it : this->sound_cache) { Mix_FreeChunk(it.second); @@ -27,7 +33,8 @@ class SoundManager SoundManager(SoundManager const&) = delete; void operator=(SoundManager const&) = delete; - std::map sound_cache; + std::map sound_cache; Mix_Chunk* loadSound(const char* fileName); + static void playSound(SoundTypes sound); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 802b19f..60ff1a5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -41,6 +41,11 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } + if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) { + std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl; + return; + } + window = SDL_CreateWindow(title, xpos, ypos, width, height, flags); if (!window) { @@ -68,6 +73,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo return; } + Mix_Volume(-1, MIX_MAX_VOLUME); + Mix_AllocateChannels(16); + //SDL_Event event; bool hasQuit = false; diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 2880fe0..ebe7bb2 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -69,6 +69,5 @@ void KeyboardController::update() lastFireTime = currentTicks; } - } } \ No newline at end of file diff --git a/src/ProjectileComponent.cpp b/src/ProjectileComponent.cpp index ad50e39..7b40298 100644 --- a/src/ProjectileComponent.cpp +++ b/src/ProjectileComponent.cpp @@ -5,6 +5,7 @@ void ProjectileComponent::init() { transformComponent = &entity->getComponent(); + SoundManager::playSound(THROW_EGG); } void ProjectileComponent::update() diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index ad3c217..e708b2b 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -2,18 +2,45 @@ #include #include +#include #include "Game.h" +#include "AssetManager.h" Mix_Chunk* SoundManager::loadSound(const char* fileName) { auto it = this->sound_cache.find(fileName); + if (it != this->sound_cache.end()) { return it->second; } + auto sound = Mix_LoadWAV(fileName); - if (sound == NULL) throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'"); + + if (sound == NULL) + throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'"); + this->sound_cache.emplace(fileName, sound); + printf("Loaded sound at '%s'\n", fileName); + return sound; -} \ No newline at end of file +} + +void SoundManager::playSound(SoundTypes sound) +{ + switch (sound) + { + case SoundTypes::STEPS: + if (Mix_PlayChannel(-1, Game::assets->getSound("steps"), 0) == -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"), 0) == -1) { + std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl; + } + break; + } +} diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 8e02ad3..21eb6e2 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -1,6 +1,8 @@ #include "TransformComponent.h" #include "Constants.h" +#include "SoundManager.h" + TransformComponent::TransformComponent() { position.zero();