123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #include <errno.h>
- #include "./assets/tsodinPog.c"
- #include "./assets/tsodinCup.c"
- #define NOB_IMPLEMENTATION
- #define NOB_STRIP_PREFIX
- #include "nob.h"
- #define PI 3.14159265359
- #define ARENA_IMPLEMENTATION
- #include "./arena.h"
- static Arena default_arena = {0};
- static Arena *context_arena = &default_arena;
- static void *context_alloc(size_t size)
- {
- assert(context_arena);
- return arena_alloc(context_arena, size);
- }
- static void *context_realloc(void *oldp, size_t oldsz, size_t newsz)
- {
- if (newsz <= oldsz) return oldp;
- return memcpy(context_alloc(newsz), oldp, oldsz);
- }
- #define STBI_MALLOC context_alloc
- #define STBI_FREE UNUSED
- #define STBI_REALLOC_SIZED context_realloc
- #define STB_IMAGE_IMPLEMENTATION
- #include "./stb_image.h"
- #define STBIW_MALLOC STBI_MALLOC
- #define STBIW_FREE STBI_FREE
- #define STBIW_REALLOC_SIZED STBI_REALLOC_SIZED
- #define STB_IMAGE_WRITE_IMPLEMENTATION
- #include "./stb_image_write.h"
- #define OLIVEC_IMPLEMENTATION
- #include "olive.c"
- #define BACKGROUND_COLOR 0xFF202020
- #define FOREGROUND_COLOR 0xFF2020FF
- #define WHITE_COLOR 0xFFAAAAAA
- #define RED_COLOR 0xFF2020AA
- #define GREEN_COLOR 0xFF20AA20
- #define BLUE_COLOR 0xFFAA2020
- #define ERROR_COLOR 0xFFFF00FF
- #define TEST_DIR_PATH "./test"
- bool canvas_stbi_load(const char *file_path, Olivec_Canvas *oc)
- {
- int width, height;
- uint32_t *pixels = (uint32_t*) stbi_load(file_path, &width, &height, NULL, 4);
- if (pixels == NULL) return false;
- *oc = olivec_canvas(pixels, width, height, width);
- return true;
- }
- bool canvas_stbi_save(Olivec_Canvas oc, const char *file_path)
- {
- return stbi_write_png(file_path, oc.width, oc.height, 4, oc.pixels, sizeof(uint32_t)*oc.stride);
- }
- typedef struct {
- Olivec_Canvas (*generate_actual_canvas)(void);
- const char *id;
- const char *expected_file_path;
- const char *actual_file_path;
- const char *diff_file_path;
- } Test_Case;
- #define DEFINE_TEST_CASE(name) \
- { \
- .generate_actual_canvas = test_##name, \
- .id = #name, \
- .expected_file_path = TEST_DIR_PATH "/" #name "_expected.png", \
- .actual_file_path = TEST_DIR_PATH "/" #name "_actual.png", \
- .diff_file_path = TEST_DIR_PATH "/" #name "_diff.png", \
- }
- bool update_test_case(const Test_Case *tc)
- {
- Olivec_Canvas actual_canvas = tc->generate_actual_canvas();
- const char *expected_file_path = tc->expected_file_path;
- if (!canvas_stbi_save(actual_canvas, expected_file_path)) {
- fprintf(stderr, "ERROR: could not write file %s: %s\n", expected_file_path, strerror(errno));
- return(false);
- }
- printf("%s: Generated %s\n", tc->id, expected_file_path);
- return(true);
- }
- Olivec_Canvas canvas_alloc(size_t width, size_t height)
- {
- uint32_t *pixels = context_alloc(sizeof(uint32_t)*width*height);
- return olivec_canvas(pixels, width, height, width);
- }
- typedef enum {
- REPLAY_PASSED,
- REPLAY_FAILED,
- REPLAY_ERRORED,
- } Replay_Result;
- static inline size_t min_size(size_t a, size_t b)
- {
- if (a < b) return a;
- return b;
- }
- static inline size_t max_size(size_t a, size_t b)
- {
- if (a > b) return a;
- return b;
- }
- Replay_Result run_test_case(const char *program_path, const Test_Case *tc)
- {
- printf("%s:", tc->id);
- fflush(stdout);
- const char *expected_file_path = tc->expected_file_path;
- const char *actual_file_path = tc->actual_file_path;
- const char *diff_file_path = tc->diff_file_path;
- Olivec_Canvas actual_canvas = tc->generate_actual_canvas();
- Olivec_Canvas expected_canvas;
- if (!canvas_stbi_load(expected_file_path, &expected_canvas)) {
- fprintf(stderr, "\n");
- fprintf(stderr, " ERROR: could not read %s: %s\n", expected_file_path, stbi_failure_reason());
- if (errno == ENOENT) {
- fprintf(stderr, " HINT: Consider running `$ %s update %s` to create it\n", program_path, tc->id);
- }
- return(REPLAY_ERRORED);
- }
- bool failed = false;
- if (expected_canvas.width != actual_canvas.width || expected_canvas.height != actual_canvas.height) {
- failed = true;
- }
- Olivec_Canvas diff_canvas =
- canvas_alloc(
- max_size(expected_canvas.width, actual_canvas.width),
- max_size(expected_canvas.height, actual_canvas.height));
- olivec_fill(diff_canvas, ERROR_COLOR);
- for (size_t y = 0; y < min_size(expected_canvas.height, actual_canvas.height); ++y) {
- for (size_t x = 0; x < min_size(expected_canvas.width, actual_canvas.width); ++x) {
- uint32_t expected_pixel = OLIVEC_PIXEL(expected_canvas, x, y);
- uint32_t actual_pixel = OLIVEC_PIXEL(actual_canvas, x, y);
- if (expected_pixel != actual_pixel) {
- OLIVEC_PIXEL(diff_canvas, x, y) = ERROR_COLOR;
- failed = true;
- } else {
- OLIVEC_PIXEL(diff_canvas, x, y) = expected_pixel;
- }
- }
- }
- if (failed) {
- fprintf(stderr, "\n");
- if (!canvas_stbi_save(actual_canvas, actual_file_path)) {
- fprintf(stderr, " ERROR: could not write image file with actual pixels %s: %s\n", actual_file_path, strerror(errno));
- return(REPLAY_ERRORED);
- }
- if (!canvas_stbi_save(diff_canvas, diff_file_path)) {
- fprintf(stderr, " ERROR: could not wrilte diff image file %s: %s\n", diff_file_path, strerror(errno));
- return(REPLAY_ERRORED);
- }
- fprintf(stderr, " TEST FAILURE: unexpected pixels in generated image\n");
- fprintf(stderr, " Expected: %s\n", expected_file_path);
- fprintf(stderr, " Actual: %s\n", actual_file_path);
- fprintf(stderr, " Diff: %s\n", diff_file_path);
- fprintf(stderr, " HINT: If this behaviour is intentional confirm that by updating the image with `$ %s update`\n", program_path);
- return(REPLAY_FAILED);
- }
- printf(" OK\n");
- return(REPLAY_PASSED);
- }
- Olivec_Canvas test_fill_rect(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_rect(oc, width/2 - width/8, height/2 - height/8, width/4, height/4, RED_COLOR);
- olivec_rect(oc, width - 1, height - 1, -width/2, -height/2, GREEN_COLOR);
- olivec_rect(oc, -width/4, -height/4, width/2, height/2, BLUE_COLOR);
- return oc;
- }
- Olivec_Canvas test_fill_circle(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_circle(oc, 0, 0, width/2, RED_COLOR);
- olivec_circle(oc, width/2, height/2, width/4, BLUE_COLOR);
- olivec_circle(oc, width*3/4, height*3/4, -width/4, GREEN_COLOR);
- return oc;
- }
- Olivec_Canvas test_draw_line(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_line(oc, 0, 0, width, height, RED_COLOR);
- olivec_line(oc, width, 0, 0, height, BLUE_COLOR);
- olivec_line(oc, width/2, 0, width/2, height, GREEN_COLOR);
- return oc;
- }
- Olivec_Canvas test_fill_triangle(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- {
- int x1 = width/2, y1 = height/8;
- int x2 = width/8, y2 = height/2;
- int x3 = width*7/8, y3 = height*7/8;
- olivec_triangle(oc, x1, y1, x2, y2, x3, y3, RED_COLOR);
- }
- {
- int x1 = width/2, y1 = height*2/8;
- int x2 = width*2/8, y2 = height/2;
- int x3 = width*6/8, y3 = height/2;
- olivec_triangle(oc, x1, y1, x2, y2, x3, y3, GREEN_COLOR);
- }
- {
- int x1 = width/8, y1 = height/8;
- int x2 = width/8, y2 = height*3/8;
- int x3 = width*3/8, y3 = height*3/8;
- olivec_triangle(oc, x1, y1, x2, y2, x3, y3, BLUE_COLOR);
- }
- return oc;
- }
- Olivec_Canvas test_alpha_blending(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_rect(oc, 0, 0, width*3/4, height*3/4, RED_COLOR);
- olivec_rect(oc, width-1, height-1, -width*3/4, -height*3/4, 0x5520AA20);
- olivec_circle(oc, width/2, height/2, width/4, 0xBBAA2020);
- olivec_triangle(oc, 0, height-1, width-1, height-1, width/2, 0, 0xBB20AAAA);
- olivec_triangle3c(oc, 0, 0, width-1, 0, width/2, height-1, 0xBB2020AA, 0xBB20AA20, 0xBBAA2020);
- return oc;
- }
- Olivec_Canvas test_checker_example(void)
- {
- int width = 800/2;
- int height = 600/2;
- int cols = (8*2);
- int rows = (6*2);
- int cell_width = (width/cols);
- int cell_height = (height/rows);
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- for (int y = 0; y < rows; ++y) {
- for (int x = 0; x < cols; ++x) {
- uint32_t color = BACKGROUND_COLOR;
- if ((x + y)%2 == 0) {
- color = 0xFF2020FF;
- }
- olivec_rect(oc, x*cell_width, y*cell_height, cell_width, cell_height, color);
- }
- }
- return oc;
- }
- Olivec_Canvas test_circle_example(void)
- {
- int width = 800/2;
- int height = 600/2;
- int cols = (8*2);
- int rows = (6*2);
- int cell_width = (width/cols);
- int cell_height = (height/rows);
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- for (int y = 0; y < rows; ++y) {
- for (int x = 0; x < cols; ++x) {
- float u = (float)x/cols;
- float v = (float)y/rows;
- float t = (u + v)/2;
- int radius = cell_width;
- if (cell_height < radius) radius = cell_height;
- olivec_circle(oc,
- x*cell_width + cell_width/2, y*cell_height + cell_height/2,
- (size_t) (radius/8*(1 - t) + radius/2*t),
- FOREGROUND_COLOR);
- }
- }
- return oc;
- }
- Olivec_Canvas test_lines_circle(void)
- {
- int width = 800/2;
- int height = 600/2;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- size_t n = 20;
- float angle = 2*PI/n;
- float length = width;
- if (length > height) length = height;
- length /= 3;
- float x1 = width/2;
- float y1 = height/2;
- for (size_t i = 0; i < n; ++i) {
- float x2 = x1 + cosf(angle*i)*length;
- float y2 = y1 + sinf(angle*i)*length;
- olivec_line(oc, x1, y1, x2, y2, 0xFF1818FF);
- }
- return oc;
- }
- Olivec_Canvas test_lines_example(void)
- {
- int width = 800/2;
- int height = 600/2;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_line(oc, 0, 0, width, height, FOREGROUND_COLOR);
- olivec_line(oc, width, 0, 0, height, FOREGROUND_COLOR);
- olivec_line(oc, 0, 0, width/4, height, 0xFF20FF20);
- olivec_line(oc, width/4, 0, 0, height, 0xFF20FF20);
- olivec_line(oc, width, 0, width/4*3, height, 0xFF20FF20);
- olivec_line(oc, width/4*3, 0, width, height, 0xFF20FF20);
- olivec_line(oc, 0, height/2, width, height/2, 0xFFFF3030);
- olivec_line(oc, width/2, 0, width/2, height, 0xFFFF3030);
- return oc;
- }
- Olivec_Canvas test_hello_world_text_rendering(void)
- {
- size_t size = 5;
- const char *text = "hello, world";
- size_t text_len = strlen(text);
- Olivec_Canvas oc = canvas_alloc(400, 150);
- olivec_fill(oc, BACKGROUND_COLOR);
- olivec_text(oc, text, oc.width/2 - OLIVEC_DEFAULT_FONT_WIDTH*size*text_len/2, oc.height/2 - OLIVEC_DEFAULT_FONT_HEIGHT*size/2, olivec_default_font, size, FOREGROUND_COLOR);
- return oc;
- }
- Olivec_Canvas test_line_edge_cases(void)
- {
- size_t width = 10;
- size_t height = 10;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- // One pixel line
- olivec_line(oc, width/2, height/2, width/2, height/2, FOREGROUND_COLOR);
- // Out-of-bounds horizontally
- olivec_line(oc, width + 10, height/2, width + 20, height/2, FOREGROUND_COLOR);
- // Out-of-bounds vertically
- olivec_line(oc, width/2, height + 10, width/2, height + 20, FOREGROUND_COLOR);
- return oc;
- }
- Olivec_Canvas test_frame(void)
- {
- size_t width = 256;
- size_t height = 128;
- Olivec_Canvas oc = canvas_alloc(width, height);
- olivec_fill(oc, BACKGROUND_COLOR);
- {
- size_t w = width/2;
- size_t h = height/2;
- olivec_frame(oc, 0, 0, w, h, 1, RED_COLOR);
- }
- {
- olivec_frame(oc, width/2, height/2, width, height, 1, GREEN_COLOR);
- }
- // Odd thiccness
- {
- size_t w = width/2;
- size_t h = height/2;
- size_t t = 5;
- olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
- olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, 1, RED_COLOR);
- }
- // Even thiccness
- {
- size_t w = width/4 + 1;
- size_t h = height/4;
- size_t t = 6;
- olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
- olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, 1, RED_COLOR);
- }
- // Zero thiccness
- {
- size_t w = width/8;
- size_t h = height/8;
- size_t t = 0;
- olivec_frame(oc, width/2 - w/2, height/2 - h/2, w, h, t, WHITE_COLOR);
- }
- return oc;
- }
- Olivec_Canvas test_sprite_blend(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas dst = canvas_alloc(width, height);
- olivec_fill(dst, RED_COLOR);
- Olivec_Canvas src = canvas_alloc(width, height);
- for (size_t y = 0; y < src.height; ++y) {
- for (size_t x = 0; x < src.width; ++x) {
- if ((x + y)%2 == 0) {
- OLIVEC_PIXEL(src, x, y) = 0;
- } else {
- OLIVEC_PIXEL(src, x, y) = GREEN_COLOR;
- }
- }
- }
- olivec_sprite_blend(dst, 0, 0, width, height, src);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_out_of_bounds_cut(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas dst = canvas_alloc(width, height);
- Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
- olivec_fill(dst, RED_COLOR);
- olivec_sprite_blend(dst, -width/2, -height/2, width, height, src);
- olivec_sprite_blend(dst, width/2, -height/2, width, height, src);
- olivec_sprite_blend(dst, -width/2, height/2, width, height, src);
- olivec_sprite_blend(dst, width/2, height/2, width, height, src);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_flip(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas dst = canvas_alloc(width, height);
- Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
- olivec_fill(dst, RED_COLOR);
- olivec_sprite_blend(dst, 0, 0, width/2, height/2, src);
- olivec_sprite_blend(dst, width - 1, 0, -width/2, height/2, src);
- olivec_sprite_blend(dst, 0, height - 1, width/2, -height/2, src);
- olivec_sprite_blend(dst, width - 1, height - 1, -width/2, -height/2, src);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_flip_cut(void)
- {
- size_t width = 128;
- size_t height = 128;
- Olivec_Canvas dst = canvas_alloc(width, height);
- Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
- olivec_fill(dst, RED_COLOR);
- olivec_sprite_blend(dst, -width/2, -height/2, width, height, src);
- olivec_sprite_blend(dst, width - 1 + width/2, -height/2, -width, height, src);
- olivec_sprite_blend(dst, -width/2, height - 1 + height/2, width, -height, src);
- olivec_sprite_blend(dst, width - 1 + width/2, height - 1 + height/2, -width, -height, src);
- return dst;
- }
- Olivec_Canvas test_empty_rect(void)
- {
- size_t w = 8;
- size_t h = 8;
- Olivec_Canvas dst = canvas_alloc(w, h);
- olivec_fill(dst, BACKGROUND_COLOR);
- olivec_rect(dst, w/2, h/2, 0, 0, FOREGROUND_COLOR);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_empty_rect(void)
- {
- size_t w = 8;
- size_t h = 8;
- Olivec_Canvas dst = canvas_alloc(w, h);
- Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
- olivec_fill(dst, BACKGROUND_COLOR);
- olivec_sprite_blend(dst, 0, 0, 0, 0, src);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_null(void)
- {
- size_t w = 8;
- size_t h = 8;
- Olivec_Canvas dst = canvas_alloc(w, h);
- olivec_fill(dst, BACKGROUND_COLOR);
- olivec_sprite_blend(dst, 0, 0, w, h, OLIVEC_CANVAS_NULL);
- return dst;
- }
- Olivec_Canvas test_sprite_blend_vs_copy(void)
- {
- Olivec_Canvas tsodinCup = olivec_canvas(tsodinCup_pixels, tsodinCup_width, tsodinCup_height, tsodinCup_width);
- size_t w = tsodinCup.width;
- size_t h = tsodinCup.height*2;
- Olivec_Canvas dst = canvas_alloc(w, h);
- olivec_fill(dst, RED_COLOR);
- olivec_sprite_blend(dst, 0, 0, tsodinCup.width, tsodinCup.height, tsodinCup);
- olivec_sprite_copy(dst, 0, tsodinCup.height, tsodinCup.width, tsodinCup.height, tsodinCup);
- return dst;
- }
- Olivec_Canvas test_barycentric_overflow(void)
- {
- size_t w = 256;
- size_t h = 256;
- Olivec_Canvas dst = canvas_alloc(w, h);
- olivec_fill(dst, 0xFF181818);
- olivec_triangle3c(dst, w/4, h/4, w/2, 0, 0, h, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000);
- return dst;
- }
- Olivec_Canvas test_triangle_order_flip(void)
- {
- size_t w = 256;
- size_t h = 256;
- Olivec_Canvas dst = canvas_alloc(w, h);
- olivec_fill(dst, 0xFF181818);
- olivec_triangle3c(
- dst,
- w/4, h/4,
- 0, h,
- w, 0,
- 0xFF00FF00,
- 0xFFFF0000,
- 0xFF0000FF);
- return dst;
- }
- Olivec_Canvas test_bilinear_interpolation(void)
- {
- size_t factor = 2;
- Olivec_Canvas src = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
- Olivec_Canvas dst = canvas_alloc(src.width*factor*2, src.height*factor);
- olivec_fill(dst, RED_COLOR);
- olivec_sprite_copy(dst, 0, 0, src.width*factor, src.height*factor, src);
- olivec_sprite_copy_bilinear(dst, src.width*factor, 0, src.width*factor, src.height*factor, src);
- return dst;
- }
- Olivec_Canvas test_fill_ellipse(void)
- {
- size_t factor = 3;
- Olivec_Canvas dst = canvas_alloc(100*factor, 50*factor);
- olivec_fill(dst, BACKGROUND_COLOR);
- olivec_ellipse(dst, dst.width/2, dst.height/2, dst.width/3, dst.height/3, RED_COLOR);
- return dst;
- }
- Olivec_Canvas test_line_bug_offset(void)
- {
- size_t factor = 3;
- size_t width = 100*factor;
- size_t height = 50*factor;
- Olivec_Canvas dst = canvas_alloc(width, height);
- olivec_fill(dst, BACKGROUND_COLOR);
- int x1 = 50;
- int y1 = 100;
- int x2 = 0;
- int y2 = -100;
- olivec_line(dst, x1, y1, x2, y2, GREEN_COLOR);
- olivec_circle(dst, x1, y1, 5, RED_COLOR);
- return dst;
- }
- Test_Case test_cases[] = {
- DEFINE_TEST_CASE(fill_rect),
- DEFINE_TEST_CASE(fill_circle),
- DEFINE_TEST_CASE(draw_line),
- DEFINE_TEST_CASE(fill_triangle),
- DEFINE_TEST_CASE(alpha_blending),
- DEFINE_TEST_CASE(checker_example),
- DEFINE_TEST_CASE(circle_example),
- DEFINE_TEST_CASE(lines_example),
- DEFINE_TEST_CASE(hello_world_text_rendering),
- DEFINE_TEST_CASE(lines_circle),
- DEFINE_TEST_CASE(line_edge_cases),
- DEFINE_TEST_CASE(frame),
- DEFINE_TEST_CASE(sprite_blend),
- DEFINE_TEST_CASE(sprite_blend_out_of_bounds_cut),
- DEFINE_TEST_CASE(sprite_blend_flip),
- DEFINE_TEST_CASE(sprite_blend_flip_cut),
- DEFINE_TEST_CASE(sprite_blend_empty_rect),
- DEFINE_TEST_CASE(empty_rect),
- DEFINE_TEST_CASE(sprite_blend_null),
- DEFINE_TEST_CASE(sprite_blend_vs_copy),
- DEFINE_TEST_CASE(triangle_order_flip),
- DEFINE_TEST_CASE(barycentric_overflow),
- DEFINE_TEST_CASE(bilinear_interpolation),
- DEFINE_TEST_CASE(fill_ellipse),
- DEFINE_TEST_CASE(line_bug_offset),
- };
- #define TEST_CASES_COUNT (sizeof(test_cases)/sizeof(test_cases[0]))
- void list_available_tests(void)
- {
- fprintf(stderr, "Available tests:\n");
- for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
- fprintf(stderr, " %s\n", test_cases[i].id);
- }
- }
- Test_Case *find_test_case_by_id(const char *id)
- {
- for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
- if (strcmp(test_cases[i].id, id) == 0) {
- return &test_cases[i];
- }
- }
- return NULL;
- }
- typedef struct {
- int (*run)(const char *program_path, int argc, char **argv);
- const char *id;
- const char *description;
- } Subcmd;
- void usage(const char *program_path);
- int subcmd_run(const char *program_path, int argc, char **argv)
- {
- if (argc <= 0) {
- for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
- if (run_test_case(program_path, &test_cases[i]) == REPLAY_ERRORED) return(1);
- arena_reset(&default_arena);
- }
- } else {
- const char *test_case_id = shift(argv, argc);
- Test_Case *tc = find_test_case_by_id(test_case_id);
- if (tc == NULL) {
- list_available_tests();
- fprintf(stderr, "ERROR: could not find test case `%s`\n", test_case_id);
- return(1);
- }
- if (run_test_case(program_path, tc) == REPLAY_ERRORED) return(1);
- }
- return 0;
- }
- int subcmd_update(const char *program_path, int argc, char **argv)
- {
- UNUSED(program_path);
- if (argc <= 0) {
- for (size_t i = 0; i < TEST_CASES_COUNT; ++i) {
- if (!update_test_case(&test_cases[i])) return(1);
- arena_reset(&default_arena);
- }
- } else {
- const char *test_case_id = shift(argv, argc);
- Test_Case *tc = find_test_case_by_id(test_case_id);
- if (tc == NULL) {
- list_available_tests();
- fprintf(stderr, "ERROR: could not find test case `%s`\n", test_case_id);
- return(1);
- }
- if (!update_test_case(tc)) return(1);
- }
- return 0;
- }
- int subcmd_list(const char *program_path, int argc, char **argv)
- {
- UNUSED(program_path);
- UNUSED(argc);
- UNUSED(argv);
- list_available_tests();
- return 0;
- }
- int subcmd_help(const char *program_path, int argc, char **argv)
- {
- UNUSED(argc);
- UNUSED(argv);
- usage(program_path);
- return 0;
- }
- #define DEFINE_SUBCMD(name, desc) \
- { \
- .run = subcmd_##name, \
- .id = #name, \
- .description = desc, \
- }
- Subcmd subcmds[] = {
- DEFINE_SUBCMD(run, "Run the tests"),
- DEFINE_SUBCMD(update, "Update the tests"),
- DEFINE_SUBCMD(list, "List all available tests"),
- DEFINE_SUBCMD(help, "Print this help message"),
- };
- #define SUBCMDS_COUNT (sizeof(subcmds)/sizeof(subcmds[0]))
- Subcmd *find_subcmd_by_id(const char *id)
- {
- for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
- if (strcmp(subcmds[i].id, id) == 0) {
- return &subcmds[i];
- }
- }
- return NULL;
- }
- void usage(const char *program_path)
- {
- fprintf(stderr, "Usage: %s [Subcommand]\n", program_path);
- fprintf(stderr, "Subcommands:\n");
- int width = 0;
- for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
- int len = strlen(subcmds[i].id);
- if (width < len) width = len;
- }
- for (size_t i = 0; i < SUBCMDS_COUNT; ++i) {
- fprintf(stderr, " %-*s - %s\n", width, subcmds[i].id, subcmds[i].description);
- }
- }
- int main(int argc, char **argv)
- {
- int result = 0;
- {
- const char *program_path = shift(argv, argc);
- if (argc <= 0) {
- usage(program_path);
- fprintf(stderr, "ERROR: no subcommand is provided\n");
- return_defer(1);
- }
- const char *subcmd_id = shift(argv, argc);
- Subcmd *subcmd = find_subcmd_by_id(subcmd_id);
- if (subcmd != NULL) {
- return_defer(subcmd->run(program_path, argc, argv));
- } else {
- usage(program_path);
- fprintf(stderr, "ERROR: unknown subcommand `%s`\n", subcmd_id);
- return_defer(1);
- }
- }
- defer:
- arena_free(&default_arena);
- return result;
- }
|