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.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 framebuffer.c (limited to 'framebuffer.c') diff --git a/framebuffer.c b/framebuffer.c new file mode 100644 index 0000000..b6e922e --- /dev/null +++ b/framebuffer.c @@ -0,0 +1,59 @@ +#include + +#include "framebuffer.h" + +Framebuffer fb_new(size_t width, size_t height) { + Framebuffer fb; + + /* In case malloc() fails and we return early */ + fb.width = 0; + fb.height = 0; + + fb.fb = malloc(sizeof(Pixel *) * height); + if (fb.fb == NULL) return fb; + + for (size_t i = 0; i < height; i++) { + fb.fb[i] = malloc(sizeof(Pixel) * width); + if (fb.fb[i] == NULL) { + /* FIXME: Memory leak; should free all prev. malloced rows */ + fb.fb = NULL; + return fb; + } + } + + fb.width = width; + fb.height = height; + return fb; +} + +void fb_free(Framebuffer *fb) { + if (fb->fb == NULL) goto ret; + + for (size_t i = 0; i < fb->height; i++) { + free(fb->fb[i]); + } + free(fb->fb); + +ret: + fb->width = 0; + fb->height = 0; + return; +} + +void fb_fill(Framebuffer fb, Pixel pixel) { + for (size_t row = 0; row < fb.height; row++) { + for (size_t col = 0; col < fb.width; col++) { + fb_put(fb, row, col, pixel); + } + } +} + +void inline fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel) { + fb.fb[row][col] = pixel; +} + +void fb_init(void); + +void fb_cleanup(void); + +void fb_print(Framebuffer fb); -- cgit v1.2.3