Browse Source

Merge gallery into tests

rexim 3 years ago
parent
commit
8a5bb54a80

+ 2 - 4
build.sh

@@ -13,10 +13,8 @@ build_vc_example() {
 
 mkdir -p ./build/
 
-clang -Wall -Wextra -ggdb -o ./build/test -Ithirdparty test.c -lm &
-clang -Wall -Wextra -ggdb -o ./build/gallery -Ithirdparty -I. examples/gallery.c &
-clang -Wall -Wextra -ggdb -o ./build/png2c -Ithirdparty png2c.c -lm &
-wait
+clang -Wall -Wextra -ggdb -o ./build/test -Ithirdparty test.c -lm
+clang -Wall -Wextra -ggdb -o ./build/png2c -Ithirdparty png2c.c -lm
 
 mkdir -p ./build/assets/
 ./build/png2c ./assets/tsodinPog.png > ./build/assets/tsodinPog.c

+ 0 - 118
examples/gallery.c

@@ -1,118 +0,0 @@
-// This example generates the gallery that is displayed in the README of the project
-#include <stdio.h>
-#include <stdint.h>
-#include <stdbool.h>
-#include <errno.h>
-#include <string.h>
-
-#define STB_IMAGE_WRITE_IMPLEMENTATION
-#include "./stb_image_write.h"
-
-#define OLIVEC_IMPLEMENTATION
-#include "olive.c"
-
-#define WIDTH 800
-#define HEIGHT 600
-
-#define COLS (8*2)
-#define ROWS (6*2)
-#define CELL_WIDTH  (WIDTH/COLS)
-#define CELL_HEIGHT (HEIGHT/ROWS)
-
-#define BACKGROUND_COLOR 0xFF202020
-#define FOREGROUND_COLOR 0xFF2020FF
-
-#define IMGS_DIR_PATH "./imgs"
-
-static uint32_t pixels[WIDTH*HEIGHT];
-
-bool checker_example(void)
-{
-    Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
-
-    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);
-        }
-    }
-
-    const char *file_path = IMGS_DIR_PATH"/checker.png";
-    printf("Generated %s\n", file_path);
-    if (!stbi_write_png(file_path, WIDTH, HEIGHT, 4, pixels, WIDTH*sizeof(uint32_t))) {
-        fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path, strerror(errno));
-        return false;
-    }
-    return true;
-}
-
-float lerpf(float a, float b, float t)
-{
-    return a + (b - a)*t;
-}
-
-bool circle_example(void)
-{
-    Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
-    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;
-
-            size_t 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) lerpf(radius/8, radius/2, t),
-                          FOREGROUND_COLOR);
-        }
-    }
-
-    const char *file_path = IMGS_DIR_PATH"/circle.png";
-    printf("Generated %s\n", file_path);
-    if (!stbi_write_png(file_path, WIDTH, HEIGHT, 4, pixels, WIDTH*sizeof(uint32_t))) {
-        fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path, strerror(errno));
-        return false;
-    }
-    return true;
-}
-
-bool lines_example(void)
-{
-    Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
-
-    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);
-
-    const char *file_path = IMGS_DIR_PATH"/lines.png";
-    printf("Generated %s\n", file_path);
-    if (!stbi_write_png(file_path, WIDTH, HEIGHT, 4, pixels, WIDTH*sizeof(uint32_t))) {
-        fprintf(stderr, "ERROR: could not save file %s: %s\n", file_path, strerror(errno));
-        return false;
-    }
-    return true;
-}
-
-int main(void)
-{
-    if (!checker_example()) return -1;
-    if (!circle_example()) return -1;
-    if (!lines_example()) return -1;
-    return 0;
-}

BIN
imgs/circle.png


+ 81 - 0
test.c

@@ -51,6 +51,7 @@ static void *context_realloc(void *oldp, size_t oldsz, size_t newsz)
 #include "olive.c"
 
 #define BACKGROUND_COLOR 0xFF202020
+#define FOREGROUND_COLOR 0xFF2020FF
 #define RED_COLOR 0xFF2020AA
 #define GREEN_COLOR 0xFF20AA20
 #define BLUE_COLOR 0xFFAA2020
@@ -262,12 +263,92 @@ Olivec_Canvas test_alpha_blending(void)
     return oc;
 }
 
+Olivec_Canvas test_checker_example(void)
+{
+    int width = 800;
+    int height = 600;
+    int cols = (8*2);
+    int rows = (6*2);
+    int cell_width = (width/cols);
+    int cell_height = (height/rows);
+    uint32_t *pixels = context_alloc(width*height*sizeof(uint32_t));
+    Olivec_Canvas oc = olivec_canvas(pixels, width, height, width);
+
+    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;
+    int height = 600;
+    int cols = (8*2);
+    int rows = (6*2);
+    int cell_width = (width/cols);
+    int cell_height = (height/rows);
+    uint32_t *pixels = context_alloc(width*height*sizeof(uint32_t));
+    Olivec_Canvas oc = olivec_canvas(pixels, width, height, width);
+    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_example(void)
+{
+    int width = 800;
+    int height = 600;
+    uint32_t *pixels = context_alloc(width*height*sizeof(uint32_t));
+    Olivec_Canvas oc = olivec_canvas(pixels, width, height, width);
+
+    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;
+}
+
 Test_Case test_cases[] = {
     DEFINE_TEST_CASE(test_fill_rect),
     DEFINE_TEST_CASE(test_fill_circle),
     DEFINE_TEST_CASE(test_draw_line),
     DEFINE_TEST_CASE(test_fill_triangle),
     DEFINE_TEST_CASE(test_alpha_blending),
+    DEFINE_TEST_CASE(test_checker_example),
+    DEFINE_TEST_CASE(test_circle_example),
+    DEFINE_TEST_CASE(test_lines_example),
 };
 #define TEST_CASES_COUNT (sizeof(test_cases)/sizeof(test_cases[0]))
 

+ 0 - 0
imgs/checker.png → test/test_checker_example_expected.png


BIN
test/test_circle_example_expected.png


+ 0 - 0
imgs/lines.png → test/test_lines_example_expected.png