cmdfx 0.1.0
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
canvas.h File Reference

Canvas API for drawing shapes and text on the terminal. More...

Go to the source code of this file.

Functions

int Canvas_getWidth ()
 Gets the width of the canvas, which is the width of the terminal.
 
int Canvas_getHeight ()
 Gets the height of the canvas, which is the height of the terminal.
 
void Canvas_clearScreen ()
 Clears the screen.
 
void Canvas_setCursor (int x, int y)
 Sets the cursor position.
 
int Canvas_getCursorX ()
 Gets the cursor position.
 
int Canvas_getCursorY ()
 Gets the cursor position.
 
void Canvas_setChar (int x, int y, char c)
 Sets a character at a specific position.
 
void Canvas_setAnsiCurrent (const char *ansi)
 Appends the ANSI code at the current cursor position.
 
void Canvas_setAnsi (int x, int y, const char *ansi)
 Appends the ANSI code at a specific position.
 
int Canvas_isCursorVisible ()
 Gets the cursor visibility.
 
void Canvas_hideCursor ()
 Hides the cursor.
 
void Canvas_showCursor ()
 Shows the cursor.
 
void Canvas_resetFormat ()
 Resets all formatting at the current cursor position.
 
void Canvas_setForeground (int rgb)
 Sets the foreground color at the current cursor position.
 
void Canvas_setBackground (int rgb)
 Sets the background color at the current cursor position.
 
void Canvas_setColor8 (int color)
 Sets the color at the current cursor position using the built-in 8-bit color palette.
 
void Canvas_setForeground256 (int color)
 Sets the foreground color at the current cursor position using the built-in 256-color palette.
 
void Canvas_setBackground256 (int color)
 Sets the background color at the current cursor position using the built-in 256-color palette.
 
void Canvas_enableBold ()
 Enables the bold text attribute.
 
void Canvas_disableBold ()
 Disables the bold text attribute.
 
void Canvas_enableDim ()
 Enables the dim text attribute.
 
void Canvas_disableDim ()
 Disables the dim text attribute.
 
void Canvas_enableItalic ()
 Enables the italic text attribute.
 
void Canvas_disableItalic ()
 Disables the italic text attribute.
 
void Canvas_enableUnderline ()
 Enables the underline text attribute.
 
void Canvas_disableUnderline ()
 Disables the underline text attribute.
 
void Canvas_enableBlink ()
 Enables the blink text attribute.
 
void Canvas_disableBlink ()
 Disables the blink text attribute.
 
void Canvas_enableInvert ()
 Enables the invert text attribute.
 
void Canvas_disableInvert ()
 Disables the invert text attribute.
 
void Canvas_enableHidden ()
 Enables the hidden text attribute.
 
void Canvas_disableHidden ()
 Disables the hidden text attribute.
 
void Canvas_enableStrikethrough ()
 Enables the strikethrough text attribute.
 
void Canvas_disableStrikethrough ()
 Disables the strikethrough text attribute.
 
void Canvas_hLine (int x, int y, int width, char c)
 Draws a horizontal line.
 
void Canvas_vLine (int x, int y, int height, char c)
 Draws a vertical line.
 
void Canvas_rect (int x, int y, int width, int height, char c)
 Draws a hollow rectangle.
 
void Canvas_fillRect (int x, int y, int width, int height, char c)
 Fills a rectangle with a character.
 
void Canvas_circle (int x, int y, int radius, char c)
 Draws a hollow circle.
 
void Canvas_fillCircle (int x, int y, int radius, char c)
 Fills a circle with a character.
 
void Canvas_ellipse (int x, int y, int xradius, int yradius, char c)
 Draws a hollow ellipse.
 
void Canvas_fillEllipse (int x, int y, int xradius, int yradius, char c)
 Fills an ellipse with a character.
 
void Canvas_line (int x1, int y1, int x2, int y2, char c)
 Draws a line between two points.
 
void Canvas_drawText (int x, int y, const char *text)
 Draws text at the current cursor position.
 
void Canvas_drawAscii (int x, int y, char ascii[8][5])
 Draws an ASCII character at a specific position.
 
void Canvas_drawAsciiText (int x, int y, char character, const char *text)
 Draws text at a specific position.
 

Variables

char ASCII_EMPTY [8][5]
 Represents empty space in ASCII art.
 
char ASCII_UPPER_A [8][5]
 An 'A' character in ASCII art using the '#' character.
 
char ASCII_LOWER_A [8][5]
 An 'a' character in ASCII art using the '#' character.
 
char ASCII_UPPER_B [8][5]
 A 'B' character in ASCII art using the '#' character.
 
