#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);