Browse Source

Moving entire timer code in timer.h

legacy
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 8 years ago
parent
commit
ebce27315e
3 changed files with 64 additions and 99 deletions
  1. +1
    -1
      src/Makefile.am
  2. +63
    -7
      src/lol/sys/timer.h
  3. +0
    -91
      src/sys/timer.cpp

+ 1
- 1
src/Makefile.am View File

@@ -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 \


+ 63
- 7
src/lol/sys/timer.h View File

@@ -10,6 +10,9 @@

#pragma once

#include <chrono>
#include <thread>

//
// 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<std::chrono::duration<float>>(tp - tp0).count();
}

static void WaitSeconds(float seconds)
{
if (seconds > 0.0f)
std::this_thread::sleep_for(std::chrono::duration<float>(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;


+ 0
- 91
src/sys/timer.cpp View File

@@ -1,91 +0,0 @@
//
// 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.
//

#include <lol/engine-internal.h>

#include <cstdlib>
#include <stdint.h>

#include <chrono>

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<duration<float>>(tp - tp0).count();
}

static void WaitSeconds(float seconds)
{
if (seconds > 0.0f)
std::this_thread::sleep_for(std::chrono::duration<float>(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 */


Loading…
Cancel
Save