char ASCII_LOWER_B [8][5]
 A 'b' character in ASCII art using the '#' character.
 
char ASCII_UPPER_C [8][5]
 A 'C' character in ASCII art using the '#' character.
 
char ASCII_LOWER_C [8][5]
 A 'c' character in ASCII art using the '#' character.
 
char ASCII_UPPER_D [8][5]
 A 'D' character in ASCII art using the '#' character.
 
char ASCII_LOWER_D [8][5]
 A 'd' character in ASCII art using the '#' character.
 
char ASCII_UPPER_E [8][5]
 An 'E' character in ASCII art using the '#' character.
 
char ASCII_LOWER_E [8][5]
 An 'e' character in ASCII art using the '#' character.
 
char ASCII_UPPER_F [8][5]
 A 'F' character in ASCII art using the '#' character.
 
char ASCII_LOWER_F [8][5]
 A 'f' character in ASCII art using the '#' character.
 
char ASCII_UPPER_G [8][5]
 A 'G' character in ASCII art using the '#' character.
 
char ASCII_LOWER_G [8][5]
 A 'g' character in ASCII art using the '#' character.
 
char ASCII_UPPER_H [8][5]
 A 'H' character in ASCII art using the '#' character.
 
char ASCII_LOWER_H [8][5]
 A 'h' character in ASCII art using the '#' character.
 
char ASCII_UPPER_I [8][5]
 An 'I' character in ASCII art using the '#' character.
 
char ASCII_LOWER_I [8][5]
 An 'i' character in ASCII art using the '#' character.
 
char ASCII_UPPER_J [8][5]
 A 'J' character in ASCII art using the '#' character.
 
char ASCII_LOWER_J [8][5]
 A 'j' character in ASCII art using the '#' character.
 
char ASCII_UPPER_K [8][5]
 A 'K' character in ASCII art using the '#' character.
 
char ASCII_LOWER_K [8][5]
 A 'k' character in ASCII art using the '#' character.
 
char ASCII_UPPER_L [8][5]
 A 'L' character in ASCII art using the '#' character.
 
char ASCII_LOWER_L [8][5]
 A 'l' character in ASCII art using the '#' character.
 
char ASCII_UPPER_M [8][5]
 A 'M' character in ASCII art using the '#' character.
 
char ASCII_LOWER_M [8][5]
 A 'm' character in ASCII art using the '#' character.
 
char ASCII_UPPER_N [8][5]
 A 'N' character in ASCII art using the '#' character.
 
char ASCII_LOWER_N [8][5]
 A 'n' character in ASCII art using the '#' character.
 
char ASCII_UPPER_O [8][5]
 An 'O' character in ASCII art using the '#' character.
 
char ASCII_LOWER_O [8][5]
 An 'o' character in ASCII art using the '#' character.
 
char ASCII_UPPER_P [8][5]
 A 'P' character in ASCII art using the '#' character.
 
char ASCII_LOWER_P [8][5]
 A 'p' character in ASCII art using the '#' character.
 
char ASCII_UPPER_Q [8][5]
 A 'Q' character in ASCII art using the '#' character.
 
char ASCII_LOWER_Q [8][5]
 A 'q' character in ASCII art using the '#' character.
 
char ASCII_UPPER_R [8][5]
 A 'R' character in ASCII art using the '#' character.
 
char ASCII_LOWER_R [8][5]
 A 'r' character in ASCII art using the '#' character.
 
char ASCII_UPPER_S [8][5]
 A 'S' character in ASCII art using the '#' character.
 
char ASCII_LOWER_S [8][5]
 A 's' character in ASCII art using the '#' character.
 
char ASCII_UPPER_T [8][5]
 A 'T' character in ASCII art using the '#' character.
 
char ASCII_LOWER_T [8][5]
 A 't' character in ASCII art using the '#' character.
 
char ASCII_UPPER_U [8][5]
 An 'U' character in ASCII art using the '#' character.
 
char ASCII_LOWER_U [8][5]
 A 'u' character in ASCII art using the '#' character.
 
char ASCII_UPPER_V [8][5]
 A 'V' character in ASCII art using the '#' character.
 
char ASCII_LOWER_V [8][5]
 A 'v' character in ASCII art using the '#' character.
 
char ASCII_UPPER_W [8][5]
 A 'W' character in ASCII art using the '#' character.
 
char ASCII_LOWER_W [8][5]
 A 'w' character in ASCII art using the '#' character.
 
char ASCII_UPPER_X [8][5]
 An 'X' character in ASCII art using the '#' character.
 
