Add simple FPS cap
This commit is contained in:
parent
4bd2bff03e
commit
de68ccac0d
1 changed files with 55 additions and 9 deletions
64
src/main.zig
64
src/main.zig
|
@ -6,6 +6,52 @@ const zm = @import("zmath");
|
||||||
const VulkanRenderer = @import("vulkan_renderer.zig").VulkanRenderer;
|
const VulkanRenderer = @import("vulkan_renderer.zig").VulkanRenderer;
|
||||||
const Vector3 = @import("utilities.zig").Vector3;
|
const Vector3 = @import("utilities.zig").Vector3;
|
||||||
|
|
||||||
|
// 100 fps
|
||||||
|
const TARGET_MS_F32: f32 = 16.0;
|
||||||
|
|
||||||
|
const Delta = struct {
|
||||||
|
current_time: u64 = 0,
|
||||||
|
last_time: u64 = 0,
|
||||||
|
|
||||||
|
delta_ms: u64 = 0,
|
||||||
|
delta_f32: f32 = 0.0,
|
||||||
|
|
||||||
|
delay_amount: u32 = 0,
|
||||||
|
|
||||||
|
perf_frequency: f64,
|
||||||
|
|
||||||
|
_counter: u32 = 0,
|
||||||
|
|
||||||
|
pub fn new() Delta {
|
||||||
|
return .{
|
||||||
|
.perf_frequency = @as(f64, @floatFromInt(sdl.getPerformanceFrequency())),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start(self: *Delta) void {
|
||||||
|
self.last_time = sdl.getPerformanceCounter();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick(self: *Delta) void {
|
||||||
|
self.current_time = sdl.getPerformanceCounter();
|
||||||
|
self.delta_ms = self.current_time - self.last_time;
|
||||||
|
|
||||||
|
const delta_f64 = @as(f64, @floatFromInt(self.delta_ms)) / (self.perf_frequency * 1000.0);
|
||||||
|
self.delta_f32 = @as(f32, @floatCast(delta_f64));
|
||||||
|
|
||||||
|
self.delay_amount = @as(u32, @intFromFloat(@floor(TARGET_MS_F32 - self.delta_f32)));
|
||||||
|
|
||||||
|
// Limit the output to the terminal
|
||||||
|
if (self._counter % 100 == 0) {
|
||||||
|
const fps = self.perf_frequency / @as(f64, @floatFromInt(self.delta_ms));
|
||||||
|
std.debug.print("ms/f: {d}\tFPS: {d}\n", .{ delta_f64 * 1000.0, fps });
|
||||||
|
}
|
||||||
|
|
||||||
|
self.last_time = self.current_time;
|
||||||
|
self._counter += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const window = try initWindow();
|
const window = try initWindow();
|
||||||
defer sdl.quit();
|
defer sdl.quit();
|
||||||
|
@ -19,10 +65,10 @@ pub fn main() !void {
|
||||||
var vulkan_renderer = try VulkanRenderer.init(window, allocator);
|
var vulkan_renderer = try VulkanRenderer.init(window, allocator);
|
||||||
defer vulkan_renderer.deinit();
|
defer vulkan_renderer.deinit();
|
||||||
|
|
||||||
|
var delta = Delta.new();
|
||||||
|
delta.start();
|
||||||
|
|
||||||
var angle: f32 = 0.0;
|
var angle: f32 = 0.0;
|
||||||
var now: u64 = sdl.getPerformanceCounter();
|
|
||||||
var last: u64 = 0;
|
|
||||||
var delta_time: f32 = 0.0;
|
|
||||||
|
|
||||||
mainLoop: while (true) {
|
mainLoop: while (true) {
|
||||||
while (sdl.pollEvent()) |ev| {
|
while (sdl.pollEvent()) |ev| {
|
||||||
|
@ -37,11 +83,9 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
last = now;
|
delta.tick();
|
||||||
now = sdl.getPerformanceCounter();
|
|
||||||
delta_time = @as(f32, @floatFromInt(now - last)) * 1000.0 / @as(f32, @floatFromInt(now));
|
|
||||||
|
|
||||||
angle += 10.0 * delta_time;
|
angle += 200.0 * delta.delta_f32;
|
||||||
|
|
||||||
if (angle > 360.0) {
|
if (angle > 360.0) {
|
||||||
angle -= 360.0;
|
angle -= 360.0;
|
||||||
|
@ -50,13 +94,15 @@ pub fn main() !void {
|
||||||
var first_model = zm.rotationZ(angle);
|
var first_model = zm.rotationZ(angle);
|
||||||
var second_model = zm.rotationZ(-angle * 2);
|
var second_model = zm.rotationZ(-angle * 2);
|
||||||
|
|
||||||
first_model = zm.mul(first_model, zm.translation(0.0, 0.0, -2.5));
|
first_model = zm.mul(first_model, zm.translation(1.0, 0.0, -2.5));
|
||||||
second_model = zm.mul(second_model, zm.translation(0.0, 0.0, -4.5));
|
second_model = zm.mul(second_model, zm.translation(-1.0, 0.0, -4.5));
|
||||||
|
|
||||||
try vulkan_renderer.updateModel(0, first_model);
|
try vulkan_renderer.updateModel(0, first_model);
|
||||||
try vulkan_renderer.updateModel(1, second_model);
|
try vulkan_renderer.updateModel(1, second_model);
|
||||||
|
|
||||||
try vulkan_renderer.draw();
|
try vulkan_renderer.draw();
|
||||||
|
|
||||||
|
sdl.delay(delta.delay_amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue