Compare commits

...

3 Commits

Author SHA1 Message Date
Attila Uygun 96d6a52a74 Update timer.h
Remove Timer class. Add ElapsedTimer and DeltaTimer classes that use
std::chrono::high_resolution_clock
2023-08-08 00:32:07 +02:00
Attila Uygun 10823cd459 Fix memory issue in VulkanContext::SwapBuffers 2023-08-08 00:02:20 +02:00
Attila Uygun 26514fd142 Use base::PIf 2023-08-07 23:58:10 +02:00
9 changed files with 44 additions and 92 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

@ -150,7 +150,7 @@ void Player::AddNuke(int n) {
if (!nuke_symbol_animator_.IsPlaying(Animator::kRotation)) {
nuke_symbol_animator_.SetRotation(
M_PI * 5, 2, std::bind(SmootherStep, std::placeholders::_1));
PIf * 5, 2, std::bind(SmootherStep, std::placeholders::_1));
nuke_symbol_animator_.Play(Animator::kRotation, false);
}
}
@ -220,7 +220,7 @@ void Player::Fire(DamageType type, Vector2f dir) {
dir.Normalize();
float cos_theta = dir.DotProduct(Vector2f(1, 0));
float theta = acos(cos_theta) + M_PI;
float theta = acos(cos_theta) + PIf;
beam_[type].SetTheta(theta);
auto offset = beam_[type].GetRotation() * (len / 2);
beam_[type].Translate({offset.y, -offset.x});
@ -285,7 +285,7 @@ void Player::SetupWeapons() {
weapon_[i].SetFrame(wepon_warmup_frame[i]);
warmup_animator_[i].SetFrames(wepon_warmup_frame_count, wepon_anim_speed);
warmup_animator_[i].SetRotation(M_PI * 2, 20.0f);
warmup_animator_[i].SetRotation(PIf * 2, 20.0f);
warmup_animator_[i].Attach(&weapon_[i]);
warmup_animator_[i].Play(Animator::kRotation, true);

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();

View File

@ -1536,24 +1536,13 @@ bool VulkanContext::SwapBuffers() {
/*pWaitSemaphores*/
(separate_present_queue_) ? &image_ownership_semaphores_[frame_index_]
: &draw_complete_semaphores_[frame_index_],
/*swapchainCount*/ 0,
/*pSwapchain*/ nullptr,
/*pImageIndices*/ nullptr,
/*swapchainCount*/ 1,
/*pSwapchains*/ &window_.swapchain,
/*pImageIndices*/ &window_.current_buffer,
/*pResults*/ nullptr,
};
VkSwapchainKHR* swapchains = (VkSwapchainKHR*)alloca(sizeof(VkSwapchainKHR*));
uint32_t* pImageIndices = (uint32_t*)alloca(sizeof(uint32_t*));
present.pSwapchains = swapchains;
present.pImageIndices = pImageIndices;
DCHECK(window_.swapchain != VK_NULL_HANDLE);
swapchains[present.swapchainCount] = window_.swapchain;
pImageIndices[present.swapchainCount] = window_.current_buffer;
present.swapchainCount++;
err = QueuePresentKHR(present_queue_, &present);
frame_index_ += 1;