diff --git a/main.zig b/main.zig index 781d9e6..72d45f4 100644 --- a/main.zig +++ b/main.zig @@ -7,6 +7,8 @@ const stdout = std.io.getStdOut(); const out = stdout.writer(); const stdin = std.io.getStdIn().reader(); const sleep = std.time.sleep; +const primitive = @import("primitives.zig"); +const Point = primitive.Point; const c = @cImport({ @cDefine("_NO_CRT_STDIO_INLINE", "1"); @@ -20,27 +22,6 @@ const Winsize = struct { ws_xpixel: c_ushort = 0, ws_ypixel: c_ushort = 0, }; -const Direction = struct { - x: i8 = 1, - y: i8 = 1, - z: i8 = 1, -}; -const Point = struct { - x: i32, - y: i32, - z: i32, - direction: Direction, //for movement state -}; -const Vec3 = struct { - a: Point, - b: Point, - c: Point, -}; -const Triangle = struct { - bufa: *i32, - bufb: *i32, - bufc: *i32, -}; pub fn main() !void { var win = Winsize{}; @@ -83,18 +64,6 @@ fn draw() !void { var buffer = try allocator.alloc(u8, buffer_size); //utf-8 can be 4 bytes long defer allocator.free(buffer); - - var vec = Vec3{ - .a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - .b = Point{ .x = 50, .y = 40, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - .c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - }; - var vec2 = Vec3{ - .a = Point{ .x = 20, .y = 20, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - .b = Point{ .x = 50, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - .c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, - }; - _ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); //video loop while (true) { @@ -109,31 +78,31 @@ fn draw() !void { slice[bytes_written + ii] = 0; } } - try triangle(buffer, w, vec); + try triangle(buffer, w, primitive.vec); //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 }); try out.print("\x1B[2J", .{}); //sleep(100000); - try triangle(buffer, w, vec); - mv_axis_border_bounce(w, &vec.a, 'x'); - mv_axis_border_bounce(w, &vec.a, 'y'); - mv_axis_border_bounce(w, &vec.b, 'x'); - mv_axis_border_bounce(w, &vec.b, 'y'); - mv_axis_border_bounce(w, &vec.c, 'x'); - mv_axis_border_bounce(w, &vec.c, 'y'); + try triangle(buffer, w, primitive.vec); + mv_axis_border_bounce(w, &primitive.vec.a, 'x'); + mv_axis_border_bounce(w, &primitive.vec.a, 'y'); + mv_axis_border_bounce(w, &primitive.vec.b, 'x'); + mv_axis_border_bounce(w, &primitive.vec.b, 'y'); + mv_axis_border_bounce(w, &primitive.vec.c, 'x'); + mv_axis_border_bounce(w, &primitive.vec.c, 'y'); - try triangle(buffer, w, vec2); - mv_axis_border_bounce(w, &vec2.a, 'x'); - mv_axis_border_bounce(w, &vec2.a, 'y'); - mv_axis_border_bounce(w, &vec2.b, 'x'); - mv_axis_border_bounce(w, &vec2.b, 'Y'); - mv_axis_border_bounce(w, &vec2.c, 'x'); - mv_axis_border_bounce(w, &vec2.c, 'y'); + try triangle(buffer, w, primitive.vec2); + mv_axis_border_bounce(w, &primitive.vec2.a, 'x'); + mv_axis_border_bounce(w, &primitive.vec2.a, 'y'); + mv_axis_border_bounce(w, &primitive.vec2.b, 'x'); + mv_axis_border_bounce(w, &primitive.vec2.b, 'Y'); + mv_axis_border_bounce(w, &primitive.vec2.c, 'x'); + mv_axis_border_bounce(w, &primitive.vec2.c, 'y'); try fill_draw(w, buffer); try out.print("{s}", .{buffer}); - _ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); + //_ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); sleep(30000000); } } @@ -168,7 +137,7 @@ fn fill_draw(w: Winsize, buffer: []u8) !void { } } if (ii > start and ii < end and fill == 1 and std.mem.eql(u8, utf_slice, &code) == false) { - _ = try std.unicode.utf8Encode('X', utf_slice); + _ = try std.unicode.utf8Encode('●', utf_slice); } } } @@ -305,7 +274,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { } } -fn triangle(buffer: []u8, w: Winsize, vec: Vec3) !void { +fn triangle(buffer: []u8, w: Winsize, vec: primitive.Vec3) !void { try bresenham(buffer, w, vec.a, vec.b); try bresenham(buffer, w, vec.b, vec.c); try bresenham(buffer, w, vec.c, vec.a); diff --git a/primitives.zig b/primitives.zig new file mode 100644 index 0000000..c1970f0 --- /dev/null +++ b/primitives.zig @@ -0,0 +1,31 @@ +pub const Direction = struct { + x: i8 = 1, + y: i8 = 1, + z: i8 = 1, +}; +pub const Point = struct { + x: i32, + y: i32, + z: i32, + direction: Direction, //for movement state +}; +pub const Vec3 = struct { + a: Point, + b: Point, + c: Point, +}; +pub const Triangle = struct { + bufa: *i32, + bufb: *i32, + bufc: *i32, +}; +pub var vec = Vec3{ + .a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, + .b = Point{ .x = 50, .y = 40, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, + .c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, +}; +pub var vec2 = Vec3{ + .a = Point{ .x = 20, .y = 20, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, + .b = Point{ .x = 50, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, + .c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } }, +};