From cab23412592c3dfcd3b6a9a64db91efdf9d5701e Mon Sep 17 00:00:00 2001 From: Jason Hooks Date: Sat, 6 Nov 2010 13:11:09 -0400 Subject: [PATCH] Preliminary region sound framework --- apps/openmw/engine.cpp | 79 +++++++++++++++++++++-------------- apps/openmw/engine.hpp | 5 ++- apps/openmw/mwworld/world.cpp | 53 +++++++++++++++++++++-- apps/openmw/mwworld/world.hpp | 11 ++++- 4 files changed, 111 insertions(+), 37 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 913d1f679..54fa6ab46 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -7,7 +7,8 @@ #include #include - +#include +#include #include #include "mwgui/window_manager.hpp" @@ -59,49 +60,41 @@ void OMW::Engine::executeLocalScripts() void OMW::Engine::MP3Lookup() { boost::filesystem::directory_iterator dir_iter(mDataDir / "Music/Explore/"), dir_end; - //std::list files; - - nFiles = 0; + std::string mp3extension = ".mp3"; for(;dir_iter != dir_end; dir_iter++) { if(boost::filesystem::extension(*dir_iter) == mp3extension) { - files.push_front((*dir_iter)); - nFiles++; + files.push_back(*dir_iter); } - }//*/ + } } void OMW::Engine::startRandomTitle() { + std::vector::iterator fileIter; - std::list::iterator fileIter; - if(nFiles > 0) + if(files.size() > 0) { fileIter = files.begin(); - srand ( time(NULL) ); - + srand ( time(NULL) ); + int r = rand() % files.size() + 1; //old random code - int r = rand() % nFiles + 1; //old random code - - //int lowest=1, highest=nFiles; - //int range=(highest-lowest)+1; - //int r = lowest+int(range*rand()/(highest + 1.0)); - for(int i = 1; i < r; i++) - { - fileIter++; - } - std::string music = fileIter->file_string(); - try - { - std::cout << "Playing " << music << "\n"; + for(int i = 1; i < r; i++) + { + fileIter++; + } + std::string music = fileIter->file_string(); + try + { + std::cout << "Playing " << music << "\n"; mEnvironment.mSoundManager->streamMusic(music); - } - catch(std::exception &e) - { - std::cout << " Music Error: " << e.what() << "\n"; - } + } + catch(std::exception &e) + { + std::cout << " Music Error: " << e.what() << "\n"; + } } } @@ -112,9 +105,31 @@ bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt) // Play some good 'ol tunes startRandomTitle(); } - - //tesprintf("HERE");t.mSoundManager. + + //If the region has changed + if(mEnvironment.mWorld->getIsExterior() && test.name != mEnvironment.mWorld->getCurrentRegion().name){ + test = mEnvironment.mWorld->getCurrentRegion(); + if(test.soundList.size() > 0) + { + std::vector::iterator soundIter = test.soundList.begin(); + while (!(soundIter == test.soundList.end())) + { + ESM::NAME32 go = soundIter->sound; + char chance = soundIter->chance; + std::cout << "Sound: " << go.name <<" Chance:" << (int) chance << "\n"; + soundIter++; + } + + } + + + //printf("REGION: %s\n", test.name); + } + else if(!mEnvironment.mWorld->getIsExterior()) + { + test.name = ""; + } try { @@ -165,6 +180,7 @@ bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt) { std::cerr << "Error in framelistener: " << e.what() << std::endl; } + //std::cout << "TESTING2"; return true; } @@ -274,6 +290,7 @@ void OMW::Engine::go() assert (!mMaster.empty()); MP3Lookup(); + test.name = ""; diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index f9b1caab4..500fe2808 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -52,8 +52,8 @@ namespace OMW class Engine : private Ogre::FrameListener { - std::list files; - int nFiles; + std::vector files; + //int nFiles; boost::filesystem::path mDataDir; OEngine::Render::OgreRenderer mOgre; std::string mCellName; @@ -68,6 +68,7 @@ namespace OMW Compiler::Extensions mExtensions; Compiler::Context *mScriptContext; OEngine::GUI::MyGUIManager *mGuiManager; + ESM::Region test; int focusFrameCounter; static const int focusUpdateFrame = 10; diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index ca29facfd..3e91b76b8 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -18,6 +18,10 @@ #include "environment.hpp" #include "class.hpp" + +#include "refdata.hpp" +#include "globals.hpp" + namespace { template @@ -62,6 +66,30 @@ namespace namespace MWWorld { + ESM::ESMReader World::getEsmReader(){ + return mEsm; + } + + Ptr::CellStore World::getMCurrentCell() + { + return *mCurrentCell; + } + + + ESM::Region World::getCurrentRegion() + { + return *currentRegion; + } + + + bool World::getIsExterior() + { + return isExterior; + } + void World::setIsExterior(bool set) + { + isExterior = set; + } void World::insertInteriorScripts (ESMS::CellStore& cell) { listCellScripts (mStore, cell.activators, mLocalScripts, &cell); @@ -315,6 +343,7 @@ namespace MWWorld : mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0), mSky (false), mCellChanged (false), mEnvironment (environment) { + isExterior = false; boost::filesystem::path masterPath (dataDir); masterPath /= master; @@ -605,6 +634,8 @@ namespace MWWorld adjustSky(); mCellChanged = true; + isExterior = false; + //currentRegion->name = ""; } void World::changeCell (int X, int Y, const ESM::Position& position) @@ -685,6 +716,7 @@ namespace MWWorld { int x = 0; int y = 0; + isExterior = true; positionToIndex (position.pos[0], position.pos[1], x, y); @@ -695,9 +727,21 @@ namespace MWWorld { // first try named cells if (const ESM::Cell *cell = mStore.cells.searchExtByName (cellName)) + { + //mCellChanged = true; + + getExteriorRegion(cell->region); return cell; - // didn't work -> now check for regions + } + + + return getExteriorRegion(cellName); + } + + const ESM::Cell *World::getExteriorRegion(const std::string& cellName) const + { + // didn't work -> now check for regions std::string cellName2 = ESMS::RecListT::toLower (cellName); for (ESMS::RecListT::MapType::const_iterator iter (mStore.regions.list.begin()); @@ -705,6 +749,7 @@ namespace MWWorld { if (ESMS::RecListT::toLower (iter->second.name)==cellName2) { + *currentRegion = iter->second; if (const ESM::Cell *cell = mStore.cells.searchExtByRegion (iter->first)) return cell; @@ -712,8 +757,8 @@ namespace MWWorld } } - return 0; - } + return 0; + } void World::markCellAsUnchanged() { @@ -794,6 +839,8 @@ namespace MWWorld } } + + void World::positionToIndex (float x, float y, int &cellX, int &cellY) const { const int cellSize = 8192; diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index 832c90bf0..56cba9e9a 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -39,10 +39,16 @@ namespace MWWorld class World { + public: - typedef std::list > ScriptList; + ESM::ESMReader getEsmReader(); + Ptr::CellStore getMCurrentCell(); + ESM::Region getCurrentRegion(); + bool getIsExterior(); + void setIsExterior(bool set); + private: typedef std::map CellRenderCollection; @@ -61,7 +67,9 @@ namespace MWWorld MWWorld::Globals *mGlobalVariables; bool mSky; bool mCellChanged; + bool isExterior; Environment& mEnvironment; + ESM::Region *currentRegion; // not implemented World (const World&); @@ -146,6 +154,7 @@ namespace MWWorld void changeToExteriorCell (const ESM::Position& position); const ESM::Cell *getExterior (const std::string& cellName) const; + const ESM::Cell *getExteriorRegion (const std::string& cellName) const; ///< Return a cell matching the given name or a 0-pointer, if there is no such cell. void markCellAsUnchanged();