From 79676aee15bdab383d42ae8d86598312c398bb44 Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 6 May 2022 17:46:50 +0200 Subject: [PATCH] Make Environment a storage of referencing pointers instead of owned Engine controls lifetime of managers therefore it should own them. Environment is only access provider. This allows to avoid redundant virtual calls and also some functions from managers base classes can be removed if they are used only by Engine. --- apps/openmw/engine.cpp | 140 ++++++++++-------- apps/openmw/engine.hpp | 57 +++++++- apps/openmw/mwbase/environment.cpp | 168 ++-------------------- apps/openmw/mwbase/environment.hpp | 101 ++++++------- apps/openmw/mwinput/actionmanager.cpp | 4 +- apps/openmw/mwlua/uibindings.cpp | 2 +- apps/openmw/mwmechanics/actors.cpp | 10 +- apps/openmw/mwmechanics/character.cpp | 6 +- apps/openmw/mwphysics/physicssystem.cpp | 2 +- apps/openmw/mwrender/landmanager.cpp | 2 +- apps/openmw/mwrender/renderingmanager.cpp | 2 +- apps/openmw/mwworld/player.cpp | 6 +- components/misc/notnullptr.hpp | 33 +++++ 13 files changed, 246 insertions(+), 287 deletions(-) create mode 100644 components/misc/notnullptr.hpp diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 650f6511f9..a99bd72831 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -271,7 +271,7 @@ namespace void OMW::Engine::executeLocalScripts() { - MWWorld::LocalScripts& localScripts = mEnvironment.getWorld()->getLocalScripts(); + MWWorld::LocalScripts& localScripts = mWorld->getLocalScripts(); localScripts.startIteration(); std::pair script; @@ -279,7 +279,7 @@ void OMW::Engine::executeLocalScripts() { MWScript::InterpreterContext interpreterContext ( &script.second.getRefData().getLocals(), script.second); - mEnvironment.getScriptManager()->run (script.first, interpreterContext); + mScriptManager->run (script.first, interpreterContext); } } @@ -297,7 +297,7 @@ bool OMW::Engine::frame(float frametime) // update input { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - mEnvironment.getInputManager()->update(frametime, false); + mInputManager->update(frametime, false); } // When the window is minimized, pause the game. Currently this *has* to be here to work around a MyGUI bug. @@ -306,21 +306,21 @@ bool OMW::Engine::frame(float frametime) { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - if (!mEnvironment.getWindowManager()->isWindowVisible()) + if (!mWindowManager->isWindowVisible()) { - mEnvironment.getSoundManager()->pausePlayback(); + mSoundManager->pausePlayback(); return false; } else - mEnvironment.getSoundManager()->resumePlayback(); + mSoundManager->resumePlayback(); // sound if (mUseSound) - mEnvironment.getSoundManager()->update(frametime); + mSoundManager->update(frametime); } // Main menu opened? Then scripts are also paused. - bool paused = mEnvironment.getWindowManager()->containsMode(MWGui::GM_MainMenu); + bool paused = mWindowManager->containsMode(MWGui::GM_MainMenu); // Should be called after input manager update and before any change to the game world. // It applies to the game world queued changes from the previous frame. @@ -329,35 +329,35 @@ bool OMW::Engine::frame(float frametime) // update game state { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - mEnvironment.getStateManager()->update (frametime); + mStateManager->update (frametime); } - bool guiActive = mEnvironment.getWindowManager()->isGuiMode(); + bool guiActive = mWindowManager->isGuiMode(); { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - if (mEnvironment.getStateManager()->getState() != MWBase::StateManager::State_NoGame) + if (mStateManager->getState() != MWBase::StateManager::State_NoGame) { if (!paused) { - if (mEnvironment.getWorld()->getScriptsEnabled()) + if (mWorld->getScriptsEnabled()) { // local scripts executeLocalScripts(); // global scripts - mEnvironment.getScriptManager()->getGlobalScripts().run(); + mScriptManager->getGlobalScripts().run(); } - mEnvironment.getWorld()->markCellAsUnchanged(); + mWorld->markCellAsUnchanged(); } if (!guiActive) { - double hours = (frametime * mEnvironment.getWorld()->getTimeScaleFactor()) / 3600.0; - mEnvironment.getWorld()->advanceTime(hours, true); - mEnvironment.getWorld()->rechargeItems(frametime, true); + double hours = (frametime * mWorld->getTimeScaleFactor()) / 3600.0; + mWorld->advanceTime(hours, true); + mWorld->rechargeItems(frametime, true); } } } @@ -366,16 +366,16 @@ bool OMW::Engine::frame(float frametime) { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - if (mEnvironment.getStateManager()->getState() != MWBase::StateManager::State_NoGame) + if (mStateManager->getState() != MWBase::StateManager::State_NoGame) { - mEnvironment.getMechanicsManager()->update(frametime, guiActive); + mMechanicsManager->update(frametime, guiActive); } - if (mEnvironment.getStateManager()->getState() == MWBase::StateManager::State_Running) + if (mStateManager->getState() == MWBase::StateManager::State_Running) { - MWWorld::Ptr player = mEnvironment.getWorld()->getPlayerPtr(); + MWWorld::Ptr player = mWorld->getPlayerPtr(); if(!guiActive && player.getClass().getCreatureStats(player).isDead()) - mEnvironment.getStateManager()->endGame(); + mStateManager->endGame(); } } @@ -383,9 +383,9 @@ bool OMW::Engine::frame(float frametime) { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - if (mEnvironment.getStateManager()->getState() != MWBase::StateManager::State_NoGame) + if (mStateManager->getState() != MWBase::StateManager::State_NoGame) { - mEnvironment.getWorld()->updatePhysics(frametime, guiActive, frameStart, frameNumber, *stats); + mWorld->updatePhysics(frametime, guiActive, frameStart, frameNumber, *stats); } } @@ -393,16 +393,16 @@ bool OMW::Engine::frame(float frametime) { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - if (mEnvironment.getStateManager()->getState() != MWBase::StateManager::State_NoGame) + if (mStateManager->getState() != MWBase::StateManager::State_NoGame) { - mEnvironment.getWorld()->update(frametime, guiActive); + mWorld->update(frametime, guiActive); } } // update GUI { ScopedProfile profile(frameStart, frameNumber, *timer, *stats); - mEnvironment.getWindowManager()->update(frametime); + mWindowManager->update(frametime); } if (stats->collectStats("resource")) @@ -443,7 +443,6 @@ OMW::Engine::Engine(Files::ConfigurationManager& configurationManager) , mExportFonts(false) , mRandomSeed(0) , mScriptContext (nullptr) - , mLuaManager (nullptr) , mFSStrict (false) , mScriptBlacklistUse (true) , mNewGame (false) @@ -471,6 +470,17 @@ OMW::Engine::~Engine() mEnvironment.cleanup(); + mMechanicsManager = nullptr; + mDialogueManager = nullptr; + mJournal = nullptr; + mScriptManager = nullptr; + mWindowManager = nullptr; + mWorld = nullptr; + mSoundManager = nullptr; + mInputManager = nullptr; + mStateManager = nullptr; + mLuaManager = nullptr; + delete mScriptContext; mScriptContext = nullptr; @@ -701,8 +711,8 @@ void OMW::Engine::setWindowIcon() void OMW::Engine::prepareEngine (Settings::Manager & settings) { - mEnvironment.setStateManager ( - std::make_unique (mCfgMgr.getUserDataPath() / "saves", mContentFiles)); + mStateManager = std::make_unique(mCfgMgr.getUserDataPath() / "saves", mContentFiles); + mEnvironment.setStateManager(*mStateManager); mStereoManager = std::make_unique(mViewer); @@ -723,6 +733,7 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) Settings::Manager::getString("texture mipmap", "General"), Settings::Manager::getInt("anisotropy", "General") ); + mEnvironment.setResourceSystem(*mResourceSystem); int numThreads = Settings::Manager::getInt("preload num threads", "Cells"); if (numThreads <= 0) @@ -744,9 +755,8 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) mViewer->addEventHandler(mScreenCaptureHandler); - auto luaMgr = std::make_unique(mVFS.get(), (mResDir / "lua_libs").string()); - mLuaManager = luaMgr.get(); - mEnvironment.setLuaManager(std::move(luaMgr)); + mLuaManager = std::make_unique(mVFS.get(), (mResDir / "lua_libs").string()); + mEnvironment.setLuaManager(*mLuaManager); // Create input and UI first to set up a bootstrapping environment for // showing a loading screen and keeping the window responsive while doing so @@ -799,34 +809,36 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) mStereoManager->disableStereoForNode(guiRoot); rootNode->addChild(guiRoot); - auto windowMgr = std::make_unique(mWindow, mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(), + mWindowManager = std::make_unique(mWindow, mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(), mCfgMgr.getLogPath().string() + std::string("/"), myguiResources, mScriptConsoleMode, mTranslationDataStorage, mEncoding, mExportFonts, Version::getOpenmwVersionDescription(mResDir.string()), mCfgMgr.getUserConfigPath().string(), shadersSupported); - auto* windowMgrInternal = windowMgr.get(); - mEnvironment.setWindowManager (std::move(windowMgr)); + mEnvironment.setWindowManager(*mWindowManager); - auto inputMgr = std::make_unique(mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab); - mEnvironment.setInputManager(std::move(inputMgr)); + mInputManager = std::make_unique(mWindow, mViewer, mScreenCaptureHandler, + mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab); + mEnvironment.setInputManager(*mInputManager); // Create sound system - mEnvironment.setSoundManager (std::make_unique(mVFS.get(), mUseSound)); + mSoundManager = std::make_unique(mVFS.get(), mUseSound); + mEnvironment.setSoundManager(*mSoundManager); if (!mSkipMenu) { const std::string& logo = Fallback::Map::getString("Movies_Company_Logo"); if (!logo.empty()) - mEnvironment.getWindowManager()->playVideo(logo, true); + mWindowManager->playVideo(logo, true); } // Create the world - mEnvironment.setWorld(std::make_unique(mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(), + mWorld = std::make_unique(mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(), mFileCollections, mContentFiles, mGroundcoverFiles, mEncoder, mActivationDistanceOverride, mCellName, - mStartupScript, mResDir.string(), mCfgMgr.getUserDataPath().string())); - mEnvironment.getWorld()->setupPlayer(); + mStartupScript, mResDir.string(), mCfgMgr.getUserDataPath().string()); + mWorld->setupPlayer(); + mEnvironment.setWorld(*mWorld); - windowMgrInternal->setStore(mEnvironment.getWorld()->getStore()); - windowMgrInternal->initUI(); + mWindowManager->setStore(mWorld->getStore()); + mWindowManager->initUI(); //Load translation data mTranslationDataStorage.setEncoder(mEncoder); @@ -839,21 +851,25 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) mScriptContext = new MWScript::CompilerContext (MWScript::CompilerContext::Type_Full); mScriptContext->setExtensions (&mExtensions); - mEnvironment.setScriptManager (std::make_unique(mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode, - mScriptBlacklistUse ? mScriptBlacklist : std::vector())); + mScriptManager = std::make_unique(mWorld->getStore(), *mScriptContext, mWarningsMode, + mScriptBlacklistUse ? mScriptBlacklist : std::vector()); + mEnvironment.setScriptManager(*mScriptManager); // Create game mechanics system - mEnvironment.setMechanicsManager (std::make_unique()); + mMechanicsManager = std::make_unique(); + mEnvironment.setMechanicsManager(*mMechanicsManager); // Create dialog system - mEnvironment.setJournal (std::make_unique()); - mEnvironment.setDialogueManager (std::make_unique(mExtensions, mTranslationDataStorage)); - mEnvironment.setResourceSystem(mResourceSystem.get()); + mJournal = std::make_unique(); + mEnvironment.setJournal(*mJournal); + + mDialogueManager = std::make_unique(mExtensions, mTranslationDataStorage); + mEnvironment.setDialogueManager(*mDialogueManager); // scripts if (mCompileAll) { - std::pair result = mEnvironment.getScriptManager()->compileAll(); + std::pair result = mScriptManager->compileAll(); if (result.first) Log(Debug::Info) << "compiled " << result.second << " of " << result.first << " scripts (" @@ -1014,25 +1030,25 @@ void OMW::Engine::go() // Start the game if (!mSaveGameFile.empty()) { - mEnvironment.getStateManager()->loadGame(mSaveGameFile); + mStateManager->loadGame(mSaveGameFile); } else if (!mSkipMenu) { // start in main menu - mEnvironment.getWindowManager()->pushGuiMode (MWGui::GM_MainMenu); - mEnvironment.getSoundManager()->playTitleMusic(); + mWindowManager->pushGuiMode (MWGui::GM_MainMenu); + mSoundManager->playTitleMusic(); const std::string& logo = Fallback::Map::getString("Movies_Morrowind_Logo"); if (!logo.empty()) - mEnvironment.getWindowManager()->playVideo(logo, true); + mWindowManager->playVideo(logo, true); } else { - mEnvironment.getStateManager()->newGame (!mNewGame); + mStateManager->newGame (!mNewGame); } - if (!mStartupScript.empty() && mEnvironment.getStateManager()->getState() == MWState::StateManager::State_Running) + if (!mStartupScript.empty() && mStateManager->getState() == MWState::StateManager::State_Running) { - mEnvironment.getWindowManager()->executeInConsole(mStartupScript); + mWindowManager->executeInConsole(mStartupScript); } LuaWorker luaWorker(this); // starts a separate lua thread if "lua num threads" > 0 @@ -1041,7 +1057,7 @@ void OMW::Engine::go() double simulationTime = 0.0; Misc::FrameRateLimiter frameRateLimiter = Misc::makeFrameRateLimiter(mEnvironment.getFrameRateLimit()); const std::chrono::steady_clock::duration maxSimulationInterval(std::chrono::milliseconds(200)); - while (!mViewer->done() && !mEnvironment.getStateManager()->hasQuitRequest()) + while (!mViewer->done() && !mStateManager->hasQuitRequest()) { const double dt = std::chrono::duration_cast>(std::min( frameRateLimiter.getLastFrameDuration(), @@ -1060,7 +1076,7 @@ void OMW::Engine::go() mViewer->eventTraversal(); mViewer->updateTraversal(); - mEnvironment.getWorld()->updateWindowManager(); + mWorld->updateWindowManager(); luaWorker.allowUpdate(); // if there is a separate Lua thread, it starts the update now @@ -1068,7 +1084,7 @@ void OMW::Engine::go() luaWorker.finishUpdate(); - bool guiActive = mEnvironment.getWindowManager()->isGuiMode(); + bool guiActive = mWindowManager->isGuiMode(); if (!guiActive) simulationTime += dt; } diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index d30bd33e80..c281f032ea 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -64,6 +64,51 @@ namespace SceneUtil } } +namespace MWState +{ + class StateManager; +} + +namespace MWGui +{ + class WindowManager; +} + +namespace MWInput +{ + class InputManager; +} + +namespace MWSound +{ + class SoundManager; +} + +namespace MWWorld +{ + class World; +} + +namespace MWScript +{ + class ScriptManager; +} + +namespace MWMechanics +{ + class MechanicsManager; +} + +namespace MWDialogue +{ + class DialogueManager; +} + +namespace MWDialogue +{ + class Journal; +} + struct SDL_Window; namespace OMW @@ -75,6 +120,16 @@ namespace OMW std::unique_ptr mVFS; std::unique_ptr mResourceSystem; osg::ref_ptr mWorkQueue; + std::unique_ptr mWorld; + std::unique_ptr mSoundManager; + std::unique_ptr mScriptManager; + std::unique_ptr mWindowManager; + std::unique_ptr mMechanicsManager; + std::unique_ptr mDialogueManager; + std::unique_ptr mJournal; + std::unique_ptr mInputManager; + std::unique_ptr mStateManager; + std::unique_ptr mLuaManager; MWBase::Environment mEnvironment; ToUTF8::FromType mEncoding; ToUTF8::Utf8Encoder* mEncoder; @@ -111,8 +166,6 @@ namespace OMW Compiler::Extensions mExtensions; Compiler::Context *mScriptContext; - MWLua::LuaManager* mLuaManager; - Files::Collections mFileCollections; bool mFSStrict; Translation::Storage mTranslationDataStorage; diff --git a/apps/openmw/mwbase/environment.cpp b/apps/openmw/mwbase/environment.cpp index 1b212c73ab..70027989a6 100644 --- a/apps/openmw/mwbase/environment.cpp +++ b/apps/openmw/mwbase/environment.cpp @@ -19,7 +19,7 @@ MWBase::Environment *MWBase::Environment::sThis = nullptr; MWBase::Environment::Environment() { - assert(!sThis); + assert(sThis == nullptr); sThis = this; } @@ -28,167 +28,21 @@ MWBase::Environment::~Environment() sThis = nullptr; } -void MWBase::Environment::setWorld (std::unique_ptr&& world) -{ - mWorld = std::move(world); -} - -void MWBase::Environment::setSoundManager (std::unique_ptr&& soundManager) -{ - mSoundManager = std::move(soundManager); -} - -void MWBase::Environment::setScriptManager (std::unique_ptr&& scriptManager) -{ - mScriptManager = std::move(scriptManager); -} - -void MWBase::Environment::setWindowManager (std::unique_ptr&& windowManager) -{ - mWindowManager = std::move(windowManager); -} - -void MWBase::Environment::setMechanicsManager (std::unique_ptr&& mechanicsManager) -{ - mMechanicsManager = std::move(mechanicsManager); -} - -void MWBase::Environment::setDialogueManager (std::unique_ptr&& dialogueManager) -{ - mDialogueManager = std::move(dialogueManager); -} - -void MWBase::Environment::setJournal (std::unique_ptr&& journal) -{ - mJournal = std::move(journal); -} - -void MWBase::Environment::setInputManager (std::unique_ptr&& inputManager) -{ - mInputManager = std::move(inputManager); -} - -void MWBase::Environment::setStateManager (std::unique_ptr&& stateManager) -{ - mStateManager = std::move(stateManager); -} - -void MWBase::Environment::setLuaManager (std::unique_ptr&& luaManager) -{ - mLuaManager = std::move(luaManager); -} - -void MWBase::Environment::setResourceSystem (Resource::ResourceSystem *resourceSystem) -{ - mResourceSystem = resourceSystem; -} - -void MWBase::Environment::setFrameDuration (float duration) -{ - mFrameDuration = duration; -} - -void MWBase::Environment::setFrameRateLimit(float limit) -{ - mFrameRateLimit = limit; -} - -float MWBase::Environment::getFrameRateLimit() const -{ - return mFrameRateLimit; -} - -MWBase::World *MWBase::Environment::getWorld() const -{ - assert (mWorld); - return mWorld.get(); -} - -MWBase::SoundManager *MWBase::Environment::getSoundManager() const -{ - assert (mSoundManager); - return mSoundManager.get(); -} - -MWBase::ScriptManager *MWBase::Environment::getScriptManager() const -{ - assert (mScriptManager); - return mScriptManager.get(); -} - -MWBase::WindowManager *MWBase::Environment::getWindowManager() const -{ - assert (mWindowManager); - return mWindowManager.get(); -} - -MWBase::MechanicsManager *MWBase::Environment::getMechanicsManager() const -{ - assert (mMechanicsManager); - return mMechanicsManager.get(); -} - -MWBase::DialogueManager *MWBase::Environment::getDialogueManager() const -{ - assert (mDialogueManager); - return mDialogueManager.get(); -} - -MWBase::Journal *MWBase::Environment::getJournal() const -{ - assert (mJournal); - return mJournal.get(); -} - -MWBase::InputManager *MWBase::Environment::getInputManager() const -{ - assert (mInputManager); - return mInputManager.get(); -} - -MWBase::StateManager *MWBase::Environment::getStateManager() const -{ - assert (mStateManager); - return mStateManager.get(); -} - -MWBase::LuaManager *MWBase::Environment::getLuaManager() const -{ - assert (mLuaManager); - return mLuaManager.get(); -} - -Resource::ResourceSystem *MWBase::Environment::getResourceSystem() const -{ - return mResourceSystem; -} - -float MWBase::Environment::getFrameDuration() const -{ - return mFrameDuration; -} - void MWBase::Environment::cleanup() { - mMechanicsManager.reset(); - mDialogueManager.reset(); - mJournal.reset(); - mScriptManager.reset(); - mWindowManager.reset(); - mWorld.reset(); - mSoundManager.reset(); - mInputManager.reset(); - mStateManager.reset(); - mLuaManager.reset(); + mMechanicsManager = nullptr; + mDialogueManager = nullptr; + mJournal = nullptr; + mScriptManager = nullptr; + mWindowManager = nullptr; + mWorld = nullptr; + mSoundManager = nullptr; + mInputManager = nullptr; + mStateManager = nullptr; + mLuaManager = nullptr; mResourceSystem = nullptr; } -const MWBase::Environment& MWBase::Environment::get() -{ - assert (sThis); - return *sThis; -} - void MWBase::Environment::reportStats(unsigned int frameNumber, osg::Stats& stats) const { mMechanicsManager->reportStats(frameNumber, stats); diff --git a/apps/openmw/mwbase/environment.hpp b/apps/openmw/mwbase/environment.hpp index 00af5d2869..e8ea605a9a 100644 --- a/apps/openmw/mwbase/environment.hpp +++ b/apps/openmw/mwbase/environment.hpp @@ -1,6 +1,8 @@ #ifndef GAME_BASE_ENVIRONMENT_H #define GAME_BASE_ENVIRONMENT_H +#include + #include namespace osg @@ -34,25 +36,19 @@ namespace MWBase { static Environment *sThis; - std::unique_ptr mWorld; - std::unique_ptr mSoundManager; - std::unique_ptr mScriptManager; - std::unique_ptr mWindowManager; - std::unique_ptr mMechanicsManager; - std::unique_ptr mDialogueManager; - std::unique_ptr mJournal; - std::unique_ptr mInputManager; - std::unique_ptr mStateManager; - std::unique_ptr mLuaManager; - Resource::ResourceSystem* mResourceSystem{}; - float mFrameDuration{}; - float mFrameRateLimit{}; - - Environment (const Environment&); - ///< not implemented - - Environment& operator= (const Environment&); - ///< not implemented + World* mWorld = nullptr; + SoundManager* mSoundManager = nullptr; + ScriptManager* mScriptManager = nullptr; + WindowManager* mWindowManager = nullptr; + MechanicsManager* mMechanicsManager = nullptr; + DialogueManager* mDialogueManager = nullptr; + Journal* mJournal = nullptr; + InputManager* mInputManager = nullptr; + StateManager* mStateManager = nullptr; + LuaManager* mLuaManager = nullptr; + Resource::ResourceSystem* mResourceSystem = nullptr; + float mFrameRateLimit = 0; + float mFrameDuration = 0; public: @@ -60,63 +56,70 @@ namespace MWBase ~Environment(); - void setWorld (std::unique_ptr&& world); + Environment(const Environment&) = delete; + + Environment& operator=(const Environment&) = delete; + + void setWorld(World& value) { mWorld = &value; } + + void setSoundManager(SoundManager& value) { mSoundManager = &value; } - void setSoundManager (std::unique_ptr&& soundManager); + void setScriptManager(ScriptManager& value) { mScriptManager = &value; } - void setScriptManager (std::unique_ptr&& scriptManager); + void setWindowManager(WindowManager& value) { mWindowManager = &value; } - void setWindowManager (std::unique_ptr&& windowManager); + void setMechanicsManager(MechanicsManager& value) { mMechanicsManager = &value; } - void setMechanicsManager (std::unique_ptr&& mechanicsManager); + void setDialogueManager(DialogueManager& value) { mDialogueManager = &value; } - void setDialogueManager (std::unique_ptr&& dialogueManager); + void setJournal(Journal& value) { mJournal = &value; } - void setJournal (std::unique_ptr&& journal); + void setInputManager(InputManager& value) { mInputManager = &value; } - void setInputManager (std::unique_ptr&& inputManager); + void setStateManager(StateManager& value) { mStateManager = &value; } - void setStateManager (std::unique_ptr&& stateManager); + void setLuaManager(LuaManager& value) { mLuaManager = &value; } - void setLuaManager (std::unique_ptr&& luaManager); + void setResourceSystem(Resource::ResourceSystem& value) { mResourceSystem = &value; } - void setResourceSystem (Resource::ResourceSystem *resourceSystem); + Misc::NotNullPtr getWorld() const { return mWorld; } - void setFrameDuration (float duration); - ///< Set length of current frame in seconds. + Misc::NotNullPtr getSoundManager() const { return mSoundManager; } - void setFrameRateLimit(float frameRateLimit); - float getFrameRateLimit() const; + Misc::NotNullPtr getScriptManager() const { return mScriptManager; } - World *getWorld() const; + Misc::NotNullPtr getWindowManager() const { return mWindowManager; } - SoundManager *getSoundManager() const; + Misc::NotNullPtr getMechanicsManager() const { return mMechanicsManager; } - ScriptManager *getScriptManager() const; + Misc::NotNullPtr getDialogueManager() const { return mDialogueManager; } - WindowManager *getWindowManager() const; + Misc::NotNullPtr getJournal() const { return mJournal; } - MechanicsManager *getMechanicsManager() const; + Misc::NotNullPtr getInputManager() const { return mInputManager; } - DialogueManager *getDialogueManager() const; + Misc::NotNullPtr getStateManager() const { return mStateManager; } - Journal *getJournal() const; + Misc::NotNullPtr getLuaManager() const { return mLuaManager; } - InputManager *getInputManager() const; + Misc::NotNullPtr getResourceSystem() const { return mResourceSystem; } - StateManager *getStateManager() const; + float getFrameRateLimit() const { return mFrameRateLimit; } - LuaManager *getLuaManager() const; + void setFrameRateLimit(float value) { mFrameRateLimit = value; } - Resource::ResourceSystem *getResourceSystem() const; + float getFrameDuration() const { return mFrameDuration; } - float getFrameDuration() const; + void setFrameDuration(float value) { mFrameDuration = value; } void cleanup(); - ///< Delete all mw*-subsystems. - static const Environment& get(); - ///< Return instance of this class. + /// Return instance of this class. + static const Environment& get() + { + assert(sThis != nullptr); + return *sThis; + } void reportStats(unsigned int frameNumber, osg::Stats& stats) const; }; diff --git a/apps/openmw/mwinput/actionmanager.cpp b/apps/openmw/mwinput/actionmanager.cpp index 63b0a197a7..eae6996acd 100644 --- a/apps/openmw/mwinput/actionmanager.cpp +++ b/apps/openmw/mwinput/actionmanager.cpp @@ -157,8 +157,8 @@ namespace MWInput void ActionManager::executeAction(int action) { MWBase::Environment::get().getLuaManager()->inputEvent({MWBase::LuaManager::InputEvent::Action, action}); - auto* inputManager = MWBase::Environment::get().getInputManager(); - auto* windowManager = MWBase::Environment::get().getWindowManager(); + const auto inputManager = MWBase::Environment::get().getInputManager(); + const auto windowManager = MWBase::Environment::get().getWindowManager(); // trigger action activated switch (action) { diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index 5b66c8b945..b66aa35368 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -200,7 +200,7 @@ namespace MWLua }; api["setConsoleSelectedObject"] = [luaManager=context.mLuaManager](const sol::object& obj) { - auto* wm = MWBase::Environment::get().getWindowManager(); + const auto wm = MWBase::Environment::get().getWindowManager(); if (obj == sol::nil) luaManager->addAction([wm]{ wm->setConsoleSelectedObject(MWWorld::Ptr()); }); else diff --git a/apps/openmw/mwmechanics/actors.cpp b/apps/openmw/mwmechanics/actors.cpp index 01c404c563..a659c30341 100644 --- a/apps/openmw/mwmechanics/actors.cpp +++ b/apps/openmw/mwmechanics/actors.cpp @@ -495,7 +495,7 @@ namespace MWMechanics getActorsSidingWith(actor1, allies1, cachedAllies); - auto* mechanicsManager = MWBase::Environment::get().getMechanicsManager(); + const auto mechanicsManager = MWBase::Environment::get().getMechanicsManager(); // If an ally of actor1 has been attacked by actor2 or has attacked actor2, start combat between actor1 and actor2 for (const MWWorld::Ptr& ally : allies1) { @@ -583,7 +583,7 @@ namespace MWMechanics } // Make guards go aggressive with creatures that are in combat, unless the creature is a follower or escorter - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); if (!aggressive && actor1.getClass().isClass(actor1, "Guard") && !actor2.getClass().isNpc() && creatureStats2.getAiSequence().isInCombat()) { // Check if the creature is too far @@ -917,7 +917,7 @@ namespace MWMechanics //If holding a light... if(heldIter.getType() == MWWorld::ContainerStore::Type_Light) { - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); // Use time from the player's light if(isPlayer) { @@ -974,8 +974,8 @@ namespace MWMechanics if (playerStats.isWerewolf()) return; - auto* mechanicsManager = MWBase::Environment::get().getMechanicsManager(); - auto* world = MWBase::Environment::get().getWorld(); + const auto mechanicsManager = MWBase::Environment::get().getMechanicsManager(); + const auto world = MWBase::Environment::get().getWorld(); if (actorClass.isClass(ptr, "Guard") && creatureStats.getAiSequence().isInPursuit() && !creatureStats.getAiSequence().isInCombat() && creatureStats.getMagicEffects().get(ESM::MagicEffect::CalmHumanoid).getMagnitude() == 0) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index 26baba30d4..209229efc5 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -207,7 +207,7 @@ std::string CharacterController::chooseRandomGroup (const std::string& prefix, i void CharacterController::refreshHitRecoilAnims(CharacterState& idle) { - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); auto& charClass = mPtr.getClass(); auto& stats = charClass.getCreatureStats(mPtr); bool recovery = stats.getHitRecovery(); @@ -1101,7 +1101,7 @@ void CharacterController::updateIdleStormState(bool inwater) return; } - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); if (world->isInStorm()) { osg::Vec3f stormDirection = world->getStormDirection(); @@ -1139,7 +1139,7 @@ bool CharacterController::updateCarriedLeftVisible(const int weaptype) const bool CharacterController::updateState(CharacterState idle) { - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); auto& prng = world->getPrng(); MWBase::SoundManager* sndMgr = MWBase::Environment::get().getSoundManager(); diff --git a/apps/openmw/mwphysics/physicssystem.cpp b/apps/openmw/mwphysics/physicssystem.cpp index 0dd177e4b8..1c1a2d6fbd 100644 --- a/apps/openmw/mwphysics/physicssystem.cpp +++ b/apps/openmw/mwphysics/physicssystem.cpp @@ -789,7 +789,7 @@ namespace MWPhysics void PhysicsSystem::moveActors() { auto* player = getActor(MWMechanics::getPlayer()); - auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); // copy new ptr position in temporary vector. player is handled separately as its movement might change active cell. std::vector> newPositions; diff --git a/apps/openmw/mwrender/landmanager.cpp b/apps/openmw/mwrender/landmanager.cpp index 6af1d9782c..2395eeab69 100644 --- a/apps/openmw/mwrender/landmanager.cpp +++ b/apps/openmw/mwrender/landmanager.cpp @@ -25,7 +25,7 @@ osg::ref_ptr LandManager::getLand(int x, int y) return static_cast(obj.get()); else { - const auto* world = MWBase::Environment::get().getWorld(); + const auto world = MWBase::Environment::get().getWorld(); if (!world) return nullptr; const ESM::Land* land = world->getStore().get().search(x,y); diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 944e947cd1..52b647add3 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -804,7 +804,7 @@ namespace MWRender } else if (mode == Render_Scene) { - auto* wm = MWBase::Environment::get().getWindowManager(); + const auto wm = MWBase::Environment::get().getWindowManager(); unsigned int mask = wm->getCullMask(); bool enabled = !(mask&sToggleWorldMask); if (enabled) diff --git a/apps/openmw/mwworld/player.cpp b/apps/openmw/mwworld/player.cpp index 64acf1c10f..dd5ad0c0f9 100644 --- a/apps/openmw/mwworld/player.cpp +++ b/apps/openmw/mwworld/player.cpp @@ -522,11 +522,11 @@ namespace MWWorld void Player::update() { auto player = getPlayer(); - auto* world = MWBase::Environment::get().getWorld(); - auto* rendering = world->getRenderingManager(); + const auto world = MWBase::Environment::get().getWorld(); + const auto rendering = world->getRenderingManager(); auto& store = world->getStore(); auto& playerClass = player.getClass(); - auto* windowMgr = MWBase::Environment::get().getWindowManager(); + const auto windowMgr = MWBase::Environment::get().getWindowManager(); if (player.getCell()->isExterior()) { diff --git a/components/misc/notnullptr.hpp b/components/misc/notnullptr.hpp new file mode 100644 index 0000000000..a7e02c3614 --- /dev/null +++ b/components/misc/notnullptr.hpp @@ -0,0 +1,33 @@ +#ifndef OPENMW_COMPONENTS_MISC_NOTNULLPTR_H +#define OPENMW_COMPONENTS_MISC_NOTNULLPTR_H + +#include +#include +#include + +namespace Misc +{ + template + class NotNullPtr + { + public: + NotNullPtr(T* value) + : mValue(value) + { + assert(mValue != nullptr); + } + + NotNullPtr(std::nullptr_t) = delete; + + operator T*() const { return mValue; } + + T* operator->() const { return mValue; } + + T& operator*() const { return *mValue; } + + private: + T* mValue; + }; +} + +#endif