Removed the leak, but still lost on the game update loop

This commit is contained in:
Nate Anderson
2025-01-22 10:51:04 -07:00
parent 7716ddf26a
commit 4b186784bc
3 changed files with 75 additions and 58 deletions
+21 -15
View File
@@ -38,6 +38,9 @@ pub fn main() !void {
var quit = false;
var game_timer = Timer{};
game_timer.start();
var frame_time = Timer{};
frame_time.start();
@@ -72,9 +75,10 @@ pub fn main() !void {
// Wait for time to render screen, keep running physics
accumulator = accumulator + SCREEN_TICKS_PER_FRAME;
const total_phys_ticks: u32 = @intFromFloat(PHYS_TICKS_PER_UPDATE * PHYS_UPDATES_PER_RENDER);
const phys_done_at_ticks: u32 = sdl.SDL_GetTicks() + total_phys_ticks;
const game_time_ms: u32 = @intFromFloat(game_timer.getTicks());
const phys_done_at_ticks: u32 = game_time_ms + total_phys_ticks;
var phys_ticks_used: f64 = 0;
std.debug.print("phys ticks/upd: {d}, Acc: {d}\nTotal phys ticks {d}ms\nPhys done at {d}ms\n", .{ PHYS_TICKS_PER_UPDATE, accumulator, total_phys_ticks, phys_done_at_ticks });
std.debug.print("Phys ticks/upd: {d}\nAcc: {d}\nTotal phys ticks {d}ms\nPhys done at {d}ms\n", .{ PHYS_TICKS_PER_UPDATE, accumulator, total_phys_ticks, phys_done_at_ticks });
// Run a physics update
while (accumulator >= PHYS_TICKS_PER_UPDATE) {
// Control loop here
@@ -86,6 +90,7 @@ pub fn main() !void {
},
sdl.SDL_KEYDOWN => {
switch (event.key.keysym.sym) {
sdl.SDLK_RETURN => {},
else => {},
}
log("Got key event: %i\n", event.key.keysym.sym);
@@ -96,23 +101,24 @@ pub fn main() !void {
prev_game_state = current_game_state;
current_game_state = try game_state_controller.update_tick();
const phys_ticks_this_update: f64 = current_game_state.?.phys_ticks - phys_ticks_used;
accumulator -= phys_ticks_this_update;
std.debug.print(" PU in {d}ms ", .{phys_ticks_this_update});
std.debug.print(" Acc:{d} - ", .{accumulator});
// std.debug.print(" PU in {d}ms ", .{phys_ticks_this_update});
// std.debug.print(" Acc:{d} - ", .{accumulator});
// TODO dont turn it into an int, take the extra phys_frames and do a partial render
if (phys_ticks_this_update < PHYS_TICKS_PER_UPDATE) {
const ticks: u32 = @intFromFloat(PHYS_TICKS_PER_UPDATE - phys_ticks_this_update);
sdl.SDL_Delay(ticks);
accumulator -= PHYS_TICKS_PER_UPDATE;
} else {
std.debug.print("\nPHYSICS JANK\nPhysics Update took {d}ms, target: {d}ms\n", .{ phys_ticks_this_update, PHYS_TICKS_PER_UPDATE });
accumulator -= phys_ticks_this_update;
}
// if (phys_ticks_this_update < PHYS_TICKS_PER_UPDATE) {
// const ticks: u32 = @intFromFloat(PHYS_TICKS_PER_UPDATE - phys_ticks_this_update);
// sdl.SDL_Delay(ticks);
// const ticksf: f64 = @floatFromInt(ticks);
// accumulator -= (phys_ticks_this_update + ticksf);
// } else {
// // std.debug.print("PHYSICS JANK: Physics Update took {d}ms, target: {d}ms\n", .{ phys_ticks_this_update, PHYS_TICKS_PER_UPDATE });
// accumulator -= phys_ticks_this_update;
// }
phys_ticks_used = current_game_state.?.phys_ticks;
std.debug.print(" PhyTicks this render: {d} ", .{current_game_state.?.phys_ticks});
// std.debug.print(" PhyTicks this render: {d} ", .{current_game_state.?.phys_ticks});
}
// TODO lerp together prev and current states
// Otherwise will have physics jank
@@ -120,7 +126,7 @@ pub fn main() !void {
std.debug.print("Took {d} phys frames\n", .{current_game_state.?.phys_updates_since_render});
try game_state_controller.render(current_game_state.?);
prev_frame_ms = @floatFromInt(frame_time.getTicks());
prev_frame_ms = frame_time.getTicks();
std.debug.print("Rendered to screen in {d}ms\n***\n***\n", .{prev_frame_ms});
frame_time.reset();
// const frame_time_ticks = frame_time.getTicks();