little demo
This commit is contained in:
parent
e1f5730a01
commit
b8a9788d65
100
main.zig
100
main.zig
@ -3,7 +3,9 @@ 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 ioctl = std.c.ioctl;
|
||||||
|
const stdout = std.io.getStdOut();
|
||||||
|
const out = stdout.writer();
|
||||||
|
const stdin = std.io.getStdIn().reader();
|
||||||
const c = @cImport({
|
const c = @cImport({
|
||||||
@cDefine("_NO_CRT_STDIO_INLINE", "1");
|
@cDefine("_NO_CRT_STDIO_INLINE", "1");
|
||||||
@cInclude("sys/ioctl.h");
|
@cInclude("sys/ioctl.h");
|
||||||
@ -116,42 +118,82 @@ fn fillBuffer() !void {
|
|||||||
|
|
||||||
var w = Winsize{};
|
var w = Winsize{};
|
||||||
var i: u64 = 0;
|
var i: u64 = 0;
|
||||||
|
var buf = std.mem.zeroes([2]u8);
|
||||||
|
var d: i8 = 1;
|
||||||
|
var d2: i8 = 1;
|
||||||
|
var d3: i8 = -1;
|
||||||
|
var d4: i8 = -1;
|
||||||
|
|
||||||
|
buf[0] = 0;
|
||||||
w = try getTermDimension();
|
w = try getTermDimension();
|
||||||
const file = try std.fs.cwd().createFile("buffer.out", .{});
|
const file = try std.fs.cwd().createFile("buffer.out", .{});
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
var buffer = try allocator.alloc(u8, w.ws_row * w.ws_col * 4); //utf-8 can be 4 bytes long
|
var buffer = try allocator.alloc(u8, w.ws_row * w.ws_col * 4); //utf-8 can be 4 bytes long
|
||||||
defer allocator.free(buffer);
|
defer allocator.free(buffer);
|
||||||
|
var vec = Vec3{
|
||||||
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 = 20, .y = 20, .z = 0 },
|
.a = Point{ .x = 20, .y = 20, .z = 0 },
|
||||||
.b = Point{ .x = 50, .y = 30, .z = 0 },
|
.b = Point{ .x = 50, .y = 30, .z = 0 },
|
||||||
.c = Point{ .x = 30, .y = 1, .z = 0 },
|
.c = Point{ .x = 30, .y = 1, .z = 0 },
|
||||||
};
|
};
|
||||||
try triangle(buffer, w, vec);
|
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
|
while (true) {
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 28, .z = 0 });
|
buffer[0] = 0;
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 });
|
i = 0;
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 39, .z = 0 });
|
while (i < buffer.len) : (i += 4) {
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 30, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 });
|
const slice = buffer[i .. i + 4];
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 60, .y = 30, .z = 0 }, Point{ .x = 70, .y = 5, .z = 0 });
|
_ = try std.unicode.utf8Encode('_', slice);
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 100, .y = 20, .z = 0 }, Point{ .x = 30, .y = 30, .z = 0 });
|
//slice[0] = 'm';
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 100, .y = 10, .z = 0 }, Point{ .x = 90, .y = 40, .z = 0 });
|
}
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 90, .y = 10, .z = 0 });
|
try pixel(buffer, w, 5, 10, '\u{1F702}');
|
||||||
//_ = try bresenham(buffer, w, Point{ .x = 100, .y = 30, .z = 0 }, Point{ .x = 60, .y = 10, .z = 0 });
|
try triangle(buffer, w, vec);
|
||||||
try file.writeAll(buffer);
|
//_ = try bresenham(buffer, w, Point{ .x = 10, .y = 10, .z = 0 }, Point{ .x = 30, .y = 12, .z = 0 });
|
||||||
|
// try file.writeAll(buffer);
|
||||||
|
|
||||||
|
if (vec.c.x < w.ws_col) {
|
||||||
|
vec.c.x += d;
|
||||||
|
}
|
||||||
|
if (vec.c.x == w.ws_col or vec.c.x == 0) {
|
||||||
|
d *= -1;
|
||||||
|
vec.c.x += d;
|
||||||
|
}
|
||||||
|
if (vec.a.y < w.ws_row) {
|
||||||
|
vec.a.y += d2;
|
||||||
|
}
|
||||||
|
if (vec.a.y == w.ws_row or vec.a.y == 0) {
|
||||||
|
d2 *= -1;
|
||||||
|
vec.a.y += d2;
|
||||||
|
}
|
||||||
|
if (vec.b.y < w.ws_row) {
|
||||||
|
vec.b.y += d3;
|
||||||
|
}
|
||||||
|
if (vec.b.y == w.ws_row or vec.b.y == 0) {
|
||||||
|
d3 *= -1;
|
||||||
|
vec.b.y += d3;
|
||||||
|
}
|
||||||
|
if (vec.b.x < w.ws_col) {
|
||||||
|
vec.b.x += d4;
|
||||||
|
}
|
||||||
|
if (vec.b.x == w.ws_col or vec.b.x == 0) {
|
||||||
|
d4 *= -1;
|
||||||
|
vec.b.x += d4;
|
||||||
|
}
|
||||||
|
|
||||||
|
try triangle(buffer, w, vec);
|
||||||
|
try out.print("\x1B[2J", .{});
|
||||||
|
|
||||||
|
print("\x1b[45\x1b[37", .{});
|
||||||
|
try out.print("{s}", .{buffer});
|
||||||
|
|
||||||
|
_ = try stdin.readUntilDelimiterOrEof(&buf, '\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
fn pixel(buffer: []u8, w: Winsize, x: i64, y: i64, symbol: u21) !void {
|
||||||
const i: usize = @intCast(4 * (x + y * w.ws_col));
|
const i: usize = @intCast(4 * (x + y * w.ws_col));
|
||||||
if (i < buffer.len) { //-1 ??
|
if (i < buffer.len) { //-1 ??
|
||||||
const slice = buffer[i .. i + 4];
|
const slice = buffer[i .. i + 4];
|
||||||
_ = try std.unicode.utf8Encode(symbol, slice);
|
_ = try std.unicode.utf8Encode(symbol, slice);
|
||||||
|
print("\x1b[46\x1b[43", .{});
|
||||||
} else {
|
} else {
|
||||||
print("Error illegal memory access\n", .{});
|
print("Error illegal memory access\n", .{});
|
||||||
}
|
}
|
||||||
@ -173,7 +215,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (dx < dy and dy > 0 and dx > 0) {
|
} else if (dx < dy and dy > 0 and dx > 0) {
|
||||||
while (y <= p2.y) : (y += 1) {
|
while (y <= p2.y) : (y += 1) {
|
||||||
_ = try pixel(buf, w, x, y, ',');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += dx;
|
er += dx;
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
x += 1;
|
x += 1;
|
||||||
@ -182,7 +224,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
|
} else if (@abs(dy) < dx and dy < 0 and dx > 0) {
|
||||||
while (x <= p2.x) : (x += 1) {
|
while (x <= p2.x) : (x += 1) {
|
||||||
_ = try pixel(buf, w, x, y, '🡇');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er -= dy;
|
er -= dy;
|
||||||
if (2 * er >= dx) {
|
if (2 * er >= dx) {
|
||||||
y -= 1;
|
y -= 1;
|
||||||
@ -191,7 +233,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
|
} else if (@abs(dy) > dx and dy < 0 and dx > 0) {
|
||||||
while (y >= p2.y) : (y -= 1) {
|
while (y >= p2.y) : (y -= 1) {
|
||||||
_ = try pixel(buf, w, x, y, '=');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += dx;
|
er += dx;
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
x += 1;
|
x += 1;
|
||||||
@ -200,7 +242,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
|
} else if (@abs(dx) <= dy and dx < 0 and dy > 0) {
|
||||||
while (y <= p2.y) : (y += 1) {
|
while (y <= p2.y) : (y += 1) {
|
||||||
_ = try pixel(buf, w, x, y, '5');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += @abs(dx);
|
er += @abs(dx);
|
||||||
if (2 * er >= dy) {
|
if (2 * er >= dy) {
|
||||||
x -= 1;
|
x -= 1;
|
||||||
@ -209,7 +251,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
|
} else if (@abs(dx) > dy and dy > 0 and dx < 0) {
|
||||||
while (x >= p2.x) : (x -= 1) {
|
while (x >= p2.x) : (x -= 1) {
|
||||||
_ = try pixel(buf, w, x, y, '6');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += dy;
|
er += dy;
|
||||||
if (2 * er >= @abs(dx)) {
|
if (2 * er >= @abs(dx)) {
|
||||||
y += 1;
|
y += 1;
|
||||||
@ -218,7 +260,7 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (dx <= dy and dx < 0 and dy < 0) {
|
} else if (dx <= dy and dx < 0 and dy < 0) {
|
||||||
while (x >= p2.x) : (x -= 1) {
|
while (x >= p2.x) : (x -= 1) {
|
||||||
_ = try pixel(buf, w, x, y, '7');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += @abs(dy);
|
er += @abs(dy);
|
||||||
if (2 * er >= @abs(dx)) {
|
if (2 * er >= @abs(dx)) {
|
||||||
y -= 1;
|
y -= 1;
|
||||||
@ -227,8 +269,8 @@ fn bresenham(buf: []u8, w: Winsize, p1: Point, p2: Point) !void {
|
|||||||
}
|
}
|
||||||
} else if (dx > dy and dy < 0 and dx < 0) {
|
} else if (dx > dy and dy < 0 and dx < 0) {
|
||||||
while (y >= p2.y) : (y -= 1) {
|
while (y >= p2.y) : (y -= 1) {
|
||||||
print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
//print("x:{},y:{},er:{},dx:{},dy:{}\n", .{ x, y, er, dx, dy });
|
||||||
_ = try pixel(buf, w, x, y, '8');
|
_ = try pixel(buf, w, x, y, '.');
|
||||||
er += @abs(dx);
|
er += @abs(dx);
|
||||||
if (2 * er >= @abs(dx)) {
|
if (2 * er >= @abs(dx)) {
|
||||||
x -= 1;
|
x -= 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user