From ebffda717b1b633baba7a313267505614f89f26e Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 13 Apr 2013 11:44:16 +0200 Subject: Implemented AudioChange (partially) There is some issues due to #173 Issue #172 --- src/object/robotmain.cpp | 82 +++++++++++++++++++++++++++++++++++++++++- src/object/robotmain.h | 18 +++++++++- src/sound/oalsound/alsound.cpp | 48 ++++++++++--------------- src/sound/oalsound/alsound.h | 2 +- src/sound/sound.h | 16 +++++---- 5 files changed, 126 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/object/robotmain.cpp b/src/object/robotmain.cpp index 90d5d03..ca18f54 100644 --- a/src/object/robotmain.cpp +++ b/src/object/robotmain.cpp @@ -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(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 diff --git a/src/object/robotmain.h b/src/object/robotmain.h index fc62072..d3b18bc 100644 --- a/src/object/robotmain.h +++ b/src/object/robotmain.h @@ -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]; diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 3ce975b..48fcc15 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -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(mCurrentMusic->GetSoundType()) != rank) { - // check if we have music in cache - for (auto music : mMusicCache) { - if (static_cast(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(); - } - if (mMusic.find(rank) == mMusic.end()) { - GetLogger()->Info("Requested music %d was not found.\n", rank); - return false; - } + std::stringstream file; + file << m_soundPath << "/" << filename; - Buffer *buffer = new Buffer(); - mMusicCache.push_front(buffer); - buffer->LoadFromFile(mMusic.at(rank), static_cast(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(-1)); + mCurrentMusic->SetBuffer(buffer); mCurrentMusic->SetVolume(mMusicVolume); mCurrentMusic->SetLoop(bRepeat); diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 5701997..7e0503a 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -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 mSounds; std::map mChannels; - std::deque mMusicCache; Channel *mCurrentMusic; Math::Vector mEye; Math::Vector mLookat; diff --git a/src/sound/sound.h b/src/sound/sound.h index 70139ea..f101518 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -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 mMusic; + std::string m_soundPath; }; -- cgit v1.2.3-1-g7c22