]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
moving music to world
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Dec 2015 21:30:08 +0000 (16:30 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 3 Dec 2015 21:30:08 +0000 (16:30 -0500)
include/world.h
main.cpp
src/gameplay.cpp
src/world.cpp

index 952c2e5de4a56eef13e92018da46b034195371bc..11918c8ff3cd53473a7932a10229d70e38647327 100644 (file)
@@ -83,6 +83,9 @@ protected:
        
        Texturec *bgTex;
        
+       Mix_Music *bgmObj;
+       char *bgm;
+       
 public:
 
        /*
@@ -134,6 +137,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.
        */
index fb07b387e2f2922439cd6199aabfa7193c281f3d..1ce60497d466c21a3928c440350102d0e6a576bf 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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;
index 958356e99b6e3df1515ad4acf6930541b1ad7c27..f54c60704587d9077262c1ae2343bb52721a096b 100644 (file)
@@ -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);
 }
 
index e5203dfc08d15c1823d5ee09d6edf6017f0b782c..ff4c4611c95306e125e7bc2ec3ff6d8347f06484 100644 (file)
@@ -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;