Update timer.h

Remove Timer class. Add ElapsedTimer and DeltaTimer classes that use
std::chrono::high_resolution_clock
This commit is contained in:
Attila Uygun 2023-08-08 00:32:07 +02:00
parent 10823cd459
commit 96d6a52a74
7 changed files with 38 additions and 75 deletions

View File

@ -51,7 +51,6 @@ add_library(kaliber SHARED
../../../src/base/log.cc
../../../src/base/task_runner.cc
../../../src/base/thread_pool.cc
../../../src/base/timer.cc
../../../src/demo/credits.cc
../../../src/demo/demo.cc
../../../src/demo/enemy.cc

View File

@ -15,7 +15,6 @@ source_set("base") {
"task_runner.h",
"thread_pool.cc",
"thread_pool.h",
"timer.cc",
"timer.h",
"vecmath.h",
]

View File

@ -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(&currentTime, 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

View File

@ -1,31 +1,47 @@
#ifndef BASE_TIMER_H
#define BASE_TIMER_H
#include <sys/time.h>
#include <chrono>
#include <thread>
namespace base {
class Timer {
class ElapsedTimer {
public:
Timer();
~Timer() = default;
ElapsedTimer() { time_ = std::chrono::high_resolution_clock::now(); }
void Reset();
void Update();
static void Sleep(float duration);
float GetSecondsPassed() const { return seconds_passed_; }
float GetSecondsAccumulated() const { return seconds_accumulated_; }
// Return seconds passed since creating the object.
double Elapsed() const {
auto current_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = current_time - time_;
return diff.count();
}
private:
float seconds_passed_ = 0.0f;
float seconds_accumulated_ = 0.0f;
timeval last_time_;
std::chrono::time_point<std::chrono::high_resolution_clock> 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
#endif // BASE_TIMER_H

View File

@ -5,6 +5,7 @@
#include <alsa/asoundlib.h>
#include "base/log.h"
#include "base/timer.h"
using namespace base;
@ -186,7 +187,7 @@ void AudioSinkAlsa::AudioThreadMain() {
if (terminate_audio_thread_.load(std::memory_order_relaxed))
return;
// Avoid busy-looping.
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Sleep(1);
}
delegate_->RenderAudio(buffer.get(), num_frames);

View File

@ -2,6 +2,7 @@
#include "base/log.h"
#include "base/task_runner.h"
#include "base/timer.h"
#include "engine/animator.h"
#include "engine/asset/font.h"
#include "engine/asset/image.h"
@ -72,7 +73,7 @@ Engine& Engine::Get() {
void Engine::Run() {
Initialize();
timer_.Reset();
DeltaTimer timer;
float accumulator = 0.0;
float frame_frac = 0.0f;
@ -83,17 +84,13 @@ void Engine::Run() {
if (platform_->should_exit())
return;
if (!renderer_->IsInitialzed()) {
timer_.Reset();
input_queue_.clear();
if (!renderer_->IsInitialzed())
continue;
}
Draw(frame_frac);
// Accumulate time.
timer_.Update();
accumulator += timer_.GetSecondsPassed();
accumulator += timer.Delta();
// Subdivide the frame time using fixed time steps.
while (accumulator >= time_step_) {
@ -511,7 +508,6 @@ void Engine::LostFocus() {
}
void Engine::GainedFocus(bool from_interstitial_ad) {
timer_.Reset();
audio_mixer_->Resume();
if (game_)

View File

@ -9,7 +9,6 @@
#include "base/random.h"
#include "base/thread_pool.h"
#include "base/timer.h"
#include "base/vecmath.h"
#include "engine/persistent_data.h"
#include "engine/platform/platform_observer.h"
@ -214,7 +213,6 @@ class Engine : public PlatformObserver {
unsigned int replay_index_ = 0;
base::ThreadPool thread_pool_;
base::Timer timer_;
base::Randomf random_;
void Initialize();