MusicManager.cpp
Go to the documentation of this file.00001 //*** MusicManager.cpp *** 00002 00003 #include "MusicManager.h" 00004 #include "Debug.h" 00005 #include "Music.h" 00006 #include "Asset.h" 00007 00008 00009 //*** Constructor *** 00010 00011 MusicManager::MusicManager(): 00012 currentMusic_(0), 00013 currentMusicVolume_(1), 00014 currentMusicLoop_(true), 00015 switchMusic_(0), 00016 switchMusicVolume_(1), 00017 switchMusicLoop_(true), 00018 state_(State_NotPlaying), 00019 fadeInTime_(0), 00020 fadeOutTime_(0), 00021 crossFadeTime_(0), 00022 currentTime_(0) 00023 { 00024 } 00025 00026 00027 //*** Destructor *** 00028 00029 MusicManager::~MusicManager() 00030 { 00031 if (currentMusic_) 00032 { 00033 currentMusic_->Stop(); 00034 delete currentMusic_; 00035 currentMusic_=0; 00036 } 00037 00038 if (switchMusic_) 00039 { 00040 switchMusic_->Stop(); 00041 delete switchMusic_; 00042 switchMusic_=0; 00043 } 00044 } 00045 00046 00047 //*** Update *** 00048 00049 void MusicManager::Update(float deltaTime) 00050 { 00051 switch (state_) 00052 { 00053 case State_FadingIn: 00054 { 00055 currentTime_+=deltaTime; 00056 if (currentTime_>fadeInTime_) 00057 { 00058 currentMusic_->SetVolume(currentMusicVolume_); 00059 state_=State_Playing; 00060 return; 00061 } 00062 00063 currentMusic_->SetVolume(currentMusicVolume_*(currentTime_/fadeInTime_)); 00064 } break; 00065 00066 case State_FadingOut: 00067 { 00068 currentTime_+=deltaTime; 00069 if (currentTime_>fadeOutTime_) 00070 { 00071 StopMusic(0); 00072 return; 00073 } 00074 00075 currentMusic_->SetVolume(currentMusicVolume_*(1.0f-(currentTime_/fadeOutTime_))); 00076 } break; 00077 00078 case State_SwitchFadingOut: 00079 { 00080 currentTime_+=deltaTime; 00081 if (currentTime_>fadeOutTime_) 00082 { 00083 StopMusic(0); 00084 PlayMusic(switchMusicFilename_,switchMusicLoop_,switchMusicVolume_,fadeInTime_); 00085 return; 00086 } 00087 00088 currentMusic_->SetVolume(currentMusicVolume_*(1.0f-(currentTime_/fadeOutTime_))); 00089 } break; 00090 00091 00092 case State_CrossFading: 00093 { 00094 currentTime_+=deltaTime; 00095 if (currentTime_>crossFadeTime_) 00096 { 00097 currentMusic_->SetVolume(currentMusicVolume_); 00098 state_=State_Playing; 00099 if (switchMusic_) 00100 { 00101 switchMusic_->Stop(); 00102 delete switchMusic_; 00103 switchMusic_=0; 00104 switchMusicFilename_=Filename(); 00105 switchMusicVolume_=1; 00106 switchMusicLoop_=true; 00107 } 00108 return; 00109 } 00110 00111 currentMusic_->SetVolume(currentMusicVolume_*(currentTime_/crossFadeTime_)); 00112 switchMusic_->SetVolume(currentMusicVolume_*(1.0f-(currentTime_/crossFadeTime_))); 00113 } break; 00114 } 00115 } 00116 00117 00118 //*** PlayMusic *** 00119 00120 void MusicManager::PlayMusic(const Filename& filename, bool loop, float volume, float fadeInTime) 00121 { 00122 // If we're already playing this music, we don't need to do anything 00123 if (state_!=State_SwitchFadingOut && currentMusicFilename_==filename && currentMusicVolume_==volume && currentMusicLoop_==loop) 00124 { 00125 if (state_==State_FadingOut) 00126 { 00127 state_=State_Playing; 00128 } 00129 return; 00130 } 00131 if (state_==State_SwitchFadingOut && switchMusicFilename_==filename && switchMusicVolume_==volume && switchMusicLoop_==loop) 00132 { 00133 return; 00134 } 00135 00136 StopMusic(0); 00137 00138 // If no fade in requested, just start the music straight away 00139 if (fadeInTime==0) 00140 { 00141 state_=State_Playing; 00142 currentMusic_=new Music(filename); 00143 currentMusic_->SetVolume(volume); 00144 currentMusic_->SetLooping(loop); 00145 currentMusic_->Play(); 00146 currentMusicFilename_=filename; 00147 currentMusicVolume_=volume; 00148 currentMusicLoop_=loop; 00149 return; 00150 } 00151 00152 // Start playing new music, and set it up to fade in 00153 currentMusic_=new Music(filename); 00154 currentMusic_->SetVolume(0); 00155 currentMusic_->SetLooping(loop); 00156 currentMusic_->Play(); 00157 currentMusicFilename_=filename; 00158 currentMusicVolume_=volume; 00159 currentMusicLoop_=loop; 00160 state_=State_FadingIn; 00161 fadeInTime_=fadeInTime; 00162 currentTime_=0; 00163 } 00164 00165 00166 //*** StopMusic *** 00167 00168 void MusicManager::StopMusic(float fadeOutTime) 00169 { 00170 // If we're not playing, don't do anything 00171 if (state_==State_NotPlaying && !currentMusic_ && !switchMusic_) 00172 { 00173 return; 00174 } 00175 00176 // If no fadeout requested, just stop the music immediately 00177 if (fadeOutTime==0) 00178 { 00179 if (currentMusic_) 00180 { 00181 currentMusic_->Stop(); 00182 delete currentMusic_; 00183 currentMusic_=0; 00184 currentMusicFilename_=Filename(); 00185 currentMusicVolume_=1; 00186 currentMusicLoop_=true; 00187 } 00188 00189 if (switchMusic_) 00190 { 00191 switchMusic_->Stop(); 00192 delete switchMusic_; 00193 switchMusic_=0; 00194 switchMusicFilename_=Filename(); 00195 switchMusicVolume_=1; 00196 switchMusicLoop_=true; 00197 } 00198 state_=State_NotPlaying; 00199 return; 00200 } 00201 00202 // If fade out requested, set it up so it can be handled in Update (unless we're already fading out) 00203 if (state_!=State_FadingOut) 00204 { 00205 state_=State_FadingOut; 00206 fadeOutTime_=fadeOutTime; 00207 currentTime_=0; 00208 } 00209 } 00210 00211 00212 //*** SwitchMusic *** 00213 00214 void MusicManager::SwitchMusic(const Filename& filename, bool loop, float volume, float fadeOutTime, float fadeInTime) 00215 { 00216 // If we're already playing this music, we don't need to do anything 00217 if (state_!=State_SwitchFadingOut && currentMusicFilename_==filename && currentMusicVolume_==volume && currentMusicLoop_==loop) 00218 { 00219 return; 00220 } 00221 if (state_==State_SwitchFadingOut && switchMusicFilename_==filename && switchMusicVolume_==volume && switchMusicLoop_==loop) 00222 { 00223 return; 00224 } 00225 00226 00227 // If no fadeout time requested, or we're not currently playing, start fading in straight away 00228 if (fadeOutTime==0 || currentMusic_==0) 00229 { 00230 PlayMusic(filename,loop,volume,fadeInTime); 00231 return; 00232 } 00233 00234 // Set up fading out, so it can be done in the Update 00235 switchMusicFilename_=filename; 00236 switchMusicVolume_=volume; 00237 switchMusicLoop_=loop; 00238 state_=State_SwitchFadingOut; 00239 fadeOutTime_=fadeOutTime; 00240 fadeInTime_=fadeInTime; 00241 currentTime_=0; 00242 } 00243 00244 00245 //*** CrossFadeMusic *** 00246 00247 void MusicManager::CrossFadeMusic(const Filename& filename, bool loop, float volume, float crossFadeTime) 00248 { 00249 // If we're already playing this music, we don't need to do anything 00250 if (state_!=State_SwitchFadingOut && currentMusicFilename_==filename && currentMusicVolume_==volume && currentMusicLoop_==loop) 00251 { 00252 return; 00253 } 00254 if (state_==State_SwitchFadingOut && switchMusicFilename_==filename && switchMusicVolume_==volume && switchMusicLoop_==loop) 00255 { 00256 return; 00257 } 00258 00259 00260 // If we're not playing any music, or no crossfade time requested, we can just fade this in straight away 00261 if (currentMusic_==0 || crossFadeTime==0) 00262 { 00263 PlayMusic(filename,loop,volume,crossFadeTime); 00264 return; 00265 } 00266 00267 00268 // Start playing new music, and set it up to fade in 00269 switchMusic_=currentMusic_; 00270 switchMusicFilename_=currentMusicFilename_; 00271 switchMusicVolume_=currentMusicVolume_; 00272 switchMusicLoop_=currentMusicLoop_; 00273 currentMusic_=new Music(filename); 00274 currentMusic_->SetVolume(0); 00275 currentMusic_->SetLooping(loop); 00276 currentMusic_->Play(); 00277 currentMusicFilename_=filename; 00278 currentMusicVolume_=volume; 00279 currentMusicLoop_=loop; 00280 state_=State_CrossFading; 00281 crossFadeTime_=crossFadeTime; 00282 currentTime_=0; 00283 } 00284 00285 00286 //*** GetCurrentMusicFilename *** 00287 00288 const Filename& MusicManager::GetCurrentMusicFilename() 00289 { 00290 return currentMusicFilename_; 00291 } 00292 00293 00294 //*** GetCurrentMusic *** 00295 00296 Music* MusicManager::GetCurrentMusic() 00297 { 00298 return currentMusic_; 00299 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
