summaryrefslogtreecommitdiff
path: root/framebuffer.h
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-06-21 01:26:48 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-06-21 01:26:48 +0300
commit8e4a57e034d98777fc58ff90c542c96f18f0e575 (patch)
tree033d8d8a788c43a95ce6cece39ecd971ede9577b /framebuffer.h
downloadwafflestone-8e4a57e034d98777fc58ff90c542c96f18f0e575.tar.xz
Initial commit
Diffstat (limited to 'framebuffer.h')
-rw-r--r--framebuffer.h71
1 files changed, 71 insertions, 0 deletions
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 <stdint.h> /* uint8_t */
+#include <stddef.h> /* wchar_t */
+#include <sys/types.h> /* 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_ */