char ASCII_LOWER_X [8][5]
 A 'x' character in ASCII art using the '#' character.
 
char ASCII_UPPER_Y [8][5]
 A 'Y' character in ASCII art using the '#' character.
 
char ASCII_LOWER_Y [8][5]
 A 'y' character in ASCII art using the '#' character.
 
char ASCII_UPPER_Z [8][5]
 A 'Z' character in ASCII art using the '#' character.
 
char ASCII_LOWER_Z [8][5]
 A 'z' character in ASCII art using the '#' character.
 
char ASCII_ZERO [8][5]
 A '0' character in ASCII art using the '#' character.
 
char ASCII_ONE [8][5]
 A '1' character in ASCII art using the '#' character.
 
char ASCII_TWO [8][5]
 A '2' character in ASCII art using the '#' character.
 
char ASCII_THREE [8][5]
 A '3' character in ASCII art using the '#' character.
 
char ASCII_FOUR [8][5]
 A '4' character in ASCII art using the '#' character.
 
char ASCII_FIVE [8][5]
 A '5' character in ASCII art using the '#' character.
 
char ASCII_SIX [8][5]
 A '6' character in ASCII art using the '#' character.
 
char ASCII_SEVEN [8][5]
 A '7' character in ASCII art using the '#' character.
 
char ASCII_EIGHT [8][5]
 A '8' character in ASCII art using the '#' character.
 
char ASCII_NINE [8][5]
 A '9' character in ASCII art using the '#' character.
 
char ASCII_MAP [128][8][5]
 Represents a mapping of ASCII characters to their respective ASCII art.
 

Detailed Description

Canvas API for drawing shapes and text on the terminal.

Author
Gregory Mitchell (me@gm.nosp@m.itch.nosp@m.215.x.nosp@m.yz)
Version
0.1.0
Date
2025-01-20

Function Documentation

◆ Canvas_circle()

void Canvas_circle ( int x,
int y,
int radius,
char c )

Draws a hollow circle.

Parameters
xX coordinate.
yY coordinate.
radiusRadius of the circle.
cThe character to draw the circle with.

◆ Canvas_clearScreen()

void Canvas_clearScreen ( )

Clears the screen.

This method uses a system call to clear the screen.

◆ Canvas_disableBlink()

void Canvas_disableBlink ( )

Disables the blink text attribute.

This is a simple print of the ANSI code "\033[25m".

◆ Canvas_disableBold()

void Canvas_disableBold ( )

Disables the bold text attribute.

This is a simple print of the ANSI code "\033[22m".

Both this and Canvas_disableDim share the same code, meaning it will disable both bold and dim text if both are enabled.

◆ Canvas_disableDim()

void Canvas_disableDim ( )

Disables the dim text attribute.

This is a simple print of the ANSI code "\033[22m".

Faint is not widely supported, and is often rendered as normal text.

Both this and Canvas_disableBold share the same code, meaning it will disable both bold and dim text if both are enabled.

◆ Canvas_disableHidden()

void Canvas_disableHidden ( )

Disables the hidden text attribute.

This is a simple print of the ANSI code "\033[28m".

◆ Canvas_disableInvert()

void Canvas_disableInvert ( )

Disables the invert text attribute.

This is a simple print of the ANSI code "\033[27m".

◆ Canvas_disableItalic()

void Canvas_disableItalic ( )

Disables the italic text attribute.

This is a simple print of the ANSI code "\033[23m".

Italic is not widely supported, and is often rendered as normal text.

◆ Canvas_disableStrikethrough()

void Canvas_disableStrikethrough ( )

Disables the strikethrough text attribute.

This is a simple print of the ANSI code "\033[29m".

◆ Canvas_disableUnderline()

void Canvas_disableUnderline ( )

Disables the underline text attribute.

This is a simple print of the ANSI code "\033[24m".

◆ Canvas_drawAscii()

void Canvas_drawAscii ( int x,
int y,
char ascii[8][5] )

Draws an ASCII character at a specific position.

Parameters
xX coordinate.
yY coordinate.
asciiThe ASCII character as a 2D array of characters.

◆ Canvas_drawAsciiText()

void Canvas_drawAsciiText ( int x,
int y,
char character,
const char * text )

Draws text at a specific position.

This method uses the Canvas_drawAscii method to draw ASCII characters. The ASCII characters are stored in pre-defined 2D arrays that are declared in this header. Characters that are not present in the header will be skipped.

Parameters
xX coordinate.
yY coordinate.
characterThe character to use when drawing.
textThe text to draw.

◆ Canvas_drawText()

void Canvas_drawText ( int x,
int y,
const char * text )

Draws text at the current cursor position.

