From 0b4239a67997f3450f60144ac19fb51a8bb0c957 Mon Sep 17 00:00:00 2001 From: jonathan santis Date: Tue, 22 Oct 2024 12:59:47 +0200 Subject: [PATCH] sdf --- main.zig | 144 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 129 insertions(+), 15 deletions(-) diff --git a/main.zig b/main.zig index 00ec4a1..2c80c58 100644 --- a/main.zig +++ b/main.zig @@ -2,27 +2,64 @@ 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 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 Point = struct { + x: u32, + y: u32, + z: u32, +}; +const Vec3 = struct { + a: Point, + b: Point, + c: Point, +}; pub fn main() !void { - //const buffer: [:0]u8 = undefined; - std.debug.print("Hello, {s}\n", .{"World"}); + 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| { - const buffer = buf; - _ = buffer; print("Buffer:{s}\n", .{buf}); - - const slice = getNum("123dsfi843w653487 38745 631875 61385631856__87234982374924__auf_123_45t63sadf9875,231,1"); - //_ = slice; - //print("number1: {d}", .{slice[0]}); - for (slice) |num| { - print("extracted number:\n{d}\n", .{num}); - } + 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; @@ -57,12 +94,12 @@ fn getNum(str1: []const u8) []u64 { return &num_res; } fn createFile() !void { - const file = try std.fs.cwd().openFile("junk-file.txt", .{}); + 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) }); + 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); @@ -74,3 +111,80 @@ fn createFile() !void { print("not equal\n", .{}); } } +fn fillBuffer() !void { + const allocator = std.heap.page_allocator; + + var w = Winsize{}; + var i: u64 = 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); + + buffer[0] = 0; + while (i < buffer.len) : (i += 4) { + const slice = buffer[i .. i + 4]; + _ = try std.unicode.utf8Encode('\u{2588}', slice); + } + try pixel(buffer, w, 5, 10, '\u{1F702}'); + const vec = Vec3{ + .a = Point{ .x = 0, .y = 1, .z = 2 }, + .b = Point{ .x = 0, .y = 1, .z = 20 }, + .c = Point{ .x = 0, .y = 1, .z = 2 }, + }; + _ = vec; + //triangle(vec); + _ = try bresenham(buffer, w, Point{ .x = 10, .y = 40, .z = 0 }, Point{ .x = 100, .y = 20, .z = 0 }); + try file.writeAll(buffer); +} +fn pixel(buffer: []u8, w: Winsize, x: u64, y: u64, symbol: u21) !void { + const i = 4 * (x + y * w.ws_col); + if (i < buffer.len) { + const slice = buffer[i .. i + 4]; + print("slice:{u}\n", .{slice}); + _ = try std.unicode.utf8Encode(symbol, slice); + print("slice after:{u}\n", .{slice}); + print("cols{}\ni: {}\n", .{ w.ws_col, i }); + } else { + print("Error illegal memory access\n", .{}); + } +} +fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void { + var x: u64 = p1.x; + var y: u64 = p1.y; + const dx = (p2.x - p1.x); + const a: u32 = 20; + const b: u32 = 40; + var c1: i64 = 0; + c1 = a - b; + print("c:{}\n", .{c1}); + print("p2: {} - {},{}", .{ p2.y, p1.y, p2.y - p1.y }); + var dy: i64 = ((p2.y) - (p1.y)); + //const slope = dx/dy; + var er: i64 = 0; + while (x <= p2.x) : (x += 1) { + print("[bresenham]x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy }); + _ = try pixel(buf, w, x, y, '.'); + + if (dy > 0) { + er += dy; + if (2 * er >= dx) { + y += 1; + er -= dx; + } + } else if (dy < 0) { + er -= dy; + if (2 * er >= dx) { + y -= 1; + er += dx; + } + } + } + dy = 0; +} + +//fn triangle(vec: Vec3) void { + +//}