From 403730174aeaadcf7a8aad842bc5319050411ef9 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 16 Nov 2025 18:23:37 -0800 Subject: Initial commit --- test/test.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/test.c (limited to 'test/test.c') diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..668dec8 --- /dev/null +++ b/test/test.c @@ -0,0 +1,88 @@ +#include + +#include +#include +#include + +typedef struct RGB { uint8_t r, g, b; } RGB; + +static bool WritePPM(int width, int height, const RGB* image, const char* path) { + const size_t num_pixels = width * height; + + FILE* file = fopen(path, "wb"); + if (!file) { + return false; + } + + fprintf(file, "P6\n%d %d\n255\n", width, height); + if (fwrite(image, sizeof(RGB), num_pixels, file) != num_pixels) { + fclose(file); + return false; + } + + fclose(file); + return true; +} + +void ToRGB(int width, int height, const sgPixel* rgba, RGB* rgb) { + assert(rgba); + assert(rgb); + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + const sgPixel p = *rgba++; + *rgb++ = (RGB){p.r, p.g, p.b}; + } + } +} + +static void Checkerboard(swgfx* gfx, int width, int height) { + assert(gfx); + const sgPixel colour = (sgPixel){255, 0, 255, 255}; + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + if (((x ^ y) & 1) == 1) { + const sgVec2i position = (sgVec2i){x, y}; + sgPixels(gfx, 1, &position, colour); + } + } + } +} + +static void TestTriangle(swgfx* gfx) { + assert(gfx); + + const int BufferWidth = 160; + const int BufferHeight = 120; + const int N = BufferWidth * BufferHeight; + const sgVec2i BufferDims = (sgVec2i){.x = BufferWidth, .y = BufferHeight}; + + sgPixel colour[N]; + RGB rgb[N]; + + sgColourBuffer(gfx, BufferDims, colour); + sgClear(gfx); + Checkerboard(gfx, BufferWidth, BufferHeight); + + const sgVec2 p0 = (sgVec2){20, 20}; + const sgVec2 p1 = (sgVec2){80, 20}; + const sgVec2 p2 = (sgVec2){50, 50}; + const sgTri2 tri = (sgTri2){p0, p1, p2}; + sgTriangles2(gfx, 1, &tri); + + ToRGB(BufferWidth, BufferHeight, colour, rgb); + WritePPM(BufferWidth, BufferHeight, rgb, "triangle.ppm"); +} + +int main() { + swgfx* gfx = 0; + if (!(gfx = sgNew())) { + fprintf(stderr, "Failed to create swgfx\n"); + return 1; + } + + TestTriangle(gfx); + + sgDel(&gfx); + return 0; +} + -- cgit v1.2.3