diff --git a/src/main.zig b/src/main.zig index f572339..7f8ac19 100644 --- a/src/main.zig +++ b/src/main.zig @@ -948,8 +948,9 @@ pub fn main(init: std.process.Init) u8 { const monitor_update_hz: f64 = 30; const game_update_hz: f64 = monitor_update_hz; const frame_target_time_s: f64 = 1.0 / game_update_hz; - const frame_target_time_ms: f64 = std.time.ms_per_s * frame_target_time_s; - const frame_target_time_ns = std.Io.Duration.fromNanoseconds(std.time.ns_per_ms * @as(u64, @intFromFloat(frame_target_time_ms))); + // const frame_target_time_ms: f64 = std.time.ms_per_s * frame_target_time_s; + // const frame_target_time_ns = std.Io.Duration.fromNanoseconds(std.time.ns_per_ms * @as(u64, @intFromFloat(frame_target_time_ms))); + // const frame_target_time = std.Io.Duration.fromNanoseconds(@intFromFloat(std.time.ns_per_s * frame_target_time_s)); // Memory var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); @@ -973,7 +974,7 @@ pub fn main(init: std.process.Init) u8 { std.debug.print("[Wayland] Added frame callback with ret {d}.\n", .{ret}); } - var start = std.Io.Clock.Timestamp.now(io, clock); + var start = clock.now(io); var frame_count: u32 = 0; @@ -985,41 +986,7 @@ pub fn main(init: std.process.Init) u8 { }, }; while (wl_state.running) { - // pollEvents(&wl_state, &fds); - var count: u32 = 0; - outer: while (true) { - const cont = std.posix.poll(&fds, 0) catch |err| { - switch (err) { - error.Unexpected => { - std.debug.print("Poll error: Unexpected.\n", .{}); - }, - error.SystemResources => { - std.debug.print("Poll error: SystemResources: Kernel has no space for table allocations.\n", .{}); - }, - else => { - std.debug.print("Poll error: network stuff idk.\n", .{}); - }, - } - - return 0; - }; - - if (cont == 0) break; - - for (fds) |fd| { - if (fd.revents & std.posix.POLL.IN > 0) { - const ret = c.wl_display_dispatch(wl_state.display); - count += 1; - if (ret < 0) { - std.debug.print("Dispatch error {d}.\n", .{ret}); - wl_state.running = false; - break :outer; - } - } - } - } - - std.debug.print("[Wayland] Polled and dispatched {d} events.\n", .{count}); + pollEvents(&wl_state, &fds); if (wl_state.resize) { const new_pool = createSharedMemoryPool(wl_state) catch { @@ -1077,23 +1044,21 @@ pub fn main(init: std.process.Init) u8 { const render_end_time = clock.now(io); const render_time = render_start_time.durationTo(render_end_time); - std.debug.print("[Wayland] Render: {f}ms.\n", .{render_time}); + std.debug.print("[Wayland] Render: {f}.\n", .{render_time}); - var sleep_time = std.Io.Timestamp.now(io, clock); - sleep_time = sleep_time.addDuration(frame_target_time_ns); - if (render_time.toNanoseconds() < frame_target_time_ns.toNanoseconds()) { - sleep_time = sleep_time.subDuration(render_time); - std.debug.print("[Wayland] Sleeping: {d}ms.\n", .{sleep_time}); - const with_clock = std.Io.Timestamp.withClock(sleep_time, clock); - const timeout = std.Io.Timeout{ .deadline = with_clock }; - timeout.sleep(io) catch { - std.debug.print("[Wayland] Error sleeping\n", .{}); - return 1; - }; - } else { - sleep_time = std.Io.Timestamp.zero; - std.debug.print("[Wayland] MISSED FRAME\n", .{}); - } + // if (render_time.toNanoseconds() < frame_target_time_ns.toNanoseconds()) { + // const sleep_duration: std.Io.Clock.Duration = .{ + // .raw = .{ .nanoseconds = frame_target_time.nanoseconds - render_time.nanoseconds }, + // .clock = clock, + // }; + // std.debug.print("[Wayland] Sleeping: {f}.\n", .{sleep_duration.raw}); + // sleep_duration.sleep(io) catch { + // std.debug.print("[Wayland] Error sleeping\n", .{}); + // return 1; + // }; + // } else { + // std.debug.print("[Wayland] MISSED FRAME\n", .{}); + // } c.wl_surface_attach(wl_state.surface, wl_state.buffer, 0, 0); c.wl_surface_damage_buffer(wl_state.surface, 0, 0, wl_state.window_width, wl_state.window_height); @@ -1103,16 +1068,14 @@ pub fn main(init: std.process.Init) u8 { wl_state.frame_commited = false; - // std.posix.nanosleep(0, std.time.ns_per_ms * 32); - - const last = std.Io.Clock.Timestamp.now(io, clock); + const last = clock.now(io); const current = start.durationTo(last); start = last; // std.debug.print( // "[Wayland] Render: {d:2.3}ms, Sleeping: {d:2.3}ms, Frame: {d:.3}ms.\n", // .{ render_time_ms, sleep_time_ms, current_ms }, // ); - std.debug.print("[Wayland] Frame done: {f}ms.\n", .{current.raw}); + std.debug.print("[Wayland] Frame done: {f}.\n", .{current}); frame_count += 1; }