Selaa lähdekoodia

sys: minor tweaks to the Timer class

legacy
Sam Hocevar 9 vuotta sitten
vanhempi
commit
9b6e2995fb
1 muutettua tiedostoa jossa 24 lisäystä ja 34 poistoa
  1. +24
    -34
      src/lol/sys/timer.h

+ 24
- 34
src/lol/sys/timer.h Näytä tiedosto

@@ -1,11 +1,14 @@
// //
// Lol Engine
// Lol Engine
// //
// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
// 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.
// Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
// © 2016 Guillaume Bittoun <guillaume.bittoun@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. 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 the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
// //


#pragma once #pragma once
@@ -21,63 +24,50 @@
namespace lol namespace lol
{ {


/*
* Timer implementation class
*/

class Timer class Timer
{ {
private:

float GetSeconds(bool reset)
{
std::chrono::steady_clock::time_point
tp = std::chrono::steady_clock::now(),
tp0 = m_tp;

if (reset)
m_tp = tp;

return std::chrono::duration_cast<std::chrono::duration<float>>(tp - tp0).count();
}

public: public:
Timer() Timer()
: m_tp()
{
(void)GetSeconds(true);
}

~Timer()
{ {
(void)get_seconds(true);
} }


void Reset() void Reset()
{ {
(void)GetSeconds(true);
(void)get_seconds(true);
} }


float Get() float Get()
{ {
return GetSeconds(true);
return get_seconds(true);
} }


float Poll() float Poll()
{ {
return GetSeconds(false);
return get_seconds(false);
} }


void Wait(float seconds) void Wait(float seconds)
{ {
if (seconds > 0.0f) if (seconds > 0.0f)
{ {
float secs_elapsed = GetSeconds(false);
float secs_elapsed = get_seconds(false);
std::this_thread::sleep_for(std::chrono::duration<float>(seconds - secs_elapsed)); std::this_thread::sleep_for(std::chrono::duration<float>(seconds - secs_elapsed));
} }
} }


private: private:
std::chrono::steady_clock::time_point m_tp; std::chrono::steady_clock::time_point m_tp;

float get_seconds(bool reset)
{
auto tp = std::chrono::steady_clock::now(), tp0 = m_tp;

if (reset)
m_tp = tp;

return std::chrono::duration_cast<std::chrono::duration<float>>(tp - tp0).count();
}
}; };


} /* namespace lol */ } /* namespace lol */


Ladataan…
Peruuta
Tallenna