add delta time calculation for frame-rate independent movement

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-04-17 10:06:51 -07:00
parent 7e8cbc78ff
commit 305c446aa2

View file

@ -67,11 +67,20 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type {
swap_chain: ?gpu.SwapChain, swap_chain: ?gpu.SwapChain,
swap_chain_format: gpu.Texture.Format, swap_chain_format: gpu.Texture.Format,
/// The amount of time (in seconds) that has passed since the last frame was rendered.
///
/// For example, if you are animating a cube which should rotate 360 degrees every second,
/// instead of writing (360.0 / 60.0) and assuming the frame rate is 60hz, write
/// (360.0 * app.delta_time)
delta_time: f64 = 0,
delta_time_ns: u64 = 0,
// Internals // Internals
native_instance: gpu.NativeInstance, native_instance: gpu.NativeInstance,
surface: ?gpu.Surface, surface: ?gpu.Surface,
current_desc: gpu.SwapChain.Descriptor, current_desc: gpu.SwapChain.Descriptor,
target_desc: gpu.SwapChain.Descriptor, target_desc: gpu.SwapChain.Descriptor,
timer: std.time.Timer,
const Self = @This(); const Self = @This();
@ -217,6 +226,7 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type {
.surface = surface, .surface = surface,
.swap_chain = swap_chain, .swap_chain = swap_chain,
.swap_chain_format = swap_chain_format, .swap_chain_format = swap_chain_format,
.timer = try std.time.Timer.start(),
.current_desc = descriptor, .current_desc = descriptor,
.target_desc = descriptor, .target_desc = descriptor,
}; };
@ -236,6 +246,9 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type {
while (!app.window.shouldClose()) { while (!app.window.shouldClose()) {
try glfw.pollEvents(); try glfw.pollEvents();
app.delta_time_ns = app.timer.lap();
app.delta_time = @intToFloat(f64, app.delta_time_ns) / @intToFloat(f64, std.time.ns_per_s);
var framebuffer_size = try app.window.getFramebufferSize(); var framebuffer_size = try app.window.getFramebufferSize();
app.target_desc.width = framebuffer_size.width; app.target_desc.width = framebuffer_size.width;
app.target_desc.height = framebuffer_size.height; app.target_desc.height = framebuffer_size.height;
@ -257,7 +270,6 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type {
} }
try funcs.frame(app, app.context); try funcs.frame(app, app.context);
std.time.sleep(16 * std.time.ns_per_ms); // TODO: this is very naive
} }
} }
}; };