diff options
-rw-r--r-- | include/events.hpp | 9 | ||||
-rw-r--r-- | include/world.hpp | 21 | ||||
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | src/entities.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 58 | ||||
-rw-r--r-- | xml/!town.xml | 4 | ||||
-rw-r--r-- | 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 <string> +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<BGMToggleEvent>(*this); @@ -206,13 +208,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. * * @see setToLeft() @@ -294,6 +289,12 @@ protected: void drawBackgrounds(); public: + /** + * The filename of the world's BGM file. + * + * @see setBGM() + */ + std::string bgm; CoolArray<Particles> 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); @@ -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<BGMToggleEvent>(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<BGMToggleEvent>(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); } /** @@ -1123,29 +1114,6 @@ void World::save(const std::string& s) } /** - * 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 * world's background type. @@ -1926,7 +1894,7 @@ loadWorldFromXMLNoSave(std::string path) { tmp->setBackground(static_cast<WorldBGType>(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 @@ <generation type="Random" width="1600"/> <time>6000</time> <spawnx>-300</spawnx> - <npc name="Sanc" hasDialog="true" health="1" x="713.36627" y="62.598007" dindex="9999"/> - <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="102.43407" y="65.999008" dindex="0"/> + <npc name="Sanc" hasDialog="true" health="1" x="-136.30008" y="62.998978" dindex="9999"/> + <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="748" y="63.398766" dindex="0"/> <structure type="1" spawnx="300" alive="1"/> <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/> <chest alive="1"/> 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 @@ <?xml version="1.0"?> <IndoorWorld> - <style background="1" bgm="assets/music/embark.wav" folder="assets/style/classic/"/> + <style background="1" bgm="assets/music/theme_jazz.wav" folder="assets/style/classic/"/> <floor width="1600"/> <link outside="town.xml"/> <npc name="Bob" hasDialog="false" spawnx="30"/> |