Restructure into different files

This commit is contained in:
jonathan santis 2024-10-30 10:38:15 +01:00
parent ed91d9c711
commit 4ab7bf6ce1
6 changed files with 228 additions and 167 deletions

178
main.zig
View File

@ -2,26 +2,22 @@ const std = @import("std");
const expect = std.testing.expect; const expect = std.testing.expect;
const print = std.debug.print; const print = std.debug.print;
const getenv = std.posix.getenv; const getenv = std.posix.getenv;
const ioctl = std.c.ioctl;
const stdout = std.io.getStdOut(); const stdout = std.io.getStdOut();
const out = stdout.writer(); const out = stdout.writer();
const stdin = std.io.getStdIn().reader(); const stdin = std.io.getStdIn().reader();
const sleep = std.time.sleep; const sleep = std.time.sleep;
const primitive = @import("primitives.zig"); const primitive = @import("primitives.zig");
const Point = primitive.Point; const Point = primitive.Point;
const pixel = primitive.pixel;
const triangle = primitive.triangle;
const c = @cImport({ const term = @import("term.zig");
@cDefine("_NO_CRT_STDIO_INLINE", "1"); const Winsize = term.Winsize;
@cInclude("sys/ioctl.h"); const getTermDimension = term.getTermDimension;
@cInclude("stdio.h");
});
const Winsize = struct { const movement = @import("movement.zig");
ws_row: c_ushort = 0, const mv_axis_border_bounce = movement.mv_axis_border_bounce;
ws_col: c_ushort = 0,
ws_xpixel: c_ushort = 0,
ws_ypixel: c_ushort = 0,
};
pub fn main() !void { pub fn main() !void {
var win = Winsize{}; var win = Winsize{};
@ -32,19 +28,6 @@ pub fn main() !void {
try draw(); try draw();
} }
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 draw() !void { fn draw() !void {
const allocator = std.heap.page_allocator; const allocator = std.heap.page_allocator;
@ -65,10 +48,10 @@ fn draw() !void {
var buffer = try allocator.alloc(u8, buffer_size); //utf-8 can be 4 bytes long var buffer = try allocator.alloc(u8, buffer_size); //utf-8 can be 4 bytes long
defer allocator.free(buffer); defer allocator.free(buffer);
primitive.vec3[0].a.x = 10; primitive.obj[0].a.x = 10;
print("primitive.vec3[0].a.x:{}\n", .{primitive.vec3[0].a.x}); print("primitive.obj[0].a.x:{}\n", .{primitive.obj[0].a.x});
print("primitive.vec3[0].b.x:{}\n", .{primitive.vec3[0].b.x}); print("primitive.obj[0].b.x:{}\n", .{primitive.obj[0].b.x});
print("primitive.vec3[9].b.x:{}\n", .{primitive.vec3[9].b.x}); print("primitive.obj[9].b.x:{}\n", .{primitive.obj[9].b.x});
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); _ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
//video loop //video loop
while (true) { while (true) {
@ -148,140 +131,3 @@ fn fill_draw(w: Winsize, buffer: []u8) !void {
} }
} }
} }
fn mv_axis_border_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);
//TODO: check length, set not used bytes to zero
} 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: 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);
}

44
movement.zig Normal file
View File

@ -0,0 +1,44 @@
const term = @import("term.zig");
const Winsize = term.Winsize;
const primitive = @import("primitives.zig");
const Point = primitive.Point;
pub fn mv_axis_border_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.*;
}
}

33
numParse.zig Normal file
View File

@ -0,0 +1,33 @@
fn getNum(str1: []const u8) []u64 {
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;
}

View File

@ -1,3 +1,7 @@
const std = @import("std");
const term = @import("term.zig");
const Winsize = term.Winsize;
pub const Direction = struct { pub const Direction = struct {
x: i8 = 1, x: i8 = 1,
y: i8 = 1, y: i8 = 1,
@ -29,4 +33,101 @@ pub var vec2 = Vec3{
.b = Point{ .x = 50, .y = 30, .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 } }, .c = Point{ .x = 30, .y = 1, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
}; };
pub var vec3: [10]Vec3 = undefined; pub var obj: [10]Vec3 = undefined;
pub 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);
//TODO: check length, set not used bytes to zero
} else {
//print("Error illegal memory access\n", .{});
}
}
pub 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);
}
}
}
}
pub fn triangle(buffer: []u8, w: Winsize, vector: Vec3) !void {
try bresenham(buffer, w, vector.a, vector.b);
try bresenham(buffer, w, vector.b, vector.c);
try bresenham(buffer, w, vector.c, vector.a);
}

27
term.zig Normal file
View File

@ -0,0 +1,27 @@
const std = @import("std");
const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("sys/ioctl.h");
@cInclude("stdio.h");
});
const ioctl = std.c.ioctl;
pub const Winsize = struct {
ws_row: c_ushort = 0,
ws_col: c_ushort = 0,
ws_xpixel: c_ushort = 0,
ws_ypixel: c_ushort = 0,
};
pub 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;
}

10
test-str.zig Normal file
View File

@ -0,0 +1,10 @@
pub fn main() void {
_ = ohyes("sdf"); // everything is fine
}
fn ohyes(str: []const u8) []const u8 {
//const foo = "ohyes";
_ = str;
return "sdfg"; // foo is already a pointer, or rather
// foo was a pointer all along
}