From 817552caa99a34e752d13c5bd42f14251604d075 Mon Sep 17 00:00:00 2001 From: Tilo-K Date: Fri, 16 Jan 2026 22:38:53 +0100 Subject: [PATCH] feat: generate image --- .gitignore | 1 + src/main.zig | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index d8c8979..8f5321e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .zig-cache zig-out +*.ppm diff --git a/src/main.zig b/src/main.zig index a870073..3c2b055 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,13 +1,29 @@ const std = @import("std"); pub fn main() !void { - var allocator = std.heap.ArenaAllocator{}; - defer allocator.deinit(); + const img_width = 512; + const img_height = 512; - const alloc = allocator.allocator(); + var buf: [img_width * img_height * 3]u8 = undefined; - const hello_world = try std.fmt.allocPrint(alloc, "Hello, World!\n", .{}); - defer allocator.free(hello_world); + const stdout = std.fs.File.stdout(); + var writer_f: std.fs.File.Writer = stdout.writer(&buf); + var writer = &writer_f.interface; - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + try writer.print("P3\n{d} {d}\n255\n", .{ img_width, img_height }); + try writer.flush(); + for (0..img_height) |j| { + for (0..img_width) |i| { + const r = @as(f64, @floatFromInt(j)) / (img_width - 1); + const g = 0.0; + const b = @as(f64, @floatFromInt(i)) / (img_height - 1); + + const ir = @as(i64, @intFromFloat(r * 255.999)); + const ig = @as(i64, @intFromFloat(g * 255.999)); + const ib = @as(i64, @intFromFloat(b * 255.999)); + + try writer.print("{d} {d} {d}\n", .{ ir, ig, ib }); + } + } + try writer.flush(); }