aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-10-12 08:25:59 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-10-12 08:25:59 -0400
commit1493986b6b22d83cf6c1641a02202bd7b36258a1 (patch)
tree884f5aa69f782a4fb2d3694584aaca90a93a174a /src
parentf46be773dd1283688ec1feda1c50e6edaf6d1113 (diff)
worldsystem gets music
Diffstat (limited to 'src')
-rw-r--r--src/entities.cpp2
-rw-r--r--src/world.cpp58
2 files changed, 25 insertions, 35 deletions
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)