From b8a9788d651a2e73ac27135790f22c20482472ff Mon Sep 17 00:00:00 2001 From: jonathan santis Date: Thu, 24 Oct 2024 12:41:37 +0200 Subject: [PATCH] little demo --- main.zig | 100 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 29 deletions(-) diff --git a/main.zig b/main.zig index 6bc5e7d..204e557 100644 --- a/main.zig +++ b/main.zig @@ -3,7 +3,9 @@ const expect = std.testing.expect; const print = std.debug.print; const getenv = std.posix.getenv; const ioctl = std.c.ioctl; - +const stdout = std.io.getStdOut(); +const out = stdout.writer(); +const stdin = std.io.getStdIn().reader(); const c = @cImport({ @cDefine("_NO_CRT_STDIO_INLINE", "1"); @cInclude("sys/ioctl.h"); @@ -116,42 +118,82 @@ fn fillBuffer() !void { var w = Winsize{}; var i: u64 = 0; + var buf = std.mem.zeroes([2]u8); + var d: i8 = 1; + var d2: i8 = 1; + var d3: i8 = -1; + var d4: i8 = -1; + + buf[0] = 0; w = try getTermDimension(); const file = try std.fs.cwd().createFile("buffer.out", .{}); defer file.close(); var buffer = try allocator.alloc(u8, w.ws_row * w.ws_col * 4); //utf-8 can be 4 bytes long defer allocator.free(buffer); - - buffer[0] = 0; - while (i < buffer.len) : (i += 4) { - const slice = buffer[i .. i + 4]; - _ = try std.unicode.utf8Encode('\u{2588}', slice); - } - try pixel(buffer, w, 5, 10, '\u{1F702}'); - const vec = Vec3{ + var vec = Vec3{ .a = Point{ .x = 20, .y = 20, .z = 0 }, .b = Point{ .x = 50, .y = 30, .z = 0 }, .c = Point{ .x = 30, .y = 1, .z = 0 }, }; - try triangle(buffer, w, vec); - //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 28, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 39, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 30, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 60, .y = 30, .z = 0 }, Point{ .x = 70, .y = 5, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 100, .y = 20, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 100, .y = 10, .z = 0 }, Point{ .x = 90, .y = 40, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 90, .y = 10, .z = 0 }); - //_ = try bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 }); - try file.writeAll(buffer); + + while (true) { + buffer[0] = 0; + i = 0; + while (i < buffer.len) : (i += 4) { + const slice = buffer[i .. i + 4]; + _ = try std.unicode.utf8Encode('_', slice); + //slice[0] = 'm'; + } + try pixel(buffer, w, 5, 10, '\u{1F702}'); + try triangle(buffer, w, vec); + //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 }); + // try file.writeAll(buffer); + + if (vec.c.x < w.ws_col) { + vec.c.x += d; + } + if (vec.c.x == w.ws_col or vec.c.x == 0) { + d *= -1; + vec.c.x += d; + } + if (vec.a.y < w.ws_row) { + vec.a.y += d2; + } + if (vec.a.y == w.ws_row or vec.a.y == 0) { + d2 *= -1; + vec.a.y += d2; + } + if (vec.b.y < w.ws_row) { + vec.b.y += d3; + } + if (vec.b.y == w.ws_row or vec.b.y == 0) { + d3 *= -1; + vec.b.y += d3; + } + if (vec.b.x < w.ws_col) { + vec.b.x += d4; + } + if (vec.b.x == w.ws_col or vec.b.x == 0) { + d4 *= -1; + vec.b.x += d4; + } + + try triangle(buffer, w, vec); + try out.print("\x1B[2J", .{}); + + print("\x1b[45\x1b[37", .{}); + try out.print("{s}", .{buffer}); + + _ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); + } } fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void { const i: usize = @intCast(4 * (x + y * w.ws_col)); if (i < buffer.len) { //-1 ?? const slice = buffer[i .. i + 4]; _ = try std.unicode.utf8Encode(symbol, slice); + print("\x1b[46\x1b[43", .{}); } else { print("Error illegal memory access\n", .{}); } @@ -173,7 +215,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (dx < dy and dy > 0 and dx > 0) { while (y <= p2.y) : (y += 1) { - _ = try pixel(buf, w, x, y, ','); + _ = try pixel(buf, w, x, y, '.'); er += dx; if (2 * er >= dy) { x += 1; @@ -182,7 +224,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (@abs(dy) < dx and dy < 0 and dx > 0) { while (x <= p2.x) : (x += 1) { - _ = try pixel(buf, w, x, y, '🡇'); + _ = try pixel(buf, w, x, y, '.'); er -= dy; if (2 * er >= dx) { y -= 1; @@ -191,7 +233,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (@abs(dy) > dx and dy < 0 and dx > 0) { while (y >= p2.y) : (y -= 1) { - _ = try pixel(buf, w, x, y, '='); + _ = try pixel(buf, w, x, y, '.'); er += dx; if (2 * er >= dy) { x += 1; @@ -200,7 +242,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (@abs(dx) <= dy and dx < 0 and dy > 0) { while (y <= p2.y) : (y += 1) { - _ = try pixel(buf, w, x, y, '5'); + _ = try pixel(buf, w, x, y, '.'); er += @abs(dx); if (2 * er >= dy) { x -= 1; @@ -209,7 +251,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (@abs(dx) > dy and dy > 0 and dx < 0) { while (x >= p2.x) : (x -= 1) { - _ = try pixel(buf, w, x, y, '6'); + _ = try pixel(buf, w, x, y, '.'); er += dy; if (2 * er >= @abs(dx)) { y += 1; @@ -218,7 +260,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (dx <= dy and dx < 0 and dy < 0) { while (x >= p2.x) : (x -= 1) { - _ = try pixel(buf, w, x, y, '7'); + _ = try pixel(buf, w, x, y, '.'); er += @abs(dy); if (2 * er >= @abs(dx)) { y -= 1; @@ -227,8 +269,8 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } else if (dx > dy and dy < 0 and dx < 0) { while (y >= p2.y) : (y -= 1) { - print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); - _ = try pixel(buf, w, x, y, '8'); + //print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); + _ = try pixel(buf, w, x, y, '.'); er += @abs(dx); if (2 * er >= @abs(dx)) { x -= 1;