From e9e1c8d4dde704debe322d3c33224b3d90d6a13e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 7 Aug 2015 08:31:34 +0200 Subject: [PATCH] Add proper initializations and remove manual memory management in app classes --- src/app/app.cpp | 151 ++++++++++---------- src/app/app.h | 14 +- src/app/input.cpp | 1 + src/app/input.h | 4 +- src/app/main.cpp | 60 ++++---- src/app/system.cpp | 34 +++-- src/app/system.h | 20 ++- src/app/system_linux.h | 9 +- src/app/system_windows.h | 9 +- src/common/pathman.cpp | 11 +- src/common/pathman.h | 5 +- src/graphics/engine/engine.cpp | 26 ++-- src/graphics/engine/engine.h | 7 +- src/graphics/engine/text.cpp | 2 + test/unit/app/app_test.cpp | 22 ++- test/unit/graphics/engine/lightman_test.cpp | 15 +- 16 files changed, 194 insertions(+), 196 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 85199230..157b2728 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -100,11 +100,14 @@ struct ApplicationPrivate -CApplication::CApplication() - : m_private(MakeUnique()) - , m_configFile(MakeUnique()) - , m_input(MakeUnique()) - , m_pathManager(MakeUnique()) +CApplication::CApplication(CSystemUtils* systemUtils) + : m_systemUtils(systemUtils), + m_private(MakeUnique()), + m_configFile(MakeUnique()), + m_input(MakeUnique()), + m_pathManager(MakeUnique(systemUtils)), + m_performanceCounters(), + m_performanceCountersData() { m_exitCode = 0; m_active = false; @@ -128,14 +131,14 @@ CApplication::CApplication() m_absTime = 0.0f; m_relTime = 0.0f; - m_baseTimeStamp = GetSystemUtils()->CreateTimeStamp(); - m_curTimeStamp = GetSystemUtils()->CreateTimeStamp(); - m_lastTimeStamp = GetSystemUtils()->CreateTimeStamp(); + m_baseTimeStamp = m_systemUtils->CreateTimeStamp(); + m_curTimeStamp = m_systemUtils->CreateTimeStamp(); + m_lastTimeStamp = m_systemUtils->CreateTimeStamp(); for (int i = 0; i < PCNT_MAX; ++i) { - m_performanceCounters[i][0] = GetSystemUtils()->CreateTimeStamp(); - m_performanceCounters[i][1] = GetSystemUtils()->CreateTimeStamp(); + m_performanceCounters[i][0] = m_systemUtils->CreateTimeStamp(); + m_performanceCounters[i][1] = m_systemUtils->CreateTimeStamp(); } m_joystickEnabled = false; @@ -158,15 +161,51 @@ CApplication::CApplication() CApplication::~CApplication() { - GetSystemUtils()->DestroyTimeStamp(m_baseTimeStamp); - GetSystemUtils()->DestroyTimeStamp(m_curTimeStamp); - GetSystemUtils()->DestroyTimeStamp(m_lastTimeStamp); + m_systemUtils->DestroyTimeStamp(m_baseTimeStamp); + m_systemUtils->DestroyTimeStamp(m_curTimeStamp); + m_systemUtils->DestroyTimeStamp(m_lastTimeStamp); for (int i = 0; i < PCNT_MAX; ++i) { - GetSystemUtils()->DestroyTimeStamp(m_performanceCounters[i][0]); - GetSystemUtils()->DestroyTimeStamp(m_performanceCounters[i][1]); + m_systemUtils->DestroyTimeStamp(m_performanceCounters[i][0]); + m_systemUtils->DestroyTimeStamp(m_performanceCounters[i][1]); } + + m_joystickEnabled = false; + + m_controller.reset(); + m_sound.reset(); + + if (m_engine != nullptr) + { + m_engine->Destroy(); + + m_engine.reset(); + } + + if (m_device != nullptr) + { + m_device->Destroy(); + + m_device.reset(); + } + + if (m_private->joystick != nullptr) + { + SDL_JoystickClose(m_private->joystick); + m_private->joystick = nullptr; + } + + if (m_private->surface != nullptr) + { + SDL_FreeSurface(m_private->surface); + m_private->surface = nullptr; + } + + IMG_Quit(); + + if (SDL_WasInit(0)) + SDL_Quit(); } CEventQueue* CApplication::GetEventQueue() @@ -558,7 +597,7 @@ bool CApplication::Create() { GetLogger()->Error("Unknown graphics device: %s\n", m_graphics.c_str()); GetLogger()->Info("Changing to default device\n"); - GetSystemUtils()->SystemDialog(SDT_ERROR, "Graphics initialization error", "You have selected invalid graphics device with -graphics switch. Game will use default OpenGL device instead."); + m_systemUtils->SystemDialog(SDT_ERROR, "Graphics initialization error", "You have selected invalid graphics device with -graphics switch. Game will use default OpenGL device instead."); m_device = Gfx::CreateDevice(m_deviceConfig, "opengl"); } } @@ -575,7 +614,7 @@ bool CApplication::Create() } // Create the 3D engine - m_engine = MakeUnique(this); + m_engine = MakeUnique(this, m_systemUtils); m_engine->SetDevice(m_device.get()); @@ -654,44 +693,6 @@ bool CApplication::CreateVideoSurface() return true; } -void CApplication::Destroy() -{ - m_joystickEnabled = false; - - m_controller.reset(); - m_sound.reset(); - - if (m_engine != nullptr) - { - m_engine->Destroy(); - - m_engine.reset(); - } - - if (m_device != nullptr) - { - m_device->Destroy(); - - m_device.reset(); - } - - if (m_private->joystick != nullptr) - { - SDL_JoystickClose(m_private->joystick); - m_private->joystick = nullptr; - } - - if (m_private->surface != nullptr) - { - SDL_FreeSurface(m_private->surface); - m_private->surface = nullptr; - } - - IMG_Quit(); - - SDL_Quit(); -} - void CApplication::Restart() { m_restart = true; @@ -728,7 +729,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig) std::string(SDL_GetError()) + std::string("\n") + std::string("Previous mode will be restored"); GetLogger()->Error(error.c_str()); - GetSystemUtils()->SystemDialog( SDT_ERROR, "COLOBOT - Error", error); + m_systemUtils->SystemDialog( SDT_ERROR, "COLOBOT - Error", error); restore = true; ChangeVideoConfig(m_lastDeviceConfig); @@ -741,7 +742,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig) std::string error = std::string("SDL error while restoring previous video mode:\n") + std::string(SDL_GetError()); GetLogger()->Error(error.c_str()); - GetSystemUtils()->SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", error); + m_systemUtils->SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", error); // Fatal error, so post the quit event @@ -885,9 +886,9 @@ int CApplication::Run() { m_active = true; - GetSystemUtils()->GetCurrentTimeStamp(m_baseTimeStamp); - GetSystemUtils()->GetCurrentTimeStamp(m_lastTimeStamp); - GetSystemUtils()->GetCurrentTimeStamp(m_curTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_lastTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_curTimeStamp); MoveMouse(Math::Point(0.5f, 0.5f)); // center mouse on start @@ -1022,14 +1023,12 @@ int CApplication::Run() if (m_lowCPU) { - GetSystemUtils()->Usleep(20000); // should still give plenty of fps + m_systemUtils->Usleep(20000); // should still give plenty of fps } } } end: - Destroy(); - return m_exitCode; } @@ -1339,8 +1338,8 @@ void CApplication::ResetTimeAfterLoading() void CApplication::InternalResumeSimulation() { - GetSystemUtils()->GetCurrentTimeStamp(m_baseTimeStamp); - GetSystemUtils()->CopyTimeStamp(m_curTimeStamp, m_baseTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp); + m_systemUtils->CopyTimeStamp(m_curTimeStamp, m_baseTimeStamp); m_realAbsTimeBase = m_realAbsTime; m_absTimeBase = m_exactAbsTime; } @@ -1354,7 +1353,7 @@ void CApplication::SetSimulationSpeed(float speed) { m_simulationSpeed = speed; - GetSystemUtils()->GetCurrentTimeStamp(m_baseTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_baseTimeStamp); m_realAbsTimeBase = m_realAbsTime; m_absTimeBase = m_exactAbsTime; @@ -1366,12 +1365,12 @@ Event CApplication::CreateUpdateEvent() if (m_simulationSuspended) return Event(EVENT_NULL); - GetSystemUtils()->CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp); - GetSystemUtils()->GetCurrentTimeStamp(m_curTimeStamp); + m_systemUtils->CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp); + m_systemUtils->GetCurrentTimeStamp(m_curTimeStamp); - long long absDiff = GetSystemUtils()->TimeStampExactDiff(m_baseTimeStamp, m_curTimeStamp); + long long absDiff = m_systemUtils->TimeStampExactDiff(m_baseTimeStamp, m_curTimeStamp); long long newRealAbsTime = m_realAbsTimeBase + absDiff; - long long newRealRelTime = GetSystemUtils()->TimeStampExactDiff(m_lastTimeStamp, m_curTimeStamp); + long long newRealRelTime = m_systemUtils->TimeStampExactDiff(m_lastTimeStamp, m_curTimeStamp); if (newRealAbsTime < m_realAbsTime || newRealRelTime < 0) { @@ -1804,12 +1803,12 @@ bool CApplication::GetLowCPU() const void CApplication::StartPerformanceCounter(PerformanceCounter counter) { - GetSystemUtils()->GetCurrentTimeStamp(m_performanceCounters[counter][0]); + m_systemUtils->GetCurrentTimeStamp(m_performanceCounters[counter][0]); } void CApplication::StopPerformanceCounter(PerformanceCounter counter) { - GetSystemUtils()->GetCurrentTimeStamp(m_performanceCounters[counter][1]); + m_systemUtils->GetCurrentTimeStamp(m_performanceCounters[counter][1]); } float CApplication::GetPerformanceCounterData(PerformanceCounter counter) const @@ -1828,13 +1827,13 @@ void CApplication::ResetPerformanceCounters() void CApplication::UpdatePerformanceCountersData() { - long long sum = GetSystemUtils()->TimeStampExactDiff(m_performanceCounters[PCNT_ALL][0], - m_performanceCounters[PCNT_ALL][1]); + long long sum = m_systemUtils->TimeStampExactDiff(m_performanceCounters[PCNT_ALL][0], + m_performanceCounters[PCNT_ALL][1]); for (int i = 0; i < PCNT_MAX; ++i) { - long long diff = GetSystemUtils()->TimeStampExactDiff(m_performanceCounters[i][0], - m_performanceCounters[i][1]); + long long diff = m_systemUtils->TimeStampExactDiff(m_performanceCounters[i][0], + m_performanceCounters[i][1]); m_performanceCountersData[static_cast(i)] = static_cast(diff) / static_cast(sum); diff --git a/src/app/app.h b/src/app/app.h index 443677ce..4c318a47 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -31,8 +31,6 @@ #include "graphics/core/device.h" -#include "graphics/engine/engine.h" - #include "object/level_category.h" @@ -44,12 +42,13 @@ class CEventQueue; class CController; class CSoundInterface; class CInput; -class CObjectManager; class CPathManager; +class CSystemUtils; +struct SystemTimeStamp; namespace Gfx { -class CModelManager; +class CEngine; } /** @@ -190,7 +189,7 @@ class CApplication : public CSingleton { public: //! Constructor (can only be called once!) - CApplication(); + CApplication(CSystemUtils* systemUtils); //! Destructor ~CApplication(); @@ -212,9 +211,6 @@ public: //! Returns the message of error (set to something if exit code is not 0) const std::string& GetErrorMessage() const; - //! Cleans up before exit - void Destroy(); - //! Restart void Restart(); //! Should we restart after app quits? @@ -357,6 +353,8 @@ protected: void UpdatePerformanceCountersData(); protected: + //! System utils instance + CSystemUtils* m_systemUtils; //! Private (SDL-dependent data) std::unique_ptr m_private; //! Global event queue diff --git a/src/app/input.cpp b/src/app/input.cpp index 2db0f5ca..80ec7ec7 100644 --- a/src/app/input.cpp +++ b/src/app/input.cpp @@ -35,6 +35,7 @@ template<> CInput* CSingleton::m_instance = nullptr; CInput::CInput() + : m_keyPresses() { m_keyTable = { diff --git a/src/app/input.h b/src/app/input.h index 52d8c8c5..2282c45e 100644 --- a/src/app/input.h +++ b/src/app/input.h @@ -53,9 +53,9 @@ struct InputBinding struct JoyAxisBinding { //! Axis index or AXIS_INVALID - int axis; + int axis = 0; //! True to invert axis value - bool invert; + bool invert = false; }; //! Invalid value for axis binding (no axis assigned) diff --git a/src/app/main.cpp b/src/app/main.cpp index 8e327527..1d5a7061 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -31,7 +31,7 @@ #endif #include "common/logger.h" -#include "common/misc.h" +#include "common/make_unique.h" #include "common/restext.h" #include "common/resources/resourcemanager.h" @@ -40,6 +40,8 @@ #include #endif +#include +#include /* Doxygen main page */ @@ -91,20 +93,30 @@ int SDL_MAIN_FUNC(int argc, char *argv[]) // Workaround for character encoding in argv on Windows #if PLATFORM_WINDOWS - int wargc; + int wargc = 0; wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc); - if(wargv == nullptr) + if (wargv == nullptr) { logger.Error("CommandLineToArgvW failed\n"); return 1; } - argv = new char*[wargc]; - for(int i = 0; i < wargc; i++) { + + std::vector> windowsArgs; + for (int i = 0; i < wargc; i++) + { std::wstring warg = wargv[i]; std::string arg = CSystemUtilsWindows::UTF8_Encode(warg); - argv[i] = new char[arg.length()+1]; - strcpy(argv[i], arg.c_str()); + std::vector argVec(arg.begin(), arg.end()); + argVec.push_back('\0'); + windowsArgs.push_back(std::move(argVec)); } + + auto windowsArgvPtrs = MakeUniqueArray(wargc); + for (int i = 0; i < wargc; i++) + windowsArgvPtrs[i] = windowsArgs[i].data(); + + argv = windowsArgvPtrs.get(); + LocalFree(wargv); #endif @@ -119,50 +131,44 @@ int SDL_MAIN_FUNC(int argc, char *argv[]) int code = 0; while (true) { - CSystemUtils* systemUtils = CSystemUtils::Create(); // platform-specific utils + auto systemUtils = CSystemUtils::Create(); // platform-specific utils systemUtils->Init(); - CApplication* app = new CApplication(); // single instance of the application + CApplication app(systemUtils.get()); // single instance of the application - ParseArgsStatus status = app->ParseArguments(argc, argv); + ParseArgsStatus status = app.ParseArguments(argc, argv); if (status == PARSE_ARGS_FAIL) { systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", "Invalid commandline arguments!\n"); - return app->GetExitCode(); + return app.GetExitCode(); } else if (status == PARSE_ARGS_HELP) { - return app->GetExitCode(); + return app.GetExitCode(); } - if (! app->Create()) + if (! app.Create()) { - app->Destroy(); // ensure a clean exit - code = app->GetExitCode(); - if ( code != 0 && !app->GetErrorMessage().empty() ) + code = app.GetExitCode(); + if (code != 0 && !app.GetErrorMessage().empty()) { - systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", app->GetErrorMessage()); + systemUtils->SystemDialog(SDT_ERROR, "COLOBOT - Fatal Error", app.GetErrorMessage()); } logger.Info("Didn't run main loop. Exiting with code %d\n", code); return code; } - code = app->Run(); - bool restarting = app->IsRestarting(); + code = app.Run(); - delete app; - delete systemUtils; - if(!restarting) break; + bool restarting = app.IsRestarting(); + + if (!restarting) + break; } logger.Info("Exiting with code %d\n", code); - #if PLATFORM_WINDOWS - // See the workaround above - delete[] argv; - #endif - return code; } diff --git a/src/app/system.cpp b/src/app/system.cpp index 65461314..e5252fdf 100644 --- a/src/app/system.cpp +++ b/src/app/system.cpp @@ -22,6 +22,8 @@ #include "common/config.h" +#include "common/make_unique.h" + #if defined(PLATFORM_WINDOWS) #include "app/system_windows.h" @@ -37,25 +39,24 @@ #include -template<> -CSystemUtils* CSingleton::m_instance = nullptr; - - -CSystemUtils* CSystemUtils::Create() +std::unique_ptr CSystemUtils::Create() { - assert(m_instance == nullptr); + std::unique_ptr instance; #if defined(PLATFORM_WINDOWS) - m_instance = new CSystemUtilsWindows(); + instance = MakeUnique(); #elif defined(PLATFORM_LINUX) - m_instance = new CSystemUtilsLinux(); + instance = MakeUnique(); #elif defined(PLATFORM_MACOSX) - m_instance = new CSystemUtilsMacOSX(); + instance = MakeUnique(); #else - m_instance = new CSystemUtilsOther(); + instance = MakeUnique(); #endif - return m_instance; + return instance; } +CSystemUtils::~CSystemUtils() +{} + SystemDialogResult CSystemUtils::ConsoleSystemDialog(SystemDialogType type, const std::string& title, const std::string& message) { switch (type) @@ -144,12 +145,19 @@ SystemDialogResult CSystemUtils::ConsoleSystemDialog(SystemDialogType type, cons SystemTimeStamp* CSystemUtils::CreateTimeStamp() { - return new SystemTimeStamp(); + auto timeStamp = MakeUnique(); + SystemTimeStamp* timeStampPtr = timeStamp.get(); + m_timeStamps.push_back(std::move(timeStamp)); + return timeStampPtr; } void CSystemUtils::DestroyTimeStamp(SystemTimeStamp *stamp) { - delete stamp; + for (auto& timeStamp : m_timeStamps) + { + if (timeStamp.get() == stamp) + timeStamp.reset(); + } } void CSystemUtils::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src) diff --git a/src/app/system.h b/src/app/system.h index 74702de3..7539f253 100644 --- a/src/app/system.h +++ b/src/app/system.h @@ -24,9 +24,9 @@ #pragma once -#include "common/singleton.h" - +#include #include +#include /** * \enum SystemDialogType @@ -88,11 +88,13 @@ struct SystemTimeStamp; * This class provides system-specific utilities like displaying user dialogs and * querying system timers for exact timestamps. */ -class CSystemUtils : public CSingleton +class CSystemUtils { public: + virtual ~CSystemUtils(); + //! Creates system utils for specific platform - static CSystemUtils* Create(); + static std::unique_ptr Create(); //! Performs platform-specific initialization virtual void Init() = 0; @@ -134,11 +136,7 @@ public: //! Sleep for given amount of microseconds virtual void Usleep(int usecs) = 0; + +private: + std::vector> m_timeStamps; }; - -//! Global function to get CSystemUtils instance -inline CSystemUtils* GetSystemUtils() -{ - return CSystemUtils::GetInstancePointer(); -} - diff --git a/src/app/system_linux.h b/src/app/system_linux.h index e39e39dd..f90d2017 100644 --- a/src/app/system_linux.h +++ b/src/app/system_linux.h @@ -29,12 +29,7 @@ struct SystemTimeStamp { - timespec clockTime; - - SystemTimeStamp() - { - clockTime.tv_sec = clockTime.tv_nsec = 0; - } + timespec clockTime = {0, 0}; }; class CSystemUtilsLinux : public CSystemUtils @@ -52,6 +47,6 @@ public: virtual void Usleep(int usec) override; private: - bool m_zenityAvailable; + bool m_zenityAvailable = false; }; diff --git a/src/app/system_windows.h b/src/app/system_windows.h index 10230384..991d73ab 100644 --- a/src/app/system_windows.h +++ b/src/app/system_windows.h @@ -27,12 +27,7 @@ struct SystemTimeStamp { - long long counterValue; - - SystemTimeStamp() - { - counterValue = 0; - } + long long counterValue = 0; }; class CSystemUtilsWindows : public CSystemUtils @@ -54,6 +49,6 @@ public: static std::wstring UTF8_Decode(const std::string &str); protected: - long long m_counterFrequency; + long long m_counterFrequency = 0; }; diff --git a/src/common/pathman.cpp b/src/common/pathman.cpp index c6eaedee..f74bc127 100644 --- a/src/common/pathman.cpp +++ b/src/common/pathman.cpp @@ -41,19 +41,20 @@ template<> CPathManager* CSingleton::m_instance = nullptr; -CPathManager::CPathManager() +CPathManager::CPathManager(CSystemUtils* systemUtils) + : m_systemUtils(systemUtils) { #ifdef PORTABLE m_dataPath = "./data"; m_langPath = "./lang"; m_savePath = "./saves"; #else - m_dataPath = GetSystemUtils()->GetDataPath(); - m_langPath = GetSystemUtils()->GetLangPath(); + m_dataPath = m_systemUtils->GetDataPath(); + m_langPath = m_systemUtils->GetLangPath(); #ifdef DEV_BUILD m_savePath = "./saves"; #else - m_savePath = GetSystemUtils()->GetSaveDir(); + m_savePath = m_systemUtils->GetSaveDir(); #endif #endif } @@ -164,7 +165,7 @@ void CPathManager::LoadModsFromDir(const std::string &dir) #endif } } - catch(std::exception &e) + catch (std::exception &e) { GetLogger()->Warn("Unable to load mods from directory '%s': %s\n", dir.c_str(), e.what()); } diff --git a/src/common/pathman.h b/src/common/pathman.h index 7cf505a4..e42e9b4c 100644 --- a/src/common/pathman.h +++ b/src/common/pathman.h @@ -28,6 +28,8 @@ #include +class CSystemUtils; + /** * \class CPathManager * \brief Class for managing data/lang/save paths, and %something% replacements @@ -35,7 +37,7 @@ class CPathManager : public CSingleton { public: - CPathManager(); + CPathManager(CSystemUtils* systemUtils); ~CPathManager(); void SetDataPath(std::string dataPath); @@ -57,6 +59,7 @@ private: void LoadModsFromDir(const std::string &dir); private: + CSystemUtils* m_systemUtils; //! Data path std::string m_dataPath; //! Lang path diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 7eb00b60..e248cac2 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -22,6 +22,7 @@ #include "app/app.h" #include "app/input.h" +#include "app/system.h" #include "common/image.h" #include "common/key.h" @@ -62,15 +63,16 @@ template<> Gfx::CEngine* CSingleton::m_instance = nullptr; namespace Gfx { -CEngine::CEngine(CApplication *app) - : m_ambientColor(), +CEngine::CEngine(CApplication *app, CSystemUtils* systemUtils) + : m_app(app), + m_systemUtils(systemUtils), + m_ambientColor(), m_fogColor(), m_deepView(), m_fogStart(), m_highlightRank(), m_mice() { - m_app = app; m_device = nullptr; m_lightMan = nullptr; @@ -180,8 +182,8 @@ CEngine::CEngine(CApplication *app) m_mouseType = ENG_MOUSE_NORM; m_fpsCounter = 0; - m_lastFrameTime = GetSystemUtils()->CreateTimeStamp(); - m_currentFrameTime = GetSystemUtils()->CreateTimeStamp(); + m_lastFrameTime = m_systemUtils->CreateTimeStamp(); + m_currentFrameTime = m_systemUtils->CreateTimeStamp(); m_shadowColor = 0.5f; @@ -219,9 +221,9 @@ CEngine::~CEngine() m_terrain = nullptr; m_pause = nullptr; - GetSystemUtils()->DestroyTimeStamp(m_lastFrameTime); + m_systemUtils->DestroyTimeStamp(m_lastFrameTime); m_lastFrameTime = nullptr; - GetSystemUtils()->DestroyTimeStamp(m_currentFrameTime); + m_systemUtils->DestroyTimeStamp(m_currentFrameTime); m_currentFrameTime = nullptr; } @@ -334,8 +336,8 @@ bool CEngine::Create() params.mipmap = false; m_miceTexture = LoadTexture("textures/interface/mouse.png", params); - GetSystemUtils()->GetCurrentTimeStamp(m_currentFrameTime); - GetSystemUtils()->GetCurrentTimeStamp(m_lastFrameTime); + m_systemUtils->GetCurrentTimeStamp(m_currentFrameTime); + m_systemUtils->GetCurrentTimeStamp(m_lastFrameTime); return true; } @@ -436,11 +438,11 @@ void CEngine::FrameUpdate() { m_fpsCounter++; - GetSystemUtils()->GetCurrentTimeStamp(m_currentFrameTime); - float diff = GetSystemUtils()->TimeStampDiff(m_lastFrameTime, m_currentFrameTime, STU_SEC); + m_systemUtils->GetCurrentTimeStamp(m_currentFrameTime); + float diff = m_systemUtils->TimeStampDiff(m_lastFrameTime, m_currentFrameTime, STU_SEC); if (diff > 1.0f) { - GetSystemUtils()->CopyTimeStamp(m_lastFrameTime, m_currentFrameTime); + m_systemUtils->CopyTimeStamp(m_lastFrameTime, m_currentFrameTime); m_fps = m_fpsCounter / diff; m_fpsCounter = 0; diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index dbd59fce..ba02663d 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -24,8 +24,6 @@ #pragma once -#include "app/system.h" - #include "common/singleton.h" #include "graphics/core/color.h" @@ -52,6 +50,8 @@ class CObject; class CSoundInterface; class CImage; class CPauseManager; +class CSystemUtils; +struct SystemTimeStamp; struct Event; @@ -616,7 +616,7 @@ struct EngineMouse class CEngine : public CSingleton { public: - CEngine(CApplication* app); + CEngine(CApplication* app, CSystemUtils* systemUtils); ~CEngine(); //! Sets the device to be used @@ -1295,6 +1295,7 @@ protected: protected: CApplication* m_app; + CSystemUtils* m_systemUtils; CSoundInterface* m_sound; CDevice* m_device; std::unique_ptr m_modelManager; diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 80fc49aa..798cf8f3 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -22,6 +22,8 @@ #include "app/app.h" +#include "graphics/engine/engine.h" + #include "common/image.h" #include "common/logger.h" #include "common/stringutils.h" diff --git a/test/unit/app/app_test.cpp b/test/unit/app/app_test.cpp index 8df5620f..1b9da5df 100644 --- a/test/unit/app/app_test.cpp +++ b/test/unit/app/app_test.cpp @@ -19,13 +19,7 @@ #include "app/app.h" -#if defined(PLATFORM_WINDOWS) - #include "app/system_windows.h" -#elif defined(PLATFORM_LINUX) - #include "app/system_linux.h" -#else - #include "app/system_other.h" -#endif +#include "app/system_other.h" #include "common/make_unique.h" @@ -49,7 +43,8 @@ struct FakeSystemTimeStamp : public SystemTimeStamp class CApplicationWrapper : public CApplication { public: - CApplicationWrapper() + CApplicationWrapper(CSystemUtils* systemUtils) + : CApplication(systemUtils) { SDL_Init(0); m_eventQueue = MakeUnique(); @@ -97,6 +92,7 @@ protected: std::unique_ptr m_app; MockRepository m_mocks; CSystemUtils* m_systemUtils; + std::vector> m_timeStamps; private: int m_stampUid; @@ -106,7 +102,6 @@ private: void ApplicationUT::SetUp() { m_systemUtils = m_mocks.Mock(); - CSystemUtils::ReplaceInstance(m_systemUtils); m_mocks.OnCall(m_systemUtils, CSystemUtils::GetDataPath).Return(""); m_mocks.OnCall(m_systemUtils, CSystemUtils::GetLangPath).Return(""); @@ -118,23 +113,24 @@ void ApplicationUT::SetUp() m_mocks.OnCall(m_systemUtils, CSystemUtils::GetCurrentTimeStamp).Do(std::bind(&ApplicationUT::GetCurrentTimeStamp, this, ph::_1)); m_mocks.OnCall(m_systemUtils, CSystemUtils::TimeStampExactDiff).Do(std::bind(&ApplicationUT::TimeStampExactDiff, this, ph::_1, ph::_2)); - m_app.reset(new CApplicationWrapper()); + m_app = MakeUnique(m_systemUtils); } void ApplicationUT::TearDown() { m_app.reset(); - CSystemUtils::ReplaceInstance(nullptr); } SystemTimeStamp* ApplicationUT::CreateTimeStamp() { - return new FakeSystemTimeStamp(++m_stampUid); + auto stamp = MakeUnique(++m_stampUid); + auto stampPtr = stamp.get(); + m_timeStamps.push_back(std::move(stamp)); + return stampPtr; } void ApplicationUT::DestroyTimeStamp(SystemTimeStamp *stamp) { - delete static_cast(stamp); } void ApplicationUT::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src) diff --git a/test/unit/graphics/engine/lightman_test.cpp b/test/unit/graphics/engine/lightman_test.cpp index 736a79a3..35a688c5 100644 --- a/test/unit/graphics/engine/lightman_test.cpp +++ b/test/unit/graphics/engine/lightman_test.cpp @@ -16,8 +16,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://gnu.org/licenses */ + #include "graphics/engine/lightman.h" +#include "common/make_unique.h" + #include "graphics/core/device.h" #include @@ -34,7 +37,6 @@ class LightManagerUT : public testing::Test { protected: LightManagerUT() : - m_systemUtils(nullptr), m_engine(nullptr), m_device(nullptr) {} @@ -42,7 +44,6 @@ protected: {} void SetUp() override; - void TearDown() override; void PrepareLightTesting(int maxLights, Math::Vector eyePos); void CheckLightSorting(EngineObjectType objectType, const std::vector& expectedLights); @@ -53,7 +54,6 @@ protected: std::unique_ptr m_lightManager; MockRepository m_mocks; - CSystemUtils* m_systemUtils; CEngine* m_engine; CDevice* m_device; @@ -65,17 +65,10 @@ private: void LightManagerUT::SetUp() { - m_systemUtils = m_mocks.Mock(); - CSystemUtils::ReplaceInstance(m_systemUtils); m_engine = m_mocks.Mock(); m_device = m_mocks.Mock(); - m_lightManager.reset(new CLightManager(m_engine)); -} - -void LightManagerUT::TearDown() -{ - CSystemUtils::ReplaceInstance(nullptr); + m_lightManager = MakeUnique(m_engine); } void LightManagerUT::PrepareLightTesting(int maxLights, Math::Vector eyePos)