all: move mach.Timer, core Timer/Frequency to mach.time module
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
d62ddbb6cd
commit
133c89638b
16 changed files with 90 additions and 110 deletions
65
src/time/Frequency.zig
Normal file
65
src/time/Frequency.zig
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
const mach = @import("../main.zig");
|
||||
const Timer = @import("Timer.zig");
|
||||
|
||||
pub const Frequency = @This();
|
||||
|
||||
/// The target frequency (e.g. 60hz) or zero for unlimited
|
||||
target: u32 = 0,
|
||||
|
||||
/// The estimated delay that is needed to achieve the target frequency. Updated during tick()
|
||||
delay_ns: u64 = 0,
|
||||
|
||||
/// The actual measured frequency. This is updated every second.
|
||||
rate: u32 = 0,
|
||||
|
||||
delta_time: ?*f32 = null,
|
||||
delta_time_ns: *u64 = undefined,
|
||||
|
||||
/// Internal fields, this must be initialized via a call to start().
|
||||
internal: struct {
|
||||
// The frame number in this second's cycle. e.g. zero to 59
|
||||
count: u32,
|
||||
timer: Timer,
|
||||
last_time: u64,
|
||||
} = undefined,
|
||||
|
||||
/// Starts the timer used for frequency calculation. Must be called once before anything else.
|
||||
pub fn start(f: *Frequency) !void {
|
||||
f.internal = .{
|
||||
.count = 0,
|
||||
.timer = try Timer.start(),
|
||||
.last_time = 0,
|
||||
};
|
||||
}
|
||||
|
||||
/// Tick should be called at each occurrence (e.g. frame)
|
||||
pub inline fn tick(f: *Frequency) void {
|
||||
var current_time = f.internal.timer.readPrecise();
|
||||
|
||||
if (f.delta_time) |delta_time| {
|
||||
f.delta_time_ns.* = current_time -| f.internal.last_time;
|
||||
delta_time.* = @as(f32, @floatFromInt(f.delta_time_ns.*)) / @as(f32, @floatFromInt(mach.time.ns_per_s));
|
||||
}
|
||||
|
||||
if (current_time >= mach.time.ns_per_s) {
|
||||
f.rate = f.internal.count;
|
||||
f.internal.count = 0;
|
||||
f.internal.timer.reset();
|
||||
current_time -= mach.time.ns_per_s;
|
||||
}
|
||||
f.internal.last_time = current_time;
|
||||
f.internal.count += 1;
|
||||
|
||||
if (f.target != 0) {
|
||||
const limited_count = @min(f.target, f.internal.count);
|
||||
const target_time_per_tick: u64 = (mach.time.ns_per_s / f.target);
|
||||
const target_time = target_time_per_tick * limited_count;
|
||||
if (current_time > target_time) {
|
||||
f.delay_ns = 0;
|
||||
} else {
|
||||
f.delay_ns = target_time - current_time;
|
||||
}
|
||||
} else {
|
||||
f.delay_ns = 0;
|
||||
}
|
||||
}
|
||||
38
src/time/Timer.zig
Normal file
38
src/time/Timer.zig
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
const mach = @import("../main.zig");
|
||||
const std = @import("std");
|
||||
|
||||
const Timer = @This();
|
||||
|
||||
// TODO: support a WASM-based timer as well, which is the primary reason this abstraction exists.
|
||||
|
||||
timer: std.time.Timer,
|
||||
|
||||
/// Initialize the timer.
|
||||
pub fn start() !Timer {
|
||||
return .{ .timer = try std.time.Timer.start() };
|
||||
}
|
||||
|
||||
/// Reads the timer value since start or the last reset in nanoseconds.
|
||||
pub inline fn readPrecise(timer: *Timer) u64 {
|
||||
return timer.timer.read();
|
||||
}
|
||||
|
||||
/// Reads the timer value since start or the last reset in seconds.
|
||||
pub inline fn read(timer: *Timer) f32 {
|
||||
return @as(f32, @floatFromInt(timer.readPrecise())) / @as(f32, @floatFromInt(mach.time.ns_per_s));
|
||||
}
|
||||
|
||||
/// Resets the timer value to 0/now.
|
||||
pub inline fn reset(timer: *Timer) void {
|
||||
timer.timer.reset();
|
||||
}
|
||||
|
||||
/// Returns the current value of the timer in nanoseconds, then resets it.
|
||||
pub inline fn lapPrecise(timer: *Timer) u64 {
|
||||
return timer.timer.lap();
|
||||
}
|
||||
|
||||
/// Returns the current value of the timer in seconds, then resets it.
|
||||
pub inline fn lap(timer: *Timer) f32 {
|
||||
return @as(f32, @floatFromInt(timer.lapPrecise())) / @as(f32, @floatFromInt(mach.time.ns_per_s));
|
||||
}
|
||||
11
src/time/main.zig
Normal file
11
src/time/main.zig
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
pub const std = @import("std");
|
||||
|
||||
pub const ns_per_s = std.time.ns_per_s;
|
||||
|
||||
pub const Timer = @import("Timer.zig");
|
||||
pub const Frequency = @import("Frequency.zig");
|
||||
|
||||
test {
|
||||
_ = Timer;
|
||||
_ = Frequency;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue