This commit is contained in:
jonathan santis 2024-10-24 14:53:49 +02:00
parent 034c2c8b66
commit 448e309c04

View File

@ -6,6 +6,8 @@ const ioctl = std.c.ioctl;
const stdout = std.io.getStdOut();
const out = stdout.writer();
const stdin = std.io.getStdIn().reader();
const sleep = std.time.sleep;
const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("sys/ioctl.h");
@ -133,11 +135,17 @@ fn fillBuffer() !void {
var buffer = try allocator.alloc(u8, w.ws_row * w.ws_col * 4); //utf-8 can be 4 bytes long
defer allocator.free(buffer);
var vec = 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 } },
};
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 } },
};
while (true) {
buffer[0] = 0;
@ -151,18 +159,28 @@ fn fillBuffer() !void {
try triangle(buffer, w, vec);
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
try triangle(buffer, w, vec);
sleep(20000000);
try out.print("\x1B[2J", .{});
try triangle(buffer, w, vec);
bounce(w, &vec.a, 'x');
bounce(w, &vec.a, 'y');
bounce(w, &vec.b, 'x');
bounce(w, &vec.b, 'y');
bounce(w, &vec.c, 'x');
bounce(w, &vec.c, 'y');
print("\x1b[45\x1b[37", .{});
try out.print("{s}", .{buffer});
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
try triangle(buffer, w, vec2);
bounce(w, &vec2.a, 'x');
bounce(w, &vec2.a, 'y');
bounce(w, &vec2.b, 'x');
bounce(w, &vec2.b, 'Y');
bounce(w, &vec2.c, 'x');
bounce(w, &vec2.c, 'y');
try out.print("{s}", .{buffer});
sleep(1000000);
//_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
}
}
fn bounce(w: Winsize, point: *Point, axis: u8) void {
@ -184,7 +202,7 @@ fn bounce(w: Winsize, point: *Point, axis: u8) void {
'X' => {
cor = &point.x;
upper = w.ws_col;
dir = &point.direction.y;
dir = &point.direction.x;
dir.* = -1;
},
'Y' => {
@ -196,7 +214,7 @@ fn bounce(w: Winsize, point: *Point, axis: u8) void {
else => {},
}
if (cor.* < upper) {
if (cor.* < upper and cor.* > 0) {
cor.* += dir.*;
}
if (cor.* >= upper or cor.* <= 0) {
@ -210,9 +228,8 @@ fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
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", .{});
//print("Error illegal memory access\n", .{});
}
}
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
@ -223,7 +240,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
var er: i64 = 0;
if (dx >= dy 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;
@ -232,7 +249,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;
@ -241,7 +258,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;
@ -250,7 +267,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;
@ -259,7 +276,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, '.');
_ = try pixel(buf, w, x, y, '');
er += @abs(dx);
if (2 * er >= dy) {
x -= 1;
@ -268,7 +285,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, '.');
_ = try pixel(buf, w, x, y, '');
er += dy;
if (2 * er >= @abs(dx)) {
y += 1;
@ -277,7 +294,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, '.');
_ = try pixel(buf, w, x, y, '');
er += @abs(dy);
if (2 * er >= @abs(dx)) {
y -= 1;
@ -287,7 +304,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) {
//print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
_ = try pixel(buf, w, x, y, '.');
_ = try pixel(buf, w, x, y, '');
er += @abs(dx);
if (2 * er >= @abs(dx)) {
x -= 1;