From 8e4a57e034d98777fc58ff90c542c96f18f0e575 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Sun, 21 Jun 2026 01:26:48 +0300 Subject: Initial commit --- framebuffer.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 framebuffer.h (limited to 'framebuffer.h') diff --git a/framebuffer.h b/framebuffer.h new file mode 100644 index 0000000..1b0254f --- /dev/null +++ b/framebuffer.h @@ -0,0 +1,71 @@ +#ifndef FRAMEBUFFER_H_ +#define FRAMEBUFFER_H_ + +#include /* uint8_t */ +#include /* wchar_t */ +#include /* ssize_t */ + +/* Globals for the terminal size, set by fb_init() */ +extern size_t TERM_WIDTH, TERM_HEIGHT; + +/* Represents a 24 bit colour, 8 bit per channel */ +typedef struct Colour_s { + uint8_t r, g, b; +} Colour; + +/* Basic colours */ +#define C_BLACK (Colour){0, 0, 0} +#define C_WHITE (Colour){255, 255, 255} +#define C_RED (Colour){255, 0, 0} +#define C_GREEN (Colour){0, 255, 0} +#define C_BLUE (Colour){0, 0, 255} +#define C_YELLOW (Colour){255, 255, 0} +#define C_CYAN (Colour){0, 255, 255} +#define C_MAGENTA (Colour){255, 0, 255} + +/* One 'pixel' of the image, AKA one character in the terminal. + * For a solid colour pixel, set ch to a space and change the bg colour */ +typedef struct Pixel_s { + Colour fg, bg; + wchar_t ch; /* the char of the pixel */ +} Pixel; + +/* Solid colour pixels */ +#define P_BLACK (Pixel){C_BLACK, C_BLACK, ' '} +#define P_WHITE (Pixel){C_WHITE, C_WHITE, ' '} +#define P_RED (Pixel){C_RED, C_RED, ' '} +#define P_GREEN (Pixel){C_GREEN, C_GREEN, ' '} +#define P_BLUE (Pixel){C_BLUE, C_BLUE, ' '} +#define P_YELLOW (Pixel){C_YELLOW, C_YELLOW, ' '} +#define P_CYAN (Pixel){C_CYAN, C_CYAN, ' '} +#define P_MAGENTA (Pixel){C_MAGENTA, C_MAGENTA, ' '} + +typedef struct Framebuffer_s { + Pixel **fb; /* Adressed as fb[row][col] or fb[x][y] */ + size_t width, height; +} Framebuffer; + +/* Check if fb == NULL in case malloc() fails */ +Framebuffer fb_new(size_t width, size_t height); + +void fb_free(Framebuffer *fb); + +/* Fills fb with pixel */ +void fb_fill(Framebuffer fb, Pixel pixel); + +/* Fills fb with black */ +#define fb_clear(fb) fb_fill(fb, P_BLACK) + +/* Puts a pixel onto fb in a specified position */ +void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel); + +/* Initializes the terminal and sets TERM_WIDTH and TERM_HEIGHT */ +void fb_init(void); + +/* Undoes everything fb_init() did */ +void fb_cleanup(void); + +/* Prints fb to the screen */ +void fb_print(Framebuffer fb); + +#endif /* FRAMEBUFFER_H_ */ -- cgit v1.2.3