|
| 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 | +} |
0 commit comments