diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-12-03 16:30:08 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-12-03 16:30:08 -0500 |
commit | dcd2d1e9ed9df2b67a48acc442c9e816b677b208 (patch) | |
tree | bb9da022908f71323bde29ad8787adf097f3b725 | |
parent | 67803d1c1ef1bb96a6cdfdfecba943378e0ec5d1 (diff) |
moving music to world
-rw-r--r-- | include/world.h | 11 | ||||
-rw-r--r-- | main.cpp | 12 | ||||
-rw-r--r-- | src/gameplay.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 24 |
4 files changed, 47 insertions, 2 deletions
diff --git a/include/world.h b/include/world.h index 952c2e5..11918c8 100644 --- a/include/world.h +++ b/include/world.h @@ -83,6 +83,9 @@ protected: Texturec *bgTex; + Mix_Music *bgmObj; + char *bgm; + public: /* @@ -135,6 +138,14 @@ public: void setBackground(WORLD_BG_TYPE bgt); /* + * Start/stop background music. + */ + + void setBGM(const char *path); + void bgmPlay(void); + void bgmStop(void); + + /* * Looks for the furthest back layer in this world and adds a new layer of width `width` behind it. */ @@ -426,7 +426,7 @@ int main(/*int argc, char *argv[]*/){ horn = Mix_LoadWAV("assets/air-horn-club-sample_1.wav"); // Mix_VolumeMusic(15); // Set the volume - Mix_PlayMusic( music, -1 ); // Play music forever + //Mix_PlayMusic( music, -1 ); // Play music forever /* * Load sprites used in the inventory menu. See src/inventory.cpp @@ -482,6 +482,7 @@ void mainLoop(void){ static unsigned int prevTime = 0, // Used for timing operations currentTime = 0, // prevPrevTime= 0; // + World *prev; if(!currentTime){ // Initialize currentTime if it hasn't been currentTime=millis(); @@ -500,8 +501,15 @@ void mainLoop(void){ /* * Run the logic handler if MSEC_PER_TICK milliseconds have passed. */ - + + prev = currentWorld; ui::handleEvents(); + + if(prev != currentWorld){ + prev->bgmStop(); + currentWorld->bgmPlay(); + } + if(prevPrevTime + MSEC_PER_TICK <= currentTime){ logic(); prevPrevTime = currentTime; diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 958356e..f54c607 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -77,6 +77,7 @@ void initEverything(void){ playerSpawnHill=new World(); playerSpawnHill->setBackground(BG_FOREST); + playerSpawnHill->setBGM("assets/music/embark.wav"); /*if((load=fopen("world.dat","rb"))){ playerSpawnHill->load(load); @@ -135,6 +136,7 @@ void initEverything(void){ playerSpawnHill->npc[0]->addAIFunc(giveTestQuest,false); + currentWorld->bgmPlay(); atexit(destroyEverything); } diff --git a/src/world.cpp b/src/world.cpp index e5203df..ff4c461 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -64,6 +64,8 @@ void World::load(FILE *s){ World::World(void){ + bgm = NULL; + /* * Nullify pointers to other worlds. */ @@ -282,6 +284,28 @@ void World::update(Player *p,unsigned int delta){ } } +void World::setBGM(const char *path){ + if(!bgm) delete[] bgm; + if(!path){ + bgm = new char[strlen(path) + 1]; + strcpy(bgm,path); + bgmObj = Mix_LoadMUS(bgm); + if(!bgmObj){ + std::cout<<"Failed to load song file "<<path<<": "<<Mix_GetError()<<std::endl; + } + } +} + +void World::bgmPlay(void){ + Mix_VolumeMusic(15); + Mix_PlayMusic(bgmObj,-1); // Loop infinitely +} + +void World::bgmStop(void){ + Mix_FreeMusic(bgmObj); + bgmObj = NULL; +} + int worldShade = 0; extern vec2 offset; |