diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index cd32cb3b0d..e0848eecf6 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -672,18 +672,18 @@ void OMW::Engine::setWindowIcon() void OMW::Engine::prepareEngine (Settings::Manager & settings) { mEnvironment.setStateManager ( - new MWState::StateManager (mCfgMgr.getUserDataPath() / "saves", mContentFiles)); + std::make_unique (mCfgMgr.getUserDataPath() / "saves", mContentFiles)); createWindow(settings); osg::ref_ptr rootNode (new osg::Group); mViewer->setSceneData(rootNode); - mVFS.reset(new VFS::Manager(mFSStrict)); + mVFS = std::make_unique(mFSStrict); VFS::registerArchives(mVFS.get(), mFileCollections, mArchives, true); - mResourceSystem.reset(new Resource::ResourceSystem(mVFS.get())); + mResourceSystem = std::make_unique(mVFS.get()); mResourceSystem->getSceneManager()->setUnRefImageDataAfterApply(false); // keep to Off for now to allow better state sharing mResourceSystem->getSceneManager()->setFilterSettings( Settings::Manager::getString("texture mag filter", "General"), @@ -712,8 +712,9 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) mViewer->addEventHandler(mScreenCaptureHandler); - mLuaManager = new MWLua::LuaManager(mVFS.get(), (mResDir / "lua_libs").string()); - mEnvironment.setLuaManager(mLuaManager); + auto luaMgr = std::make_unique(mVFS.get(), (mResDir / "lua_libs").string()); + mLuaManager = luaMgr.get(); + mEnvironment.setLuaManager(std::move(luaMgr)); // Create input and UI first to set up a bootstrapping environment for // showing a loading screen and keeping the window responsive while doing so @@ -784,33 +785,35 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) guiRoot->setName("GUI Root"); guiRoot->setNodeMask(MWRender::Mask_GUI); rootNode->addChild(guiRoot); - MWGui::WindowManager* window = new MWGui::WindowManager(mWindow, mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(), + + auto windowMgr = 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); - mEnvironment.setWindowManager (window); + auto* windowMgrInternal = windowMgr.get(); + mEnvironment.setWindowManager (std::move(windowMgr)); - MWInput::InputManager* input = new MWInput::InputManager (mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab); - mEnvironment.setInputManager (input); + auto inputMgr = std::make_unique(mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab); + mEnvironment.setInputManager (std::move(inputMgr)); // Create sound system - mEnvironment.setSoundManager (new MWSound::SoundManager(mVFS.get(), mUseSound)); + mEnvironment.setSoundManager (std::make_unique(mVFS.get(), mUseSound)); if (!mSkipMenu) { const std::string& logo = Fallback::Map::getString("Movies_Company_Logo"); if (!logo.empty()) - window->playVideo(logo, true); + mEnvironment.getWindowManager()->playVideo(logo, true); } // Create the world - mEnvironment.setWorld( new MWWorld::World (mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(), + mEnvironment.setWorld(std::make_unique(mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(), mFileCollections, mContentFiles, mGroundcoverFiles, mEncoder, mActivationDistanceOverride, mCellName, mStartupScript, mResDir.string(), mCfgMgr.getUserDataPath().string())); mEnvironment.getWorld()->setupPlayer(); - window->setStore(mEnvironment.getWorld()->getStore()); - window->initUI(); + windowMgrInternal->setStore(mEnvironment.getWorld()->getStore()); + windowMgrInternal->initUI(); //Load translation data mTranslationDataStorage.setEncoder(mEncoder); @@ -823,16 +826,15 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) mScriptContext = new MWScript::CompilerContext (MWScript::CompilerContext::Type_Full); mScriptContext->setExtensions (&mExtensions); - mEnvironment.setScriptManager (new MWScript::ScriptManager (mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode, + mEnvironment.setScriptManager (std::make_unique(mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode, mScriptBlacklistUse ? mScriptBlacklist : std::vector())); // Create game mechanics system - MWMechanics::MechanicsManager* mechanics = new MWMechanics::MechanicsManager; - mEnvironment.setMechanicsManager (mechanics); + mEnvironment.setMechanicsManager (std::make_unique()); // Create dialog system - mEnvironment.setJournal (new MWDialogue::Journal); - mEnvironment.setDialogueManager (new MWDialogue::DialogueManager (mExtensions, mTranslationDataStorage)); + mEnvironment.setJournal (std::make_unique()); + mEnvironment.setDialogueManager (std::make_unique(mExtensions, mTranslationDataStorage)); mEnvironment.setResourceSystem(mResourceSystem.get()); // scripts diff --git a/apps/openmw/mwbase/environment.cpp b/apps/openmw/mwbase/environment.cpp index 71940f67d2..1b212c73ab 100644 --- a/apps/openmw/mwbase/environment.cpp +++ b/apps/openmw/mwbase/environment.cpp @@ -18,68 +18,64 @@ MWBase::Environment *MWBase::Environment::sThis = nullptr; MWBase::Environment::Environment() -: mWorld (nullptr), mSoundManager (nullptr), mScriptManager (nullptr), mWindowManager (nullptr), - mMechanicsManager (nullptr), mDialogueManager (nullptr), mJournal (nullptr), mInputManager (nullptr), - mStateManager (nullptr), mLuaManager (nullptr), mResourceSystem (nullptr), mFrameDuration (0), mFrameRateLimit(0.f) { - assert (!sThis); + assert(!sThis); sThis = this; } MWBase::Environment::~Environment() { - cleanup(); sThis = nullptr; } -void MWBase::Environment::setWorld (World *world) +void MWBase::Environment::setWorld (std::unique_ptr&& world) { - mWorld = world; + mWorld = std::move(world); } -void MWBase::Environment::setSoundManager (SoundManager *soundManager) +void MWBase::Environment::setSoundManager (std::unique_ptr&& soundManager) { - mSoundManager = soundManager; + mSoundManager = std::move(soundManager); } -void MWBase::Environment::setScriptManager (ScriptManager *scriptManager) +void MWBase::Environment::setScriptManager (std::unique_ptr&& scriptManager) { - mScriptManager = scriptManager; + mScriptManager = std::move(scriptManager); } -void MWBase::Environment::setWindowManager (WindowManager *windowManager) +void MWBase::Environment::setWindowManager (std::unique_ptr&& windowManager) { - mWindowManager = windowManager; + mWindowManager = std::move(windowManager); } -void MWBase::Environment::setMechanicsManager (MechanicsManager *mechanicsManager) +void MWBase::Environment::setMechanicsManager (std::unique_ptr&& mechanicsManager) { - mMechanicsManager = mechanicsManager; + mMechanicsManager = std::move(mechanicsManager); } -void MWBase::Environment::setDialogueManager (DialogueManager *dialogueManager) +void MWBase::Environment::setDialogueManager (std::unique_ptr&& dialogueManager) { - mDialogueManager = dialogueManager; + mDialogueManager = std::move(dialogueManager); } -void MWBase::Environment::setJournal (Journal *journal) +void MWBase::Environment::setJournal (std::unique_ptr&& journal) { - mJournal = journal; + mJournal = std::move(journal); } -void MWBase::Environment::setInputManager (InputManager *inputManager) +void MWBase::Environment::setInputManager (std::unique_ptr&& inputManager) { - mInputManager = inputManager; + mInputManager = std::move(inputManager); } -void MWBase::Environment::setStateManager (StateManager *stateManager) +void MWBase::Environment::setStateManager (std::unique_ptr&& stateManager) { - mStateManager = stateManager; + mStateManager = std::move(stateManager); } -void MWBase::Environment::setLuaManager (LuaManager *luaManager) +void MWBase::Environment::setLuaManager (std::unique_ptr&& luaManager) { - mLuaManager = luaManager; + mLuaManager = std::move(luaManager); } void MWBase::Environment::setResourceSystem (Resource::ResourceSystem *resourceSystem) @@ -105,61 +101,61 @@ float MWBase::Environment::getFrameRateLimit() const MWBase::World *MWBase::Environment::getWorld() const { assert (mWorld); - return mWorld; + return mWorld.get(); } MWBase::SoundManager *MWBase::Environment::getSoundManager() const { assert (mSoundManager); - return mSoundManager; + return mSoundManager.get(); } MWBase::ScriptManager *MWBase::Environment::getScriptManager() const { assert (mScriptManager); - return mScriptManager; + return mScriptManager.get(); } MWBase::WindowManager *MWBase::Environment::getWindowManager() const { assert (mWindowManager); - return mWindowManager; + return mWindowManager.get(); } MWBase::MechanicsManager *MWBase::Environment::getMechanicsManager() const { assert (mMechanicsManager); - return mMechanicsManager; + return mMechanicsManager.get(); } MWBase::DialogueManager *MWBase::Environment::getDialogueManager() const { assert (mDialogueManager); - return mDialogueManager; + return mDialogueManager.get(); } MWBase::Journal *MWBase::Environment::getJournal() const { assert (mJournal); - return mJournal; + return mJournal.get(); } MWBase::InputManager *MWBase::Environment::getInputManager() const { assert (mInputManager); - return mInputManager; + return mInputManager.get(); } MWBase::StateManager *MWBase::Environment::getStateManager() const { assert (mStateManager); - return mStateManager; + return mStateManager.get(); } MWBase::LuaManager *MWBase::Environment::getLuaManager() const { assert (mLuaManager); - return mLuaManager; + return mLuaManager.get(); } Resource::ResourceSystem *MWBase::Environment::getResourceSystem() const @@ -174,35 +170,17 @@ float MWBase::Environment::getFrameDuration() const void MWBase::Environment::cleanup() { - delete mMechanicsManager; - mMechanicsManager = nullptr; - - delete mDialogueManager; - mDialogueManager = nullptr; - - delete mJournal; - mJournal = nullptr; - - delete mScriptManager; - mScriptManager = nullptr; - - delete mWindowManager; - mWindowManager = nullptr; - - delete mWorld; - mWorld = nullptr; - - delete mSoundManager; - mSoundManager = nullptr; - - delete mInputManager; - mInputManager = nullptr; - - delete mStateManager; - mStateManager = nullptr; - - delete mLuaManager; - mLuaManager = nullptr; + mMechanicsManager.reset(); + mDialogueManager.reset(); + mJournal.reset(); + mScriptManager.reset(); + mWindowManager.reset(); + mWorld.reset(); + mSoundManager.reset(); + mInputManager.reset(); + mStateManager.reset(); + mLuaManager.reset(); + mResourceSystem = nullptr; } const MWBase::Environment& MWBase::Environment::get() diff --git a/apps/openmw/mwbase/environment.hpp b/apps/openmw/mwbase/environment.hpp index b2600e7dd5..00af5d2869 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 + namespace osg { class Stats; @@ -28,25 +30,23 @@ namespace MWBase /// /// This class allows each mw-subsystem to access any others subsystem's top-level manager class. /// - /// \attention Environment takes ownership of the manager class instances it is handed over in - /// the set* functions. class Environment { static Environment *sThis; - World *mWorld; - SoundManager *mSoundManager; - ScriptManager *mScriptManager; - WindowManager *mWindowManager; - MechanicsManager *mMechanicsManager; - DialogueManager *mDialogueManager; - Journal *mJournal; - InputManager *mInputManager; - StateManager *mStateManager; - LuaManager *mLuaManager; - Resource::ResourceSystem *mResourceSystem; - float mFrameDuration; - float mFrameRateLimit; + 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 @@ -60,25 +60,25 @@ namespace MWBase ~Environment(); - void setWorld (World *world); + void setWorld (std::unique_ptr&& world); - void setSoundManager (SoundManager *soundManager); + void setSoundManager (std::unique_ptr&& soundManager); - void setScriptManager (MWBase::ScriptManager *scriptManager); + void setScriptManager (std::unique_ptr&& scriptManager); - void setWindowManager (WindowManager *windowManager); + void setWindowManager (std::unique_ptr&& windowManager); - void setMechanicsManager (MechanicsManager *mechanicsManager); + void setMechanicsManager (std::unique_ptr&& mechanicsManager); - void setDialogueManager (DialogueManager *dialogueManager); + void setDialogueManager (std::unique_ptr&& dialogueManager); - void setJournal (Journal *journal); + void setJournal (std::unique_ptr&& journal); - void setInputManager (InputManager *inputManager); + void setInputManager (std::unique_ptr&& inputManager); - void setStateManager (StateManager *stateManager); + void setStateManager (std::unique_ptr&& stateManager); - void setLuaManager (LuaManager *luaManager); + void setLuaManager (std::unique_ptr&& luaManager); void setResourceSystem (Resource::ResourceSystem *resourceSystem);