Skip to content

Commit b86d447

Browse files
committed
Updating framepacing example, removing post-tick lifecycle function after adding fixed-tick
1 parent 2be893d commit b86d447

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

src/examples/framepacing.zig

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const state = struct {
2121
var start_x: f32 = 120.0;
2222
var x_pos: f32 = 120.0;
2323
var x_pos_delta: f32 = 120.0;
24+
var x_pos_fixed: f32 = 120.0;
25+
var speed: f32 = 300.0;
2426
};
2527

2628
// This example shows the simple debug drawing functions.
@@ -37,6 +39,7 @@ pub fn registerModule() !void {
3739
.name = "frame_pacing_test",
3840
.init_fn = on_init,
3941
.tick_fn = on_tick,
42+
.fixed_tick_fn = on_fixed_tick,
4043
.draw_fn = on_draw,
4144
.cleanup_fn = on_cleanup,
4245
};
@@ -60,8 +63,8 @@ fn on_init() void {
6063
}
6164

6265
fn on_tick(delta: f32) void {
63-
state.x_pos += (1.0 / 60.0) * 300.0;
64-
state.x_pos_delta += delta * 300.0;
66+
state.x_pos += (1.0 / 60.0) * state.speed;
67+
state.x_pos_delta += delta * state.speed;
6568

6669
if(state.x_pos > 600.0)
6770
state.x_pos = 0.0;
@@ -73,10 +76,28 @@ fn on_tick(delta: f32) void {
7376
std.os.exit(0);
7477
}
7578

79+
fn on_fixed_tick(fixed_delta: f32) void {
80+
state.x_pos_fixed += fixed_delta * state.speed;
81+
82+
if(state.x_pos_fixed > 600.0)
83+
state.x_pos_fixed = 0.0;
84+
}
85+
7686
fn on_draw() void {
7787
// Draw our debug cat image, but use the color override to tint it!
78-
graphics.drawDebugRectangle(texture, state.start_x + state.x_pos, 120.0, 100.0, 100.0, colors.white);
79-
graphics.drawDebugRectangle(texture, state.start_x + state.x_pos_delta, 260.0, 100.0, 100.0, colors.white);
88+
var y_pos: f32 = 100.0;
89+
const y_spacing: f32 = 120.0;
90+
91+
graphics.drawDebugText(10, y_pos - 30, "raw tick:");
92+
graphics.drawDebugRectangle(texture, state.start_x + state.x_pos, y_pos, 100.0, 100.0, colors.white);
93+
y_pos += y_spacing;
94+
95+
graphics.drawDebugText(10, y_pos - 90, "delta tick:");
96+
graphics.drawDebugRectangle(texture, state.start_x + state.x_pos_delta, y_pos, 100.0, 100.0, colors.white);
97+
y_pos += y_spacing;
98+
99+
graphics.drawDebugText(10, y_pos - 150, "fixed tick:");
100+
graphics.drawDebugRectangle(texture, state.start_x + state.x_pos_fixed, y_pos, 100.0, 100.0, colors.white);
80101
}
81102

82103
fn on_cleanup() void {

src/framework/modules.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub const Module = struct {
1414
start_fn: ?*const fn () void = null,
1515
stop_fn: ?*const fn () void = null,
1616
tick_fn: ?*const fn (f32) void = null,
17-
post_tick_fn: ?*const fn () void = null,
17+
fixed_tick_fn: ?*const fn (f32) void = null,
1818
pre_draw_fn: ?*const fn () void = null,
1919
draw_fn: ?*const fn () void = null,
2020
post_draw_fn: ?*const fn () void = null,
@@ -72,10 +72,14 @@ pub fn tickModules(delta_time: f32) void {
7272
if (module.value_ptr.tick_fn != null)
7373
module.value_ptr.tick_fn.?(delta_time);
7474
}
75-
it = modules.iterator();
75+
}
76+
77+
/// Calls the fixed tick and function of all modules
78+
pub fn fixedTickModules(fixed_delta_time: f32) void {
79+
var it = modules.iterator();
7680
while (it.next()) |module| {
77-
if (module.value_ptr.post_tick_fn != null)
78-
module.value_ptr.post_tick_fn.?();
81+
if (module.value_ptr.fixed_tick_fn != null)
82+
module.value_ptr.fixed_tick_fn.?(fixed_delta_time);
7983
}
8084
}
8185

src/framework/platform/app.zig

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const state = struct {
2222
var target_fps: ?u64 = null;
2323
var target_fps_ns: u64 = undefined;
2424

25-
// Fixed timestamp length, if set
26-
var fixed_timestep_delta_ns: ?u64 = null;
27-
var fixed_timestep_delta_f: f32 = 0.0;
25+
// Fixed timestep length - defaults to 40 per second
26+
var fixed_timestep_delta_ns: ?u64 = @intFromFloat((1.0 / 40.0) * NS_PER_SECOND_F);
27+
var fixed_timestep_delta_f: f32 = 1.0 / 40.0;
2828

2929
// game loop timers
3030
var game_loop_timer: time.Timer = undefined;
@@ -139,14 +139,13 @@ fn on_frame() void {
139139
// keep ticking until we catch up to the actual time
140140
while (state.time_accumulator_ns >= fixed_delta_ns) {
141141
// fixed timestamp, tick at our constant rate
142-
modules.tickModules(state.fixed_timestep_delta_f);
142+
modules.fixedTickModules(state.fixed_timestep_delta_f);
143143
state.time_accumulator_ns -= fixed_delta_ns;
144144
}
145-
} else {
146-
// tick as fast as possible!
147-
modules.tickModules(state.delta_time);
148145
}
149146

147+
modules.tickModules(state.delta_time);
148+
150149
// tell modules we are getting ready to draw!
151150
modules.preDrawModules();
152151

src/framework/platform/input.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ const state = struct {
165165
pub fn registerModule() !void {
166166
const inputSubsystem = modules.Module{
167167
.name = "subsystem.input",
168-
.post_tick_fn = on_post_tick,
169168
.post_draw_fn = on_post_draw,
170169
};
171170

@@ -184,7 +183,7 @@ pub fn deinit() void {
184183
}
185184

186185
/// App lifecycle event that happens after ticking
187-
fn on_post_tick() void {
186+
fn on_post_draw() void {
188187
// reset the 'just pressed' states
189188
for (0..state.keyboard_just_pressed.len) |i| {
190189
state.keyboard_just_pressed[i] = false;
@@ -200,9 +199,7 @@ fn on_post_tick() void {
200199
// reset the mouse delta state.
201200
state.mouse_dx = 0;
202201
state.mouse_dy = 0;
203-
}
204202

205-
fn on_post_draw() void {
206203
// reset the mouse delta state.
207204
state.mouse_frame_dx = 0;
208205
state.mouse_frame_dy = 0;

0 commit comments

Comments
 (0)