From efba4456e75e9ef89ee07d84246eda23d70d1e96 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 24 Nov 2021 17:01:12 -0700 Subject: [PATCH] Make the example more interesting --- src/main.zig | 109 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/src/main.zig b/src/main.zig index d497ef6..03bb199 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,6 +1,7 @@ -const std = @import("std"); - const mfb = @import("minifb"); +const std = @import("std"); +const min = std.math.min; +const max = std.math.max; const Width = 800; const Height = 600; @@ -9,38 +10,102 @@ fn grey(value: u8) u32 { return mfb.rgb(value, value, value); } +const Coords = struct { + x: i32, + y: i32, +}; + +const State = struct { + bgValue: u8, + increasing: bool, +}; + +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)); +} + +const Buffer = struct { + alloc: *std.mem.Allocator, + slice: []u32, + width: u32, + height: u32, + + pub fn init(alloc: *std.mem.Allocator, width: u32, height: u32) anyerror!Buffer { + const slice = try alloc.alloc(u32, width*height); + return Buffer { + .alloc=alloc, + .slice=slice, + .width=width, + .height=height, + }; + } + + pub fn deinit(self: Buffer) void { + self.alloc.free(self.slice); + } + + pub fn setPixel(self: Buffer, coords: Coords, color: u32) void { + const x = @intCast(u32, coords.x); + const y = @intCast(u32, coords.y); + self.slice[y*self.width + x] = color; + } + + pub fn drawRectangle(self: Buffer, a: Coords, b: Coords, color: u32) void { + var start = Coords{ + .x=max(0, min(a.x, b.x)), + .y=max(0, min(a.y, b.y)), + }; + var end = Coords{ + .x=min(@intCast(i32, self.width), max(a.x, b.x)), + .y=min(@intCast(i32, self.height), max(a.y, b.y)), + }; + + var y = start.y; + while (y < end.y) { + var x = start.x; + while (x < end.x) { + self.setPixel(.{.x=x, .y=y}, color); + x += 1; + } + y += 1; + } + } +}; + pub fn main() anyerror!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); - var color: u32 = grey(0); - const deltaColor = grey(1); - var increasing = true; - var rawBuffer: [Width*Height]u32 = undefined; - const buffer = rawBuffer[0..rawBuffer.len]; - std.mem.set(u32, buffer, color); + var state = State { + .increasing=true, + .bgValue=0, + }; + var buf = try Buffer.init(alloc, Width, Height); + defer buf.deinit(); + render(buf, state); while (win.waitSync()) { - if (increasing) { - if (color < grey(255)) { - color += deltaColor; + if (state.increasing) { + if (state.bgValue < 255) { + state.bgValue += 1; } else { - increasing = false; + state.increasing = false; } } else { - if (color > grey(0)) { - color -= deltaColor; + if (state.bgValue > 0) { + state.bgValue -= 1; } else { - increasing = true; + state.increasing = true; } } - std.mem.set(u32, buffer, color); - var i: u32 = 0; - while (i < Width) { - buffer[i] = mfb.rgb(255,0,0); - i += 1; - } - _ = win.update(buffer) catch unreachable; + + render(buf, state); + // TODO Handle mfb state + _ = win.update(buf.slice) catch unreachable; } }