Implemented AudioChange (partially)

There is some issues due to #173
Issue #172
dev-ui
krzys-h 2013-04-13 11:44:16 +02:00
parent 69e52e5f24
commit ebffda717b
5 changed files with 126 additions and 40 deletions

View File

@ -3478,6 +3478,8 @@ bool CRobotMain::EventFrame(const Event &event)
CheckEndMission(true);
}
UpdateAudio(true);
if (m_winDelay > 0.0f && !m_editLock)
{
m_winDelay -= event.rTime;
@ -3824,6 +3826,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
m_lockedSatCom = false;
m_endingWinRank = 0;
m_endingLostRank = 0;
m_audioChangeTotal = 0;
m_endTakeTotal = 0;
m_endTakeResearch = 0;
m_endTakeWinDelay = 2.0f;
@ -3983,6 +3986,24 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
m_audioRepeat = OpInt(line, "repeat", 1);
}
if (Cmd(line, "AudioChange") && !resetObject && m_version >= 2)
{
int i = m_audioChangeTotal;
if (i < 10)
{
m_audioChange[i].pos = OpPos(line, "pos")*g_unit;
m_audioChange[i].dist = OpFloat(line, "dist", 8.0f)*g_unit;
m_audioChange[i].type = OpTypeObject(line, "type", OBJECT_NULL);
m_audioChange[i].min = OpInt(line, "min", 1);
m_audioChange[i].max = OpInt(line, "max", 9999);
OpString(line, "filename", m_audioChange[i].music);
m_audioChange[i].repeat = OpInt(line, "repeat", 1);
m_audioChange[i].changed = false;
m_audioChangeTotal ++;
}
}
if (Cmd(line, "AmbientColor") && !resetObject)
{
m_engine->SetAmbientColor(OpColor(line, "air", Gfx::Color(0.533f, 0.533f, 0.533f, 0.533f)), 0);
@ -6567,6 +6588,64 @@ void CRobotMain::ResetCreate()
}
}
//! Updates the audiotracks
void CRobotMain::UpdateAudio(bool frame)
{
CInstanceManager* iMan = CInstanceManager::GetInstancePointer();
for (int t = 0; t < m_audioChangeTotal; t++)
{
Math::Vector bPos = m_audioChange[t].pos;
bPos.y = 0.0f;
Math::Vector oPos;
int nb = 0;
for (int i = 0; i < 1000000; i++)
{
CObject* obj = static_cast<CObject*>(iMan->SearchInstance(CLASS_OBJECT, i));
if (obj == nullptr) break;
// Do not use GetActif () because an invisible worm (underground)
// should be regarded as existing here!
if (obj->GetLock()) continue;
if (obj->GetRuin()) continue;
if (!obj->GetEnable()) continue;
ObjectType type = obj->GetType();
if (type == OBJECT_SCRAP2 ||
type == OBJECT_SCRAP3 ||
type == OBJECT_SCRAP4 ||
type == OBJECT_SCRAP5) // wastes?
{
type = OBJECT_SCRAP1;
}
if (type != m_audioChange[t].type) continue;
if (obj->GetTruck() == 0)
oPos = obj->GetPosition(0);
else
oPos = obj->GetTruck()->GetPosition(0);
oPos.y = 0.0f;
if (Math::DistanceProjected(oPos, bPos) <= m_audioChange[t].dist)
nb ++;
}
if (nb >= m_audioChange[t].min &&
nb <= m_audioChange[t].max &&
!m_audioChange[t].changed)
{
CLogger::GetInstancePointer()->Debug("Changing music...\n");
m_sound->StopMusic();
m_sound->PlayMusic(std::string(m_audioChange[t].music), m_audioChange[t].repeat);
m_audioChange[t].changed = true;
}
}
}
//! Checks if the mission is over
Error CRobotMain::CheckEndMission(bool frame)
{
@ -7067,11 +7146,12 @@ float CRobotMain::GetTracePrecision()
//! Starts music with a mission
void CRobotMain::StartMusic()
{
CLogger::GetInstancePointer()->Debug("Starting music...\n");
if (m_audioTrack != 0)
{
m_sound->StopMusic();
m_sound->PlayMusic(m_audioTrack, m_audioRepeat);
}
}
}
//! Removes hilite and tooltip

View File

