update module

This commit is contained in:
2024-10-09 00:55:57 -04:00
parent 1b47194249
commit e29f27266a
3 changed files with 80 additions and 26 deletions

View File

@@ -1,13 +1,13 @@
draw :: (screen: *Screen, x: s64, y: s64, char: PIXEL_TYPE = .PIXEL_SOLID, color: COLOR = .FG_WHITE) { draw :: (screen: *Screen, x: s64, y: s64, char: PIXEL_CHAR = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
using screen; using screen;
if x >= 0 && x < width && y >= 0 && y < height { if x >= 0 && x < width && y >= 0 && y < height {
buffer[y * width + x].type = char; buffer[y * width + x].char = char;
buffer[y * width + x].color = color; 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) { draw_line :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, char: PIXEL_CHAR = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
x, y, dx, dy, dx1, dy1, xe, ye, px, py : s64; x, y, dx, dy, dx1, dy1, xe, ye, px, py : s64;
dx = x2 - x1; dx = x2 - x1;
@@ -72,7 +72,7 @@ draw_line :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, char: PIXEL_T
} }
} }
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_triangle :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, x3: s64, y3: s64, char: PIXEL_CHAR = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
draw_line(x1, y1, x2, y2, char, color); draw_line(x1, y1, x2, y2, char, color);
draw_line(x2, y2, x3, y3, char, color); draw_line(x2, y2, x3, y3, char, color);
draw_line(x3, y3, x1, y1, char, color); draw_line(x3, y3, x1, y1, char, color);
@@ -81,23 +81,26 @@ draw_triangle :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, x3: s64,
clip :: (screen: *Screen, x: *s64, y: *s64) { clip :: (screen: *Screen, x: *s64, y: *s64) {
using screen; using screen;
if x < 0 then x = 0; if x.* < 0 then x.* = 0;
if x >= width then x = width; if x.* >= width then x.* = width;
if y < 0 then y = 0; if y.* < 0 then y.* = 0;
if y >= height then y = height; 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) { fill :: (screen: *Screen, x1: s64, y1: s64, x2: s64, y2: s64, char: PIXEL_CHAR = .PIXEL_SOLID, color: COLOR = .FG_WHITE) {
clip(screen, *x1, *y1); clip(screen, *x1, *y1);
clip(screen, *x2, *y2); clip(screen, *x2, *y2);
for x: x1..x2 { for x: x1..x2 {
for y: y1..y2 { for y: y1..y2 {
draw(x, y, char, color); draw(screen, x, y, char, color);
} }
} }
} }
fill_entire_screen :: (screen: *Screen, char: PIXEL_CHAR = .PIXEL_SOLID, color: COLOR = .BG_BLACK) {
fill(screen, 0, 0, screen.width, screen.height, char, color);
}
draw_string :: (screen: *Screen, x: s64, y: s64, text: string, c: COLOR = .FG_WHITE) { draw_string :: (screen: *Screen, x: s64, y: s64, text: string, c: COLOR = .FG_WHITE) {
assert(false, "not implemented"); assert(false, "not implemented");

View File

@@ -1,3 +1,60 @@
/******************************************************
* Copyright 2024 Max Amundsen - All Rights Reserved
******************************************************
Console Screen Rendering:
Terminal emulators, or consoles, are programs that display text from a sequence of characters.
These characters are usually fixed-width, meaning each character takes up the same amount of space on screen.
The dimensions of your console window determines how the console will display the text in rows and columns.
This module is designed to draw simple 2d, or 3d shapes, inside a standard terminal emulator program.
--------------------------------------------------------------
Suppose the following sequence of characters is sent to the console:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U']
For a console displaying 7 characters horizontally, and 3 characters vertically, the result would be the following:
0123456
_________
0 |ABCDEFG|
1 |HIJKLMN|
2 |OPQRSTU|
---------
=> Calculating the two dimensional area of the console output yields the number of elements in the initial 1D sequence:
(7 * 3) = 21
This may appear obvious, however this is an important connection to make, when considering how the console represents its output.
--------------------------------------------------------------
The console application automatically transforms our 1D sequence, to a 2D sequence displayed on your monitor, based on the size of the console window.
As displayed in the above figure, the 0th element, 'A', in the sequence represents (0,0) in 2D space, while the 9th element, 'J' represents (2,1).
The console application will automatically draw the characters on your screen for you.
Because the console application is responsible for transforming our 1D array into a 2D output, we must provide the console with a 1D array.
Therefore in order to draw 2D and 3D elements on the screen, we must construct procedures in the code to represent the transformations from
higher dimensions, down to the first dimension.
This simple 2d -> 1d transformation is implemented as the `draw` procedure found in the `draw.jai` file in this module.
*/
Screen :: struct {
width : s64;
height: s64;
buffer: [..] Char_Info;
}
Char_Info :: struct {
color: COLOR;
char : PIXEL_CHAR;
}
COLOR :: enum u32 { COLOR :: enum u32 {
FG_BLACK :: 0x0000; FG_BLACK :: 0x0000;
FG_DARK_BLUE :: 0x0001; FG_DARK_BLUE :: 0x0001;
@@ -33,24 +90,13 @@ COLOR :: enum u32 {
BG_WHITE :: 0x00F0; BG_WHITE :: 0x00F0;
} }
PIXEL_TYPE :: enum u32 { PIXEL_CHAR :: enum u32 {
PIXEL_SOLID :: 0x2588; PIXEL_SOLID :: 0x2588;
PIXEL_THREEQUARTERS :: 0x2593; PIXEL_THREEQUARTERS :: 0x2593;
PIXEL_HALF :: 0x2592; PIXEL_HALF :: 0x2592;
PIXEL_QUARTER :: 0x2591; 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) { init_screen :: (screen: *Screen, width: s64, height: s64) {
buffer : [..] Char_Info; buffer : [..] Char_Info;

View File

@@ -1,10 +1,15 @@
main :: () { main :: () {
screen := New(Screen); screen := New(Screen);
init_screen(screen, 5, 2); init_screen(screen, 3, 2);
draw_line(screen, 0, 0, 2, 0); fill_entire_screen(screen, color = .FG_BLACK);
draw(screen, 3, 0, color = .FG_GREEN);
// draw(screen, 0, 0, color = .FG_GREEN);
draw_line(screen, 0, 0, 0, 1);
// draw(screen, 3, 0, color = .FG_GREEN);
// Render to terminal output
//swap_buffer(screen);
print("%\n", screen.buffer); print("%\n", screen.buffer);
} }