Skip to content

Commit 2be893d

Browse files
committed
Adding a framepacing example for testing
1 parent ee71a6d commit 2be893d

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

build.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub fn build(b: *std.Build) void {
4646
buildExample(b, "clear", target, optimize, delve_module, lib);
4747
buildExample(b, "debugdraw", target, optimize, delve_module, lib);
4848
buildExample(b, "forest", target, optimize, delve_module, lib);
49+
buildExample(b, "framepacing", target, optimize, delve_module, lib);
4950
buildExample(b, "lua", target, optimize, delve_module, lib);
5051
buildExample(b, "meshes", target, optimize, delve_module, lib);
5152
buildExample(b, "stresstest", target, optimize, delve_module, lib);

src/examples/framepacing.zig

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const std = @import("std");
2+
const delve = @import("delve");
3+
const app = delve.app;
4+
5+
const debug = delve.debug;
6+
const papp = delve.platform.app;
7+
const graphics = delve.platform.graphics;
8+
const colors = delve.colors;
9+
const images = delve.images;
10+
const input = delve.platform.input;
11+
const math = delve.math;
12+
const modules = delve.modules;
13+
const fps_module = delve.module.fps_counter;
14+
15+
pub const test_asset = @embedFile("static/test.gif");
16+
17+
var texture: graphics.Texture = undefined;
18+
var test_image: images.Image = undefined;
19+
20+
const state = struct {
21+
var start_x: f32 = 120.0;
22+
var x_pos: f32 = 120.0;
23+
var x_pos_delta: f32 = 120.0;
24+
};
25+
26+
// This example shows the simple debug drawing functions.
27+
// These functions are slow, but a quick way to get stuff on screen!
28+
29+
pub fn main() !void {
30+
try registerModule();
31+
try fps_module.registerModule();
32+
try app.start(app.AppConfig{ .title = "Delve Framework - Frame Pacing Test" });
33+
}
34+
35+
pub fn registerModule() !void {
36+
const debugDrawExample = modules.Module{
37+
.name = "frame_pacing_test",
38+
.init_fn = on_init,
39+
.tick_fn = on_tick,
40+
.draw_fn = on_draw,
41+
.cleanup_fn = on_cleanup,
42+
};
43+
44+
try modules.registerModule(debugDrawExample);
45+
}
46+
47+
fn on_init() void {
48+
debug.log("Frame pacing example module initializing", .{});
49+
50+
papp.setTargetFPS(60);
51+
// papp.setFixedTimestep(1.0 / 60.0);
52+
53+
fps_module.showFPS(true);
54+
55+
test_image = images.loadBytes(test_asset) catch {
56+
debug.log("Could not load test texture", .{});
57+
return;
58+
};
59+
texture = graphics.Texture.init(&test_image);
60+
}
61+
62+
fn on_tick(delta: f32) void {
63+
state.x_pos += (1.0 / 60.0) * 300.0;
64+
state.x_pos_delta += delta * 300.0;
65+
66+
if(state.x_pos > 600.0)
67+
state.x_pos = 0.0;
68+
69+
if(state.x_pos_delta > 600.0)
70+
state.x_pos_delta = 0.0;
71+
72+
if (input.isKeyJustPressed(.ESCAPE))
73+
std.os.exit(0);
74+
}
75+
76+
fn on_draw() void {
77+
// 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);
80+
}
81+
82+
fn on_cleanup() void {
83+
debug.log("Frame pacing example module cleaning up", .{});
84+
test_image.destroy();
85+
}

src/framework/modules/fps_counter.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ pub fn registerModule() !void {
2626
try modules.registerModule(fpsCounter);
2727
}
2828

29-
pub fn on_cleanup() void {
29+
fn on_cleanup() void {
3030
if (last_fps_str != null) {
3131
allocator.free(last_fps_str.?);
3232
last_fps_str = null;
3333
}
3434
}
3535

36-
pub fn on_tick(delta: f32) void {
36+
fn on_tick(delta: f32) void {
3737
_ = delta;
3838

3939
if (input.isKeyPressed(.LEFT_SHIFT) and input.isKeyJustPressed(.F)) {
4040
toggleFPS();
4141
}
4242
}
4343

44-
pub fn on_draw() void {
44+
fn on_draw() void {
4545
if (!show_fps)
4646
return;
4747

@@ -67,7 +67,7 @@ pub fn on_draw() void {
6767
drawFPS(fps_string);
6868
}
6969

70-
pub fn drawFPS(fps_string: [:0]u8) void {
70+
fn drawFPS(fps_string: [:0]u8) void {
7171
graphics.setDebugTextScale(1.0, 1.0);
7272
graphics.setDebugTextColor4b(0x88, 0x88, 0x88, 0xFF);
7373
graphics.drawDebugText(2, 2, fps_string);

0 commit comments

Comments
 (0)