Code for changing music in pause mode

As requested by @Emxx52. Only code for now, we don't have the music yet. Temporairly in developements builds music will change to Prototype (in CBot editor) and Constructive Destruction (in SatCom)
dev-ui
krzys-h 2013-12-31 16:58:21 +01:00
parent 4a237f5925
commit 999490e88b
9 changed files with 153 additions and 21 deletions

View File

@ -15,6 +15,7 @@
// * You should have received a copy of the GNU General Public License // * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/. // * along with this program. If not, see http://www.gnu.org/licenses/.
#include "app/app.h"
#include "app/pausemanager.h" #include "app/pausemanager.h"
#include "common/logger.h" #include "common/logger.h"
@ -25,18 +26,43 @@ template<> CPauseManager* CSingleton<CPauseManager>::m_instance = nullptr;
CPauseManager::CPauseManager() CPauseManager::CPauseManager()
{ {
m_sound = CApplication::GetInstancePointer()->GetSound();
m_pause = PAUSE_NONE; m_pause = PAUSE_NONE;
} }
CPauseManager::~CPauseManager() CPauseManager::~CPauseManager()
{ {
m_sound = nullptr;
} }
void CPauseManager::SetPause(PauseType pause) void CPauseManager::SetPause(PauseType pause)
{ {
if(pause != PAUSE_NONE) { if(pause != PAUSE_NONE) {
if(m_pause != pause) if(m_pause != pause) {
CLogger::GetInstancePointer()->Info("Game paused - %s\n", GetPauseName(pause).c_str()); CLogger::GetInstancePointer()->Info("Game paused - %s\n", GetPauseName(pause).c_str());
switch(pause) {
case PAUSE_EDITOR:
// TODO: We don't have this music yet
// m_sound->PlayPauseMusic("");
#if DEV_BUILD
m_sound->PlayPauseMusic("Prototype.ogg");
#endif
break;
case PAUSE_SATCOM:
// TODO: We don't have this music yet
// m_sound->PlayPauseMusic("");
#if DEV_BUILD
m_sound->PlayPauseMusic("Constructive.ogg");
#endif
break;
default:
// Don't change music
break;
}
}
m_pause = pause; m_pause = pause;
} else } else
@ -45,8 +71,10 @@ void CPauseManager::SetPause(PauseType pause)
void CPauseManager::ClearPause() void CPauseManager::ClearPause()
{ {
if(m_pause != PAUSE_NONE) if(m_pause != PAUSE_NONE) {
CLogger::GetInstancePointer()->Info("Game resumed\n"); CLogger::GetInstancePointer()->Info("Game resumed\n");
m_sound->StopPauseMusic();
}
m_pause = PAUSE_NONE; m_pause = PAUSE_NONE;
} }
@ -70,14 +98,15 @@ std::string CPauseManager::GetPauseName(PauseType pause)
{ {
switch(pause) switch(pause)
{ {
case PAUSE_NONE: return "None"; case PAUSE_NONE: return "None";
case PAUSE_USER: return "User"; case PAUSE_USER: return "User";
case PAUSE_SATCOM: return "SatCom"; case PAUSE_SATCOM: return "SatCom";
case PAUSE_DIALOG: return "Dialog"; case PAUSE_SATCOMMOVIE: return "SatCom opening animation";
case PAUSE_EDITOR: return "CBot editor"; case PAUSE_DIALOG: return "Dialog";
case PAUSE_VISIT: return "Visit"; case PAUSE_EDITOR: return "CBot editor";
case PAUSE_CHEAT: return "Cheat console"; case PAUSE_VISIT: return "Visit";
case PAUSE_PHOTO: return "Photo mode"; case PAUSE_CHEAT: return "Cheat console";
case PAUSE_PHOTO: return "Photo mode";
default: assert(false); // Should never happen default: assert(false); // Should never happen
} }
} }

View File

@ -22,6 +22,7 @@
#pragma once #pragma once
#include "common/singleton.h" #include "common/singleton.h"
#include "sound/sound.h"
#include <string> #include <string>
@ -30,6 +31,7 @@ enum PauseType {
PAUSE_NONE = 0, PAUSE_NONE = 0,
PAUSE_USER, PAUSE_USER,
PAUSE_SATCOM, PAUSE_SATCOM,
PAUSE_SATCOMMOVIE,
PAUSE_DIALOG, PAUSE_DIALOG,
PAUSE_EDITOR, PAUSE_EDITOR,
PAUSE_VISIT, PAUSE_VISIT,
@ -52,6 +54,9 @@ public:
private: private:
std::string GetPauseName(PauseType pause); std::string GetPauseName(PauseType pause);
private:
CSoundInterface* m_sound;
PauseType m_pause; PauseType m_pause;
}; };

View File

@ -2174,7 +2174,7 @@ void CRobotMain::StartDisplayInfo(int index, bool movie)
{ {
m_movieInfoIndex = index; m_movieInfoIndex = index;
m_movie->Start(MM_SATCOMopen, 2.5f); m_movie->Start(MM_SATCOMopen, 2.5f);
ChangePause(PAUSE_SATCOM); ChangePause(PAUSE_SATCOMMOVIE);
m_infoObject = DeselectAll(); // removes the control buttons m_infoObject = DeselectAll(); // removes the control buttons
m_displayText->HideText(true); m_displayText->HideText(true);
return; return;

View File

@ -31,6 +31,8 @@ ALSound::ALSound()
m_currentMusic = nullptr; m_currentMusic = nullptr;
m_eye.LoadZero(); m_eye.LoadZero();
m_lookat.LoadZero(); m_lookat.LoadZero();
m_previousMusic.fadeTime = 0.0f;
m_previousMusic.music = nullptr;
} }
@ -62,6 +64,11 @@ void ALSound::CleanUp()
{ {
delete item.music; delete item.music;
} }
if (m_previousMusic.music)
{
delete m_previousMusic.music;
}
for (auto item : m_sounds) for (auto item : m_sounds)
{ {
@ -552,6 +559,15 @@ void ALSound::FrameMove(float delta)
} }
} }
if (m_previousMusic.fadeTime > 0.0f) {
if (m_previousMusic.currentTime >= m_previousMusic.fadeTime) {
m_previousMusic.music->Pause();
} else {
m_previousMusic.currentTime += delta;
m_previousMusic.music->SetVolume(((m_previousMusic.fadeTime-m_previousMusic.currentTime) / m_previousMusic.fadeTime) * m_musicVolume);
}
}
for (auto it : toRemove) for (auto it : toRemove)
m_oldMusic.remove(it); m_oldMusic.remove(it);
} }
@ -609,6 +625,7 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim
buffer = new Buffer(); buffer = new Buffer();
buffer->LoadFromFile(file.str(), static_cast<Sound>(-1)); buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
m_music[filename] = buffer;
} }
else else
{ {
@ -633,6 +650,43 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim
return true; return true;
} }
bool ALSound::PlayPauseMusic(const std::string &filename)
{
if (m_previousMusic.fadeTime > 0.0f) {
OldMusic old;
old.music = m_currentMusic;
old.fadeTime = 2.0f;
old.currentTime = 0.0f;
m_oldMusic.push_back(old);
m_currentMusic = nullptr;
} else {
if (m_currentMusic) {
m_previousMusic.music = m_currentMusic;
m_previousMusic.fadeTime = 2.0f;
m_previousMusic.currentTime = 0.0f;
m_currentMusic = nullptr;
}
}
return PlayMusic(filename, true);
}
void ALSound::StopPauseMusic()
{
if (m_previousMusic.fadeTime > 0.0f) {
StopMusic();
m_currentMusic = m_previousMusic.music;
m_previousMusic.music = nullptr;
if(m_currentMusic != nullptr) {
m_currentMusic->SetVolume(m_musicVolume);
if(m_previousMusic.currentTime >= m_previousMusic.fadeTime) {
m_currentMusic->Play();
}
}
m_previousMusic.fadeTime = 0.0f;
}
}
bool ALSound::RestartMusic() bool ALSound::RestartMusic()
{ {

View File

@ -72,12 +72,14 @@ public:
bool StopAll(); bool StopAll();
bool MuteAll(bool bMute); bool MuteAll(bool bMute);
bool PlayMusic(int rank, bool bRepeat, float fadeTime=5.0f); bool PlayMusic(int rank, bool bRepeat, float fadeTime=2.0f);
bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=5.0f); bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=2.0f);
bool RestartMusic(); bool RestartMusic();
void SuspendMusic(); void SuspendMusic();
void StopMusic(float fadeTime=5.0f); void StopMusic(float fadeTime=2.0f);
bool IsPlayingMusic(); bool IsPlayingMusic();
bool PlayPauseMusic(const std::string &filename);
void StopPauseMusic();
private: private:
void CleanUp(); void CleanUp();
@ -94,6 +96,7 @@ private:
std::map<int, Channel*> m_channels; std::map<int, Channel*> m_channels;
Channel *m_currentMusic; Channel *m_currentMusic;
std::list<OldMusic> m_oldMusic; std::list<OldMusic> m_oldMusic;
OldMusic m_previousMusic;
Math::Vector m_eye; Math::Vector m_eye;
Math::Vector m_lookat; Math::Vector m_lookat;
}; };