@ -99,13 +99,25 @@ struct EndTake
Math::Vector pos;
float dist;
ObjectType type;
int min; // wins if>
int min; // wins if >
int max; // wins if <
int lost; // lost if <=
bool immediat;
char message[100];
};
struct AudioChange
{
Math::Vector pos;
float dist;
ObjectType type;
int min; // change if >
int max; // change if <
char music[100];
bool repeat;
bool changed;
};
const int MAXNEWSCRIPTNAME = 20;
@ -248,6 +260,7 @@ public:
void ResetObject();
void ResetCreate();
void UpdateAudio(bool frame);
Error CheckEndMission(bool frame);
void CheckEndMessage(const char* message);
int GetObligatoryToken();
@ -522,6 +535,9 @@ protected:
long m_endTakeResearch;
float m_endTakeWinDelay;
float m_endTakeLostDelay;
int m_audioChangeTotal;
AudioChange m_audioChange[10];
int m_obligatoryTotal;
char m_obligatoryToken[100][20];

View File

@ -539,44 +539,32 @@ void ALSound::SetListener(Math::Vector eye, Math::Vector lookat)
}
}
bool ALSound::PlayMusic(int rank, bool bRepeat)
{
std::stringstream filename;
filename << "music" << std::setfill('0') << std::setw(3) << rank << ".ogg";
return PlayMusic(filename.str(), bRepeat);
}
bool ALSound::PlayMusic(std::string filename, bool bRepeat)
{
if (!mEnabled) {
return false;
}
if (static_cast<int>(mCurrentMusic->GetSoundType()) != rank) {
// check if we have music in cache
for (auto music : mMusicCache) {
if (static_cast<int>(music->GetSoundType()) == rank) {
GetLogger()->Debug("Music loaded from cache\n");
mCurrentMusic->SetBuffer(music);
mCurrentMusic->SetVolume(mMusicVolume);
mCurrentMusic->SetLoop(bRepeat);
mCurrentMusic->Play();
return true;
}
}
// we cache only 3 music files
if (mMusicCache.size() == 3) {
mCurrentMusic->FreeBuffer();
mMusicCache.pop_back();
}
std::stringstream file;
file << m_soundPath << "/" << filename;
if (mMusic.find(rank) == mMusic.end()) {
GetLogger()->Info("Requested music %d was not found.\n", rank);
return false;
}
Buffer *buffer = new Buffer();
mMusicCache.push_front(buffer);
buffer->LoadFromFile(mMusic.at(rank), static_cast<Sound>(rank));
mCurrentMusic->SetBuffer(buffer);
mMusicCache[rank] = buffer;
if (!boost::filesystem::exists(file.str())) {
GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str());
return false;
}
// TODO: Cache
Buffer *buffer = new Buffer();
buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
mCurrentMusic->SetBuffer(buffer);
mCurrentMusic->SetVolume(mMusicVolume);
mCurrentMusic->SetLoop(bRepeat);

View File

@ -66,6 +66,7 @@ class ALSound : public CSoundInterface
bool MuteAll(bool bMute);
bool PlayMusic(int rank, bool bRepeat);
bool PlayMusic(std::string filename, bool bRepeat);
bool RestartMusic();
void SuspendMusic();
void StopMusic();
@ -91,7 +92,6 @@ class ALSound : public CSoundInterface
ALCcontext* mContext;
std::map<Sound, Buffer*> mSounds;
std::map<int, Channel*> mChannels;
std::deque<Buffer*> mMusicCache;
Channel *mCurrentMusic;
Math::Vector mEye;
Math::Vector mLookat;

View File

@ -177,12 +177,7 @@ class CSoundInterface
/** Function called to add all music files to list */
inline void AddMusicFiles(std::string path) {
for ( int i = 1; i <= 12; i++ ) {
std::stringstream filename;
filename << path << "/music" << std::setfill('0') << std::setw(3) << i << ".ogg";
if (boost::filesystem::exists(filename.str()))
mMusic[i] = filename.str();
}
m_soundPath = path;
};
/** Function called to cache sound effect file.
@ -317,6 +312,13 @@ class CSoundInterface
*/
inline virtual bool PlayMusic(int rank, bool bRepeat) {return true;};
/** Start playing music
* \param filename - name of file to play
* \param bRepeat - repeat playing
* \return return true on success
*/
inline virtual bool PlayMusic(std::string filename, bool bRepeat) {return true;};
/** Restart music
* @return return true on success
*/
@ -338,6 +340,6 @@ class CSoundInterface
inline virtual bool IsPlayingMusic() {return true;};
protected:
std::map<int, std::string> mMusic;
std::string m_soundPath;
};