From 97ebf75ce5ac003a55f77c8913fcd68d2d40f802 Mon Sep 17 00:00:00 2001 From: Isaac de Andrade Date: Thu, 29 Jan 2026 12:12:54 -0700 Subject: [PATCH] Add core/delta_time example. (#305) Add delta_time example. --- build.zig | 5 ++ examples/core/delta_time.zig | 102 +++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 examples/core/delta_time.zig diff --git a/build.zig b/build.zig index c29c970..808c112 100644 --- a/build.zig +++ b/build.zig @@ -110,6 +110,11 @@ pub fn build(b: *std.Build) !void { .path = "examples/core/basic_window.zig", .desc = "Creates a basic window with text", }, + .{ + .name = "delta_time", + .path = "examples/core/delta_time.zig", + .desc = "Show how to use frame time (delta time)", + }, .{ .name = "core_monitor_change", .path = "examples/core/core_monitor_change.zig", diff --git a/examples/core/delta_time.zig b/examples/core/delta_time.zig new file mode 100644 index 0000000..fd40d61 --- /dev/null +++ b/examples/core/delta_time.zig @@ -0,0 +1,102 @@ +// raylib [core] example - delta time +// +// Example complexity rating: [★☆☆☆] 1/4 +// +// Example originally created with raylib 5.5, last time updated with raylib 5.6-dev +// +// Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5) +// +// Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +// BSD-like license that allows static linking with closed source software +// +// Copyright (c) 2025 Robin (@RobinsAviary) +// +// Ported to Zig by Isaac de Andrade in Jan 2026 + +const rl = @import("raylib"); + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +pub fn main() anyerror!void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - delta time"); + defer rl.closeWindow(); // Close window and OpenGL context + + var current_fps: i32 = 60; + + // Store the position for the both of the circles + var delta_circle = rl.Vector2{ .x = 0, .y = screenHeight / 3.0 }; + var frame_circle = rl.Vector2{ .x = 0, .y = screenHeight * (2.0 / 3.0) }; + + // The speed applied to both circles + const speed = 10.0; + const circle_radius = 32.0; + + rl.setTargetFPS(current_fps); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!rl.windowShouldClose()) { // Detect window close button or ESC key + // Update + //---------------------------------------------------------------------------------- + // Adjust the FPS target based on the mouse wheel + const mouse_wheel = rl.getMouseWheelMove(); + + if (mouse_wheel != 0) { + current_fps += @intFromFloat(mouse_wheel); + if (current_fps < 0) current_fps = 0; + rl.setTargetFPS(current_fps); + } + + // rl.getFrameTime() returns the time it took to draw the last frame, in seconds (usually called delta time) + // Uses the delta time to make the circle look like it's moving at a "consistent" speed regardless of FPS + + // Multiply by 6.0 (an arbitrary value) in order to make the speed + // visually closer to the other circle (at 60 fps), for comparison + delta_circle.x += rl.getFrameTime() * 6 * speed; + // This circle can move faster or slower visually depending on the FPS + frame_circle.x += 0.1 * speed; + + if (delta_circle.x > screenWidth) delta_circle.x = 0; + if (frame_circle.x > screenWidth) frame_circle.x = 0; + + // Reset both circles positions + if (rl.isKeyPressed(rl.KeyboardKey.r)) { + delta_circle.x = 0; + frame_circle.x = 0; + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.beginDrawing(); + defer rl.endDrawing(); + + rl.clearBackground(rl.Color.ray_white); + + // Draw both circles to the screen + rl.drawCircleV(delta_circle, circle_radius, rl.Color.red); + rl.drawCircleV(frame_circle, circle_radius, rl.Color.blue); + + // Draw the help text + // Determine what help text to show depending on the current FPS target uses the c string format syntax + const fps_text: [:0]const u8 = if (current_fps <= 0) + rl.textFormat("FPS: unlimited (%i)", .{rl.getFPS()}) // uses C string format syntax from raylib + else + rl.textFormat("FPS: %i (target: %i)", .{ rl.getFPS(), current_fps }); + + rl.drawText(fps_text, 10, 10, 20, rl.Color.dark_gray); + // uses C string format syntax + rl.drawText(rl.textFormat("Frame time: %02.02f ms", .{rl.getFrameTime()}), 10, 30, 20, rl.Color.dark_gray); + rl.drawText("Use the scroll wheel to change the fps limit, r to reset", 10, 50, 20, rl.Color.dark_gray); + + // Draw the text above the circles + rl.drawText("FUNC: x += rl.getFrameTime() * speed", 10, 90, 20, rl.Color.red); + rl.drawText("FUNC: x += speed", 10, 240, 20, rl.Color.blue); + } +}