diff --git a/assets/sound/background_music.mp3 b/assets/sound/background_music.mp3 new file mode 100644 index 0000000..f79cd6c Binary files /dev/null and b/assets/sound/background_music.mp3 differ diff --git a/include/AssetManager.h b/include/AssetManager.h index 4992dd2..be44516 100644 --- a/include/AssetManager.h +++ b/include/AssetManager.h @@ -36,12 +36,16 @@ public: // sound management void addSoundEffect(std::string id, const char* path); + void addMusic(std::string id, const char* path); + SDL_Texture* getTexture(std::string id); Mix_Chunk* getSound(std::string id); + Mix_Music* getMusic(std::string id); private: Manager* man; std::map textures; std::map soundEffects; + std::map music; }; diff --git a/include/SoundManager.h b/include/SoundManager.h index 24ad768..f6a498b 100644 --- a/include/SoundManager.h +++ b/include/SoundManager.h @@ -38,6 +38,6 @@ class SoundManager Mix_Chunk* loadSound(const char* fileName); static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops); - // static void playMusic(...); + static void playMusic(Game* game, std::string sound, int loops); private: }; \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 77915ae..703b095 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -29,6 +29,11 @@ void AssetManager::addSoundEffect(std::string id, const char* path) soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path)); } +void AssetManager::addMusic(std::string id, const char* path) +{ + music.emplace(id, this->man->getGame()->soundManager->loadMusic(path)); +} + SDL_Texture* AssetManager::getTexture(std::string id) { return textures.at(id); } @@ -37,6 +42,11 @@ Mix_Chunk* AssetManager::getSound(std::string id) { return soundEffects.at(id); } +Mix_Music* AssetManager::getMusic(std::string id) +{ + return music.at(id); +} + void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel) { auto& projectile(man->addEntity()); diff --git a/src/Game.cpp b/src/Game.cpp index 22bf7bf..f77a908 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -156,6 +156,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); assets->addSoundEffect("steps", "assets/sound/steps.wav"); + // loading music + assets->addMusic("background_music", "assets/sound/background_music.mp3"); + //ecs implementation player1.setTeam(Entity::TeamLabel::BLUE); diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index dd39416..198f920 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -22,7 +22,10 @@ void KeyboardController::init() } void KeyboardController::update() -{ +{# + // TODO: move this, this is definitely the wrong place to put this but i wanted to put it somewhere to test it + SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED); + transform->direction.x = 0; transform->direction.y = 0; sprite->playAnimation(IDLE); diff --git a/src/SoundManager.cpp b/src/SoundManager.cpp index fe9947e..3f6f24c 100644 --- a/src/SoundManager.cpp +++ b/src/SoundManager.cpp @@ -47,8 +47,6 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName) return sound; } -// 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) { @@ -64,6 +62,17 @@ void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound, } } +void SoundManager::playMusic(Game* game, std::string music, int loops) +{ + if (Mix_PlayingMusic() != 0) + return; + + if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1) + { + std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl; + } +} + // TODO: // functions to // 1. free music and sound