Parameters
xX coordinate.
yY coordinate.
textThe text to draw.

◆ Canvas_ellipse()

void Canvas_ellipse ( int x,
int y,
int xradius,
int yradius,
char c )

Draws a hollow ellipse.

Parameters
xX coordinate.
yY coordinate.
xradiusX radius of the ellipse.
yradiusY radius of the ellipse.
cThe character to draw the ellipse with.

◆ Canvas_enableBlink()

void Canvas_enableBlink ( )

Enables the blink text attribute.

This is a simple print of the ANSI code "\033[5m".

"Blinking" refers to the text blinking on and off.

◆ Canvas_enableBold()

void Canvas_enableBold ( )

Enables the bold text attribute.

This is a simple print of the ANSI code "\033[1m".

◆ Canvas_enableDim()

void Canvas_enableDim ( )

Enables the dim text attribute.

This is a simple print of the ANSI code "\033[2m".

Faint is not widely supported, and is often rendered as normal text.

◆ Canvas_enableHidden()

void Canvas_enableHidden ( )

Enables the hidden text attribute.

Hidden is not widely supported, and is often rendered as normal text.

This is a simple print of the ANSI code "\033[8m".

"Hidden" refers to the text being hidden.

◆ Canvas_enableInvert()

void Canvas_enableInvert ( )

Enables the invert text attribute.

This is a simple print of the ANSI code "\033[7m".

"Invert" refers to the text and background colors being swapped.

◆ Canvas_enableItalic()

void Canvas_enableItalic ( )

Enables the italic text attribute.

This is a simple print of the ANSI code "\033[3m".

Italic is not widely supported, and is often rendered as normal text.

◆ Canvas_enableStrikethrough()

void Canvas_enableStrikethrough ( )

Enables the strikethrough text attribute.

Strikethrough is not widely supported, and is often rendered as normal text.

This is a simple print of the ANSI code "\033[9m".

"Strikethrough" refers to the text having a line through the middle.

◆ Canvas_enableUnderline()

void Canvas_enableUnderline ( )

Enables the underline text attribute.

This is a simple print of the ANSI code "\033[4m".

◆ Canvas_fillCircle()

void Canvas_fillCircle ( int x,
int y,
int radius,
char c )

Fills a circle with a character.

Parameters
xX coordinate.
yY coordinate.
radiusRadius of the circle.
cThe character to fill the circle with.

◆ Canvas_fillEllipse()

void Canvas_fillEllipse ( int x,
int y,
int xradius,
int yradius,
char c )

Fills an ellipse with a character.

Parameters
xX coordinate.
yY coordinate.
xradiusX radius of the ellipse.
yradiusY radius of the ellipse.
cThe character to fill the ellipse with.

◆ Canvas_fillRect()

void Canvas_fillRect ( int x,
int y,
int width,
int height,
char c )

Fills a rectangle with a character.

Parameters
xX coordinate.
yY coordinate.
widthWidth of the rectangle.
heightHeight of the rectangle.
cThe character to fill the rectangle with.

◆ Canvas_getCursorX()

int Canvas_getCursorX ( )

Gets the cursor position.

