2024-10-14 06:52:08 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const expect = std.testing.expect;
|
|
|
|
const print = std.debug.print;
|
|
|
|
const getenv = std.posix.getenv;
|
2024-10-22 10:59:47 +00:00
|
|
|
const ioctl = std.c.ioctl;
|
2024-10-24 10:41:37 +00:00
|
|
|
const stdout = std.io.getStdOut();
|
|
|
|
const out = stdout.writer();
|
|
|
|
const stdin = std.io.getStdIn().reader();
|
2024-10-24 12:53:49 +00:00
|
|
|
const sleep = std.time.sleep;
|
|
|
|
|
2024-10-22 10:59:47 +00:00
|
|
|
const c = @cImport({
|
|
|
|
@cDefine("_NO_CRT_STDIO_INLINE", "1");
|
|
|
|
@cInclude("sys/ioctl.h");
|
|
|
|
@cInclude("stdio.h");
|
|
|
|
});
|
|
|
|
|
|
|
|
const Winsize = struct {
|
|
|
|
ws_row: c_ushort = 0,
|
|
|
|
ws_col: c_ushort = 0,
|
|
|
|
ws_xpixel: c_ushort = 0,
|
|
|
|
ws_ypixel: c_ushort = 0,
|
|
|
|
};
|
2024-10-24 11:52:21 +00:00
|
|
|
const Direction = struct {
|
|
|
|
x: i8 = 1,
|
|
|
|
y: i8 = 1,
|
|
|
|
z: i8 = 1,
|
|
|
|
};
|
2024-10-22 10:59:47 +00:00
|
|
|
const Point = struct {
|
2024-10-22 12:57:57 +00:00
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
z: i32,
|
2024-10-24 11:52:21 +00:00
|
|
|
direction: Direction, //for movement state
|
2024-10-22 10:59:47 +00:00
|
|
|
};
|
|
|
|
const Vec3 = struct {
|
|
|
|
a: Point,
|
|
|
|
b: Point,
|
|
|
|
c: Point,
|
|
|
|
};
|
2024-10-14 06:52:08 +00:00
|
|
|
|
|
|
|
pub fn main() !void {
|
2024-10-22 10:59:47 +00:00
|
|
|
var win = Winsize{};
|
|
|
|
|
|
|
|
win = try getTermDimension();
|
|
|
|
|
|
|
|
print("winsize.ws_row: {}\nws_line: {}\nws_xpixel: {} \nws_ypixel: {}\n", .{ win.ws_row, win.ws_col, win.ws_xpixel, win.ws_ypixel });
|
2024-10-28 10:29:15 +00:00
|
|
|
try draw();
|
2024-10-14 06:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 10:59:47 +00:00
|
|
|
fn getTermDimension() !Winsize {
|
|
|
|
var w = Winsize{};
|
|
|
|
var ret: u16 = 0;
|
|
|
|
const err = error{CUnknown};
|
|
|
|
//print("stdout_fileno: {}\n", .{@TypeOf(std.c.STDOUT_FILENO)});
|
|
|
|
ret = @intCast(ioctl(std.c.STDOUT_FILENO, c.TIOCGWINSZ, &w));
|
|
|
|
if (ret > 0) {
|
|
|
|
return err.CUnknown;
|
|
|
|
}
|
|
|
|
//print("winsize.ws_row: {}\nws_line: {}\nws_xpixel: {} \nws_ypixel: {}\n", .{ w.ws_row, w.ws_col, w.ws_xpixel, w.ws_ypixel });
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2024-10-28 10:29:15 +00:00
|
|
|
fn draw() !void {
|
2024-10-22 10:59:47 +00:00
|
|
|
const allocator = std.heap.page_allocator;
|
|
|
|
|
|
|
|
var w = Winsize{};
|
|
|
|
var i: u64 = 0;
|
2024-10-25 11:11:22 +00:00
|
|
|
var ii: u64 = 0;
|
2024-10-24 10:41:37 +00:00
|
|
|
var buf = std.mem.zeroes([2]u8);
|
2024-10-25 11:11:22 +00:00
|
|
|
var slice: []u8 = undefined;
|
|
|
|
var bytes_written: u3 = 0;
|
2024-10-25 11:47:53 +00:00
|
|
|
var buffer_size: u32 = 0;
|
2024-10-24 10:41:37 +00:00
|
|
|
|
|
|
|
buf[0] = 0;
|
2024-10-22 10:59:47 +00:00
|
|
|
|
2024-10-25 11:47:53 +00:00
|
|
|
w = try getTermDimension();
|
|
|
|
buffer_size = w.ws_row;
|
|
|
|
buffer_size = buffer_size * w.ws_col * 4;
|
2024-10-25 12:55:26 +00:00
|
|
|
|
2024-10-25 11:47:53 +00:00
|
|
|
var buffer = try allocator.alloc(u8, buffer_size); //utf-8 can be 4 bytes long
|
2024-10-22 10:59:47 +00:00
|
|
|
defer allocator.free(buffer);
|
2024-10-24 12:53:49 +00:00
|
|
|
|
2024-10-24 10:41:37 +00:00
|
|
|
var vec = Vec3{
|
2024-10-25 11:21:51 +00:00
|
|
|
.a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
|
2024-10-25 11:47:53 +00:00
|
|
|
.b = Point{ .x = 50, .y = 40, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
|
2024-10-24 11:52:21 +00:00
|
|
|
.c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
|
2024-10-22 10:59:47 +00:00
|
|
|
};
|
2024-10-24 12:53:49 +00:00
|
|
|
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 } },
|
|
|
|
};
|
2024-10-24 10:41:37 +00:00
|
|
|
|
2024-10-25 12:55:26 +00:00
|
|
|
//video loop
|
2024-10-24 10:41:37 +00:00
|
|
|
while (true) {
|
|
|
|
buffer[0] = 0;
|
|
|
|
i = 0;
|
2024-10-25 12:55:26 +00:00
|
|
|
//clear buffer
|
2024-10-24 10:41:37 +00:00
|
|
|
while (i < buffer.len) : (i += 4) {
|
2024-10-25 11:11:22 +00:00
|
|
|
slice = buffer[i .. i + 4];
|
|
|
|
bytes_written = try std.unicode.utf8Encode(' ', slice);
|
|
|
|
ii = 0;
|
|
|
|
while ((bytes_written + ii) < 4) : (ii += 1) {
|
|
|
|
slice[bytes_written + ii] = 0;
|
|
|
|
}
|
2024-10-24 10:41:37 +00:00
|
|
|
}
|
|
|
|
try triangle(buffer, w, vec);
|
|
|
|
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
|
|
|
|
|
|
|
|
try out.print("\x1B[2J", .{});
|
2024-10-25 11:47:53 +00:00
|
|
|
//sleep(100000);
|
2024-10-24 12:53:49 +00:00
|
|
|
|
|
|
|
try triangle(buffer, w, vec);
|
2024-10-28 10:29:15 +00:00
|
|
|
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');
|
2024-10-24 10:41:37 +00:00
|
|
|
|
2024-10-24 12:53:49 +00:00
|
|
|
try triangle(buffer, w, vec2);
|
2024-10-28 10:29:15 +00:00
|
|
|
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');
|
2024-10-24 12:53:49 +00:00
|
|
|
try out.print("{s}", .{buffer});
|
2024-10-25 11:21:51 +00:00
|
|
|
sleep(30000000);
|
2024-10-24 12:53:49 +00:00
|
|
|
//_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
|
2024-10-24 10:41:37 +00:00
|
|
|
}
|
2024-10-22 10:59:47 +00:00
|
|
|
}
|
2024-10-28 10:29:15 +00:00
|
|
|
fn mv_axis_border_bounce(w: Winsize, point: *Point, axis: u8) void {
|
2024-10-24 11:52:21 +00:00
|
|
|
var cor = &point.x;
|
|
|
|
var dir = &point.direction.x;
|
|
|
|
var upper = w.ws_col;
|
|
|
|
|
|
|
|
switch (axis) {
|
|
|
|
'x' => {
|
|
|
|
cor = &point.x;
|
|
|
|
upper = w.ws_col;
|
|
|
|
dir = &point.direction.x;
|
|
|
|
},
|
|
|
|
'y' => {
|
|
|
|
cor = &point.y;
|
|
|
|
upper = w.ws_row;
|
|
|
|
dir = &point.direction.y;
|
|
|
|
},
|
|
|
|
'X' => {
|
|
|
|
cor = &point.x;
|
|
|
|
upper = w.ws_col;
|
2024-10-24 12:53:49 +00:00
|
|
|
dir = &point.direction.x;
|
2024-10-24 11:52:21 +00:00
|
|
|
dir.* = -1;
|
|
|
|
},
|
|
|
|
'Y' => {
|
|
|
|
cor = &point.y;
|
|
|
|
upper = w.ws_row;
|
|
|
|
dir = &point.direction.y;
|
|
|
|
dir.* = -1;
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
2024-10-24 12:53:49 +00:00
|
|
|
if (cor.* < upper and cor.* > 0) {
|
2024-10-24 11:52:21 +00:00
|
|
|
cor.* += dir.*;
|
|
|
|
}
|
|
|
|
if (cor.* >= upper or cor.* <= 0) {
|
|
|
|
dir.* *= -1;
|
|
|
|
cor.* += dir.*;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 12:57:57 +00:00
|
|
|
fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
|
|
|
const i: usize = @intCast(4 * (x + y * w.ws_col));
|
2024-10-24 08:15:32 +00:00
|
|
|
if (i < buffer.len) { //-1 ??
|
2024-10-22 10:59:47 +00:00
|
|
|
const slice = buffer[i .. i + 4];
|
|
|
|
_ = try std.unicode.utf8Encode(symbol, slice);
|
2024-10-25 11:47:53 +00:00
|
|
|
//TODO: check length, set not used bytes to zero
|
2024-10-22 10:59:47 +00:00
|
|
|
} else {
|
2024-10-24 12:53:49 +00:00
|
|
|
//print("Error illegal memory access\n", .{});
|
2024-10-22 10:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
2024-10-23 11:51:33 +00:00
|
|
|
var x: i32 = p1.x;
|
|
|
|
var y: i32 = p1.y;
|
2024-10-22 10:59:47 +00:00
|
|
|
const dx = (p2.x - p1.x);
|
2024-10-24 07:28:12 +00:00
|
|
|
const dy = (p2.y - p1.y);
|
2024-10-22 10:59:47 +00:00
|
|
|
var er: i64 = 0;
|
2024-10-24 06:57:37 +00:00
|
|
|
if (dx >= dy and dy > 0 and dx > 0) {
|
2024-10-23 11:51:33 +00:00
|
|
|
while (x <= p2.x) : (x += 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-23 11:51:33 +00:00
|
|
|
er += dy;
|
|
|
|
if (2 * er >= dx) {
|
|
|
|
y += 1;
|
|
|
|
er -= dx;
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (dx <= dy and dy >= 0 and dx >= 0) {
|
2024-10-23 11:51:33 +00:00
|
|
|
while (y <= p2.y) : (y += 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-23 11:51:33 +00:00
|
|
|
er += dx;
|
|
|
|
if (2 * er >= dy) {
|
|
|
|
x += 1;
|
|
|
|
er -= dy;
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (@abs(dy) <= dx and dy <= 0 and dx >= 0) {
|
2024-10-23 11:51:33 +00:00
|
|
|
while (x <= p2.x) : (x += 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-23 11:51:33 +00:00
|
|
|
er -= dy;
|
|
|
|
if (2 * er >= dx) {
|
|
|
|
y -= 1;
|
|
|
|
er -= dx;
|
|
|
|
}
|
2024-10-22 10:59:47 +00:00
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (@abs(dy) >= dx and dy <= 0 and dx >= 0) {
|
2024-10-23 12:37:15 +00:00
|
|
|
while (y >= p2.y) : (y -= 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-23 12:37:15 +00:00
|
|
|
er += dx;
|
|
|
|
if (2 * er >= dy) {
|
|
|
|
x += 1;
|
|
|
|
er += dy;
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (@abs(dx) <= dy and dx <= 0 and dy >= 0) {
|
2024-10-24 06:57:37 +00:00
|
|
|
while (y <= p2.y) : (y += 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-24 06:57:37 +00:00
|
|
|
er += @abs(dx);
|
|
|
|
if (2 * er >= dy) {
|
|
|
|
x -= 1;
|
|
|
|
er -= dy;
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (@abs(dx) >= dy and dy >= 0 and dx <= 0) {
|
2024-10-24 06:57:37 +00:00
|
|
|
while (x >= p2.x) : (x -= 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-24 06:57:37 +00:00
|
|
|
er += dy;
|
|
|
|
if (2 * er >= @abs(dx)) {
|
|
|
|
y += 1;
|
|
|
|
er -= @abs(dx);
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (dx <= dy and dx <= 0 and dy <= 0) {
|
2024-10-24 07:28:12 +00:00
|
|
|
while (x >= p2.x) : (x -= 1) {
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-24 07:28:12 +00:00
|
|
|
er += @abs(dy);
|
|
|
|
if (2 * er >= @abs(dx)) {
|
|
|
|
y -= 1;
|
|
|
|
er -= @abs(dx);
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 11:21:51 +00:00
|
|
|
} else if (dx >= dy and dy <= 0 and dx <= 0) {
|
2024-10-24 07:28:12 +00:00
|
|
|
while (y >= p2.y) : (y -= 1) {
|
2024-10-24 10:41:37 +00:00
|
|
|
//print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
2024-10-24 12:53:49 +00:00
|
|
|
_ = try pixel(buf, w, x, y, '█');
|
2024-10-24 07:28:12 +00:00
|
|
|
er += @abs(dx);
|
|
|
|
if (2 * er >= @abs(dx)) {
|
|
|
|
x -= 1;
|
2024-10-24 08:15:32 +00:00
|
|
|
er -= @abs(dy);
|
2024-10-24 07:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-22 10:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 08:15:32 +00:00
|
|
|
fn triangle(buffer: []u8, w: Winsize, vec: Vec3) !void {
|
2024-10-25 12:55:26 +00:00
|
|
|
var vec_fill: Vec3 = vec;
|
|
|
|
try bresenham(buffer, w, vec.a, vec.b);
|
|
|
|
try bresenham(buffer, w, vec.b, vec.c);
|
|
|
|
try bresenham(buffer, w, vec.c, vec.a);
|
|
|
|
while (vec_fill.a.y < vec.c.y) : (vec_fill.a.y += 1) {
|
|
|
|
try bresenham(buffer, w, vec_fill.a, vec.b);
|
|
|
|
}
|
|
|
|
while (vec_fill.a.y > vec.c.y) : (vec_fill.a.y -= 1) {
|
|
|
|
try bresenham(buffer, w, vec_fill.a, vec.b);
|
|
|
|
}
|
2024-10-24 08:15:32 +00:00
|
|
|
}
|