From ebce27315ecc6ba64760462258c44e9c8f8adf93 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Sat, 1 Oct 2016 16:52:28 +0200 Subject: [PATCH] Moving entire timer code in timer.h --- src/Makefile.am | 2 +- src/lol/sys/timer.h | 70 ++++++++++++++++++++++++++++++---- src/sys/timer.cpp | 91 --------------------------------------------- 3 files changed, 64 insertions(+), 99 deletions(-) delete mode 100644 src/sys/timer.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 09d00929..c4d8c85a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -107,7 +107,7 @@ liblol_core_sources = \ mesh/mesh.cpp mesh/mesh.h \ mesh/primitivemesh.cpp mesh/primitivemesh.h \ \ - sys/init.cpp sys/timer.cpp sys/file.cpp sys/hacks.cpp \ + sys/init.cpp sys/file.cpp sys/hacks.cpp \ sys/thread.cpp sys/threadtypes.cpp sys/getopt.cpp \ \ image/image.cpp image/image-private.h image/kernel.cpp image/pixel.cpp \ diff --git a/src/lol/sys/timer.h b/src/lol/sys/timer.h index 19e1dc4d..e2285021 100644 --- a/src/lol/sys/timer.h +++ b/src/lol/sys/timer.h @@ -10,6 +10,9 @@ #pragma once +#include +#include + // // The Timer class // --------------- @@ -18,18 +21,71 @@ namespace lol { -class TimerData; +/* + * Timer implementation class + */ + +class TimerData +{ + friend class Timer; + +private: + TimerData() + { + (void)GetSeconds(true); + } + + float GetSeconds(bool reset) + { + std::chrono::steady_clock::time_point tp, tp0 = m_tp; + tp = std::chrono::steady_clock::now(); + if (reset) + m_tp = tp; + return std::chrono::duration_cast>(tp - tp0).count(); + } + + static void WaitSeconds(float seconds) + { + if (seconds > 0.0f) + std::this_thread::sleep_for(std::chrono::duration(seconds)); + } + + std::chrono::steady_clock::time_point m_tp; +}; class Timer { public: - Timer(); - ~Timer(); + Timer() + : data(new TimerData()) + { + } + + ~Timer() + { + delete data; + } + + void Reset() + { + (void)data->GetSeconds(true); + } + + float Get() + { + return data->GetSeconds(true); + } + + float Poll() + { + return data->GetSeconds(false); + } - void Reset(); - float Get(); - float Poll(); - void Wait(float seconds); + void Wait(float seconds) + { + float secs_elapsed = data->GetSeconds(false); + data->WaitSeconds(seconds - secs_elapsed); + } private: TimerData *data; diff --git a/src/sys/timer.cpp b/src/sys/timer.cpp deleted file mode 100644 index 0d54a73e..00000000 --- a/src/sys/timer.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// -// Lol Engine -// -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. -// - -#include - -#include -#include - -#include - -using namespace std::chrono; - -namespace lol -{ - -/* - * Timer implementation class - */ - -class TimerData -{ - friend class Timer; - -private: - TimerData() - { - (void)GetSeconds(true); - } - - float GetSeconds(bool reset) - { - steady_clock::time_point tp, tp0 = m_tp; - tp = steady_clock::now(); - if (reset) - m_tp = tp; - return duration_cast>(tp - tp0).count(); - } - - static void WaitSeconds(float seconds) - { - if (seconds > 0.0f) - std::this_thread::sleep_for(std::chrono::duration(seconds)); - } - - steady_clock::time_point m_tp; -}; - -/* - * Timer public class - */ - -Timer::Timer() - : data(new TimerData()) -{ -} - -Timer::~Timer() -{ - delete data; -} - -void Timer::Reset() -{ - (void)data->GetSeconds(true); -} - -float Timer::Get() -{ - return data->GetSeconds(true); -} - -float Timer::Poll() -{ - return data->GetSeconds(false); -} - -void Timer::Wait(float seconds) -{ - float secs_elapsed = data->GetSeconds(false); - data->WaitSeconds(seconds - secs_elapsed); -} - -} /* namespace lol */ -