diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 01:26:48 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-06-21 01:26:48 +0300 |
| commit | 8e4a57e034d98777fc58ff90c542c96f18f0e575 (patch) | |
| tree | 033d8d8a788c43a95ce6cece39ecd971ede9577b /framebuffer.c | |
| download | wafflestone-8e4a57e034d98777fc58ff90c542c96f18f0e575.tar.xz | |
Initial commit
Diffstat (limited to 'framebuffer.c')
| -rw-r--r-- | framebuffer.c | 59 |
1 files changed, 59 insertions, 0 deletions
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 <stdlib.h> + +#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); |