View File

@ -74,6 +74,21 @@ bool Channel::Play()
return true; return true;
} }
bool Channel::Pause()
{
if(!m_ready || !IsPlaying())
{
return false;
}
alSourcePause(m_source);
if (alCheck())
{
GetLogger()->Warn("Could not pause audio sound source. Code: %d\n", alGetCode());
}
return true;
}
bool Channel::SetPosition(const Math::Vector &pos) bool Channel::SetPosition(const Math::Vector &pos)
{ {

View File

@ -50,6 +50,7 @@ public:
~Channel(); ~Channel();
bool Play(); bool Play();
bool Pause();
bool Stop(); bool Stop();
bool SetPosition(const Math::Vector &); bool SetPosition(const Math::Vector &);

View File

@ -59,6 +59,12 @@ void CSoundInterface::AddMusicFiles(const std::string &path)
CacheMusic("Intro2.ogg"); CacheMusic("Intro2.ogg");
CacheMusic("music010.ogg"); CacheMusic("music010.ogg");
CacheMusic("music011.ogg"); CacheMusic("music011.ogg");
// TODO: Add pause music here
// CacheMusic("");
#if DEV_BUILD
CacheMusic("Prototype.ogg");
CacheMusic("Constructive.ogg");
#endif
} }
bool CSoundInterface::Cache(Sound bSound, const std::string &bFile) bool CSoundInterface::Cache(Sound bSound, const std::string &bFile)
@ -175,3 +181,11 @@ bool CSoundInterface::IsPlayingMusic()
return true; return true;
} }
bool CSoundInterface::PlayPauseMusic(const std::string &filename)
{
return true;
}
void CSoundInterface::StopPauseMusic()
{
}

