initial draw code

This commit is contained in:
2024-10-08 15:21:18 -04:00
parent 5a4e1699b3
commit dd8474ccac
5 changed files with 200 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
swap_buffer :: (screen: *Screen) {
assert(false, "No implemented");
}

View File

@@ -0,0 +1,104 @@
draw :: (screen: *Screen, x: s64, y: s64, char: PIXEL_TYPE = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
using screen;
if x >= 0 && x < width && y >= 0 && y < height {
buffer[y * width + x].type = char;
buffer[y * width + x].color = color;
}
}
draw_line :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, char: PIXEL_TYPE = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
x, y, dx, dy, dx1, dy1, xe, ye, px, py : s64;
dx = x2 - x1;
dy = y2 - y1;
dx1 = abs(dx);
dy1 = abs(dy);
px = 2 * dy1 - dx1;
py = 2 * dx1 - dy1;
if dy1 <= dx1 {
if dx >= 0 {
x = x1;
y = y1;
xe = x2;
} else {
x = x2;
y = y2;
xe = x1;
}
draw(screen, x, y, char, color);
for 0..xe {
x += 1;
if px < 0 {
px += 2 * dy1;
} else {
if (dx < 0 && dy < 0) || (dx > 0 && dy > 0) then y += 1; else y -= 1;
px += 2 * (dy1 - dx1);
draw(screen, x, y, char, color);
}
}
} else {
if dy >= 0 {
x = x1;
y = y1;
ye = y2;
} else {
x = x2;
y = y2;
ye = y1;
}
draw(screen, x, y, char, color);
for 0..ye {
y += 1;
if py <= 0 {
py += 2 * dx1;
} else {
if (dx < 0 && dy < 0 ) || (dx > 0 && dy > 0) then x += 1; else x -= 1;
py += 2 * (dx1 - dy1);
}
draw(screen, x, y, char, color);
}
}
}
draw_triangle :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, x3: s64, y3: s64, char: PIXEL_TYPE = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
draw_line(x1, y1, x2, y2, char, color);
draw_line(x2, y2, x3, y3, char, color);
draw_line(x3, y3, x1, y1, char, color);
}
clip :: (screen: *Screen, x: *s64, y: *s64) {
using screen;
if x < 0 then x = 0;
if x >= width then x = width;
if y < 0 then y = 0;
if y >= height then y = height;
}
fill :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, char: PIXEL_TYPE = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
clip(screen, *x1, *y1);
clip(screen, *x2, *y2);
for x: x1..x2 {
for y: y1..y2 {
draw(x, y, char, color);
}
}
}
draw_string :: (screen: *Screen, x: s64, y: s64, text: string, c: COLOR = .FG_WHITE) {
assert(false, "not implemented");
}

View File

@@ -0,0 +1,8 @@
// Render current screen state to a file
render_to_file :: (screen: *Screen, preserve_color: bool, filepath: string) {
assert(false, "Not implemented");
}
#scope_file
#import "File";

View File

@@ -0,0 +1,77 @@
COLOR :: enum u32 {
FG_BLACK :: 0x0000;
FG_DARK_BLUE :: 0x0001;
FG_DARK_GREEN :: 0x0002;
FG_DARK_CYAN :: 0x0003;
FG_DARK_RED :: 0x0004;
FG_DARK_MAGENTA :: 0x0005;
FG_DARK_YELLOW :: 0x0006;
FG_GREY :: 0x0007;
FG_DARK_GREY :: 0x0008;
FG_BLUE :: 0x0009;
FG_GREEN :: 0x000A;
FG_CYAN :: 0x000B;
FG_RED :: 0x000C;
FG_MAGENTA :: 0x000D;
FG_YELLOW :: 0x000E;
FG_WHITE :: 0x000F;
BG_BLACK :: 0x0000;
BG_DARK_BLUE :: 0x0010;
BG_DARK_GREEN :: 0x0020;
BG_DARK_CYAN :: 0x0030;
BG_DARK_RED :: 0x0040;
BG_DARK_MAGENTA :: 0x0050;
BG_DARK_YELLOW :: 0x0060;
BG_GREY :: 0x0070;
BG_DARK_GREY :: 0x0080;
BG_BLUE :: 0x0090;
BG_GREEN :: 0x00A0;
BG_CYAN :: 0x00B0;
BG_RED :: 0x00C0;
BG_MAGENTA :: 0x00D0;
BG_YELLOW :: 0x00E0;
BG_WHITE :: 0x00F0;
}
PIXEL_TYPE :: enum u32 {
PIXEL_SOLID :: 0x2588;
PIXEL_THREEQUARTERS :: 0x2593;
PIXEL_HALF :: 0x2592;
PIXEL_QUARTER :: 0x2591;
}
Char_Info :: struct {
color: COLOR;
type : PIXEL_TYPE;
}
Screen :: struct {
width : s64;
height: s64;
buffer: [..] Char_Info;
}
init_screen :: (screen: *Screen, width: s64, height: s64) {
buffer : [..] Char_Info;
array_resize(*buffer, width * height);
screen.width = width;
screen.height = height;
screen.buffer = buffer;
}
resize_screen :: (screen: *Screen, width: s64, height: s64) {
screen.width = width;
screen.height = height;
array_resize(*screen.buffer, width * height);
}
#load "draw.jai";
#load "file_operations.jai";
#scope_module
#import "Basic";
#import "Math";

View File

@@ -1,5 +1,12 @@
main :: () { main :: () {
print("Hello, world!");
screen := New(Screen);
init_screen(screen, 5, 2);
draw_line(screen, 0, 0, 0, 0);
print("%\n", screen.buffer);
} }
#import "Console_Render";
#import "Basic"; #import "Basic";