Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

/home/rdbrown/doxygen/cse125g1/src/game/client/Music.cpp

00001 #include "tnl/tnlThread.h"
00002 #include "tnl/tnlRandom.h"
00003 #include "Music.h"
00004 #include "audio/audio.h"
00005 #include "resources/resources.h"
00006 
00007 using namespace TNL;
00008 using namespace MMAudio;
00009 
00010 #define SLEEP_TIME 100
00011 #define DELAY 300
00012 
00013 static char *birdfn = 0, *musicfn[2] = {0, 0};
00014 
00015 class MusicThread : public Thread {
00016   bool started;
00017   bool playing;
00018   bool music;
00019   unsigned int delay;
00020   Stream* streams[2];
00021   
00022 public:
00023   MusicThread() {
00024         started = 0;
00025         playing = 0;
00026         streams[0] = streams[1] = 0;
00027   }
00028 
00029   virtual ~MusicThread() {
00030         stop();
00031   }
00032 
00033   void stop() {
00034         if (streams[0]) {
00035           delete streams[0];
00036           streams[0] = 0;
00037         }
00038         if (streams[1]) {
00039           delete streams[1];
00040           streams[1] = 0;
00041         }
00042         playing = 0;
00043   }
00044 
00045   void playMusic(unsigned song) {
00046         if (!started) {
00047           started = true;
00048           start();
00049         }
00050         if (playing && music)
00051           return;
00052         else if (playing && !music)
00053           stop();
00054         music = true;
00055         if (!musicfn[0]) {
00056           musicfn[0] = resFindResource("sounds/music.ogg");
00057           musicfn[1] = resFindResource("sounds/tights.ogg");
00058         }
00059         streams[0] = new Stream(musicfn[song]);
00060         streams[0]->play();
00061         playing = true;
00062   }
00063   
00064   void playBirds() {
00065         if (!started) {
00066           started = true;
00067           start();
00068         }
00069         if (playing && music)
00070           stop();
00071         else if (playing && !music)
00072           return;
00073         music = false;
00074         if (!birdfn) {
00075           birdfn = resFindResource("sounds/birds1.ogg");
00076         }
00077         for (int i = 0; i < 2; i++) {
00078           streams[i] = new Stream(birdfn);
00079           streams[i]->getSource().setRolloff(.1);
00080           streams[i]->getSource().setGain(5);
00081           streams[i]->getSource().setPosition(Vector3(Random::readF()*200,
00082                                                                                                    Random::readF()*30,
00083                                                                                                    Random::readF()*200));
00084         }
00085         delay = DELAY;
00086         streams[0]->play();
00087         playing = true;
00088   }
00089 
00090   U32 run() {
00091         while (1) {
00092           if (playing) {
00093                 if (!music) {
00094                   if ((delay <= DELAY) &&
00095                           (--delay == 0)) {
00096                         streams[1]->play();
00097                   }
00098                 }
00099                 for (int i = 0; i < 2; i++) {
00100                   if (streams[i]) {
00101                         if (!streams[i]->update()) {
00102                           streams[i]->rewind();
00103                           if (!music)
00104                                 streams[i]->getSource().setPosition(Vector3(Random::readF()*200,
00105                                                                                                                          Random::readF()*30,
00106                                                                                                                          Random::readF()*200));
00107                         }
00108                   }
00109                 }
00110           }
00111           Platform::sleep(SLEEP_TIME);
00112         }
00113         return 0;
00114   }
00115 };
00116 
00117 static MusicThread musicthread;
00118 
00119 void playMusic(unsigned song) {
00120   musicthread.playMusic(song);
00121 }
00122 void playBirds() {
00123   musicthread.playBirds();
00124 }
00125 void stopMusic() {
00126   musicthread.stop();
00127 }

Generated on Thu Aug 18 16:03:08 2005 for Robin Hood: Thieves & Knights by doxygen1.2.18