From d003247120e43853eacd5a6c32ed4bd80993a2f8 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 27 Apr 2015 18:35:41 +0200 Subject: [PATCH] Provide usleep() in CSystemUtils interface --- src/app/app.cpp | 11 +++++------ src/app/system.h | 3 +++ src/app/system_linux.cpp | 6 ++++++ src/app/system_linux.h | 2 ++ src/app/system_macosx.cpp | 6 ++++++ src/app/system_macosx.h | 3 +++ src/app/system_other.cpp | 4 ++++ src/app/system_other.h | 2 ++ src/app/system_windows.cpp | 11 +++++++++++ src/app/system_windows.h | 2 ++ test/unit/app/system_mock.h | 1 + 11 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index b642cc02..5f98b4c0 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -52,7 +52,6 @@ #include #include -#include #include #include @@ -169,7 +168,7 @@ CApplication::~CApplication() { delete m_private; m_private = nullptr; - + delete m_input; m_input = nullptr; @@ -394,7 +393,7 @@ ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[]) std::string w, h; std::getline(resolution, w, 'x'); std::getline(resolution, h, 'x'); - + m_deviceConfig.size.x = atoi(w.c_str()); m_deviceConfig.size.y = atoi(h.c_str()); m_resolutionOverride = true; @@ -539,7 +538,7 @@ bool CApplication::Create() m_exitCode = 4; return false; } - + SDL_WM_SetCaption(m_windowTitle.c_str(), m_windowTitle.c_str()); } @@ -1009,7 +1008,7 @@ int CApplication::Run() if (m_lowCPU) { - usleep(20000); // should still give plenty of fps + GetSystemUtils()->Usleep(20000); // should still give plenty of fps } } } @@ -1134,7 +1133,7 @@ Event CApplication::ProcessSystemEvent() event.active.gain = m_private->currentEvent.active.gain == 1; } - + m_input->EventProcess(event); return event; diff --git a/src/app/system.h b/src/app/system.h index d52020b8..327f60dc 100644 --- a/src/app/system.h +++ b/src/app/system.h @@ -140,6 +140,9 @@ public: //! Returns the save dir location virtual std::string GetSaveDir(); + + //! Sleep for given amount of microseconds + virtual void Usleep(int usecs) = 0; }; //! Global function to get CSystemUtils instance diff --git a/src/app/system_linux.cpp b/src/app/system_linux.cpp index 6fbfb058..1a1b76d1 100644 --- a/src/app/system_linux.cpp +++ b/src/app/system_linux.cpp @@ -22,6 +22,7 @@ #include "common/logger.h" #include +#include void CSystemUtilsLinux::Init() @@ -123,3 +124,8 @@ std::string CSystemUtilsLinux::GetSaveDir() return savegameDir; } + +void CSystemUtilsLinux::Usleep(int usec) +{ + usleep(usec); +} diff --git a/src/app/system_linux.h b/src/app/system_linux.h index d8654f9c..b60954e8 100644 --- a/src/app/system_linux.h +++ b/src/app/system_linux.h @@ -50,6 +50,8 @@ public: virtual std::string GetSaveDir() OVERRIDE; + virtual void Usleep(int usec) OVERRIDE; + private: bool m_zenityAvailable; }; diff --git a/src/app/system_macosx.cpp b/src/app/system_macosx.cpp index 208b4a73..ebd1846a 100644 --- a/src/app/system_macosx.cpp +++ b/src/app/system_macosx.cpp @@ -22,6 +22,7 @@ #include "common/logger.h" #include +#include // MacOS-specific headers #include @@ -106,3 +107,8 @@ std::string CSystemUtilsMacOSX::GetSaveDir() return savegameDir; } + +void CSystemUtilsMacOSX::Usleep(int usec) +{ + usleep(usec); +} diff --git a/src/app/system_macosx.h b/src/app/system_macosx.h index f3d87345..a853ae42 100644 --- a/src/app/system_macosx.h +++ b/src/app/system_macosx.h @@ -33,6 +33,9 @@ public: virtual std::string GetDataPath() OVERRIDE; virtual std::string GetLangPath() OVERRIDE; virtual std::string GetSaveDir() OVERRIDE; + + virtual void Usleep(int usec) OVERRIDE; + private: std::string m_ASPath; std::string m_dataPath; diff --git a/src/app/system_other.cpp b/src/app/system_other.cpp index c21314c5..bedeb238 100644 --- a/src/app/system_other.cpp +++ b/src/app/system_other.cpp @@ -40,3 +40,7 @@ long long int CSystemUtilsOther::TimeStampExactDiff(SystemTimeStamp* before, Sys return (after->sdlTicks - before->sdlTicks) * 1000000ll; } +void CSystemUtilsOther::Usleep(int usec) +{ + SDL_Delay(usec / 1000); // close enough +} diff --git a/src/app/system_other.h b/src/app/system_other.h index db2d0f35..862f63ee 100644 --- a/src/app/system_other.h +++ b/src/app/system_other.h @@ -48,5 +48,7 @@ public: virtual void GetCurrentTimeStamp(SystemTimeStamp *stamp) OVERRIDE; virtual long long int GetTimeStampExactResolution() OVERRIDE; virtual long long TimeStampExactDiff(SystemTimeStamp *before, SystemTimeStamp *after) OVERRIDE; + + virtual void Usleep(int usec) OVERRIDE; }; diff --git a/src/app/system_windows.cpp b/src/app/system_windows.cpp index bc1fed90..e16b65a0 100644 --- a/src/app/system_windows.cpp +++ b/src/app/system_windows.cpp @@ -130,3 +130,14 @@ 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(NULL, TRUE, NULL); + SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); + WaitForSingleObject(timer, INFINITE); + CloseHandle(timer); +} diff --git a/src/app/system_windows.h b/src/app/system_windows.h index 538cfda9..e5141cba 100644 --- a/src/app/system_windows.h +++ b/src/app/system_windows.h @@ -48,6 +48,8 @@ public: virtual std::string GetSaveDir() OVERRIDE; + virtual void Usleep(int usec) OVERRIDE; + public: static std::string UTF8_Encode(const std::wstring &wstr); static std::wstring UTF8_Decode(const std::string &str); diff --git a/test/unit/app/system_mock.h b/test/unit/app/system_mock.h index 2f2c4640..ac7e5908 100644 --- a/test/unit/app/system_mock.h +++ b/test/unit/app/system_mock.h @@ -63,4 +63,5 @@ public: MOCK_METHOD0(GetTimeStampExactResolution, long long()); MOCK_METHOD3(TimeStampDiff, float(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit)); MOCK_METHOD2(TimeStampExactDiff, long long(SystemTimeStamp *before, SystemTimeStamp *after)); + MOCK_METHOD1(Usleep, void(int usec)); };