View File

@ -287,7 +287,7 @@ public:
* \param fadeTime - time of transition between music * \param fadeTime - time of transition between music
* \return return true on success * \return return true on success
*/ */
virtual bool PlayMusic(int rank, bool bRepeat, float fadeTime=5.0f); virtual bool PlayMusic(int rank, bool bRepeat, float fadeTime=2.0f);
/** Start playing music /** Start playing music
* \param filename - name of file to play * \param filename - name of file to play
@ -295,27 +295,38 @@ public:
* \param fadeTime - time of transition between music * \param fadeTime - time of transition between music
* \return return true on success * \return return true on success
*/ */
virtual bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=5.0f); virtual bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=2.0f);
/** Restart music /** Restart music
* @return return true on success * \return return true on success
*/ */
virtual bool RestartMusic(); virtual bool RestartMusic();
/** Susspend paying music /** Susspend playing music
* \return return true on success * \return nothing
*/ */
virtual void SuspendMusic(); virtual void SuspendMusic();
/** Stop playing music /** Stop playing music
* \return return true on success * \return nothing
*/ */
virtual void StopMusic(float fadeTime=5.0f); virtual void StopMusic(float fadeTime=2.0f);
/** Check if music if playing /** Check if music if playing
* \return return true if music is playing * \return return true if music is playing
*/ */
virtual bool IsPlayingMusic(); virtual bool IsPlayingMusic();
/** Start playing pause music
* \param filename - name of file to play
* \return return true on success
*/
virtual bool PlayPauseMusic(const std::string &filename);
/** Stop playing pause music and return to the mission music
* \return nothing
*/
virtual void StopPauseMusic();
protected: protected:
std::string m_soundPath; std::string m_soundPath;