123 lines
3.3 KiB
Plaintext
123 lines
3.3 KiB
Plaintext
/******************************************************
|
|
* 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|
|
|
---------
|
|
|
|
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).
|
|
|
|
=> 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 terminal automatically transforms our 1D sequence, to a 2D sequence displayed on your monitor, based on the size of the window.
|
|
|
|
Since the console is responsible for transforming our 1D array into a 2D output, we must provide the console with a 1D array.
|
|
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.
|
|
|
|
*/
|
|
|
|
BLANK_PIXEL : u8 : 0x20;
|
|
DEFAULT_PIXEL_CHAR : u8 : #char "#";
|
|
|
|
Vec2s64 :: struct {
|
|
x: s64;
|
|
y: s64;
|
|
}
|
|
|
|
Triangle :: struct {
|
|
p1: Vec2s64;
|
|
p3: Vec2s64;
|
|
p2: Vec2s64;
|
|
}
|
|
|
|
Quad :: struct {
|
|
p1: Vec2s64;
|
|
p2: Vec2s64;
|
|
p3: Vec2s64;
|
|
p4: Vec2s64;
|
|
}
|
|
|
|
Screen :: struct {
|
|
width : s64;
|
|
height: s64;
|
|
buffer: [..] u8;
|
|
}
|
|
|
|
init_screen :: (screen: *Screen, width: s64, height: s64) {
|
|
buffer : [..] u8;
|
|
|
|
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);
|
|
}
|
|
|
|
maybe_resize_screen :: (screen: *Screen) {
|
|
w, h := get_console_size();
|
|
|
|
should_update := false;
|
|
|
|
if w != screen.width {
|
|
should_update = true;
|
|
}
|
|
|
|
if h != screen.height {
|
|
should_update = true;
|
|
}
|
|
|
|
if should_update {
|
|
resize_screen(screen, w, h);
|
|
}
|
|
}
|
|
|
|
#load "draw.jai";
|
|
#load "rotate.jai";
|
|
#load "file_operations.jai";
|
|
#load "console.jai";
|
|
#load "math_extras.jai";
|
|
|
|
#if OS == .MACOS {
|
|
#load "macos/console.jai";
|
|
} else {
|
|
#assert false "Platform unsupported. Sorry.";
|
|
}
|
|
|
|
#scope_module
|
|
|
|
#import "Basic";
|
|
#import "Math";
|