From 149cf2a9b4af56a16afe6ee3eedd9e9c09c5d52d Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 24 Nov 2021 10:41:04 -0700 Subject: [PATCH] Added rgb function --- lib/minifb/src/minifb.zig | 6 +++++- src/main.zig | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 874149b..f7b923f 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -5,6 +5,10 @@ const minifb_c = @cImport({ }); const testing = std.testing; +pub fn rgb(r: u8, g: u8, b: u8) u32 { + return @intCast(u32, r) << 16 | @intCast(u32, g) << 8 | b; +} + pub const Window = struct{ cwin: *minifb_c.mfb_window, @@ -41,5 +45,5 @@ pub const Window = struct{ } } - + }; diff --git a/src/main.zig b/src/main.zig index a483b2e..fd270cf 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,12 +5,16 @@ const minifb = @import("minifb"); const Width = 800; const Height = 600; +fn grey(value: u8) u32 { + return minifb.rgb(value, value, value); +} + pub fn main() anyerror!void { std.log.info("All your codebase are belong to us.", .{}); var win = minifb.Window.open("Hello minifb-zig", Width, Height) catch unreachable; - var color: u32 = 0xff000000; - const deltaColor: u32 = 0x00010101; + var color: u32 = grey(0); + const deltaColor = grey(1); var increasing = true; var rawBuffer: [Width*Height]u32 = undefined; const buffer = rawBuffer[0..rawBuffer.len]; @@ -18,13 +22,13 @@ pub fn main() anyerror!void { while (win.waitSync()) { if (increasing) { - if (color < 0xffffffff) { + if (color < grey(255)) { color += deltaColor; } else { increasing = false; } } else { - if (color > 0xff000000) { + if (color > grey(0)) { color -= deltaColor; } else { increasing = true; @@ -33,7 +37,7 @@ pub fn main() anyerror!void { std.mem.set(u32, buffer, color); var i: u32 = 0; while (i < Width) { - buffer[i] = 0xffff0000; + buffer[i] = minifb.rgb(255,0,0); i += 1; } _ = win.update(buffer) catch unreachable;