From 1493986b6b22d83cf6c1641a02202bd7b36258a1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 12 Oct 2016 08:25:59 -0400 Subject: [PATCH] worldsystem gets music --- include/events.hpp | 9 ++++--- include/world.hpp | 21 +++++++---------- main.cpp | 4 ++-- src/entities.cpp | 2 +- src/world.cpp | 58 +++++++++++++++++++--------------------------- xml/!town.xml | 4 ++-- xml/bobshouse.xml | 2 +- 7 files changed, 44 insertions(+), 56 deletions(-) diff --git a/include/events.hpp b/include/events.hpp index 4d1415c..7f70c04 100644 --- a/include/events.hpp +++ b/include/events.hpp @@ -9,6 +9,8 @@ #include +class World; + struct MouseScrollEvent { MouseScrollEvent(int sd = 0) : scrollDistance(sd) {} @@ -38,10 +40,11 @@ struct GameEndEvent { }; struct BGMToggleEvent { - BGMToggleEvent(std::string f) - : file(f) {} + BGMToggleEvent(std::string f = "", World *w = nullptr) + : file(f), world(w) {} std::string file; -} + World *world; +}; #endif // EVENTS_HPP_ diff --git a/include/world.hpp b/include/world.hpp index 3b116ed..17462ca 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -144,9 +144,11 @@ private: WorldWeather weather; Mix_Music *bgmObj; + std::string bgmObjFile; public: explicit WorldSystem(void); + ~WorldSystem(void); void configure(entityx::EventManager &ev) { ev.subscribe(*this); @@ -205,13 +207,6 @@ protected: */ WorldBGType bgType; - /** - * The filename of the world's BGM file. - * - * @see setBGM() - */ - std::string bgm; - /** * The path to the XML file of the world to the left. * @@ -294,6 +289,12 @@ protected: void drawBackgrounds(); public: + /** + * The filename of the world's BGM file. + * + * @see setBGM() + */ + std::string bgm; CoolArray particles; @@ -396,12 +397,6 @@ public: // saves the world's data to an XML file, either the one provided or the current path void save(const std::string& s=""); - // plays/pauses the world's music, according to if a new world is being entered - void bgmPlay(World *prev) const; - - // sets and loads the specified BGM - void setBGM(std::string path); - // sets the world's background theme void setBackground(WorldBGType bgt); diff --git a/main.cpp b/main.cpp index f5ed944..697452e 100644 --- a/main.cpp +++ b/main.cpp @@ -217,13 +217,13 @@ int main(int argc, char *argv[]) UserError("Plot twist: The world never existed...?"); ui::menu::init(); - currentWorld->bgmPlay(nullptr); + game::events.emit(currentWorld->bgm); // spawn the arena arena = new Arena(); arena->setStyle(""); arena->setBackground(WorldBGType::Forest); - arena->setBGM("assets/music/embark.wav"); + arena->bgm = "assets/music/embark.wav"; // the main loop, in all of its gloriousness.. std::thread([&]{ diff --git a/src/entities.cpp b/src/entities.cpp index 544b1c8..0f8c3b7 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -93,7 +93,7 @@ void PlayerSystem::receive(const KeyDownEvent &kde) p->canMove = false; ui::toggleBlackFast(); ui::waitForCover(); - wsi.first->bgmPlay(currentWorld); + game::events.emit(wsi.first->bgm, wsi.first); std::tie(currentWorld, player->loc) = wsi; // using p causes segfault ui::toggleBlackFast(); ui::waitForUncover(); diff --git a/src/world.cpp b/src/world.cpp index 8bc0306..59a23eb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -131,7 +131,6 @@ XMLDocument currentXMLDoc; World:: World(void) { - bgmObj = nullptr; worldStart = 0; lineCount = 0; } @@ -144,10 +143,6 @@ World(void) World:: ~World(void) { - // SDL2_mixer's object - if (bgmObj != nullptr) - Mix_FreeMusic(bgmObj); - deleteEntities(); } @@ -1004,10 +999,6 @@ update(Player *p, unsigned int delta) entityPending.pop_back(); } } - - // handle music fades - if (!Mix_PlayingMusic()) - Mix_FadeInMusic(bgmObj, -1, 2000); } /** @@ -1122,29 +1113,6 @@ void World::save(const std::string& s) currentXMLDoc.SaveFile((s.empty() ? currentXML : xmlFolder + s).c_str(), false); } -/** - * Toggle play/stop of the background music. - * If new music is to be played a crossfade will occur, otherwise... uhm. - */ -void World::bgmPlay(World *prev) const -{ - if (prev == nullptr || bgm != prev->bgm) { - Mix_FadeOutMusic(800); - Mix_PlayMusic(bgmObj, -1); - } -} - -/** - * Set the world's BGM. - * This will load a sound file to be played while the player is in this world. - * If no file is found, no music should play. - */ -void World::setBGM(std::string path) -{ - if (!path.empty()) - bgmObj = Mix_LoadMUS((bgm = path).c_str()); -} - /** * Sets the desired theme for the world's background. * The images chosen for the background layers are selected depending on the @@ -1926,7 +1894,7 @@ loadWorldFromXMLNoSave(std::string path) { tmp->setBackground(static_cast(bgt)); // set BGM file - tmp->setBGM(wxml->StrAttribute("bgm")); + tmp->bgm = wxml->StrAttribute("bgm"); } // world generation (for outdoor areas) @@ -2144,18 +2112,40 @@ Village::Village(std::string meme, World *w) WorldSystem::WorldSystem(void) - : weather(WorldWeather::None) {} + : weather(WorldWeather::None), bgmObj(nullptr) {} + +WorldSystem::~WorldSystem(void) +{ + // SDL2_mixer's object + if (bgmObj != nullptr) + Mix_FreeMusic(bgmObj); +} void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; (void)ev; (void)dt; + + // fade in music if not playing + if (bgmObj != nullptr && !Mix_PlayingMusic()) + Mix_FadeInMusic(bgmObj, -1, 2000); } void WorldSystem::receive(const BGMToggleEvent &bte) { + std::cout << bgmObjFile << '|' << (int)(bte.world == nullptr) << '|' << bte.file << '\n'; + + if (bte.world == nullptr || bgmObjFile != bte.file) { + Mix_FadeOutMusic(800); + + if (bgmObj != nullptr) + Mix_FreeMusic(bgmObj); + bgmObjFile = bte.file; + bgmObj = Mix_LoadMUS(bgmObjFile.c_str()); + Mix_PlayMusic(bgmObj, -1); + } } void WorldSystem::setWeather(const std::string &s) diff --git a/xml/!town.xml b/xml/!town.xml index ddddfa3..28b7d55 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -4,8 +4,8 @@ -300 - - + + diff --git a/xml/bobshouse.xml b/xml/bobshouse.xml index ebf020b..87ad9b6 100644 --- a/xml/bobshouse.xml +++ b/xml/bobshouse.xml @@ -1,6 +1,6 @@ -