aboutsummaryrefslogtreecommitdiff
path: root/drw.c
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-04-22 18:40:22 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2023-04-22 18:40:22 +0200
commitf14acf7306b022c1d0dd98f5fd654dbdc38e64e5 (patch)
tree562e30fdad694f11fc1075b353be6d22d9094171 /drw.c
create ffbg
Diffstat (limited to 'drw.c')
-rw-r--r--drw.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/drw.c b/drw.c
new file mode 100644
index 0000000..b78d10a
--- /dev/null
+++ b/drw.c
@@ -0,0 +1,98 @@
+#include <sys/mman.h>
+#include <unistd.h>
+#include <math.h>
+#include <stdlib.h>
+
+#include "drw.h"
+#include "util.h"
+
+/* static function declarations */
+static Color to_color(uint32_t color);
+static uint32_t to_uint32_t(Color color);
+
+
+Canvas*
+create_drw(struct wl_shm *shm, unsigned width, unsigned height)
+{
+ struct wl_shm_pool *pool;
+ Canvas *canvas;
+ int stride;
+ int fd;
+
+ canvas = malloc(sizeof(Canvas));
+ canvas->width = width;
+ canvas->height = height;
+
+ stride = width * sizeof(uint32_t);
+ canvas->size = stride * height;
+
+ fd = allocate_shm_file(canvas->size);
+ if (fd == -1) {
+ return NULL;
+ }
+
+ canvas->data = mmap(
+ NULL,
+ canvas->size,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED,
+ fd,
+ 0
+ );
+
+ if (canvas->data == MAP_FAILED) {
+ close(fd);
+ return NULL;
+ }
+
+ pool = wl_shm_create_pool(
+ shm,
+ fd,
+ canvas->size
+ );
+
+ canvas->buffer = wl_shm_pool_create_buffer(
+ pool,
+ 0,
+ width,
+ height,
+ stride,
+ WL_SHM_FORMAT_XRGB8888
+ );
+
+ wl_shm_pool_destroy(pool);
+ close(fd);
+
+ return canvas;
+}
+
+
+void
+free_drw(Canvas *canvas)
+{
+ munmap(canvas->data, canvas->size);
+ free(canvas);
+}
+
+
+void
+draw_point(Canvas *canvas, unsigned x, unsigned y, uint32_t color)
+{
+ if (!(x < canvas->width && y < canvas->height))
+ return;
+
+ canvas->data[x + canvas->width * y] = color;
+}
+
+
+void
+draw_rect(Canvas *canvas, unsigned x, unsigned y, unsigned width, unsigned height, uint32_t color) {
+ int py;
+ int px;
+
+ for (py = y; py < y + height; ++py) {
+ for (px = x; px < x + width; ++px) {
+ draw_point(canvas, px, py, color);
+ }
+ }
+}