zig-learn/main.zig

125 lines
3.9 KiB
Zig
Raw Normal View History

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-30 09:38:15 +00:00
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-29 10:47:39 +00:00
const primitive = @import("primitives.zig");
const Point = primitive.Point;
2024-10-30 09:38:15 +00:00
const pixel = primitive.pixel;
const triangle = primitive.triangle;
2024-10-24 12:53:49 +00:00
2024-10-30 09:38:15 +00:00
const term = @import("term.zig");
const Winsize = term.Winsize;
const getTermDimension = term.getTermDimension;
2024-10-22 10:59:47 +00:00
2024-10-30 09:38:15 +00:00
const movement = @import("movement.zig");
const mv_axis_border_bounce = movement.mv_axis_border_bounce;
2024-10-14 06:52:08 +00:00
2024-10-30 09:51:13 +00:00
const definitions = @import("definitions.zig");
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();
2024-10-29 10:29:31 +00:00
print("winsize.ws_row: {}\nws_col: {}\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-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;
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
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-30 09:51:13 +00:00
definitions.obj[0].a.x = 10;
2024-10-30 13:56:09 +00:00
wait_for_enter();
// defer std.heap.page_allocator.free(tmp);
2024-10-25 12:55:26 +00:00
//video loop
2024-10-24 10:41:37 +00:00
while (true) {
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
}
2024-10-30 09:51:13 +00:00
try triangle(buffer, w, definitions.vec);
2024-10-24 10:41:37 +00:00
//_ = 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, 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');
2024-10-29 10:29:31 +00:00
try fill_draw(w, buffer);
try out.print("{s}", .{buffer});
sleep(30000000);
}
}
fn fill_draw(w: Winsize, buffer: []u8) !void {
var i: u64 = 0;
var ii: u16 = 0;
var iii: u16 = 0;
var fill: u8 = 0;
var start: u16 = 0;
var end: u16 = 0;
var code: [4]u8 = std.mem.zeroes([4]u8);
_ = try std.unicode.utf8Encode('█', &code);
while (i < buffer.len - w.ws_col * 4) : (i += w.ws_col * 4) {
var line: []u8 = buffer[i .. i + w.ws_col * 4];
fill = 0;
start = 0;
end = 0;
2024-10-28 13:39:26 +00:00
ii = 0;
2024-10-29 10:29:31 +00:00
iii = 0;
while (ii < line.len - 4) : (ii += 4) {
const utf_slice: []u8 = line[ii .. ii + 4];
if (std.mem.eql(u8, utf_slice, &code) == true and fill == 0) {
2024-10-28 13:39:26 +00:00
fill = 1;
2024-10-29 10:29:31 +00:00
start = ii;
iii = ii;
while (iii < line.len - 4) : (iii += 4) {
const lookahead: []u8 = line[iii .. iii + 4];
if (std.mem.eql(u8, lookahead, &code) == true) {
end = iii;
}
}
}
if (ii > start and ii < end and fill == 1 and std.mem.eql(u8, utf_slice, &code) == false) {
2024-10-29 10:47:39 +00:00
_ = try std.unicode.utf8Encode('●', utf_slice);
2024-10-28 13:39:26 +00:00
}
}
2024-10-24 10:41:37 +00:00
}
2024-10-22 10:59:47 +00:00
}
2024-10-30 13:56:09 +00:00
fn wait_for_enter() void {
_ = stdin.readUntilDelimiterAlloc(std.heap.page_allocator, '\n', 3) catch |err| {
print("string to long{}", .{err});
}; //Memory leak?
}