]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
worldsystem gets music
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 12 Oct 2016 12:25:59 +0000 (08:25 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 12 Oct 2016 12:25:59 +0000 (08:25 -0400)
include/events.hpp
include/world.hpp
main.cpp
src/entities.cpp
src/world.cpp
xml/!town.xml
xml/bobshouse.xml

index 4d1415c4af78b3970d7c47c4968f7cec5099bd09..7f70c04b04b96b5843de2203e165e227baf7bbed 100644 (file)
@@ -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_
index 3b116edcdd93fb895b62a2bb900a218ee12cba7c..17462ca79e6d49fc35f3869d3ce1648335bbac78 100644 (file)
@@ -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);
@@ -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>    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);
 
index f5ed9443d143bb1a47601ed84056714804660d89..697452e53e7b38698bf2b33b4f2d72c23cc29724 100644 (file)
--- 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<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([&]{
index 544b1c83186ad718c2c997681c36da07e8e34ce4..0f8c3b71b457b1e57b5b271d406e730832f02a9e 100644 (file)
@@ -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();
index 8bc03062a9861e5c6335b28b3c22fae1b898ad23..59a23eb1e1ba2e6a5820ee20287808e396576dd9 100644 (file)
@@ -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<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)
index ddddfa347086f25b7758b658359b61e6e3a1018d..28b7d55a4a8022777b71af77ae6d7d1feb688bc5 100644 (file)
@@ -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"/>
index ebf020bdad917a8d81fef9ab9fe12b3f8052c09d..87ad9b6fdbaefbec24087fb7b1f5a478e16ae407 100644 (file)
@@ -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"/>