Remove platform-specific handling of times
Now all platforms use std::chrono::high_resolution_clock and std::this_thread::sleep_for instead of platform-specific timestamp and sleep methods.fix-squashed-planets
parent
2a003a27b1
commit
285350464f
|
@ -37,6 +37,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
std::unique_ptr<CSystemUtils> CSystemUtils::Create()
|
std::unique_ptr<CSystemUtils> CSystemUtils::Create()
|
||||||
|
@ -161,6 +162,16 @@ void CSystemUtils::CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src)
|
||||||
*dst = *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<std::chrono::nanoseconds>(*after - *before).count();
|
||||||
|
}
|
||||||
|
|
||||||
float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit)
|
float CSystemUtils::TimeStampDiff(SystemTimeStamp *before, SystemTimeStamp *after, SystemTimeUnit unit)
|
||||||
{
|
{
|
||||||
long long exact = TimeStampExactDiff(before, after);
|
long long exact = TimeStampExactDiff(before, after);
|
||||||
|
@ -192,3 +203,8 @@ std::string CSystemUtils::GetSaveDir()
|
||||||
{
|
{
|
||||||
return std::string("saves");
|
return std::string("saves");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSystemUtils::Usleep(int usecs)
|
||||||
|
{
|
||||||
|
std::this_thread::sleep_for(std::chrono::microseconds{usecs});
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -74,12 +75,7 @@ enum SystemTimeUnit
|
||||||
STU_USEC
|
STU_USEC
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
using SystemTimeStamp = std::chrono::time_point<std::chrono::high_resolution_clock>;
|
||||||
* 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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class CSystemUtils
|
* \class CSystemUtils
|
||||||
|
@ -115,7 +111,7 @@ public:
|
||||||
TEST_VIRTUAL void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src);
|
TEST_VIRTUAL void CopyTimeStamp(SystemTimeStamp *dst, SystemTimeStamp *src);
|
||||||
|
|
||||||
//! Returns a time stamp associated with current time
|
//! 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
|
//! Returns a difference between two timestamps in given time unit
|
||||||
/** The difference is \a after - \a before. */
|
/** The difference is \a after - \a before. */
|
||||||
|
@ -123,7 +119,7 @@ public:
|
||||||
|
|
||||||
//! Returns the exact (in nanosecond units) difference between two timestamps
|
//! Returns the exact (in nanosecond units) difference between two timestamps
|
||||||
/** The difference is \a after - \a before. */
|
/** 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)
|
//! Returns the data path (containing textures, levels, helpfiles, etc)
|
||||||
virtual std::string GetDataPath();
|
virtual std::string GetDataPath();
|
||||||
|
@ -135,7 +131,7 @@ public:
|
||||||
virtual std::string GetSaveDir();
|
virtual std::string GetSaveDir();
|
||||||
|
|
||||||
//! Sleep for given amount of microseconds
|
//! Sleep for given amount of microseconds
|
||||||
virtual void Usleep(int usecs) = 0;
|
virtual void Usleep(int usecs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<SystemTimeStamp>> m_timeStamps;
|
std::vector<std::unique_ptr<SystemTimeStamp>> m_timeStamps;
|
||||||
|
|
|
@ -83,17 +83,6 @@ SystemDialogResult CSystemUtilsLinux::SystemDialog(SystemDialogType type, const
|
||||||
return result;
|
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 CSystemUtilsLinux::GetSaveDir()
|
||||||
{
|
{
|
||||||
std::string savegameDir;
|
std::string savegameDir;
|
||||||
|
@ -120,8 +109,3 @@ std::string CSystemUtilsLinux::GetSaveDir()
|
||||||
|
|
||||||
return savegameDir;
|
return savegameDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSystemUtilsLinux::Usleep(int usec)
|
|
||||||
{
|
|
||||||
usleep(usec);
|
|
||||||
}
|
|
||||||
|
|
|
@ -28,11 +28,6 @@
|
||||||
|
|
||||||
//@colobot-lint-exclude UndefinedFunctionRule
|
//@colobot-lint-exclude UndefinedFunctionRule
|
||||||
|
|
||||||
struct SystemTimeStamp
|
|
||||||
{
|
|
||||||
timespec clockTime = {0, 0};
|
|
||||||
};
|
|
||||||
|
|
||||||
class CSystemUtilsLinux : public CSystemUtils
|
class CSystemUtilsLinux : public CSystemUtils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -40,13 +35,8 @@ public:
|
||||||
|
|
||||||
SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) 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;
|
|
||||||
|
|
||||||
std::string GetSaveDir() override;
|
std::string GetSaveDir() override;
|
||||||
|
|
||||||
void Usleep(int usec) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_zenityAvailable = false;
|
bool m_zenityAvailable = false;
|
||||||
};
|
};
|
||||||
|
|
|
@ -107,8 +107,3 @@ std::string CSystemUtilsMacOSX::GetSaveDir()
|
||||||
|
|
||||||
return savegameDir;
|
return savegameDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSystemUtilsMacOSX::Usleep(int usec)
|
|
||||||
{
|
|
||||||
usleep(usec);
|
|
||||||
}
|
|
||||||
|
|
|
@ -36,8 +36,6 @@ public:
|
||||||
std::string GetLangPath() override;
|
std::string GetLangPath() override;
|
||||||
std::string GetSaveDir() override;
|
std::string GetSaveDir() override;
|
||||||
|
|
||||||
void Usleep(int usec) override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_ASPath;
|
std::string m_ASPath;
|
||||||
std::string m_dataPath;
|
std::string m_dataPath;
|
||||||
|
|
|
@ -28,18 +28,3 @@ SystemDialogResult CSystemUtilsOther::SystemDialog(SystemDialogType type, const
|
||||||
{
|
{
|
||||||
return ConsoleSystemDialog(type, title, message);
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -30,26 +30,11 @@
|
||||||
|
|
||||||
//@colobot-lint-exclude UndefinedFunctionRule
|
//@colobot-lint-exclude UndefinedFunctionRule
|
||||||
|
|
||||||
struct SystemTimeStamp
|
|
||||||
{
|
|
||||||
Uint32 sdlTicks;
|
|
||||||
|
|
||||||
SystemTimeStamp()
|
|
||||||
{
|
|
||||||
sdlTicks = 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class CSystemUtilsOther : public CSystemUtils
|
class CSystemUtilsOther : public CSystemUtils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Init() override;
|
void Init() override;
|
||||||
SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) 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
|
//@end-colobot-lint-exclude
|
||||||
|
|
|
@ -26,11 +26,6 @@
|
||||||
|
|
||||||
void CSystemUtilsWindows::Init()
|
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)
|
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;
|
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<double>(after->counterValue - before->counterValue) * (1e9 / static_cast<double>(m_counterFrequency));
|
|
||||||
return static_cast<long long>(floatValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
//! Converts a wide Unicode string to an UTF8 string
|
//! Converts a wide Unicode string to an UTF8 string
|
||||||
std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr)
|
std::string CSystemUtilsWindows::UTF8_Encode(const std::wstring& wstr)
|
||||||
{
|
{
|
||||||
|
@ -125,14 +107,3 @@ std::string CSystemUtilsWindows::GetSaveDir()
|
||||||
|
|
||||||
return savegameDir;
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -26,11 +26,6 @@
|
||||||
|
|
||||||
//@colobot-lint-exclude UndefinedFunctionRule
|
//@colobot-lint-exclude UndefinedFunctionRule
|
||||||
|
|
||||||
struct SystemTimeStamp
|
|
||||||
{
|
|
||||||
long long counterValue = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CSystemUtilsWindows : public CSystemUtils
|
class CSystemUtilsWindows : public CSystemUtils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -38,19 +33,11 @@ public:
|
||||||
|
|
||||||
SystemDialogResult SystemDialog(SystemDialogType type, const std::string& title, const std::string& message) 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;
|
|
||||||
|
|
||||||
std::string GetSaveDir() override;
|
std::string GetSaveDir() override;
|
||||||
|
|
||||||
void Usleep(int usec) override;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::string UTF8_Encode(const std::wstring &wstr);
|
static std::string UTF8_Encode(const std::wstring &wstr);
|
||||||
static std::wstring UTF8_Decode(const std::string &str);
|
static std::wstring UTF8_Decode(const std::string &str);
|
||||||
|
|
||||||
protected:
|
|
||||||
long long m_counterFrequency = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//@end-colobot-lint-exclude
|
//@end-colobot-lint-exclude
|
||||||
|
|
Loading…
Reference in New Issue