diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/test.c | 88 |
1 files changed, 88 insertions, 0 deletions
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 @@ | |||
| 1 | #include <swgfx.h> | ||
| 2 | |||
| 3 | #include <assert.h> | ||
| 4 | #include <stdbool.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | |||
| 7 | typedef struct RGB { uint8_t r, g, b; } RGB; | ||
| 8 | |||
| 9 | static bool WritePPM(int width, int height, const RGB* image, const char* path) { | ||
| 10 | const size_t num_pixels = width * height; | ||
| 11 | |||
| 12 | FILE* file = fopen(path, "wb"); | ||
| 13 | if (!file) { | ||
| 14 | return false; | ||
| 15 | } | ||
| 16 | |||
| 17 | fprintf(file, "P6\n%d %d\n255\n", width, height); | ||
| 18 | if (fwrite(image, sizeof(RGB), num_pixels, file) != num_pixels) { | ||
| 19 | fclose(file); | ||
| 20 | return false; | ||
| 21 | } | ||
| 22 | |||
| 23 | fclose(file); | ||
| 24 | return true; | ||
| 25 | } | ||
| 26 | |||
| 27 | void ToRGB(int width, int height, const sgPixel* rgba, RGB* rgb) { | ||
| 28 | assert(rgba); | ||
| 29 | assert(rgb); | ||
| 30 | for (int y = 0; y < height; ++y) { | ||
| 31 | for (int x = 0; x < width; ++x) { | ||
| 32 | const sgPixel p = *rgba++; | ||
| 33 | *rgb++ = (RGB){p.r, p.g, p.b}; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | static void Checkerboard(swgfx* gfx, int width, int height) { | ||
| 39 | assert(gfx); | ||
| 40 | const sgPixel colour = (sgPixel){255, 0, 255, 255}; | ||
| 41 | for (int y = 0; y < height; ++y) { | ||
| 42 | for (int x = 0; x < width; ++x) { | ||
| 43 | if (((x ^ y) & 1) == 1) { | ||
| 44 | const sgVec2i position = (sgVec2i){x, y}; | ||
| 45 | sgPixels(gfx, 1, &position, colour); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | static void TestTriangle(swgfx* gfx) { | ||
| 52 | assert(gfx); | ||
| 53 | |||
| 54 | const int BufferWidth = 160; | ||
| 55 | const int BufferHeight = 120; | ||
| 56 | const int N = BufferWidth * BufferHeight; | ||
| 57 | const sgVec2i BufferDims = (sgVec2i){.x = BufferWidth, .y = BufferHeight}; | ||
| 58 | |||
| 59 | sgPixel colour[N]; | ||
| 60 | RGB rgb[N]; | ||
| 61 | |||
| 62 | sgColourBuffer(gfx, BufferDims, colour); | ||
| 63 | sgClear(gfx); | ||
| 64 | Checkerboard(gfx, BufferWidth, BufferHeight); | ||
| 65 | |||
| 66 | const sgVec2 p0 = (sgVec2){20, 20}; | ||
| 67 | const sgVec2 p1 = (sgVec2){80, 20}; | ||
| 68 | const sgVec2 p2 = (sgVec2){50, 50}; | ||
| 69 | const sgTri2 tri = (sgTri2){p0, p1, p2}; | ||
| 70 | sgTriangles2(gfx, 1, &tri); | ||
| 71 | |||
| 72 | ToRGB(BufferWidth, BufferHeight, colour, rgb); | ||
| 73 | WritePPM(BufferWidth, BufferHeight, rgb, "triangle.ppm"); | ||
| 74 | } | ||
| 75 | |||
| 76 | int main() { | ||
| 77 | swgfx* gfx = 0; | ||
| 78 | if (!(gfx = sgNew())) { | ||
| 79 | fprintf(stderr, "Failed to create swgfx\n"); | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | TestTriangle(gfx); | ||
| 84 | |||
| 85 | sgDel(&gfx); | ||
| 86 | return 0; | ||
| 87 | } | ||
| 88 | |||
