Provide usleep() in CSystemUtils interface

master
Piotr Dziwinski 2015-04-27 18:35:41 +02:00
parent dec12fb977
commit d003247120
11 changed files with 45 additions and 6 deletions

View File

@ -52,7 +52,6 @@
#include <stdlib.h>
#include <libintl.h>
#include <unistd.h>
#include <getopt.h>
#include <localename.h>
@ -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;

View File

@ -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

View File

@ -22,6 +22,7 @@
#include "common/logger.h"
#include <stdlib.h>
#include <unistd.h>
void CSystemUtilsLinux::Init()
@ -123,3 +124,8 @@ std::string CSystemUtilsLinux::GetSaveDir()
return savegameDir;
}
void CSystemUtilsLinux::Usleep(int usec)
{
usleep(usec);
}

View File

@ -50,6 +50,8 @@ public:
virtual std::string GetSaveDir() OVERRIDE;
virtual void Usleep(int usec) OVERRIDE;
private:
bool m_zenityAvailable;
};

View File

@ -22,6 +22,7 @@
#include "common/logger.h"
#include <stdlib.h>
#include <unistd.h>
// MacOS-specific headers
#include <CoreFoundation/CFBundle.h>
@ -106,3 +107,8 @@ std::string CSystemUtilsMacOSX::GetSaveDir()
return savegameDir;
}
void CSystemUtilsMacOSX::Usleep(int usec)
{
usleep(usec);
}

View File

@ -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;

View File

@ -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
}

View File

@ -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;
};

View File

@ -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);
}

View File

@ -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);

View File

@ -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));
};