328 lines
9.6 KiB
Zig
328 lines
9.6 KiB
Zig
const std = @import("std");
|
|
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 sleep = std.time.sleep;
|
|
|
|
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,
|
|
};
|
|
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,
|
|
};
|
|
|
|
pub fn main() !void {
|
|
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 });
|
|
try fillBuffer();
|
|
try createFile();
|
|
if (getenv("TERM")) |buf| {
|
|
print("Buffer:{s}\n", .{buf});
|
|
const slice = getNum(buf);
|
|
_ = slice;
|
|
//for (slice) |num| {
|
|
// print("extracted number:\n{d}\n", .{num});
|
|
//}
|
|
} else {
|
|
print("Error getenv\n", .{});
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
fn getNum(str1: []const u8) []u64 {
|
|
//_ = str1;
|
|
var pos: u64 = 0;
|
|
var num = std.mem.zeroes([64]u64);
|
|
var num_res = std.mem.zeroes([16]u64);
|
|
var it: u64 = 0;
|
|
for (str1, 0..) |elem, it2| {
|
|
if (elem >= 48 and elem <= 57) { //is ascii number?
|
|
num[pos] = @intCast(elem - 48);
|
|
if (pos <= @bitSizeOf(@TypeOf(pos))) {
|
|
pos += 1;
|
|
} else {
|
|
print("ERROR int u64 oveflow! pos {d} ,max {d}\n", .{ pos, @bitSizeOf(@TypeOf(pos)) });
|
|
}
|
|
}
|
|
//Calculate int out of characters
|
|
if ((elem < 48 or elem > 57) or it2 == (str1.len - 1)) {
|
|
if (pos != 0) {
|
|
for (0..pos) |i| {
|
|
if (i < pos - 1) {
|
|
num_res[it] = num_res[it] + num[i] * (std.math.pow(u64, 10, (pos - i - 1)));
|
|
} else {
|
|
num_res[it] = num_res[it] + num[i];
|
|
}
|
|
}
|
|
//print("numres:{}={}\n", .{ it, num_res[it] });
|
|
pos = 0;
|
|
it += 1;
|
|
}
|
|
}
|
|
}
|
|
return &num_res;
|
|
}
|
|
fn createFile() !void {
|
|
const file = try std.fs.cwd().openFile("junk-file.txt", .{ .mode = std.fs.File.OpenMode.read_write });
|
|
defer file.close();
|
|
|
|
const bytes_written = try file.writeAll("hello world!");
|
|
//_ = bytes_written;
|
|
std.debug.print("files written:{any}, {any}", .{ bytes_written, @TypeOf(bytes_written) });
|
|
|
|
var buffer: [100]u8 = undefined;
|
|
try file.seekTo(0);
|
|
const bytes_read = try file.readAll(&buffer);
|
|
print("buffer: {s} check: {s}\n", .{ buffer[0..bytes_read], "hello world!" });
|
|
if (std.mem.eql(u8, buffer[0 .. bytes_read - 1], "hello world!") == true) {
|
|
print("equal\n", .{});
|
|
} else {
|
|
print("not equal\n", .{});
|
|
}
|
|
}
|
|
fn fillBuffer() !void {
|
|
const allocator = std.heap.page_allocator;
|
|
|
|
var w = Winsize{};
|
|
var i: u64 = 0;
|
|
var ii: u64 = 0;
|
|
var buf = std.mem.zeroes([2]u8);
|
|
var slice: []u8 = undefined;
|
|
var bytes_written: u3 = 0;
|
|
|
|
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);
|
|
|
|
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 } },
|
|
};
|
|
|
|
//clear buffer
|
|
while (true) {
|
|
buffer[0] = 0;
|
|
i = 0;
|
|
while (i < buffer.len) : (i += 4) {
|
|
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;
|
|
}
|
|
}
|
|
try triangle(buffer, w, vec);
|
|
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
|
|
|
|
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');
|
|
|
|
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(10000000);
|
|
//_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
|
|
}
|
|
}
|
|
fn bounce(w: Winsize, point: *Point, axis: u8) void {
|
|
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;
|
|
dir = &point.direction.x;
|
|
dir.* = -1;
|
|
},
|
|
'Y' => {
|
|
cor = &point.y;
|
|
upper = w.ws_row;
|
|
dir = &point.direction.y;
|
|
dir.* = -1;
|
|
},
|
|
else => {},
|
|
}
|
|
|
|
if (cor.* < upper and cor.* > 0) {
|
|
cor.* += dir.*;
|
|
}
|
|
if (cor.* >= upper or cor.* <= 0) {
|
|
dir.* *= -1;
|
|
cor.* += dir.*;
|
|
}
|
|
}
|
|
|
|
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);
|
|
} else {
|
|
//print("Error illegal memory access\n", .{});
|
|
}
|
|
}
|
|
fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|
var x: i32 = p1.x;
|
|
var y: i32 = p1.y;
|
|
const dx = (p2.x - p1.x);
|
|
const dy = (p2.y - p1.y);
|
|
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, '█');
|
|
er += dy;
|
|
if (2 * er >= dx) {
|
|
y += 1;
|
|
er -= dx;
|
|
}
|
|
}
|
|
} else if (dx < dy and dy > 0 and dx > 0) {
|
|
while (y <= p2.y) : (y += 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er += dx;
|
|
if (2 * er >= dy) {
|
|
x += 1;
|
|
er -= dy;
|
|
}
|
|
}
|
|
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
|
|
while (x <= p2.x) : (x += 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er -= dy;
|
|
if (2 * er >= dx) {
|
|
y -= 1;
|
|
er -= dx;
|
|
}
|
|
}
|
|
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
|
|
while (y >= p2.y) : (y -= 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er += dx;
|
|
if (2 * er >= dy) {
|
|
x += 1;
|
|
er += dy;
|
|
}
|
|
}
|
|
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
|
|
while (y <= p2.y) : (y += 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er += @abs(dx);
|
|
if (2 * er >= dy) {
|
|
x -= 1;
|
|
er -= dy;
|
|
}
|
|
}
|
|
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
|
|
while (x >= p2.x) : (x -= 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er += dy;
|
|
if (2 * er >= @abs(dx)) {
|
|
y += 1;
|
|
er -= @abs(dx);
|
|
}
|
|
}
|
|
} else if (dx <= dy and dx < 0 and dy < 0) {
|
|
while (x >= p2.x) : (x -= 1) {
|
|
_ = try pixel(buf, w, x, y, '█');
|
|
er += @abs(dy);
|
|
if (2 * er >= @abs(dx)) {
|
|
y -= 1;
|
|
er -= @abs(dx);
|
|
}
|
|
}
|
|
} 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, '█');
|
|
er += @abs(dx);
|
|
if (2 * er >= @abs(dx)) {
|
|
x -= 1;
|
|
er -= @abs(dy);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn triangle(buffer: []u8, w: Winsize, vec: 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);
|
|
}
|