Introduce new timer based on vsync

This commit is contained in:
2023-08-27 21:29:43 +02:00
parent db2e5543df
commit c40a8f44a5
6 changed files with 129 additions and 22 deletions

View File

@@ -16,11 +16,13 @@ namespace JabyEngine {
namespace GPU {
struct Display {
#ifdef JABYENGINE_PAL
static constexpr size_t Width = 320;
static constexpr size_t Height = 256;
static constexpr size_t Width = 320;
static constexpr size_t Height = 256;
static constexpr uint32_t frames_per_sec = 50;
#else
static constexpr size_t Width = 320;
static constexpr size_t Height = 240;
static constexpr size_t Width = 320;
static constexpr size_t Height = 240;
static constexpr uint32_t frames_per_sec = 50;
#endif
static uint8_t current_id;

View File

@@ -0,0 +1,25 @@
#ifndef __JABYENGINE_FRAME_TIME_HELPER_HPP__
#define __JABYENGINE_FRAME_TIME_HELPER_HPP__
#include <PSX/GPU/gpu.hpp>
#include <limits.h>
namespace JabyEngine {
static constexpr double ms_per_frame = 1000.0/static_cast<double>(GPU::Display::frames_per_sec);
template<typename T>
static constexpr T ms_to_vsync_ticks(T time_ms) {
return static_cast<T>(static_cast<double>(time_ms)/ms_per_frame);
}
static constexpr uint32_t operator ""_ms(unsigned long long time) {
return static_cast<uint32_t>(ms_to_vsync_ticks(time));
}
static constexpr size_t max_ms_time_u8 = UI8_MAX*ms_per_frame;
static constexpr size_t max_ms_time_u16 = UI16_MAX*ms_per_frame;
static constexpr size_t max_ms_time_u32 = UI32_MAX;
#undef literal_operator_template
}
using JabyEngine::operator""_ms;
#endif //!__JABYENGINE_FRAME_TIME_HELPER_HPP__

View File

@@ -0,0 +1,59 @@
#ifndef __JABYENGINE_FRAME_TIMER_HPP__
#define __JABYENGINE_FRAME_TIMER_HPP__
#include "frame_time_helper.hpp"
#include <stdint.h>
namespace JabyEngine {
class MasterTime {
private:
static uint32_t value;
public:
static uint32_t read() {
return reinterpret_cast<volatile uint32_t&>(MasterTime::value);
}
template<typename T>
static T read_as() {
return static_cast<T>(MasterTime::read());
}
};
template<typename T>
class SimpleTimer {
protected:
T value = 0;
public:
constexpr SimpleTimer() = default;
bool is_expired_for(T time) const {
return static_cast<T>((MasterTime::read_as<T>() - this->value)) >= time;
}
void reset() {
this->value = MasterTime::read_as<T>();
}
};
template<typename T>
class IntervalTimer : public SimpleTimer<T> {
private:
T interval = 0;
public:
constexpr IntervalTimer() = default;
constexpr IntervalTimer(T interval) : SimpleTimer<T>(), interval(interval) {
}
void set_interval(T interval) {
this->interval = interval;
}
bool is_expired() const {
return SimpleTimer<T>::is_expired_for(this->interval);
}
};
}
#endif //!__JABYENGINE_FRAME_TIMER_HPP__