all: move mach.Timer, core Timer/Frequency to mach.time module

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-08-25 14:24:09 -07:00
parent d62ddbb6cd
commit 133c89638b
16 changed files with 90 additions and 110 deletions

View file

@ -11,7 +11,7 @@ pub const systems = .{
.tick = .{ .handler = tick }, .tick = .{ .handler = tick },
}; };
title_timer: mach.Timer, title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,
pub fn deinit(core: *mach.Core.Mod, app: *Mod) void { pub fn deinit(core: *mach.Core.Mod, app: *Mod) void {
@ -62,7 +62,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
// Store our render pipeline in our module's state, so we can access it later on. // Store our render pipeline in our module's state, so we can access it later on.
app.init(.{ app.init(.{
.title_timer = try mach.Timer.start(), .title_timer = try mach.time.Timer.start(),
.pipeline = pipeline, .pipeline = pipeline,
}); });
try updateWindowTitle(core); try updateWindowTitle(core);

View file

@ -11,7 +11,7 @@ pub const systems = .{
.tick = .{ .handler = tick }, .tick = .{ .handler = tick },
}; };
title_timer: mach.Timer, title_timer: mach.time.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,
pub fn deinit(core: *mach.Core.Mod, app: *Mod) void { pub fn deinit(core: *mach.Core.Mod, app: *Mod) void {
@ -62,7 +62,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
// Store our render pipeline in our module's state, so we can access it later on. // Store our render pipeline in our module's state, so we can access it later on.
app.init(.{ app.init(.{
.title_timer = try mach.Timer.start(), .title_timer = try mach.time.Timer.start(),
.pipeline = pipeline, .pipeline = pipeline,
}); });
try updateWindowTitle(core); try updateWindowTitle(core);

View file

@ -8,11 +8,11 @@ const Vec2 = math.Vec2;
const Vec3 = math.Vec3; const Vec3 = math.Vec3;
// Global state for our game module. // Global state for our game module.
timer: mach.Timer, timer: mach.time.Timer,
player: mach.EntityID, player: mach.EntityID,
direction: Vec2 = vec2(0, 0), direction: Vec2 = vec2(0, 0),
spawning: bool = false, spawning: bool = false,
spawn_timer: mach.Timer, spawn_timer: mach.time.Timer,
// Components our game module defines. // Components our game module defines.
pub const components = .{ pub const components = .{
@ -82,8 +82,8 @@ fn init(
// file. If this is not done, then app.state() will panic indicating the state was never // file. If this is not done, then app.state() will panic indicating the state was never
// initialized. // initialized.
app.init(.{ app.init(.{
.timer = try mach.Timer.start(), .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.time.Timer.start(),
.player = player, .player = player,
}); });

View file

@ -12,12 +12,12 @@ const Mat4x4 = math.Mat4x4;
const Glyphs = @import("Glyphs.zig"); const Glyphs = @import("Glyphs.zig");
timer: mach.Timer, timer: mach.time.Timer,
player: mach.EntityID, player: mach.EntityID,
direction: Vec2 = vec2(0, 0), direction: Vec2 = vec2(0, 0),
spawning: bool = false, spawning: bool = false,
spawn_timer: mach.Timer, spawn_timer: mach.time.Timer,
fps_timer: mach.Timer, fps_timer: mach.time.Timer,
frame_count: usize, frame_count: usize,
sprites: usize, sprites: usize,
rand: std.rand.DefaultPrng, rand: std.rand.DefaultPrng,
@ -88,10 +88,10 @@ fn init(
sprite.schedule(.update); sprite.schedule(.update);
app.init(.{ app.init(.{
.timer = try mach.Timer.start(), .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.time.Timer.start(),
.player = player, .player = player,
.fps_timer = try mach.Timer.start(), .fps_timer = try mach.time.Timer.start(),
.frame_count = 0, .frame_count = 0,
.sprites = 0, .sprites = 0,
.rand = std.rand.DefaultPrng.init(1337), .rand = std.rand.DefaultPrng.init(1337),

View file

@ -20,10 +20,10 @@ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
info_text: mach.EntityID, info_text: mach.EntityID,
info_text_style: mach.EntityID, info_text_style: mach.EntityID,
timer: mach.Timer, timer: mach.time.Timer,
gotta_go_fast: bool = false, gotta_go_fast: bool = false,
spawn_timer: mach.Timer, spawn_timer: mach.time.Timer,
fps_timer: mach.Timer, fps_timer: mach.time.Timer,
frame_count: usize, frame_count: usize,
frame_rate: usize, frame_rate: usize,
num_sprites_spawned: usize, num_sprites_spawned: usize,
@ -141,9 +141,9 @@ fn init(
app.init(.{ app.init(.{
.info_text = info_text, .info_text = info_text,
.info_text_style = style1, .info_text_style = style1,
.timer = try mach.Timer.start(), .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.time.Timer.start(),
.fps_timer = try mach.Timer.start(), .fps_timer = try mach.time.Timer.start(),
.frame_count = 0, .frame_count = 0,
.frame_rate = 0, .frame_rate = 0,
.num_sprites_spawned = 0, .num_sprites_spawned = 0,

View file

@ -17,12 +17,12 @@ const Mat4x4 = math.Mat4x4;
// TODO: banish global allocator // TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
timer: mach.Timer, timer: mach.time.Timer,
player: mach.EntityID, player: mach.EntityID,
direction: Vec2 = vec2(0, 0), direction: Vec2 = vec2(0, 0),
spawning: bool = false, spawning: bool = false,
spawn_timer: mach.Timer, spawn_timer: mach.time.Timer,
fps_timer: mach.Timer, fps_timer: mach.time.Timer,
frame_count: usize, frame_count: usize,
sprites: usize, sprites: usize,
rand: std.rand.DefaultPrng, rand: std.rand.DefaultPrng,
@ -92,10 +92,10 @@ fn init(
sprite.schedule(.update); sprite.schedule(.update);
app.init(.{ app.init(.{
.timer = try mach.Timer.start(), .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.time.Timer.start(),
.player = player, .player = player,
.fps_timer = try mach.Timer.start(), .fps_timer = try mach.time.Timer.start(),
.frame_count = 0, .frame_count = 0,
.sprites = 0, .sprites = 0,
.rand = std.rand.DefaultPrng.init(1337), .rand = std.rand.DefaultPrng.init(1337),

View file

@ -14,12 +14,12 @@ const Vec3 = math.Vec3;
const Mat3x3 = math.Mat3x3; const Mat3x3 = math.Mat3x3;
const Mat4x4 = math.Mat4x4; const Mat4x4 = math.Mat4x4;
timer: mach.Timer, timer: mach.time.Timer,
player: mach.EntityID, player: mach.EntityID,
direction: Vec2 = vec2(0, 0), direction: Vec2 = vec2(0, 0),
spawning: bool = false, spawning: bool = false,
spawn_timer: mach.Timer, spawn_timer: mach.time.Timer,
fps_timer: mach.Timer, fps_timer: mach.time.Timer,
frame_count: usize, frame_count: usize,
rand: std.rand.DefaultPrng, rand: std.rand.DefaultPrng,
time: f32, time: f32,
@ -104,10 +104,10 @@ fn init(
text.schedule(.update); text.schedule(.update);
app.init(.{ app.init(.{
.timer = try mach.Timer.start(), .timer = try mach.time.Timer.start(),
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.time.Timer.start(),
.player = player, .player = player,
.fps_timer = try mach.Timer.start(), .fps_timer = try mach.time.Timer.start(),
.frame_count = 0, .frame_count = 0,
.rand = std.rand.DefaultPrng.init(1337), .rand = std.rand.DefaultPrng.init(1337),
.time = 0, .time = 0,

View file

@ -8,8 +8,6 @@ const log = std.log.scoped(.mach);
const gamemode_log = std.log.scoped(.gamemode); const gamemode_log = std.log.scoped(.gamemode);
pub const sysgpu = @import("../main.zig").sysgpu; pub const sysgpu = @import("../main.zig").sysgpu;
pub const Timer = @import("core/Timer.zig");
const Frequency = @import("core/Frequency.zig");
pub const Platform = switch (build_options.core_platform) { pub const Platform = switch (build_options.core_platform) {
.wasm => @panic("TODO: support mach.Core WASM platform"), .wasm => @panic("TODO: support mach.Core WASM platform"),
@ -135,10 +133,10 @@ state: enum {
exited, exited,
} = .running, } = .running,
linux_gamemode: ?bool = null, linux_gamemode: ?bool = null,
frame: Frequency, frame: mach.time.Frequency,
// Might be accessed by Platform backend // Might be accessed by Platform backend
input: Frequency, input: mach.time.Frequency,
swap_chain_update: std.Thread.ResetEvent = .{}, swap_chain_update: std.Thread.ResetEvent = .{},
// GPU // GPU
@ -1095,12 +1093,6 @@ comptime {
assertHasDecl(Platform, "mousePressed"); assertHasDecl(Platform, "mousePressed");
assertHasDecl(Platform, "mouseReleased"); assertHasDecl(Platform, "mouseReleased");
assertHasDecl(Platform, "mousePosition"); assertHasDecl(Platform, "mousePosition");
// Timer
assertHasDecl(@This().Timer, "start");
assertHasDecl(@This().Timer, "read");
assertHasDecl(@This().Timer, "reset");
assertHasDecl(@This().Timer, "lap");
} }
fn assertHasDecl(comptime T: anytype, comptime decl_name: []const u8) void { fn assertHasDecl(comptime T: anytype, comptime decl_name: []const u8) void {
@ -1112,8 +1104,6 @@ fn assertHasField(comptime T: anytype, comptime field_name: []const u8) void {
} }
test { test {
@import("std").testing.refAllDecls(Timer);
@import("std").testing.refAllDecls(Frequency);
@import("std").testing.refAllDecls(Platform); @import("std").testing.refAllDecls(Platform);
@import("std").testing.refAllDeclsRecursive(InitOptions); @import("std").testing.refAllDeclsRecursive(InitOptions);

View file

@ -2,7 +2,6 @@ const std = @import("std");
const mach = @import("../main.zig"); const mach = @import("../main.zig");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const InputState = @import("InputState.zig"); const InputState = @import("InputState.zig");
const Frequency = @import("Frequency.zig");
const unicode = @import("unicode.zig"); const unicode = @import("unicode.zig");
const detectBackendType = @import("common.zig").detectBackendType; const detectBackendType = @import("common.zig").detectBackendType;
const gpu = mach.gpu; const gpu = mach.gpu;

View file

@ -5,7 +5,6 @@ const std = @import("std");
const mach = @import("../main.zig"); const mach = @import("../main.zig");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const InputState = @import("InputState.zig"); const InputState = @import("InputState.zig");
const Frequency = @import("Frequency.zig");
const unicode = @import("unicode.zig"); const unicode = @import("unicode.zig");
const detectBackendType = @import("common.zig").detectBackendType; const detectBackendType = @import("common.zig").detectBackendType;
const gpu = mach.gpu; const gpu = mach.gpu;

View file

@ -1,57 +0,0 @@
const std = @import("std");
const builtin = @import("builtin");
const PlatformTimer = if (builtin.cpu.arch == .wasm32) @panic("TODO: support WASM") else NativeTimer;
const Timer = @This();
platform: PlatformTimer,
/// Initialize the timer.
pub fn start() !Timer {
return .{ .platform = try PlatformTimer.start() };
}
/// Reads the timer value since start or the last reset in nanoseconds.
pub inline fn readPrecise(timer: *Timer) u64 {
return timer.platform.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(std.time.ns_per_s));
}
/// Resets the timer value to 0/now.
pub inline fn reset(timer: *Timer) void {
timer.platform.reset();
}
/// Returns the current value of the timer in nanoseconds, then resets it.
pub inline fn lapPrecise(timer: *Timer) u64 {
return timer.platform.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(std.time.ns_per_s));
}
const NativeTimer = struct {
timer: std.time.Timer,
pub fn start() !NativeTimer {
return .{ .timer = try std.time.Timer.start() };
}
pub inline fn read(timer: *NativeTimer) u64 {
return timer.timer.read();
}
pub inline fn reset(timer: *NativeTimer) void {
timer.timer.reset();
}
pub inline fn lap(timer: *NativeTimer) u64 {
return timer.timer.lap();
}
};

View file

@ -3,7 +3,6 @@ const w = @import("win32/win32.zig");
const mach = @import("../main.zig"); const mach = @import("../main.zig");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const InputState = @import("InputState.zig"); const InputState = @import("InputState.zig");
const Frequency = @import("Frequency.zig");
const unicode = @import("unicode.zig"); const unicode = @import("unicode.zig");
const gpu = mach.gpu; const gpu = mach.gpu;

View file

@ -4,7 +4,6 @@ const std = @import("std");
// Core // Core
pub const Core = if (build_options.want_core) @import("Core.zig") else struct {}; pub const Core = if (build_options.want_core) @import("Core.zig") else struct {};
pub const Timer = if (build_options.want_core) Core.Timer else struct {};
// Mach standard library // Mach standard library
// gamemode requires libc on linux // gamemode requires libc on linux
@ -13,6 +12,7 @@ pub const gfx = if (build_options.want_mach) @import("gfx/main.zig") else struct
pub const Audio = if (build_options.want_sysaudio) @import("Audio.zig") else struct {}; pub const Audio = if (build_options.want_sysaudio) @import("Audio.zig") else struct {};
pub const math = @import("math/main.zig"); pub const math = @import("math/main.zig");
pub const testing = @import("testing.zig"); pub const testing = @import("testing.zig");
pub const time = @import("time/main.zig");
pub const sysaudio = if (build_options.want_sysaudio) @import("sysaudio/main.zig") else struct {}; pub const sysaudio = if (build_options.want_sysaudio) @import("sysaudio/main.zig") else struct {};
pub const sysgpu = if (build_options.want_sysgpu) @import("sysgpu/main.zig") else struct {}; pub const sysgpu = if (build_options.want_sysgpu) @import("sysgpu/main.zig") else struct {};
@ -57,6 +57,7 @@ test {
_ = gfx; _ = gfx;
_ = math; _ = math;
_ = testing; _ = testing;
_ = time;
std.testing.refAllDeclsRecursive(@import("module/Archetype.zig")); std.testing.refAllDeclsRecursive(@import("module/Archetype.zig"));
std.testing.refAllDeclsRecursive(@import("module/entities.zig")); std.testing.refAllDeclsRecursive(@import("module/entities.zig"));
// std.testing.refAllDeclsRecursive(@import("module/main.zig")); // std.testing.refAllDeclsRecursive(@import("module/main.zig"));

View file

@ -1,4 +1,4 @@
const std = @import("std"); const mach = @import("../main.zig");
const Timer = @import("Timer.zig"); const Timer = @import("Timer.zig");
pub const Frequency = @This(); pub const Frequency = @This();
@ -38,21 +38,21 @@ pub inline fn tick(f: *Frequency) void {
if (f.delta_time) |delta_time| { if (f.delta_time) |delta_time| {
f.delta_time_ns.* = current_time -| f.internal.last_time; f.delta_time_ns.* = current_time -| f.internal.last_time;
delta_time.* = @as(f32, @floatFromInt(f.delta_time_ns.*)) / @as(f32, @floatFromInt(std.time.ns_per_s)); delta_time.* = @as(f32, @floatFromInt(f.delta_time_ns.*)) / @as(f32, @floatFromInt(mach.time.ns_per_s));
} }
if (current_time >= std.time.ns_per_s) { if (current_time >= mach.time.ns_per_s) {
f.rate = f.internal.count; f.rate = f.internal.count;
f.internal.count = 0; f.internal.count = 0;
f.internal.timer.reset(); f.internal.timer.reset();
current_time -= std.time.ns_per_s; current_time -= mach.time.ns_per_s;
} }
f.internal.last_time = current_time; f.internal.last_time = current_time;
f.internal.count += 1; f.internal.count += 1;
if (f.target != 0) { if (f.target != 0) {
const limited_count = @min(f.target, f.internal.count); const limited_count = @min(f.target, f.internal.count);
const target_time_per_tick: u64 = (std.time.ns_per_s / f.target); const target_time_per_tick: u64 = (mach.time.ns_per_s / f.target);
const target_time = target_time_per_tick * limited_count; const target_time = target_time_per_tick * limited_count;
if (current_time > target_time) { if (current_time > target_time) {
f.delay_ns = 0; f.delay_ns = 0;

38
src/time/Timer.zig Normal file
View 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
View 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;
}