summaryrefslogtreecommitdiff
path: root/framebuffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'framebuffer.c')
-rw-r--r--framebuffer.c59
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);