diff --git a/assets/sound/steps.wav b/assets/sound/steps.wav new file mode 100644 index 0000000..a060c29 Binary files /dev/null and b/assets/sound/steps.wav differ diff --git a/assets/sound/throw_egg.wav b/assets/sound/throw_egg.wav new file mode 100644 index 0000000..67dc0e7 Binary files /dev/null and b/assets/sound/throw_egg.wav differ diff --git a/include/AssetManager.h b/include/AssetManager.h index 61b94d2..02761cc 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -1,4 +1,5 @@ #include +#include #include #include @@ -17,10 +18,15 @@ public: //texture management void addTexture(std::string id, const char* path); + // sound management + void addSoundEffect(std::string id, const char* path); + SDL_Texture* getTexture(std::string id); + Mix_Chunk* getSound(std::string id); private: Manager* man; std::map textures; + std::map soundEffects; }; 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 new file mode 100644 index 0000000..d2370da --- /dev/null +++ b/include/SoundManager.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include + +#include "TextureManager.h" + +enum SoundTypes +{ + STEPS, + THROW_EGG, +}; + +class SoundManager +{ + public: + static SoundManager& get() + { + static SoundManager instance; + return instance; + } + + SoundManager() {} + private: + ~SoundManager() { + for (auto& it : this->sound_cache) { + Mix_FreeChunk(it.second); + } + } + + public: + SoundManager(SoundManager const&) = delete; + void operator=(SoundManager const&) = delete; + + 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/AssetManager.cpp b/src/AssetManager.cpp index 766fe27..ebe5bcf 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -1,6 +1,7 @@ #include "AssetManager.h" #include "TextureManager.h" +#include "SoundManager.h" #include "Components.h" AssetManager::AssetManager(Manager* manager) : man(manager) {} @@ -11,10 +12,19 @@ void AssetManager::addTexture(std::string id, const char* path) { textures.emplace(id, TextureManager::get().loadTexture(path)); } +void AssetManager::addSoundEffect(std::string id, const char* path) +{ + soundEffects.emplace(id, SoundManager::get().loadSound(path)); +} + SDL_Texture* AssetManager::getTexture(std::string id) { return textures.at(id); } +Mix_Chunk* AssetManager::getSound(std::string id) { + return soundEffects.at(id); +} + void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) { auto& projectile(man->addEntity()); diff --git a/src/Game.cpp b/src/Game.cpp index 470a570..88d4008 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -42,6 +42,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) { @@ -63,6 +68,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderPresent(renderer); + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) + { + std::cout << "ERROR: Mixer couldnt be initialized!" << std::endl; + return; + } + + Mix_Volume(-1, MIX_MAX_VOLUME); + Mix_AllocateChannels(16); + //SDL_Event event; bool hasQuit = false; @@ -114,6 +128,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo assets->addTexture("player2", "assets/chicken_neutral.png"); assets->addTexture("egg", "assets/egg.png"); + // loading sounds + assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); + assets->addSoundEffect("steps", "assets/sound/steps.wav"); //ecs implementation diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 2880fe0..32e9f35 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -30,20 +30,24 @@ void KeyboardController::update() if (keystates[this->up]) { transform->velocity.y = -1; sprite->playAnimation(WALK); + SoundManager::playSound(STEPS); } if (keystates[this->left]) { transform->velocity.x = -1; sprite->playAnimation(WALK); sprite->setDirection(LEFT); + SoundManager::playSound(STEPS); } if (keystates[this->down]) { transform->velocity.y = 1; sprite->playAnimation(WALK); + SoundManager::playSound(STEPS); } if (keystates[this->right]) { transform->velocity.x = 1; sprite->playAnimation(WALK); sprite->setDirection(RIGHT); + SoundManager::playSound(STEPS); } if (keystates[this->fire]) { @@ -69,6 +73,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 new file mode 100644 index 0000000..590aa46 --- /dev/null +++ b/src/SoundManager.cpp @@ -0,0 +1,50 @@ +#include "SoundManager.h" + +#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 + "'"); + + this->sound_cache.emplace(fileName, sound); + + printf("Loaded sound at '%s'\n", fileName); + + return sound; +} + +void SoundManager::playSound(SoundTypes sound) +{ + switch (sound) + { + case SoundTypes::STEPS: + if (Mix_Playing(-1) != 0) + break; + + 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();