Skip to content

Commit d0d99e3

Browse files
committed
More frame pacing work
1 parent 56c942f commit d0d99e3

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

src/examples/framepacing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn on_init() void {
5151
debug.log("Frame pacing example module initializing", .{});
5252

5353
papp.setTargetFPS(60);
54-
// papp.setFixedTimestep(1.0 / 60.0);
54+
papp.setFixedTimestep(1.0 / 30.0);
5555

5656
fps_module.showFPS(true);
5757

src/framework/platform/app.zig

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const state = struct {
2929
// game loop timers
3030
var game_loop_timer: time.Timer = undefined;
3131
var fps_update_timer: time.Timer = undefined;
32+
var did_limit_fps: bool = false;
3233

3334
// delta time vars
3435
var reset_delta: bool = true;
@@ -158,34 +159,37 @@ fn on_frame() void {
158159
modules.postDrawModules();
159160

160161
// keep under our FPS limit, if needed
161-
limitFps();
162+
state.did_limit_fps = limitFps();
162163
}
163164

164-
fn limitFps() void {
165-
if (state.target_fps != null) {
166-
// Try to hit our target FPS!
165+
fn limitFps() bool {
166+
if(state.target_fps == null)
167+
return false;
167168

168-
// Easy case, just stop here if we are under the target frame length
169-
const initial_frame_ns = state.game_loop_timer.read();
170-
if (initial_frame_ns >= state.target_fps_ns)
171-
return;
169+
// Try to hit our target FPS!
172170

173-
// Harder case, we are faster than the target frame length.
174-
// Note: time.sleep does not ensure consistent timing.
175-
// Due to this we need to sleep most of the time, but busy loop the rest.
171+
// Easy case, just stop here if we are under the target frame length
172+
const initial_frame_ns = state.game_loop_timer.read();
173+
if (initial_frame_ns >= state.target_fps_ns)
174+
return false;
176175

177-
const frame_len_ns = initial_frame_ns + NS_FPS_LIMIT_OVERHEAD;
178-
if (frame_len_ns < state.target_fps_ns) {
179-
time.sleep(state.target_fps_ns - frame_len_ns);
180-
}
176+
// Harder case, we are faster than the target frame length.
177+
// Note: time.sleep does not ensure consistent timing.
178+
// Due to this we need to sleep most of the time, but busy loop the rest.
181179

182-
// Eat up the rest of the time in a busy loop to ensure consistent frame pacing
183-
while (true) {
184-
const cur_frame_len_ns = state.game_loop_timer.read();
185-
if (cur_frame_len_ns >= state.target_fps_ns)
186-
break;
187-
}
180+
const frame_len_ns = initial_frame_ns + NS_FPS_LIMIT_OVERHEAD;
181+
if (frame_len_ns < state.target_fps_ns) {
182+
time.sleep(state.target_fps_ns - frame_len_ns);
183+
}
184+
185+
// Eat up the rest of the time in a busy loop to ensure consistent frame pacing
186+
while (true) {
187+
const cur_frame_len_ns = state.game_loop_timer.read();
188+
if (cur_frame_len_ns + 500 >= state.target_fps_ns)
189+
break;
188190
}
191+
192+
return true;
189193
}
190194

191195
/// Get time elapsed since last tick. Also calculate the FPS!
@@ -206,6 +210,13 @@ fn calcDeltaTime() DeltaTime {
206210
state.fps_framecount = 0;
207211
}
208212

213+
// if(state.did_limit_fps) {
214+
// return DeltaTime{
215+
// .f_delta_time = (@as(f32, @floatFromInt(state.target_fps_ns)) / NS_PER_SECOND_F),
216+
// .ns_delta_time = state.target_fps_ns,
217+
// };
218+
// }
219+
209220
return DeltaTime{
210221
.f_delta_time = @as(f32, @floatFromInt(nanos_since_tick)) / NS_PER_SECOND_F,
211222
.ns_delta_time = nanos_since_tick,

0 commit comments

Comments
 (0)