~kikoodx/chazo

6f5c0ac7013e5888365be65cd322660469ab1a0e — KikooDX 3 months ago cd24c59
done

only remains stdin fetching
2 files changed, 35 insertions(+), 3 deletions(-)

M src/main.zig
M src/uwo.zig
M src/main.zig => src/main.zig +25 -2
@@ 5,9 5,32 @@ pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    const image = try uwo.UwoImage.load(allocator, "test.uwo");
    var image = try uwo.UwoImage.load(allocator, "test.uwo");
    defer image.deinit(allocator);

    if (image.map_size > 0) {
        var max: u32 = image.map_size / 8;
        if (image.map_size % 8 != 0) {
            max += 1;
        }
        var used = [8]u32{ 0, 0, 0, 0, 0, 0, 0, 0 };
        var rnd = std.rand.DefaultPrng.init(@bitCast(u64, std.time.timestamp()));
        var i: usize = 0;
        while (i < image.map_size) : (i += 1) {
            var r: u8 = rnd.random().int(u3);
            while (used[r] == max) {
                r = rnd.random().int(u3);
            }
            std.log.info("{}", .{r});
            used[r] += 1;
            image.map[i * 3 + 0] = if ((r & 1) != 0) 255 else 0;
            image.map[i * 3 + 1] = if ((r & 2) != 0) 255 else 0;
            image.map[i * 3 + 2] = if ((r & 4) != 0) 255 else 0;
            std.log.info("{}, {any}", .{ i, image.map[i * 3 .. i * 3 + 3] });
            std.log.info("{any}", .{image.map[0 .. image.map_size * 3]});
        }
    }

    const stdout = std.io.getStdOut().writer();
    try stdout.print("{}", .{image});
    try image.write(stdout);
}

M src/uwo.zig => src/uwo.zig +10 -1
@@ 55,7 55,7 @@ pub const UwoImage = struct {

        if (map_size > 0) {
            var i: usize = 0;
            while (i < map_size) : (i += 1) {
            while (i < map_size * 3) : (i += 1) {
                image.map[i] = buf[buf_cursor + i];
            }
            buf_cursor += i;


@@ 78,4 78,13 @@ pub const UwoImage = struct {
    pub fn deinit(this: UwoImage, allocator: Allocator) void {
        allocator.free(this.data);
    }

    pub fn write(this: UwoImage, writer: anytype) !void {
        const header: []const u8 = &[_]u8{ 'u', 'w', 'o', @truncate(u8, this.width % 256), @truncate(u8, this.width / 256), @truncate(u8, this.height % 256), @truncate(u8, this.height / 256), this.map_size };
        try writer.writeAll(header);
        if (this.map_size > 0) {
            try writer.writeAll(this.map[0 .. this.map_size * 3]);
        }
        try writer.writeAll(this.data);
    }
};