From 38c0f36d449de5b613c741b192012fc2d5f67560 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 9 Oct 2011 09:28:36 +0200 Subject: [PATCH] Issue #19: Finished cleaning out the Engine framelistener --- apps/openmw/engine.cpp | 80 ++++----------------------- apps/openmw/engine.hpp | 5 -- apps/openmw/mwsound/soundmanager.cpp | 81 ++++++++++++++++++++++++++-- apps/openmw/mwsound/soundmanager.hpp | 22 ++++++-- 4 files changed, 104 insertions(+), 84 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 474d8d78c..614658691 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -109,76 +109,19 @@ bool OMW::Engine::frameRenderingQueued (const Ogre::FrameEvent& evt) { mEnvironment.mFrameDuration = evt.timeSinceLastFrame; - if(mShowFPS) + // sound + if (mUseSound) { - mEnvironment.mWindowManager->wmSetFPS(mOgre.getFPS()); + if (!mEnvironment.mSoundManager->isMusicPlaying()) + mEnvironment.mSoundManager->startRandomTitle(); + + mEnvironment.mSoundManager->update (evt.timeSinceLastFrame); } - if(mUseSound && !(mEnvironment.mSoundManager->isMusicPlaying())) - { - // Play some good 'ol tunes - mEnvironment.mSoundManager->startRandomTitle(); - } - - std::string effect; - - MWWorld::Ptr::CellStore *current = mEnvironment.mWorld->getPlayer().getPlayer().getCell(); - - //If the region has changed - if(!(current->cell->data.flags & current->cell->Interior) && timer.elapsed() >= 10){ - timer.restart(); - if (test.name != current->cell->region) - { - total = 0; - test = (ESM::Region) *(mEnvironment.mWorld->getStore().regions.find(current->cell->region)); - } - - if(test.soundList.size() > 0) - { - std::vector::iterator soundIter = test.soundList.begin(); - //mEnvironment.mSoundManager - if(total == 0){ - while (!(soundIter == test.soundList.end())) - { - ESM::NAME32 go = soundIter->sound; - int chance = (int) soundIter->chance; - //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; - soundIter++; - total += chance; - } - } - - int r = rand() % total; //old random code - int pos = 0; - soundIter = test.soundList.begin(); - while (!(soundIter == test.soundList.end())) - { - const ESM::NAME32 go = soundIter->sound; - int chance = (int) soundIter->chance; - //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; - soundIter++; - if( r - pos < chance) - { - effect = go.name; - //play sound - std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; - mEnvironment.mSoundManager->playSound(effect, 20.0, 1.0); - - break; - - } - pos += chance; - } - } - } - else if(current->cell->data.flags & current->cell->Interior) - { - test.name = ""; - } - - - // update GUI + if(mShowFPS) + mEnvironment.mWindowManager->wmSetFPS(mOgre.getFPS()); + mEnvironment.mWindowManager->onFrame(mEnvironment.mFrameDuration); // global scripts @@ -350,9 +293,6 @@ void OMW::Engine::go() assert (!mCellName.empty()); assert (!mMaster.empty()); - test.name = ""; - total = 0; - mOgre.configure(!boost::filesystem::is_regular_file(mCfgMgr.getOgreConfigPath()), mCfgMgr.getOgreConfigPath().string(), mCfgMgr.getLogPath().string() + std::string("/"), @@ -396,7 +336,7 @@ void OMW::Engine::go() mOgre.getCamera(), mEnvironment.mWorld->getStore(), (mDataDir), - mUseSound, mFSStrict); + mUseSound, mFSStrict, mEnvironment); // Create script system mScriptContext = new MWScript::CompilerContext (MWScript::CompilerContext::Type_Full, diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index 8bf2dbbcf..30172c304 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -14,7 +14,6 @@ #include "mwworld/environment.hpp" #include "mwworld/ptr.hpp" -#include #include namespace Compiler @@ -72,15 +71,11 @@ namespace OMW float mFocusTDiff; std::string mFocusName; - int total; - MWWorld::Environment mEnvironment; MWScript::ScriptManager *mScriptManager; Compiler::Extensions mExtensions; Compiler::Context *mScriptContext; OEngine::GUI::MyGUIManager *mGuiManager; - ESM::Region test; - boost::timer timer; Files::Collections mFileCollections; bool mFSStrict; diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 970118a13..7390e4c5c 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -1,18 +1,23 @@ #include "soundmanager.hpp" #include +#include +#include + using namespace std; +#include + #include #include #include #include #include -#include -#include -#include +#include "../mwworld/environment.hpp" +#include "../mwworld/world.hpp" +#include "../mwworld/player.hpp" /* Set up the sound manager to use Audiere, FFMPEG or MPG123/libsndfile for input. The OPENMW_USE_x macros are set in @@ -378,12 +383,18 @@ namespace MWSound SoundManager::SoundManager(Ogre::Root *root, Ogre::Camera *camera, const ESMS::ESMStore &store, boost::filesystem::path dataDir, - bool useSound, bool fsstrict) - : mData(NULL), fsStrict (fsstrict) + bool useSound, bool fsstrict, MWWorld::Environment& environment) + : mData(NULL), fsStrict (fsstrict), mEnvironment (environment) { MP3Lookup(dataDir / "Music/Explore/"); if(useSound) mData = new SoundImpl(root, camera, store, (dataDir / "Sound").string(), (dataDir / "Music").string(), fsstrict); + + + test.name = ""; + total = 0; + + } SoundManager::~SoundManager() @@ -533,4 +544,64 @@ namespace MWSound if(!mData) return; mData->updatePositions(ptr); } + + void SoundManager::update (float duration) + { + std::string effect; + + MWWorld::Ptr::CellStore *current = mEnvironment.mWorld->getPlayer().getPlayer().getCell(); + + //If the region has changed + if(!(current->cell->data.flags & current->cell->Interior) && timer.elapsed() >= 10){ + timer.restart(); + if (test.name != current->cell->region) + { + total = 0; + test = (ESM::Region) *(mEnvironment.mWorld->getStore().regions.find(current->cell->region)); + } + + if(test.soundList.size() > 0) + { + std::vector::iterator soundIter = test.soundList.begin(); + //mEnvironment.mSoundManager + if(total == 0){ + while (!(soundIter == test.soundList.end())) + { + ESM::NAME32 go = soundIter->sound; + int chance = (int) soundIter->chance; + //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; + soundIter++; + total += chance; + } + } + + int r = rand() % total; //old random code + int pos = 0; + soundIter = test.soundList.begin(); + while (!(soundIter == test.soundList.end())) + { + const ESM::NAME32 go = soundIter->sound; + int chance = (int) soundIter->chance; + //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; + soundIter++; + if( r - pos < chance) + { + effect = go.name; + //play sound + std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; + mEnvironment.mSoundManager->playSound(effect, 20.0, 1.0); + + break; + + } + pos += chance; + } + } + } + else if(current->cell->data.flags & current->cell->Interior) + { + test.name = ""; + } + + } } diff --git a/apps/openmw/mwsound/soundmanager.hpp b/apps/openmw/mwsound/soundmanager.hpp index ab9559176..7dff16c76 100644 --- a/apps/openmw/mwsound/soundmanager.hpp +++ b/apps/openmw/mwsound/soundmanager.hpp @@ -8,6 +8,9 @@ #include "../mwworld/ptr.hpp" #include + +#include + namespace Ogre { class Root; @@ -19,6 +22,11 @@ namespace ESMS struct ESMStore; } +namespace MWWorld +{ + struct Environment; +} + namespace MWSound { //SoundPtr *music; @@ -31,6 +39,11 @@ namespace MWSound SoundImpl *mData; std::vector files; bool fsStrict; + MWWorld::Environment& mEnvironment; + + int total; + ESM::Region test; + boost::timer timer; void streamMusicFull (const std::string& filename); ///< Play a soundifle @@ -38,8 +51,9 @@ namespace MWSound public: - SoundManager(Ogre::Root*, Ogre::Camera*, const ESMS::ESMStore &store, - boost::filesystem::path dataDir, bool useSound, bool fsstrict); + SoundManager(Ogre::Root*, Ogre::Camera*, const ESMS::ESMStore &store, + boost::filesystem::path dataDir, bool useSound, bool fsstrict, + MWWorld::Environment& environment); ~SoundManager(); void streamMusic(const std::string& filename); @@ -60,8 +74,6 @@ namespace MWSound bool sayDone (MWWorld::Ptr reference) const; ///< Is actor not speaking? - - void playSound (const std::string& soundId, float volume, float pitch); ///< Play a sound, independently of 3D-position @@ -81,6 +93,8 @@ namespace MWSound void updateObject(MWWorld::Ptr reference); ///< Update the position of all sounds connected to the given object. + + void update (float duration); }; }