Skip to content

Commit ee71a6d

Browse files
committed
For fixed timesteps, accumulating time in nanosecond ints instead of floats
1 parent fcf405e commit ee71a6d

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/examples/forest.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,9 @@ fn on_init() void {
146146
// capture and hide the mouse!
147147
papp.captureMouse(true);
148148

149-
// set a FPS limit, to test that as well
149+
// set a FPS limit and fixed timestep to test that as well
150150
papp.setTargetFPS(60);
151+
papp.setFixedTimestep(1.0 / 60.0);
151152

152153
sprite_batch = batcher.SpriteBatcher.init(.{}) catch {
153154
debug.showErrorScreen("Fatal error during batch init!");

src/framework/platform/app.zig

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ const NS_PER_SECOND: i64 = 1_000_000_000;
1212
const NS_PER_SECOND_F: f32 = 1_000_000_000.0;
1313
const NS_FPS_LIMIT_OVERHEAD = 1_250_000; // tuned to ensure consistent frame pacing
1414

15+
const DeltaTime = struct {
16+
f_delta_time: f32,
17+
ns_delta_time: u64,
18+
};
19+
1520
const state = struct {
1621
// FPS cap vars, if set
1722
var target_fps: ?u64 = null;
1823
var target_fps_ns: u64 = undefined;
1924

2025
// Fixed timestamp length, if set
21-
var fixed_timestep_delta: ?f32 = null;
26+
var fixed_timestep_delta_ns: ?u64 = null;
27+
var fixed_timestep_delta_f: f32 = 0.0;
2228

2329
// game loop timers
2430
var game_loop_timer: time.Timer = undefined;
@@ -28,7 +34,7 @@ const state = struct {
2834
var reset_delta: bool = true;
2935

3036
// fixed tick game loops need to keep track of a time accumulator
31-
var time_accumulator: f32 = 0.0;
37+
var time_accumulator_ns: u64 = 0;
3238

3339
// current fps, updated every second
3440
var fps: i32 = 0;
@@ -121,18 +127,20 @@ fn on_cleanup() void {
121127

122128
fn on_frame() void {
123129
// time management!
124-
state.delta_time = calcDeltaTime();
130+
const delta = calcDeltaTime();
131+
state.delta_time = delta.f_delta_time;
132+
125133
state.tick += 1;
126134
state.fps_framecount += 1;
127135

128-
if (state.fixed_timestep_delta) |fixed_delta| {
129-
state.time_accumulator += state.delta_time;
136+
if (state.fixed_timestep_delta_ns) |fixed_delta_ns| {
137+
state.time_accumulator_ns += delta.ns_delta_time;
130138

131139
// keep ticking until we catch up to the actual time
132-
while (state.time_accumulator >= fixed_delta) {
140+
while (state.time_accumulator_ns >= fixed_delta_ns) {
133141
// fixed timestamp, tick at our constant rate
134-
modules.tickModules(fixed_delta);
135-
state.time_accumulator -= fixed_delta;
142+
modules.tickModules(state.fixed_timestep_delta_f);
143+
state.time_accumulator_ns -= fixed_delta_ns;
136144
}
137145
} else {
138146
// tick as fast as possible!
@@ -182,11 +190,11 @@ fn limitFps() void {
182190
}
183191

184192
/// Get time elapsed since last tick. Also calculate the FPS!
185-
fn calcDeltaTime() f32 {
193+
fn calcDeltaTime() DeltaTime {
186194
if (state.reset_delta) {
187195
state.reset_delta = false;
188196
state.game_loop_timer.reset();
189-
return 1.0 / 60.0;
197+
return DeltaTime{ .f_delta_time = 1.0 / 60.0, .ns_delta_time = 60 / NS_PER_SECOND };
190198
}
191199

192200
// calculate the fps by counting frames each second
@@ -199,7 +207,10 @@ fn calcDeltaTime() f32 {
199207
state.fps_framecount = 0;
200208
}
201209

202-
return @as(f32, @floatFromInt(nanos_since_tick)) / NS_PER_SECOND_F;
210+
return DeltaTime{
211+
.f_delta_time = @as(f32, @floatFromInt(nanos_since_tick)) / NS_PER_SECOND_F,
212+
.ns_delta_time = nanos_since_tick,
213+
};
203214
}
204215

205216
/// Returns the current frames per second
@@ -221,7 +232,8 @@ pub fn setTargetFPS(fps_target: i32) void {
221232
}
222233

223234
pub fn setFixedTimestep(timestep_delta: f32) void {
224-
state.fixed_timestep_delta = timestep_delta;
235+
state.fixed_timestep_delta_ns = @intFromFloat(timestep_delta * NS_PER_SECOND_F);
236+
state.fixed_timestep_delta_f = timestep_delta;
225237
}
226238

227239
pub fn getCurrentDeltaTime() f32 {

0 commit comments

Comments
 (0)