summaryrefslogtreecommitdiff
path: root/tests/common_linux.h
diff options
context:
space:
mode:
authorRoberto Esteves <contact@robertoesteves.dev>2026-01-05 14:11:50 +0000
committerRoberto Esteves <contact@robertoesteves.dev>2026-01-05 14:11:50 +0000
commit52c6a7635056ee78d282ba2a55eb26f2663bb577 (patch)
tree9d37d851e63e96e53ef3836321f03d4f2839edef /tests/common_linux.h
parentda12440cabe270584ff650703e90db540d2ec4c9 (diff)
refactor some stuff:HEADmaster
- add meson build system - refactor executable to be a test - remove stdlib from base library
Diffstat (limited to 'tests/common_linux.h')
-rw-r--r--tests/common_linux.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/common_linux.h b/tests/common_linux.h
new file mode 100644
index 0000000..167b483
--- /dev/null
+++ b/tests/common_linux.h
@@ -0,0 +1,70 @@
+#include <rgl.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#define crashErrno() RGL_CRASH(strerror(errno))
+#define assertErrno(cond) RGL_ASSERT(cond, strerror(errno))
+
+void BufferAlloc(RGL_Buffer *buf) {
+ unsigned int size = buf->width * buf->height * 4;
+
+ buf->data = mmap(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+ assertErrno(buf->data != MAP_FAILED);
+}
+
+void BufferDestroy(RGL_Buffer *buf) {
+ unsigned int size = buf->width * buf->height * 4;
+ if (munmap(buf->data, size) < 0) {
+ crashErrno();
+ }
+}
+
+void ArenaInit(RGL_Arena *arena, unsigned int capacity) {
+ if (capacity) {
+ arena->capacity = capacity;
+ } else {
+ arena->capacity = 4096;
+ }
+
+ arena->data = mmap(0, arena->capacity, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+ if (arena->data == MAP_FAILED) {
+ crashErrno();
+ }
+
+ arena->size = 0;
+}
+
+void ArenaDestroy(RGL_Arena *arena) {
+ if (munmap(arena->data, arena->capacity) < 0) {
+ crashErrno();
+ }
+}
+
+int WritePPM(RGL_Buffer buf, const char *filename) {
+ int i;
+ FILE *f = fopen(filename, "wb");
+ RGL_ASSERT(f, "failed to open file");
+
+ fprintf(f, "P6\n%d %d\n255\n", buf.width, buf.height);
+
+ for (i = 0; i < buf.width * buf.height; ++i) {
+ unsigned int pixel = buf.data[i];
+
+ unsigned char r = (pixel >> 16) & 0xFF;
+ unsigned char g = (pixel >> 8) & 0xFF;
+ unsigned char b = pixel & 0xFF;
+
+ fputc(r, f);
+ fputc(g, f);
+ fputc(b, f);
+ }
+
+ fclose(f);
+ return 0;
+}