mirror of https://github.com/auygun/kaliber.git
Update timer.h
Remove Timer class. Add ElapsedTimer and DeltaTimer classes that use std::chrono::high_resolution_clock
This commit is contained in:
parent
10823cd459
commit
96d6a52a74
|
@ -51,7 +51,6 @@ add_library(kaliber SHARED
|
||||||
../../../src/base/log.cc
|
../../../src/base/log.cc
|
||||||
../../../src/base/task_runner.cc
|
../../../src/base/task_runner.cc
|
||||||
../../../src/base/thread_pool.cc
|
../../../src/base/thread_pool.cc
|
||||||
../../../src/base/timer.cc
|
|
||||||
../../../src/demo/credits.cc
|
../../../src/demo/credits.cc
|
||||||
../../../src/demo/demo.cc
|
../../../src/demo/demo.cc
|
||||||
../../../src/demo/enemy.cc
|
../../../src/demo/enemy.cc
|
||||||
|
|
|
@ -15,7 +15,6 @@ source_set("base") {
|
||||||
"task_runner.h",
|
"task_runner.h",
|
||||||
"thread_pool.cc",
|
"thread_pool.cc",
|
||||||
"thread_pool.h",
|
"thread_pool.h",
|
||||||
"timer.cc",
|
|
||||||
"timer.h",
|
"timer.h",
|
||||||
"vecmath.h",
|
"vecmath.h",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
#include "base/timer.h"
|
|
||||||
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
namespace base {
|
|
||||||
|
|
||||||
Timer::Timer() {
|
|
||||||
Reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Timer::Reset() {
|
|
||||||
gettimeofday(&last_time_, nullptr);
|
|
||||||
|
|
||||||
seconds_passed_ = 0.0f;
|
|
||||||
seconds_accumulated_ = 0.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Timer::Update() {
|
|
||||||
timeval currentTime;
|
|
||||||
gettimeofday(¤tTime, nullptr);
|
|
||||||
seconds_passed_ =
|
|
||||||
(float)(currentTime.tv_sec - last_time_.tv_sec) +
|
|
||||||
0.000001f * (float)(currentTime.tv_usec - last_time_.tv_usec);
|
|
||||||
|
|
||||||
last_time_ = currentTime;
|
|
||||||
|
|
||||||
seconds_accumulated_ += seconds_passed_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Timer::Sleep(float duration) {
|
|
||||||
Timer timer;
|
|
||||||
float accumulator = 0.0;
|
|
||||||
constexpr float epsilon = 0.0001f;
|
|
||||||
|
|
||||||
while (accumulator < duration) {
|
|
||||||
timer.Update();
|
|
||||||
accumulator += timer.GetSecondsPassed();
|
|
||||||
if (duration - accumulator > epsilon) {
|
|
||||||
float sleep_time = duration - accumulator - epsilon;
|
|
||||||
std::this_thread::sleep_for(
|
|
||||||
std::chrono::microseconds((int)(sleep_time * 1000000.0f)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace base
|
|
|
@ -1,31 +1,47 @@
|
||||||
#ifndef BASE_TIMER_H
|
#ifndef BASE_TIMER_H
|
||||||
#define BASE_TIMER_H
|
#define BASE_TIMER_H
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
namespace base {
|
namespace base {
|
||||||
|
|
||||||
class Timer {
|
class ElapsedTimer {
|
||||||
public:
|
public:
|
||||||
Timer();
|
ElapsedTimer() { time_ = std::chrono::high_resolution_clock::now(); }
|
||||||
~Timer() = default;
|
|
||||||
|
|
||||||
void Reset();
|
// Return seconds passed since creating the object.
|
||||||
|
double Elapsed() const {
|
||||||
void Update();
|
auto current_time = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> diff = current_time - time_;
|
||||||
static void Sleep(float duration);
|
return diff.count();
|
||||||
|
}
|
||||||
float GetSecondsPassed() const { return seconds_passed_; }
|
|
||||||
float GetSecondsAccumulated() const { return seconds_accumulated_; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float seconds_passed_ = 0.0f;
|
std::chrono::time_point<std::chrono::high_resolution_clock> time_;
|
||||||
float seconds_accumulated_ = 0.0f;
|
|
||||||
|
|
||||||
timeval last_time_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class DeltaTimer {
|
||||||
|
public:
|
||||||
|
DeltaTimer() { time_ = std::chrono::high_resolution_clock::now(); }
|
||||||
|
|
||||||
|
// Return seconds passed since the last call to this function.
|
||||||
|
double Delta() {
|
||||||
|
auto current_time = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> diff = current_time - time_;
|
||||||
|
time_ = current_time;
|
||||||
|
return diff.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> time_;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void Sleep(double seconds) {
|
||||||
|
std::this_thread::sleep_for(
|
||||||
|
std::chrono::duration<double, std::milli>(seconds * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace base
|
} // namespace base
|
||||||
|
|
||||||
#endif // BASE_TIMER_H
|
#endif // BASE_TIMER_H
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
|
|
||||||
#include "base/log.h"
|
#include "base/log.h"
|
||||||
|
#include "base/timer.h"
|
||||||
|
|
||||||
using namespace base;
|
using namespace base;
|
||||||
|
|
||||||
|
@ -186,7 +187,7 @@ void AudioSinkAlsa::AudioThreadMain() {
|
||||||
if (terminate_audio_thread_.load(std::memory_order_relaxed))
|
if (terminate_audio_thread_.load(std::memory_order_relaxed))
|
||||||
return;
|
return;
|
||||||
// Avoid busy-looping.
|
// Avoid busy-looping.
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
Sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
delegate_->RenderAudio(buffer.get(), num_frames);
|
delegate_->RenderAudio(buffer.get(), num_frames);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "base/log.h"
|
#include "base/log.h"
|
||||||
#include "base/task_runner.h"
|
#include "base/task_runner.h"
|
||||||
|
#include "base/timer.h"
|
||||||
#include "engine/animator.h"
|
#include "engine/animator.h"
|
||||||
#include "engine/asset/font.h"
|
#include "engine/asset/font.h"
|
||||||
#include "engine/asset/image.h"
|
#include "engine/asset/image.h"
|
||||||
|
@ -72,7 +73,7 @@ Engine& Engine::Get() {
|
||||||
void Engine::Run() {
|
void Engine::Run() {
|
||||||
Initialize();
|
Initialize();
|
||||||
|
|
||||||
timer_.Reset();
|
DeltaTimer timer;
|
||||||
float accumulator = 0.0;
|
float accumulator = 0.0;
|
||||||
float frame_frac = 0.0f;
|
float frame_frac = 0.0f;
|
||||||
|
|
||||||
|
@ -83,17 +84,13 @@ void Engine::Run() {
|
||||||
if (platform_->should_exit())
|
if (platform_->should_exit())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!renderer_->IsInitialzed()) {
|
if (!renderer_->IsInitialzed())
|
||||||
timer_.Reset();
|
|
||||||
input_queue_.clear();
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
Draw(frame_frac);
|
Draw(frame_frac);
|
||||||
|
|
||||||
// Accumulate time.
|
// Accumulate time.
|
||||||
timer_.Update();
|
accumulator += timer.Delta();
|
||||||
accumulator += timer_.GetSecondsPassed();
|
|
||||||
|
|
||||||
// Subdivide the frame time using fixed time steps.
|
// Subdivide the frame time using fixed time steps.
|
||||||
while (accumulator >= time_step_) {
|
while (accumulator >= time_step_) {
|
||||||
|
@ -511,7 +508,6 @@ void Engine::LostFocus() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::GainedFocus(bool from_interstitial_ad) {
|
void Engine::GainedFocus(bool from_interstitial_ad) {
|
||||||
timer_.Reset();
|
|
||||||
audio_mixer_->Resume();
|
audio_mixer_->Resume();
|
||||||
|
|
||||||
if (game_)
|
if (game_)
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
#include "base/random.h"
|
#include "base/random.h"
|
||||||
#include "base/thread_pool.h"
|
#include "base/thread_pool.h"
|
||||||
#include "base/timer.h"
|
|
||||||
#include "base/vecmath.h"
|
#include "base/vecmath.h"
|
||||||
#include "engine/persistent_data.h"
|
#include "engine/persistent_data.h"
|
||||||
#include "engine/platform/platform_observer.h"
|
#include "engine/platform/platform_observer.h"
|
||||||
|
@ -214,7 +213,6 @@ class Engine : public PlatformObserver {
|
||||||
unsigned int replay_index_ = 0;
|
unsigned int replay_index_ = 0;
|
||||||
|
|
||||||
base::ThreadPool thread_pool_;
|
base::ThreadPool thread_pool_;
|
||||||
base::Timer timer_;
|
|
||||||
base::Randomf random_;
|
base::Randomf random_;
|
||||||
|
|
||||||
void Initialize();
|
void Initialize();
|
||||||
|
|
Loading…
Reference in New Issue