Files
OpenRCT2-Web/src/core/Stopwatch.cpp
T

134 lines
2.4 KiB
C++
Raw Normal View History

2016-01-13 00:04:15 +00:00
#include <SDL_timer.h>
2016-01-08 20:50:23 +00:00
#include "Stopwatch.hpp"
uint64 Stopwatch::Frequency = 0;
Stopwatch::Stopwatch()
{
2016-01-23 18:58:31 +00:00
Reset();
2016-01-08 20:50:23 +00:00
}
2016-01-23 18:58:31 +00:00
uint64 Stopwatch::GetElapsedTicks() const
2016-01-08 20:50:23 +00:00
{
2016-01-23 18:58:31 +00:00
uint64 result = _total;
if (_isRunning)
{
uint64 ticks = QueryCurrentTicks();
if (ticks != 0) {
result += QueryCurrentTicks() - _last;
}
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
return _total;
2016-01-08 20:50:23 +00:00
}
2016-01-23 18:58:31 +00:00
uint64 Stopwatch::GetElapsedMilliseconds() const
2016-01-08 20:50:23 +00:00
{
2016-01-23 18:58:31 +00:00
if (Frequency == 0)
{
Frequency = QueryFrequency();
if (Frequency == 0)
{
return 0;
}
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
return (GetElapsedTicks() * 1000) / Frequency;
2016-01-08 20:50:23 +00:00
}
void Stopwatch::Reset()
{
2016-01-23 18:58:31 +00:00
_isRunning = false;
_total = 0;
_last = 0;
2016-01-08 20:50:23 +00:00
}
void Stopwatch::Start()
{
2016-01-23 18:58:31 +00:00
if (_isRunning) return;
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
uint64 ticks = QueryCurrentTicks();
if (ticks != 0)
{
_last = ticks;
_isRunning = true;
}
2016-01-08 20:50:23 +00:00
}
void Stopwatch::Restart()
{
2016-01-23 18:58:31 +00:00
Reset();
Start();
2016-01-08 20:50:23 +00:00
}
void Stopwatch::Stop()
{
2016-01-23 18:58:31 +00:00
uint64 ticks = QueryCurrentTicks();
if (ticks != 0)
{
_total += QueryCurrentTicks() - _last;
}
_isRunning = false;
2016-01-08 20:50:23 +00:00
}
uint64 Stopwatch::QueryFrequency()
{
2016-01-23 18:58:31 +00:00
return SDL_GetPerformanceFrequency();
2016-01-08 20:50:23 +00:00
}
uint64 Stopwatch::QueryCurrentTicks()
{
2016-01-23 18:58:31 +00:00
return SDL_GetPerformanceCounter();
2016-01-08 20:50:23 +00:00
}
2016-01-23 18:58:31 +00:00
extern "C"
{
#include "stopwatch.h"
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_create(stopwatch * stopwatch)
{
stopwatch->context = new Stopwatch();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_dispose(stopwatch * stopwatch)
{
delete ((Stopwatch*)stopwatch->context);
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
uint64 stopwatch_GetElapsedTicks(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->GetElapsedTicks();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
uint64 stopwatch_GetElapsedMilliseconds(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->GetElapsedMilliseconds();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_Reset(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->Reset();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_Start(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->Start();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_Restart(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->Restart();
}
2016-01-08 20:50:23 +00:00
2016-01-23 18:58:31 +00:00
void stopwatch_Stop(stopwatch * stopwatch)
{
Stopwatch * ctx = (Stopwatch*)stopwatch->context;
return ctx->Stop();
}
2016-01-08 20:50:23 +00:00
}