Files
squidout/deps/include/Box2D/Common/b2Timer.cpp
T
2015-09-09 18:04:17 +01:00

135 lines
3.0 KiB
C++

/*
* Copyright (c) 2011 Erin Catto http://box2d.org
* Copyright (c) 2014 Google, Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <Box2D/Common/b2Timer.h>
#if defined(_WIN32)
float64 b2Timer::s_invFrequency = 0.0f;
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef BOOL (WINAPI *SystemGetTimeFunc)(_Out_ LARGE_INTEGER *lpFrequency);
SystemGetTimeFunc systemGetTimeFunc = ::QueryPerformanceCounter;
SystemGetTimeFunc systemGetFreqFunc = ::QueryPerformanceFrequency;
int64 b2Timer::GetTicks()
{
LARGE_INTEGER largeInteger;
systemGetTimeFunc(&largeInteger);
return largeInteger.QuadPart;
}
b2Timer::b2Timer()
{
LARGE_INTEGER largeInteger;
if (s_invFrequency == 0.0f)
{
systemGetFreqFunc(&largeInteger);
s_invFrequency = float64(largeInteger.QuadPart);
if (s_invFrequency > 0.0f)
{
s_invFrequency = 1000.0f / s_invFrequency;
}
}
m_start = GetTicks();
}
void b2Timer::Reset()
{
m_start = GetTicks();
}
float32 b2Timer::GetMilliseconds() const
{
int64 elapsed = GetTicks() - m_start;
return (float32)(s_invFrequency * elapsed);
}
#elif defined(__linux__) || defined (__APPLE__)
#include <sys/time.h>
#include <time.h>
// systemGetTimeFunc is defined with external linkage to allow unit
// test to mock out the system time function
#if defined(__linux__)
typedef int (*SystemGetTimeFunc)(clockid_t clk_id, struct timespec *tp);
SystemGetTimeFunc systemGetTimeFunc = ::clock_gettime;
#elif defined(__APPLE__)
typedef int (*SystemGetTimeFunc)(struct timeval * tp, void * tzp);
SystemGetTimeFunc systemGetTimeFunc = ::gettimeofday;
#endif
int64 b2Timer::GetTicks()
{
static const int NSEC_PER_SEC = 1000000000;
#ifdef __linux__
timespec ts;
systemGetTimeFunc(CLOCK_MONOTONIC,&ts);
return ((int64)ts.tv_sec) * NSEC_PER_SEC + ts.tv_nsec;
#else
timeval t;
systemGetTimeFunc(&t, 0);
return ((int64)t.tv_sec) * NSEC_PER_SEC + t.tv_usec * 1000;
#endif
}
b2Timer::b2Timer()
{
Reset();
}
void b2Timer::Reset()
{
m_start = GetTicks();
}
float32 b2Timer::GetMilliseconds() const
{
static const float32 kTicksToMs = 0.000001f;
return kTicksToMs * (float32)(GetTicks() - m_start);
}
#else
b2Timer::b2Timer()
{
}
void b2Timer::Reset()
{
}
float32 b2Timer::GetMilliseconds() const
{
return 0.0f;
}
#endif