diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 48e9093..e31f2ef 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -63,3 +63,40 @@ test "set and get target FPS" { try std.testing.expectEqual(fps, getTargetFPS()); } } + +pub const Timer = struct { + ctimer: *minifb_c.mfb_timer, + + pub fn init() !Timer { + const ctimer: ?*minifb_c.mfb_timer = minifb_c.mfb_timer_create(); + if (ctimer) |value| { + return Timer {.ctimer=value}; + } else { + return error.ItBroke; + } + } + + pub fn deinit(self: Timer) void { + minifb_c.mfb_timer_destroy(self.ctimer); + } + + pub fn reset(self: Timer) void { + minifb_c.mfb_timer_reset(self.ctimer); + } + + pub fn now(self: Timer) f64 { + return minifb_c.mfb_timer_now(self.ctimer); + } + + pub fn delta(self: Timer) f64 { + return minifb_c.mfb_timer_delta(self.ctimer); + } + + pub fn getFrequency() f64 { + return minifb_c.mfb_timer_get_frequency(); + } + + pub fn getResolution() f64 { + return minifb_c.mfb_timer_get_resolution(); + } +}; \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 03bb199..14b84a6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -16,14 +16,33 @@ const Coords = struct { }; const State = struct { - bgValue: u8, - increasing: bool, -}; + period: f64, + bgValue: u8 = 0, + increasing: bool = true, + timer: mfb.Timer, -fn render(buf: Buffer, state: State) void { - buf.drawRectangle(.{.x=0,.y=0}, .{.x=Width, .y=Height}, grey(state.bgValue)); - buf.drawRectangle(.{.x=Width/3, .y=Height/3}, .{.x=2*Width/3, .y=2*Height/3}, mfb.rgb(255,0,0)); -} + pub fn init(period: f64) !State { + const timer = try mfb.Timer.init(); + return State{ + .period=period, + .timer=timer, + }; + } + + pub fn deinit(self: State) void { + self.timer.deinit(); + } + + pub fn render(self: State, buf: Buffer) void { + var bgValue = @rem(self.timer.now(), self.period) / self.period * 512; + if (bgValue >= 256) { + bgValue = 512 - bgValue; + } + + buf.drawRectangle(.{.x=0,.y=0}, .{.x=Width, .y=Height}, grey(@floatToInt(u8, bgValue))); + buf.drawRectangle(.{.x=Width/3, .y=Height/3}, .{.x=2*Width/3, .y=2*Height/3}, mfb.rgb(255,0,0)); + } +}; const Buffer = struct { alloc: *std.mem.Allocator, @@ -31,7 +50,7 @@ const Buffer = struct { width: u32, height: u32, - pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) anyerror!Buffer { + pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) !Buffer { const slice = try alloc.alloc(u32, width*height); return Buffer { .alloc=alloc, @@ -73,38 +92,23 @@ const Buffer = struct { } }; -pub fn main() anyerror!void { +pub fn main() !void { var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = &gp_allocator.allocator; std.log.info("All your codebase are belong to us.", .{}); var win = mfb.Window.open("Hello minifb-zig", Width, Height) catch unreachable; - mfb.setTargetFPS(60); + mfb.setTargetFPS(7); + + var state = try State.init(3); + defer state.deinit(); - var state = State { - .increasing=true, - .bgValue=0, - }; var buf = try Buffer.init(alloc, Width, Height); defer buf.deinit(); - render(buf, state); + state.render(buf); while (win.waitSync()) { - if (state.increasing) { - if (state.bgValue < 255) { - state.bgValue += 1; - } else { - state.increasing = false; - } - } else { - if (state.bgValue > 0) { - state.bgValue -= 1; - } else { - state.increasing = true; - } - } - - render(buf, state); + state.render(buf); // TODO Handle mfb state _ = win.update(buf.slice) catch unreachable; }