From 91f12439280eee0fb6e3c8f922882a598f6844ab Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Mon, 19 Dec 2022 00:20:15 +0100 Subject: 3.3 --- src/main.zig | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 9983774..2d42772 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,23 +1,37 @@ const std = @import("std"); +const warn = @import("std").debug.print; + +pub fn write_color(stdout: anytype, v: vec) !void { + const r = @floatToInt(usize, v[0] * 255.999); + const g = @floatToInt(usize, v[1] * 255.999); + const b = @floatToInt(usize, v[2] * 255.999); + try stdout.print("{} {} {}\n", .{ r, g, b }); +} + +const vec = @Vector(3, f32); pub fn main() !void { - const width = 256; - const height = 256; - std.debug.print("P3\n{} {}\n255\n", .{ width, height }); + const width = 512; + const height = 512; + const stdout_file = std.io.getStdOut().writer(); + var bw = std.io.bufferedWriter(stdout_file); + const stdout = bw.writer(); + + try stdout.print("P3\n{} {}\n255\n", .{ width, height }); var j: usize = height; while (j > 0) : (j -= 1) { + warn("remaining: {}\n", .{j}); var i: usize = 0; while (i < width) : (i += 1) { - var r: f32 = @intToFloat(f32, i) / (width - 1); - var g: f32 = @intToFloat(f32, j) / (height - 1); - var b: f32 = 0.25; - - var ir: usize = @floatToInt(usize, r * 255.999); - var ig: usize = @floatToInt(usize, g * 255.999); - var ib: usize = @floatToInt(usize, b * 255.999); + var color = vec{ + @intToFloat(f32, i) / (width - 1), + @intToFloat(f32, j) / (height - 1), + 0.25, + }; - std.debug.print("{} {} {}\n", .{ ir, ig, ib }); + try write_color(stdout, color); } } + try bw.flush(); } -- cgit v1.2.3