splitt up definitions

This commit is contained in:
jonathan santis 2024-10-29 11:47:39 +01:00
parent 039cc7e912
commit 1d82a2cf42
2 changed files with 51 additions and 51 deletions

View File

@ -7,6 +7,8 @@ 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 Point = primitive.Point;
const c = @cImport({ const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1"); @cDefine("_NO_CRT_STDIO_INLINE", "1");
@ -20,27 +22,6 @@ const Winsize = struct {
ws_xpixel: c_ushort = 0, ws_xpixel: c_ushort = 0,
ws_ypixel: 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,
};
const Triangle = struct {
bufa: *i32,
bufb: *i32,
bufc: *i32,
};
pub fn main() !void { pub fn main() !void {
var win = Winsize{}; var win = Winsize{};
@ -83,18 +64,6 @@ 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);
var vec = Vec3{
.a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
.b = Point{ .x = 50, .y = 40, .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 } },
};
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); _ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
//video loop //video loop
while (true) { while (true) {
@ -109,31 +78,31 @@ fn draw() !void {
slice[bytes_written + ii] = 0; slice[bytes_written + ii] = 0;
} }
} }
try triangle(buffer, w, vec); try triangle(buffer, w, primitive.vec);
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 }); //_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
try out.print("\x1B[2J", .{}); try out.print("\x1B[2J", .{});
//sleep(100000); //sleep(100000);
try triangle(buffer, w, vec); try triangle(buffer, w, primitive.vec);
mv_axis_border_bounce(w, &vec.a, 'x'); mv_axis_border_bounce(w, &primitive.vec.a, 'x');
mv_axis_border_bounce(w, &vec.a, 'y'); mv_axis_border_bounce(w, &primitive.vec.a, 'y');
mv_axis_border_bounce(w, &vec.b, 'x'); mv_axis_border_bounce(w, &primitive.vec.b, 'x');
mv_axis_border_bounce(w, &vec.b, 'y'); mv_axis_border_bounce(w, &primitive.vec.b, 'y');
mv_axis_border_bounce(w, &vec.c, 'x'); mv_axis_border_bounce(w, &primitive.vec.c, 'x');
mv_axis_border_bounce(w, &vec.c, 'y'); mv_axis_border_bounce(w, &primitive.vec.c, 'y');
try triangle(buffer, w, vec2); try triangle(buffer, w, primitive.vec2);
mv_axis_border_bounce(w, &vec2.a, 'x'); mv_axis_border_bounce(w, &primitive.vec2.a, 'x');
mv_axis_border_bounce(w, &vec2.a, 'y'); mv_axis_border_bounce(w, &primitive.vec2.a, 'y');
mv_axis_border_bounce(w, &vec2.b, 'x'); mv_axis_border_bounce(w, &primitive.vec2.b, 'x');
mv_axis_border_bounce(w, &vec2.b, 'Y'); mv_axis_border_bounce(w, &primitive.vec2.b, 'Y');
mv_axis_border_bounce(w, &vec2.c, 'x'); mv_axis_border_bounce(w, &primitive.vec2.c, 'x');
mv_axis_border_bounce(w, &vec2.c, 'y'); mv_axis_border_bounce(w, &primitive.vec2.c, 'y');
try fill_draw(w, buffer); try fill_draw(w, buffer);
try out.print("{s}", .{buffer}); try out.print("{s}", .{buffer});
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n'); //_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
sleep(30000000); sleep(30000000);
} }
} }
@ -168,7 +137,7 @@ fn fill_draw(w: Winsize, buffer: []u8) !void {
} }
} }
if (ii > start and ii < end and fill == 1 and std.mem.eql(u8, utf_slice, &code) == false) { if (ii > start and ii < end and fill == 1 and std.mem.eql(u8, utf_slice, &code) == false) {
_ = try std.unicode.utf8Encode('X', utf_slice); _ = try std.unicode.utf8Encode('', utf_slice);
} }
} }
} }
@ -305,7 +274,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
} }
} }
fn triangle(buffer: []u8, w: Winsize, vec: Vec3) !void { fn triangle(buffer: []u8, w: Winsize, vec: primitive.Vec3) !void {
try bresenham(buffer, w, vec.a, vec.b); try bresenham(buffer, w, vec.a, vec.b);
try bresenham(buffer, w, vec.b, vec.c); try bresenham(buffer, w, vec.b, vec.c);
try bresenham(buffer, w, vec.c, vec.a); try bresenham(buffer, w, vec.c, vec.a);

31
primitives.zig Normal file
View File

@ -0,0 +1,31 @@
pub const Direction = struct {
x: i8 = 1,
y: i8 = 1,
z: i8 = 1,
};
pub const Point = struct {
x: i32,
y: i32,
z: i32,
direction: Direction, //for movement state
};
pub const Vec3 = struct {
a: Point,
b: Point,
c: Point,
};
pub const Triangle = struct {
bufa: *i32,
bufb: *i32,
bufc: *i32,
};
pub var vec = Vec3{
.a = Point{ .x = 20, .y = 30, .z = 0, .direction = .{ .x = 1, .y = 1, .z = 1 } },
.b = Point{ .x = 50, .y = 40, .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 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 } },
};