mirror of
https://github.com/debauchee/barrier.git
synced 2026-05-09 07:22:21 +08:00
Refactored some platform dependent code into a new library,
lib/arch. This should make porting easier. Will probably continue to refactor a little more, moving platform dependent event handling stuff into lib/platform.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "CStopwatch.h"
|
||||
#include "CArch.h"
|
||||
|
||||
//
|
||||
// CStopwatch
|
||||
@@ -24,7 +25,7 @@ CStopwatch::CStopwatch(bool triggered) :
|
||||
m_stopped(triggered)
|
||||
{
|
||||
if (!triggered) {
|
||||
m_mark = getClock();
|
||||
m_mark = ARCH->time();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ CStopwatch::reset()
|
||||
return dt;
|
||||
}
|
||||
else {
|
||||
const double t = getClock();
|
||||
const double t = ARCH->time();
|
||||
const double dt = t - m_mark;
|
||||
m_mark = t;
|
||||
return dt;
|
||||
@@ -57,7 +58,7 @@ CStopwatch::stop()
|
||||
}
|
||||
|
||||
// save the elapsed time
|
||||
m_mark = getClock() - m_mark;
|
||||
m_mark = ARCH->time() - m_mark;
|
||||
m_stopped = true;
|
||||
}
|
||||
|
||||
@@ -70,7 +71,7 @@ CStopwatch::start()
|
||||
}
|
||||
|
||||
// set the mark such that it reports the time elapsed at stop()
|
||||
m_mark = getClock() - m_mark;
|
||||
m_mark = ARCH->time() - m_mark;
|
||||
m_stopped = false;
|
||||
}
|
||||
|
||||
@@ -93,7 +94,7 @@ CStopwatch::getTime()
|
||||
return m_mark;
|
||||
}
|
||||
else {
|
||||
return getClock() - m_mark;
|
||||
return ARCH->time() - m_mark;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +116,7 @@ CStopwatch::getTime() const
|
||||
return m_mark;
|
||||
}
|
||||
else {
|
||||
return getClock() - m_mark;
|
||||
return ARCH->time() - m_mark;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,100 +124,3 @@ CStopwatch::operator double() const
|
||||
{
|
||||
return getTime();
|
||||
}
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
|
||||
// avoid getting a lot a crap from mmsystem.h that we don't need
|
||||
#define MMNODRV // Installable driver support
|
||||
#define MMNOSOUND // Sound support
|
||||
#define MMNOWAVE // Waveform support
|
||||
#define MMNOMIDI // MIDI support
|
||||
#define MMNOAUX // Auxiliary audio support
|
||||
#define MMNOMIXER // Mixer support
|
||||
#define MMNOJOY // Joystick support
|
||||
#define MMNOMCI // MCI support
|
||||
#define MMNOMMIO // Multimedia file I/O support
|
||||
#define MMNOMMSYSTEM // General MMSYSTEM functions
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
|
||||
typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
|
||||
|
||||
static double s_freq = 0.0;
|
||||
static HINSTANCE s_mmInstance = NULL;
|
||||
static PTimeGetTime s_tgt = NULL;
|
||||
|
||||
//
|
||||
// initialize local variables
|
||||
//
|
||||
|
||||
class CStopwatchInit {
|
||||
public:
|
||||
CStopwatchInit();
|
||||
~CStopwatchInit();
|
||||
};
|
||||
static CStopwatchInit s_init;
|
||||
|
||||
CStopwatchInit::CStopwatchInit()
|
||||
{
|
||||
LARGE_INTEGER freq;
|
||||
if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
|
||||
s_freq = 1.0 / static_cast<double>(freq.QuadPart);
|
||||
}
|
||||
else {
|
||||
// load winmm.dll and get timeGetTime
|
||||
s_mmInstance = LoadLibrary("winmm");
|
||||
if (s_mmInstance) {
|
||||
s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CStopwatchInit::~CStopwatchInit()
|
||||
{
|
||||
if (s_mmInstance) {
|
||||
FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
|
||||
}
|
||||
}
|
||||
|
||||
double
|
||||
CStopwatch::getClock() const
|
||||
{
|
||||
// get time. we try three ways, in order of descending precision
|
||||
if (s_freq != 0.0) {
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter(&c);
|
||||
return s_freq * static_cast<double>(c.QuadPart);
|
||||
}
|
||||
else if (s_tgt) {
|
||||
return 0.001 * static_cast<double>(s_tgt());
|
||||
}
|
||||
else {
|
||||
return 0.001 * static_cast<double>(GetTickCount());
|
||||
}
|
||||
}
|
||||
|
||||
#elif UNIX_LIKE
|
||||
|
||||
#if TIME_WITH_SYS_TIME
|
||||
# include <sys/time.h>
|
||||
# include <time.h>
|
||||
#else
|
||||
# if HAVE_SYS_TIME_H
|
||||
# include <sys/time.h>
|
||||
# else
|
||||
# include <time.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
double
|
||||
CStopwatch::getClock() const
|
||||
{
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (double)t.tv_sec + 1.0e-6 * (double)t.tv_usec;
|
||||
}
|
||||
|
||||
#endif // UNIX_LIKE
|
||||
|
||||
Reference in New Issue
Block a user