diff --git a/src/common/system/system.cpp b/src/common/system/system.cpp index 01b633cc..81d12640 100644 --- a/src/common/system/system.cpp +++ b/src/common/system/system.cpp @@ -37,6 +37,7 @@ #include #include #include +#include std::unique_ptr CSystemUtils::Create() @@ -161,6 +162,16 @@ void CSystemUtils::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src) *dst = *src; } +void CSystemUtils::GetCurrentTimeStamp(SystemTimeStamp *stamp) +{ + *stamp = std::chrono::high_resolution_clock::now(); +} + +long long CSystemUtils::TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) +{ + return std::chrono::duration_cast(*after - *before).count(); +} + float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit) { long long exact = TimeStampExactDiff(before, after); @@ -192,3 +203,8 @@ std::string CSystemUtils::GetSaveDir() { return std::string("saves"); } + +void CSystemUtils::Usleep(int usecs) +{ + std::this_thread::sleep_for(std::chrono::microseconds{usecs}); +} diff --git a/src/common/system/system.h b/src/common/system/system.h index aaf54954..4366d8c2 100644 --- a/src/common/system/system.h +++ b/src/common/system/system.h @@ -24,6 +24,7 @@ #pragma once +#include #include #include #include @@ -74,12 +75,7 @@ enum SystemTimeUnit STU_USEC }; -/* - * Forward declaration of time stamp struct - * SystemTimeStamp should only be used in a pointer context. - * The implementation details are hidden because of platform dependence. - */ -struct SystemTimeStamp; +using SystemTimeStamp = std::chrono::time_point; /** * \class CSystemUtils @@ -115,7 +111,7 @@ public: TEST_VIRTUAL void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src); //! Returns a time stamp associated with current time - virtual void GetCurrentTimeStamp(SystemTimeStamp *stamp) = 0; + TEST_VIRTUAL void GetCurrentTimeStamp(SystemTimeStamp *stamp); //! Returns a difference between two timestamps in given time unit /** The difference is \a after - \a before. */ @@ -123,7 +119,7 @@ public: //! Returns the exact (in nanosecond units) difference between two timestamps /** The difference is \a after - \a before. */ - virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) = 0; + virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after); //! Returns the data path (containing textures, levels, helpfiles, etc) virtual std::string GetDataPath(); @@ -135,7 +131,7 @@ public: virtual std::string GetSaveDir(); //! Sleep for given amount of microseconds - virtual void Usleep(int usecs) = 0; + virtual void Usleep(int usecs); private: std::vector> m_timeStamps; diff --git a/src/common/system/system_linux.cpp b/src/common/system/system_linux.cpp index 3cd25ed2..1c40a960 100644 --- a/src/common/system/system_linux.cpp +++ b/src/common/system/system_linux.cpp @@ -83,17 +83,6 @@ SystemDialogResult CSystemUtilsLinux::SystemDialog(SystemDialogType type, const return result; } -void CSystemUtilsLinux::GetCurrentTimeStamp(SystemTimeStamp *stamp) -{ - clock_gettime(CLOCK_MONOTONIC_RAW, &stamp->clockTime); -} - -long long CSystemUtilsLinux::TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) -{ - return (after->clockTime.tv_nsec - before->clockTime.tv_nsec) + - (after->clockTime.tv_sec - before->clockTime.tv_sec) * 1000000000ll; -} - std::string CSystemUtilsLinux::GetSaveDir() { std::string savegameDir; @@ -120,8 +109,3 @@ std::string CSystemUtilsLinux::GetSaveDir() return savegameDir; } - -void CSystemUtilsLinux::Usleep(int usec) -{ - usleep(usec); -} diff --git a/src/common/system/system_linux.h b/src/common/system/system_linux.h index f1576f31..23dc197f 100644 --- a/src/common/system/system_linux.h +++ b/src/common/system/system_linux.h @@ -28,11 +28,6 @@ //@colobot-lint-exclude UndefinedFunctionRule -struct SystemTimeStamp -{ - timespec clockTime = {0, 0}; -}; - class CSystemUtilsLinux : public CSystemUtils { public: @@ -40,13 +35,8 @@ public: SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) override; - void GetCurrentTimeStamp(SystemTimeStamp *stamp) override; - long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) override; - std::string GetSaveDir() override; - void Usleep(int usec) override; - private: bool m_zenityAvailable = false; }; diff --git a/src/common/system/system_macosx.cpp b/src/common/system/system_macosx.cpp index ede5b481..f41c0f29 100644 --- a/src/common/system/system_macosx.cpp +++ b/src/common/system/system_macosx.cpp @@ -107,8 +107,3 @@ std::string CSystemUtilsMacOSX::GetSaveDir() return savegameDir; } - -void CSystemUtilsMacOSX::Usleep(int usec) -{ - usleep(usec); -} diff --git a/src/common/system/system_macosx.h b/src/common/system/system_macosx.h index 5b572ec4..233a3c1d 100644 --- a/src/common/system/system_macosx.h +++ b/src/common/system/system_macosx.h @@ -36,8 +36,6 @@ public: std::string GetLangPath() override; std::string GetSaveDir() override; - void Usleep(int usec) override; - private: std::string m_ASPath; std::string m_dataPath; diff --git a/src/common/system/system_other.cpp b/src/common/system/system_other.cpp index a3311a74..91e61f84 100644 --- a/src/common/system/system_other.cpp +++ b/src/common/system/system_other.cpp @@ -28,18 +28,3 @@ SystemDialogResult CSystemUtilsOther::SystemDialog(SystemDialogType type, const { return ConsoleSystemDialog(type, title, message); } - -void CSystemUtilsOther::GetCurrentTimeStamp(SystemTimeStamp* stamp) -{ - stamp->sdlTicks = SDL_GetTicks(); -} - -long long int CSystemUtilsOther::TimeStampExactDiff(SystemTimeStamp* before, SystemTimeStamp* after) -{ - return (after->sdlTicks - before->sdlTicks) * 1000000ll; -} - -void CSystemUtilsOther::Usleep(int usec) -{ - SDL_Delay(usec / 1000); // close enough -} diff --git a/src/common/system/system_other.h b/src/common/system/system_other.h index ac80701b..ba87b842 100644 --- a/src/common/system/system_other.h +++ b/src/common/system/system_other.h @@ -30,26 +30,11 @@ //@colobot-lint-exclude UndefinedFunctionRule -struct SystemTimeStamp -{ - Uint32 sdlTicks; - - SystemTimeStamp() - { - sdlTicks = 0; - } -}; - class CSystemUtilsOther : public CSystemUtils { public: void Init() override; SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) override; - - void GetCurrentTimeStamp(SystemTimeStamp *stamp) override; - long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) override; - - void Usleep(int usec) override; }; //@end-colobot-lint-exclude diff --git a/src/common/system/system_windows.cpp b/src/common/system/system_windows.cpp index f51959b4..bb32283e 100644 --- a/src/common/system/system_windows.cpp +++ b/src/common/system/system_windows.cpp @@ -26,11 +26,6 @@ void CSystemUtilsWindows::Init() { - LARGE_INTEGER freq; - QueryPerformanceFrequency(&freq); - m_counterFrequency = freq.QuadPart; - - assert(m_counterFrequency != 0); } SystemDialogResult CSystemUtilsWindows::SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) @@ -76,19 +71,6 @@ SystemDialogResult CSystemUtilsWindows::SystemDialog(SystemDialogType type, cons return SDR_OK; } -void CSystemUtilsWindows::GetCurrentTimeStamp(SystemTimeStamp* stamp) -{ - LARGE_INTEGER value; - QueryPerformanceCounter(&value); - stamp->counterValue = value.QuadPart; -} - -long long int CSystemUtilsWindows::TimeStampExactDiff(SystemTimeStamp* before, SystemTimeStamp* after) -{ - float floatValue = static_cast(after->counterValue - before->counterValue) * (1e9 / static_cast(m_counterFrequency)); - return static_cast(floatValue); -} - //! Converts a wide Unicode string to an UTF8 string std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr) { @@ -125,14 +107,3 @@ std::string CSystemUtilsWindows::GetSaveDir() return savegameDir; } - -void CSystemUtilsWindows::Usleep(int usec) -{ - LARGE_INTEGER ft; - ft.QuadPart = -(10 * usec); // Convert to 100 nanosecond interval, negative value indicates relative time - - HANDLE timer = CreateWaitableTimer(nullptr, TRUE, nullptr); - SetWaitableTimer(timer, &ft, 0, nullptr, nullptr, 0); - WaitForSingleObject(timer, INFINITE); - CloseHandle(timer); -} diff --git a/src/common/system/system_windows.h b/src/common/system/system_windows.h index 74f02455..3d437a7c 100644 --- a/src/common/system/system_windows.h +++ b/src/common/system/system_windows.h @@ -26,11 +26,6 @@ //@colobot-lint-exclude UndefinedFunctionRule -struct SystemTimeStamp -{ - long long counterValue = 0; -}; - class CSystemUtilsWindows : public CSystemUtils { public: @@ -38,19 +33,11 @@ public: SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) override; - void GetCurrentTimeStamp(SystemTimeStamp *stamp) override; - long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) override; - std::string GetSaveDir() override; - void Usleep(int usec) override; - public: static std::string UTF8_Encode(const std::wstring &wstr); static std::wstring UTF8_Decode(const std::string &str); - -protected: - long long m_counterFrequency = 0; }; //@end-colobot-lint-exclude