diff options
| author | Roberto Esteves <contact@robertoesteves.dev> | 2026-01-05 14:11:50 +0000 |
|---|---|---|
| committer | Roberto Esteves <contact@robertoesteves.dev> | 2026-01-05 14:11:50 +0000 |
| commit | 52c6a7635056ee78d282ba2a55eb26f2663bb577 (patch) | |
| tree | 9d37d851e63e96e53ef3836321f03d4f2839edef /tests | |
| parent | da12440cabe270584ff650703e90db540d2ec4c9 (diff) | |
- add meson build system
- refactor executable to be a test
- remove stdlib from base library
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common_linux.h | 70 | ||||
| -rw-r--r-- | tests/meson.build | 8 | ||||
| -rw-r--r-- | tests/test_stroke.c | 33 |
3 files changed, 111 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; +} diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..f4f46e7 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,8 @@ +exe_stroke = executable( + 'testStroke', + 'test_stroke.c', + link_with: [lib_rgl], + include_directories: incdir, +) + +test('stroke', exe_stroke) diff --git a/tests/test_stroke.c b/tests/test_stroke.c new file mode 100644 index 0000000..1bcafd1 --- /dev/null +++ b/tests/test_stroke.c @@ -0,0 +1,33 @@ +#include "common_linux.h" + +int main(int argc, char *argv[]) { + RGL_Context ctx; + RGL_Buffer buf; + + buf.width = 800; + buf.height = 600; + + ArenaInit(&ctx.queue.arena, 0); + ctx.queue.commands = (RGL_Command *)ctx.queue.arena.data; + + BufferAlloc(&buf); + + ctx.color = RGL_COLOR(255, 255, 255, 255); + + RGL_BeginPath(&ctx, PathStroke); + RGL_PathMoveTo(&ctx, 0, 0); + RGL_PathMoveTo(&ctx, 799, 599); + RGL_PathMoveTo(&ctx, 799, 0); + RGL_PathMoveTo(&ctx, 0, 599); + RGL_EndPath(&ctx); + + RGL_Draw(&ctx, &buf); + + if (WritePPM(buf, "output.ppm")) + return 1; + + ArenaDestroy(&ctx.queue.arena); + BufferDestroy(&buf); + + return 0; +} |