On POSIX, this method uses the ESC[6n ANSI code to get the cursor position. This can make the method more expensive than on Windows, where the cursor position is stored internally. On Windows, this method uses the GetConsoleScreenBufferInfo function to get the cursor position.

Returns
The cursor position.

◆ Canvas_getCursorY()

int Canvas_getCursorY ( )

Gets the cursor position.

On POSIX, this method uses the ESC[6n ANSI code to get the cursor position. This can make the method more expensive than on Windows, where the cursor position is stored internally. On Windows, this method uses the GetConsoleScreenBufferInfo function to get the cursor position.

Returns
The cursor position.

◆ Canvas_getHeight()

int Canvas_getHeight ( )

Gets the height of the canvas, which is the height of the terminal.

Returns
The height of the canvas.

◆ Canvas_getWidth()

int Canvas_getWidth ( )

Gets the width of the canvas, which is the width of the terminal.

Returns
The width of the canvas.

◆ Canvas_hideCursor()

void Canvas_hideCursor ( )

Hides the cursor.

On POSIX, this is a simple print of the ANSI code "\033[?25l", which hides the cursor. On Windows, this is a call to the SetConsoleCursorInfo function.

◆ Canvas_hLine()

void Canvas_hLine ( int x,
int y,
int width,
char c )

Draws a horizontal line.

Parameters
xX coordinate.
yY coordinate.
widthWidth of the line.
cThe character to draw the line with.

◆ Canvas_isCursorVisible()

int Canvas_isCursorVisible ( )

Gets the cursor visibility.

On POSIX, this is handled by an internal variable. On Windows, this is determined by the cursor visibility state. Therefore, this method is often less reliable on POSIX systems, since it cannot detect changes made by other programs.

Returns
1 if the cursor is visible, 0 if it is hidden.

◆ Canvas_line()

void Canvas_line ( int x1,
int y1,
int x2,
int y2,
char c )

Draws a line between two points.

Parameters
x1The x coordinate of the first point.
y1The y coordinate of the first point.
x2The x coordinate of the second point.
y2The y coordinate of the second point.
cThe character to draw the line with.

◆ Canvas_rect()

void Canvas_rect ( int x,
int y,
int width,
int height,
char c )

Draws a hollow rectangle.

Parameters
xX coordinate.
yY coordinate.
widthWidth of the rectangle.
heightHeight of the rectangle.
cThe character to draw the rectangle with.

◆ Canvas_resetFormat()

void Canvas_resetFormat ( )

Resets all formatting at the current cursor position.

This is a simple print of the ANSI code "\033[0m", which resets all formatting at the current cursor position.

◆ Canvas_setAnsi()

void Canvas_setAnsi ( int x,
int y,
const char * ansi )

Appends the ANSI code at a specific position.

This method sets the ANSI code at a specific position. The ANSI code is a series of semicolon-separated numbers and letters that control the terminal behavior. For example, the ANSI code "\033[31m" sets the text color to red.

Parameters
xX coordinate.
yY coordinate.
ansiThe ANSI code.

◆ Canvas_setAnsiCurrent()

void Canvas_setAnsiCurrent ( const char * ansi)

Appends the ANSI code at the current cursor position.

This method sets the ANSI code at a specific position. The ANSI code is a series of semicolon-separated numbers and letters that control the terminal behavior. For example, the ANSI code "\033[31m" sets the text color to red.

Parameters
ansiThe ANSI code.

◆ Canvas_setBackground()

void Canvas_setBackground ( int rgb)

Sets the background color at the current cursor position.

This uses the ANSI code "\033[48;2;r;g;bm", where r, g, and b are the red, green, and blue values of the color, respectively.

Parameters
rgbThe RGB color.

◆ Canvas_setBackground256()

void Canvas_setBackground256 ( int color)

Sets the background color at the current cursor position using the built-in 256-color palette.

This uses the ANSI code "\033[48;5;{ID}m", where {ID} is the color number.

Parameters
colorThe color number.

◆ Canvas_setChar()

void Canvas_setChar ( int x,
int y,
char c )

Sets a character at a specific position.

Parameters
xX coordinate.
yY coordinate.
cThe character to set.

◆ Canvas_setColor8()

void Canvas_setColor8 ( int color)

Sets the color at the current cursor position using the built-in 8-bit color palette.

This uses the ANSI code "\033[{ID}m", where m is the color number.

Parameters
colorThe color number.

◆ Canvas_setCursor()

void Canvas_setCursor ( int x,
int y )

Sets the cursor position.

Parameters
xX coordinate.
yY coordinate.

◆ Canvas_setForeground()

void Canvas_setForeground ( int rgb)

Sets the foreground color at the current cursor position.

This uses the ANSI code "\033[38;2;r;g;bm", where r, g, and b are the red, green, and blue values of the color, respectively.

Parameters
rgbThe RGB color.

◆ Canvas_setForeground256()

void Canvas_setForeground256 ( int color)

Sets the foreground color at the current cursor position using the built-in 256-color palette.

This uses the ANSI code "\033[38;5;{ID}m", where {ID} is the color number.

Parameters
colorThe color number.

◆ Canvas_showCursor()

void Canvas_showCursor ( )

Shows the cursor.

On POSIX, this is a simple print of the ANSI code "\033[?25h", which shows the cursor. On Windows, this is a call to the SetConsoleCursorInfo function.

◆ Canvas_vLine()

void Canvas_vLine ( int x,
int y,
int height,
char c )

Draws a vertical line.

Parameters
xX coordinate.
yY coordinate.
heightHeight of the line.
cThe character to draw the line with.

Variable Documentation

◆ ASCII_MAP

char ASCII_MAP[128][8][5]
extern

Represents a mapping of ASCII characters to their respective ASCII art.

This 3D array is automatically initialized when Canvas_drawAsciiText is called.

This 3D array is used to map ASCII characters to their respective ASCII art. The first dimension is the ASCII character, the second dimension is the row, and the third dimension is the column. The ASCII art is represented as a 2D array of characters, and drawn using the Canvas_drawAscii method.