Browse Source

Prefix VC api elements with vc_*

rexim 2 years ago
parent
commit
a9f2909586
17 changed files with 127 additions and 132 deletions
  1. 17 17
      build.sh
  2. 1 2
      demos/3d.c
  3. 6 7
      demos/cup3d.c
  4. 3 4
      demos/squish.c
  5. 1 2
      demos/triangle.c
  6. 12 12
      demos/triangle3d.c
  7. 11 12
      demos/triangle3dTex.c
  8. 1 2
      demos/triangleTex.c
  9. 74 73
      demos/vc.c
  10. 1 1
      js/vc.js
  11. BIN
      wasm/3d.wasm
  12. BIN
      wasm/cup3d.wasm
  13. BIN
      wasm/squish.wasm
  14. BIN
      wasm/triangle.wasm
  15. BIN
      wasm/triangle3d.wasm
  16. BIN
      wasm/triangle3dTex.wasm
  17. BIN
      wasm/triangleTex.wasm

+ 17 - 17
build.sh

@@ -6,18 +6,18 @@ COMMON_CFLAGS="-Wall -Wextra -pedantic -std=c99 -ggdb -I. -I./build/ -I./dev-dep
 
 
 build_wasm_demo() {
 build_wasm_demo() {
     NAME=$1
     NAME=$1
-    clang $COMMON_CFLAGS -O2 -fno-builtin --target=wasm32 --no-standard-libraries -Wl,--no-entry -Wl,--export=render -Wl,--export=__heap_base -Wl,--allow-undefined -o ./build/demos/$NAME.wasm -DPLATFORM=WASM_PLATFORM ./demos/$NAME.c
+    clang $COMMON_CFLAGS -O2 -fno-builtin --target=wasm32 --no-standard-libraries -Wl,--no-entry -Wl,--export=vc_render -Wl,--export=__heap_base -Wl,--allow-undefined -o ./build/demos/$NAME.wasm -DVC_PLATFORM=VC_WASM_PLATFORM ./demos/$NAME.c
     cp ./build/demos/$NAME.wasm ./wasm/
     cp ./build/demos/$NAME.wasm ./wasm/
 }
 }
 
 
 build_term_demo() {
 build_term_demo() {
     NAME=$1
     NAME=$1
-    clang $COMMON_CFLAGS -O2 -o ./build/demos/$NAME.term -DPLATFORM=TERM_PLATFORM -D_XOPEN_SOURCE=600 ./demos/$NAME.c -lm
+    clang $COMMON_CFLAGS -O2 -o ./build/demos/$NAME.term -DVC_PLATFORM=VC_TERM_PLATFORM -D_XOPEN_SOURCE=600 ./demos/$NAME.c -lm
 }
 }
 
 
 build_sdl_demo() {
 build_sdl_demo() {
     NAME=$1
     NAME=$1
-    clang $COMMON_CFLAGS -O2 -o ./build/demos/$NAME.sdl -DPLATFORM=SDL_PLATFORM ./demos/$NAME.c -lm -lSDL2
+    clang $COMMON_CFLAGS -O2 -o ./build/demos/$NAME.sdl -DVC_PLATFORM=VC_SDL_PLATFORM ./demos/$NAME.c -lm -lSDL2
 }
 }
 
 
 build_vc_demo() {
 build_vc_demo() {
@@ -29,34 +29,34 @@ build_vc_demo() {
 
 
 build_all_vc_demos() {
 build_all_vc_demos() {
     mkdir -p ./build/demos
     mkdir -p ./build/demos
-    build_vc_demo triangle &
-    build_vc_demo 3d &
-    build_vc_demo squish &
-    build_vc_demo triangle3d &
-    build_vc_demo triangleTex &
-    build_vc_demo triangle3dTex &
-    build_vc_demo cup3d &
+    build_vc_demo triangle
+    build_vc_demo 3d
+    build_vc_demo squish
+    build_vc_demo triangle3d
+    build_vc_demo triangleTex
+    build_vc_demo triangle3dTex
+    build_vc_demo cup3d
     wait # TODO: the whole script must fail if one of the jobs fails
     wait # TODO: the whole script must fail if one of the jobs fails
 }
 }
 
 
 build_tools() {
 build_tools() {
     mkdir -p ./build/tools/
     mkdir -p ./build/tools/
-    clang $COMMON_CFLAGS -o ./build/tools/png2c -Ithirdparty ./tools/png2c.c -lm &
-    clang $COMMON_CFLAGS -o ./build/tools/obj2c -Ithirdparty ./tools/obj2c.c -lm &
-    clang $COMMON_CFLAGS -O2 -o ./build/tools/viewobj ./tools/viewobj.c &
+    clang $COMMON_CFLAGS -o ./build/tools/png2c -Ithirdparty ./tools/png2c.c -lm
+    clang $COMMON_CFLAGS -o ./build/tools/obj2c -Ithirdparty ./tools/obj2c.c -lm
+    clang $COMMON_CFLAGS -O2 -o ./build/tools/viewobj ./tools/viewobj.c
     wait # TODO: the whole script must fail if one of the jobs fails
     wait # TODO: the whole script must fail if one of the jobs fails
 }
 }
 
 
 build_assets() {
 build_assets() {
     mkdir -p ./build/assets/
     mkdir -p ./build/assets/
-    ./build/tools/png2c -n tsodinPog -o ./build/assets/tsodinPog.c ./assets/tsodinPog.png &
-    ./build/tools/png2c -n Sadge -o ./build/assets/Sadge.c ./assets/Sadge.png &
-    ./build/tools/obj2c ./assets/tsodinCupLowPoly.obj ./build/assets/tsodinCupLowPoly.c &
+    ./build/tools/png2c -n tsodinPog -o ./build/assets/tsodinPog.c ./assets/tsodinPog.png
+    ./build/tools/png2c -n Sadge -o ./build/assets/Sadge.c ./assets/Sadge.png
+    ./build/tools/obj2c ./assets/tsodinCupLowPoly.obj ./build/assets/tsodinCupLowPoly.c
     wait # TODO: the whole script must fail if one of the jobs fails
     wait # TODO: the whole script must fail if one of the jobs fails
 }
 }
 
 
 build_tests() {
 build_tests() {
-    clang $COMMON_CFLAGS -fsanitize=memory -o ./build/test -Ithirdparty ./test.c -lm &
+    clang $COMMON_CFLAGS -fsanitize=memory -o ./build/test -Ithirdparty ./test.c -lm
 }
 }
 
 
 build_tools
 build_tools

+ 1 - 2
demos/3d.c

@@ -1,4 +1,3 @@
-#define SCALE_DOWN_FACTOR 10
 #include "vc.c"
 #include "vc.c"
 
 
 float sqrtf(float x);
 float sqrtf(float x);
@@ -31,7 +30,7 @@ uint32_t circle_colors[] = {
 static uint32_t pixels[WIDTH*HEIGHT];
 static uint32_t pixels[WIDTH*HEIGHT];
 static float angle = 0;
 static float angle = 0;
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     angle += 0.25*PI*dt;
     angle += 0.25*PI*dt;
 
 

+ 6 - 7
demos/cup3d.c

@@ -1,4 +1,3 @@
-#define SCALE_DOWN_FACTOR 10
 #include "vc.c"
 #include "vc.c"
 #include "./assets/tsodinCupLowPoly.c"
 #include "./assets/tsodinCupLowPoly.c"
 
 
@@ -21,7 +20,7 @@ typedef struct {
     float x, y;
     float x, y;
 } Vector2;
 } Vector2;
 
 
-Vector2 make_vector2(float x, float y)
+static Vector2 make_vector2(float x, float y)
 {
 {
     Vector2 v2;
     Vector2 v2;
     v2.x = x;
     v2.x = x;
@@ -33,7 +32,7 @@ typedef struct {
     float x, y, z;
     float x, y, z;
 } Vector3;
 } Vector3;
 
 
-Vector3 make_vector3(float x, float y, float z)
+static Vector3 make_vector3(float x, float y, float z)
 {
 {
     Vector3 v3;
     Vector3 v3;
     v3.x = x;
     v3.x = x;
@@ -42,24 +41,24 @@ Vector3 make_vector3(float x, float y, float z)
     return v3;
     return v3;
 }
 }
 
 
-Vector2 project_3d_2d(Vector3 v3)
+static Vector2 project_3d_2d(Vector3 v3)
 {
 {
     return make_vector2(v3.x/v3.z, v3.y/v3.z);
     return make_vector2(v3.x/v3.z, v3.y/v3.z);
 }
 }
 
 
-Vector2 project_2d_scr(Vector2 v2)
+static Vector2 project_2d_scr(Vector2 v2)
 {
 {
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
 }
 }
 
 
-Vector3 rotate_y(Vector3 p, float delta_angle)
+static Vector3 rotate_y(Vector3 p, float delta_angle)
 {
 {
     float angle = atan2f(p.z - 1.5, p.x) + delta_angle;
     float angle = atan2f(p.z - 1.5, p.x) + delta_angle;
     float mag = sqrtf(p.x*p.x + (p.z - 1.5)*(p.z - 1.5));
     float mag = sqrtf(p.x*p.x + (p.z - 1.5)*(p.z - 1.5));
     return make_vector3(cosf(angle)*mag, p.y, sinf(angle)*mag + 1.5);
     return make_vector3(cosf(angle)*mag, p.y, sinf(angle)*mag + 1.5);
 }
 }
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     angle += 0.25*PI*dt;
     angle += 0.25*PI*dt;
 
 

+ 3 - 4
demos/squish.c

@@ -1,4 +1,3 @@
-#define SCALE_DOWN_FACTOR 10
 #include "vc.c"
 #include "vc.c"
 #include "./assets/tsodinPog.c"
 #include "./assets/tsodinPog.c"
 
 
@@ -7,12 +6,12 @@
 
 
 float sinf(float);
 float sinf(float);
 
 
-uint32_t dst[WIDTH*HEIGHT];
-float global_time = 0;
+static uint32_t dst[WIDTH*HEIGHT];
+static float global_time = 0;
 
 
 #define SRC_SCALE 3
 #define SRC_SCALE 3
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     global_time += dt;
     global_time += dt;
 
 

+ 1 - 2
demos/triangle.c

@@ -1,4 +1,3 @@
-#define SCALE_DOWN_FACTOR 20
 #include "vc.c"
 #include "vc.c"
 
 
 #define WIDTH 960
 #define WIDTH 960
@@ -31,7 +30,7 @@ static inline void rotate_point(float *x, float *y)
     *y = sinf(dir)*mag + HEIGHT/2;
     *y = sinf(dir)*mag + HEIGHT/2;
 }
 }
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
 
 

+ 12 - 12
demos/triangle3d.c

@@ -1,12 +1,12 @@
-#define SCALE_DOWN_FACTOR 20
-#include "../demos/vc.c"
+#include "vc.c"
 
 
 #define WIDTH 800
 #define WIDTH 800
 #define HEIGHT 600
 #define HEIGHT 600
-uint32_t pixels1[WIDTH*HEIGHT];
-float zbuffer1[WIDTH*HEIGHT];
-uint32_t pixels2[WIDTH*HEIGHT];
-float zbuffer2[WIDTH*HEIGHT];
+
+static uint32_t pixels1[WIDTH*HEIGHT];
+static float zbuffer1[WIDTH*HEIGHT];
+static uint32_t pixels2[WIDTH*HEIGHT];
+static float zbuffer2[WIDTH*HEIGHT];
 
 
 typedef struct {
 typedef struct {
     float x, y;
     float x, y;
@@ -24,7 +24,7 @@ typedef struct {
     float x, y, z;
     float x, y, z;
 } Vector3;
 } Vector3;
 
 
-Vector3 make_vector3(float x, float y, float z)
+static Vector3 make_vector3(float x, float y, float z)
 {
 {
     Vector3 v3;
     Vector3 v3;
     v3.x = x;
     v3.x = x;
@@ -33,24 +33,24 @@ Vector3 make_vector3(float x, float y, float z)
     return v3;
     return v3;
 }
 }
 
 
-Vector2 project_3d_2d(Vector3 v3)
+static Vector2 project_3d_2d(Vector3 v3)
 {
 {
     return make_vector2(v3.x / v3.z, v3.y / v3.z);
     return make_vector2(v3.x / v3.z, v3.y / v3.z);
 }
 }
 
 
-Vector2 project_2d_scr(Vector2 v2)
+static Vector2 project_2d_scr(Vector2 v2)
 {
 {
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
 }
 }
 
 
-float global_time = 1.0;
+static float global_time = 1.0;
 
 
 #define PI 3.14159265359
 #define PI 3.14159265359
 
 
 float sinf(float);
 float sinf(float);
 float cosf(float);
 float cosf(float);
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     global_time += dt;
     global_time += dt;
 
 
@@ -109,6 +109,6 @@ Olivec_Canvas render(float dt)
             }
             }
         }
         }
     }
     }
-    
+
     return oc1;
     return oc1;
 }
 }

+ 11 - 12
demos/triangle3dTex.c

@@ -1,19 +1,18 @@
-#define SCALE_DOWN_FACTOR 20
-#include "../demos/vc.c"
+#include "vc.c"
 #include "./assets/tsodinPog.c"
 #include "./assets/tsodinPog.c"
 
 
 #define WIDTH 800
 #define WIDTH 800
 #define HEIGHT 600
 #define HEIGHT 600
-uint32_t pixels1[WIDTH*HEIGHT];
-float zbuffer1[WIDTH*HEIGHT];
-uint32_t pixels2[WIDTH*HEIGHT];
-float zbuffer2[WIDTH*HEIGHT];
+static uint32_t pixels1[WIDTH*HEIGHT];
+static float zbuffer1[WIDTH*HEIGHT];
+static uint32_t pixels2[WIDTH*HEIGHT];
+static float zbuffer2[WIDTH*HEIGHT];
 
 
 typedef struct {
 typedef struct {
     float x, y;
     float x, y;
 } Vector2;
 } Vector2;
 
 
-Vector2 make_vector2(float x, float y)
+static Vector2 make_vector2(float x, float y)
 {
 {
     Vector2 v2;
     Vector2 v2;
     v2.x = x;
     v2.x = x;
@@ -25,7 +24,7 @@ typedef struct {
     float x, y, z;
     float x, y, z;
 } Vector3;
 } Vector3;
 
 
-Vector3 make_vector3(float x, float y, float z)
+static Vector3 make_vector3(float x, float y, float z)
 {
 {
     Vector3 v3;
     Vector3 v3;
     v3.x = x;
     v3.x = x;
@@ -34,24 +33,24 @@ Vector3 make_vector3(float x, float y, float z)
     return v3;
     return v3;
 }
 }
 
 
-Vector2 project_3d_2d(Vector3 v3)
+static Vector2 project_3d_2d(Vector3 v3)
 {
 {
     return make_vector2(v3.x / v3.z, v3.y / v3.z);
     return make_vector2(v3.x / v3.z, v3.y / v3.z);
 }
 }
 
 
-Vector2 project_2d_scr(Vector2 v2)
+static Vector2 project_2d_scr(Vector2 v2)
 {
 {
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
     return make_vector2((v2.x + 1)/2*WIDTH, (1 - (v2.y + 1)/2)*HEIGHT);
 }
 }
 
 
-float global_time = 1.0;
+static float global_time = 1.0;
 
 
 #define PI 3.14159265359
 #define PI 3.14159265359
 
 
 float sinf(float);
 float sinf(float);
 float cosf(float);
 float cosf(float);
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     global_time += dt;
     global_time += dt;
 
 

+ 1 - 2
demos/triangleTex.c

@@ -1,4 +1,3 @@
-#define SCALE_DOWN_FACTOR 20
 #include "vc.c"
 #include "vc.c"
 #include "assets/tsodinPog.c"
 #include "assets/tsodinPog.c"
 
 
@@ -16,7 +15,7 @@ float cosf(float x);
 
 
 #define PI 3.14159265359
 #define PI 3.14159265359
 
 
-Olivec_Canvas render(float dt)
+Olivec_Canvas vc_render(float dt)
 {
 {
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
     Olivec_Canvas tsodinPog = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);
     Olivec_Canvas tsodinPog = olivec_canvas(tsodinPog_pixels, tsodinPog_width, tsodinPog_height, tsodinPog_width);

+ 74 - 73
demos/vc.c

@@ -12,7 +12,7 @@
 // #define HEIGHT 600
 // #define HEIGHT 600
 // static uint32_t pixels[WIDTH*HEIGHT];
 // static uint32_t pixels[WIDTH*HEIGHT];
 //
 //
-// Olivec_Canvas render(float dt)
+// static Olivec_Canvas vc_render(float dt)
 // {
 // {
 //     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
 //     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
 //     // ...
 //     // ...
@@ -24,44 +24,42 @@
 //
 //
 // # Build
 // # Build
 // ```console
 // ```console
-// $ clang -o demo.sdl -DPLATFORM=SDL_PLATFORM demo.c -lSDL2
-// $ clang -o demo.term -DPLATFORM=TERM_PLATFORM demo.c
-// $ clang -fno-builtin --target=wasm32 --no-standard-libraries -Wl,--no-entry -Wl,--export=render -Wl,--allow-undefined -o demo.wasm -DPLATFORM=WASM_PLATFORM demo.c
+// $ clang -o demo.sdl -DVC_PLATFORM=VC_SDL_PLATFORM demo.c -lSDL2
+// $ clang -o demo.term -DVC_PLATFORM=VC_TERM_PLATFORM demo.c
+// $ clang -fno-builtin --target=wasm32 --no-standard-libraries -Wl,--no-entry -Wl,--export=render -Wl,--allow-undefined -o demo.wasm -DVC_PLATFORM=VC_WASM_PLATFORM demo.c
 // ```
 // ```
 
 
 #define OLIVEC_IMPLEMENTATION
 #define OLIVEC_IMPLEMENTATION
 #include <olive.c>
 #include <olive.c>
 
 
-// TODO: prefix VC api elements with vc_*
-// Like vc_render, VC_WASM_PLATFORM, VC_SCALE_DOWN_FACTOR, etc.
+Olivec_Canvas vc_render(float dt);
 
 
-Olivec_Canvas render(float dt);
-
-#define WASM_PLATFORM 0
-#define SDL_PLATFORM 1
-#define TERM_PLATFORM 2
-
-#ifndef PLATFORM
-#error "Please define PLATFORM macro"
+#ifndef VC_PLATFORM
+#error "Please define VC_PLATFORM macro"
 #endif
 #endif
 
 
-#if PLATFORM == SDL_PLATFORM
+// Possible values of VC_PLATFORM
+#define VC_WASM_PLATFORM 0
+#define VC_SDL_PLATFORM 1
+#define VC_TERM_PLATFORM 2
+
+#if VC_PLATFORM == VC_SDL_PLATFORM
 #include <stdio.h>
 #include <stdio.h>
 #include <SDL2/SDL.h>
 #include <SDL2/SDL.h>
 
 
 #define return_defer(value) do { result = (value); goto defer; } while (0)
 #define return_defer(value) do { result = (value); goto defer; } while (0)
 
 
-SDL_Texture *texture = NULL;
-size_t actual_width = 0;
-size_t actual_height = 0;
+static SDL_Texture *vc_sdl_texture = NULL;
+static size_t vc_sdl_actual_width = 0;
+static size_t vc_sdl_actual_height = 0;
 
 
-bool resize_texture(SDL_Renderer *renderer, size_t new_width, size_t new_height)
+static bool vc_sdl_resize_texture(SDL_Renderer *renderer, size_t new_width, size_t new_height)
 {
 {
-    if (texture != NULL) SDL_DestroyTexture(texture);
-    actual_width = new_width;
-    actual_height = new_height;
-    texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, actual_width, actual_height);
-    if (texture == NULL) return false;
+    if (vc_sdl_texture != NULL) SDL_DestroyTexture(vc_sdl_texture);
+    vc_sdl_actual_width = new_width;
+    vc_sdl_actual_height = new_height;
+    vc_sdl_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, vc_sdl_actual_width, vc_sdl_actual_height);
+    if (vc_sdl_texture == NULL) return false;
     return true;
     return true;
 }
 }
 
 
@@ -93,26 +91,26 @@ int main(void)
             while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) return_defer(0);
             while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) return_defer(0);
 
 
             // Render the texture
             // Render the texture
-            Olivec_Canvas oc_src = render(dt);
-            if (oc_src.width != actual_width || oc_src.height != actual_height) {
-                if (!resize_texture(renderer, oc_src.width, oc_src.height)) return_defer(1);
-                SDL_SetWindowSize(window, actual_width, actual_height);
+            Olivec_Canvas oc_src = vc_render(dt);
+            if (oc_src.width != vc_sdl_actual_width || oc_src.height != vc_sdl_actual_height) {
+                if (!vc_sdl_resize_texture(renderer, oc_src.width, oc_src.height)) return_defer(1);
+                SDL_SetWindowSize(window, vc_sdl_actual_width, vc_sdl_actual_height);
             }
             }
             void *pixels_dst;
             void *pixels_dst;
             int pitch;
             int pitch;
-            SDL_Rect window_rect = {0, 0, actual_width, actual_height};
-            if (SDL_LockTexture(texture, &window_rect, &pixels_dst, &pitch) < 0) return_defer(1);
-            for (size_t y = 0; y < actual_height; ++y) {
+            SDL_Rect window_rect = {0, 0, vc_sdl_actual_width, vc_sdl_actual_height};
+            if (SDL_LockTexture(vc_sdl_texture, &window_rect, &pixels_dst, &pitch) < 0) return_defer(1);
+            for (size_t y = 0; y < vc_sdl_actual_height; ++y) {
                 // TODO: it would be cool if Olivec_Canvas supported pitch in bytes instead of pixels
                 // TODO: it would be cool if Olivec_Canvas supported pitch in bytes instead of pixels
                 // It would be more flexible and we could draw on the locked texture memory directly
                 // It would be more flexible and we could draw on the locked texture memory directly
-                memcpy((char*)pixels_dst + y*pitch, oc_src.pixels + y*actual_width, actual_width*sizeof(uint32_t));
+                memcpy((char*)pixels_dst + y*pitch, oc_src.pixels + y*vc_sdl_actual_width, vc_sdl_actual_width*sizeof(uint32_t));
             }
             }
-            SDL_UnlockTexture(texture);
+            SDL_UnlockTexture(vc_sdl_texture);
 
 
             // Display the texture
             // Display the texture
             if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0) < 0) return_defer(1);
             if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0) < 0) return_defer(1);
             if (SDL_RenderClear(renderer) < 0) return_defer(1);
             if (SDL_RenderClear(renderer) < 0) return_defer(1);
-            if (SDL_RenderCopy(renderer, texture, &window_rect, &window_rect) < 0) return_defer(1);
+            if (SDL_RenderCopy(renderer, vc_sdl_texture, &window_rect, &window_rect) < 0) return_defer(1);
             SDL_RenderPresent(renderer);
             SDL_RenderPresent(renderer);
         }
         }
     }
     }
@@ -125,13 +123,13 @@ defer:
     default:
     default:
         fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
         fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
     }
     }
-    if (texture) SDL_DestroyTexture(texture);
+    if (vc_sdl_texture) SDL_DestroyTexture(vc_sdl_texture);
     if (renderer) SDL_DestroyRenderer(renderer);
     if (renderer) SDL_DestroyRenderer(renderer);
     if (window) SDL_DestroyWindow(window);
     if (window) SDL_DestroyWindow(window);
     SDL_Quit();
     SDL_Quit();
     return result;
     return result;
 }
 }
-#elif PLATFORM == TERM_PLATFORM
+#elif VC_PLATFORM == VC_TERM_PLATFORM
 
 
 #include <assert.h>
 #include <assert.h>
 #include <stdio.h>
 #include <stdio.h>
@@ -141,14 +139,14 @@ defer:
 #include <time.h>
 #include <time.h>
 #include <unistd.h>
 #include <unistd.h>
 
 
-static size_t actual_width = 0;
-static size_t actual_height = 0;
-static size_t scaled_down_width = 0;
-static size_t scaled_down_height = 0;
-static char *char_canvas = 0;
+static size_t vc_term_actual_width = 0;
+static size_t vc_term_actual_height = 0;
+static size_t vc_term_scaled_down_width = 0;
+static size_t vc_term_scaled_down_height = 0;
+static char *vc_term_char_canvas = 0;
 
 
 // TODO: use ANSI terminal colors for color_to_char
 // TODO: use ANSI terminal colors for color_to_char
-char color_to_char(uint32_t pixel)
+static char vc_term_color_to_char(uint32_t pixel)
 {
 {
     size_t r = OLIVEC_RED(pixel);
     size_t r = OLIVEC_RED(pixel);
     size_t g = OLIVEC_GREEN(pixel);
     size_t g = OLIVEC_GREEN(pixel);
@@ -164,7 +162,7 @@ char color_to_char(uint32_t pixel)
     return table[bright*n/256];
     return table[bright*n/256];
 }
 }
 
 
-uint32_t compress_pixels_chunk(Olivec_Canvas oc)
+static uint32_t vc_term_compress_pixels_chunk(Olivec_Canvas oc)
 {
 {
     size_t r = 0;
     size_t r = 0;
     size_t g = 0;
     size_t g = 0;
@@ -188,32 +186,36 @@ uint32_t compress_pixels_chunk(Olivec_Canvas oc)
     return OLIVEC_RGBA(r, g, b, a);
     return OLIVEC_RGBA(r, g, b, a);
 }
 }
 
 
-void resize_char_canvas(size_t new_width, size_t new_height)
+#ifndef VC_TERM_SCALE_DOWN_FACTOR
+#define VC_TERM_SCALE_DOWN_FACTOR 20
+#endif // VC_TERM_SCALE_DOWN_FACTOR
+
+static void vc_term_resize_char_canvas(size_t new_width, size_t new_height)
 {
 {
     // TODO: can we just do something so the divisibility is not important?
     // TODO: can we just do something so the divisibility is not important?
     // Like round the stuff or something?
     // Like round the stuff or something?
-    assert(new_width%SCALE_DOWN_FACTOR == 0 && "Width must be divisible by SCALE_DOWN_FACTOR");
-    assert(new_height%SCALE_DOWN_FACTOR == 0 && "Height must be divisible by SCALE_DOWN_FACTOR");
-    actual_width = new_width;
-    actual_height = new_height;
-    scaled_down_width  = actual_width/SCALE_DOWN_FACTOR;
-    scaled_down_height = actual_height/SCALE_DOWN_FACTOR;
-    free(char_canvas);
-    char_canvas = malloc(sizeof(*char_canvas)*scaled_down_width*scaled_down_height);
-    assert(char_canvas != NULL && "Just buy more RAM");
+    assert(new_width%VC_TERM_SCALE_DOWN_FACTOR == 0 && "Width must be divisible by VC_TERM_SCALE_DOWN_FACTOR");
+    assert(new_height%VC_TERM_SCALE_DOWN_FACTOR == 0 && "Height must be divisible by VC_TERM_SCALE_DOWN_FACTOR");
+    vc_term_actual_width = new_width;
+    vc_term_actual_height = new_height;
+    vc_term_scaled_down_width  = vc_term_actual_width/VC_TERM_SCALE_DOWN_FACTOR;
+    vc_term_scaled_down_height = vc_term_actual_height/VC_TERM_SCALE_DOWN_FACTOR;
+    free(vc_term_char_canvas);
+    vc_term_char_canvas = malloc(sizeof(*vc_term_char_canvas)*vc_term_scaled_down_width*vc_term_scaled_down_height);
+    assert(vc_term_char_canvas != NULL && "Just buy more RAM");
 }
 }
 
 
-void compress_pixels(Olivec_Canvas oc)
+static void vc_term_compress_pixels(Olivec_Canvas oc)
 {
 {
-    if (actual_width != oc.width || actual_height != oc.height) {
-        resize_char_canvas(oc.width, oc.height);
+    if (vc_term_actual_width != oc.width || vc_term_actual_height != oc.height) {
+        vc_term_resize_char_canvas(oc.width, oc.height);
     }
     }
 
 
-    for (size_t y = 0; y < scaled_down_height; ++y) {
-        for (size_t x = 0; x < scaled_down_width; ++x) {
-            Olivec_Canvas soc = olivec_subcanvas(oc, x*SCALE_DOWN_FACTOR, y*SCALE_DOWN_FACTOR, SCALE_DOWN_FACTOR,
-SCALE_DOWN_FACTOR);
-            char_canvas[y*scaled_down_width + x] = color_to_char(compress_pixels_chunk(soc));
+    for (size_t y = 0; y < vc_term_scaled_down_height; ++y) {
+        for (size_t x = 0; x < vc_term_scaled_down_width; ++x) {
+            Olivec_Canvas soc = olivec_subcanvas(oc, x*VC_TERM_SCALE_DOWN_FACTOR, y*VC_TERM_SCALE_DOWN_FACTOR, VC_TERM_SCALE_DOWN_FACTOR,
+VC_TERM_SCALE_DOWN_FACTOR);
+            vc_term_char_canvas[y*vc_term_scaled_down_width + x] = vc_term_color_to_char(vc_term_compress_pixels_chunk(soc));
         }
         }
     }
     }
 }
 }
@@ -221,27 +223,26 @@ SCALE_DOWN_FACTOR);
 int main(void)
 int main(void)
 {
 {
     for (;;) {
     for (;;) {
-        compress_pixels(render(1.f/60.f));
-        for (size_t y = 0; y < scaled_down_height; ++y) {
-            for (size_t x = 0; x < scaled_down_width; ++x) {
+        vc_term_compress_pixels(vc_render(1.f/60.f));
+        for (size_t y = 0; y < vc_term_scaled_down_height; ++y) {
+            for (size_t x = 0; x < vc_term_scaled_down_width; ++x) {
                 // TODO: different halfs of the double pixels
                 // TODO: different halfs of the double pixels
                 // We can do stuff like putc('<', stdout); putc('>', stdout);
                 // We can do stuff like putc('<', stdout); putc('>', stdout);
-                putc(char_canvas[y*scaled_down_width + x], stdout);
-                putc(char_canvas[y*scaled_down_width + x], stdout);
+                putc(vc_term_char_canvas[y*vc_term_scaled_down_width + x], stdout);
+                putc(vc_term_char_canvas[y*vc_term_scaled_down_width + x], stdout);
                 // TODO: explore the idea of figuring out aspect ratio of the character using escape ANSI codes of the terminal and rendering the image accordingly
                 // TODO: explore the idea of figuring out aspect ratio of the character using escape ANSI codes of the terminal and rendering the image accordingly
             }
             }
             putc('\n', stdout);
             putc('\n', stdout);
         }
         }
 
 
         usleep(1000*1000/60);
         usleep(1000*1000/60);
-        printf("\033[%zuA", scaled_down_height);
-        printf("\033[%zuD", scaled_down_width);
+        printf("\033[%zuA", vc_term_scaled_down_height);
+        printf("\033[%zuD", vc_term_scaled_down_width);
     }
     }
     return 0;
     return 0;
 }
 }
-
-#elif PLATFORM == WASM_PLATFORM
-// Do nothing
+#elif VC_PLATFORM == VC_WASM_PLATFORM
+// Do nothing because all the work is done in ../js/vc.js
 #else
 #else
-#error "Unknown platform"
-#endif // SDL_PLATFORM
+#error "Unknown VC platform"
+#endif // VC_SDL_PLATFORM

+ 1 - 1
js/vc.js

@@ -64,7 +64,7 @@ async function startDemo(elementId, wasmPath) {
         prev = timestamp;
         prev = timestamp;
 
 
         const buffer = w.instance.exports.memory.buffer;
         const buffer = w.instance.exports.memory.buffer;
-        w.instance.exports.render(heap_base, dt*0.001);
+        w.instance.exports.vc_render(heap_base, dt*0.001);
         const canvas = readCanvasFromMemory(buffer, heap_base);
         const canvas = readCanvasFromMemory(buffer, heap_base);
         if (canvas.width != canvas.stride) {
         if (canvas.width != canvas.stride) {
             // TODO: maybe we can preallocate a Uint8ClampedArray on JavaScript side and just copy the canvas data there to bring width and stride to the same value?
             // TODO: maybe we can preallocate a Uint8ClampedArray on JavaScript side and just copy the canvas data there to bring width and stride to the same value?

BIN
wasm/3d.wasm


BIN
wasm/cup3d.wasm


BIN
wasm/squish.wasm


BIN
wasm/triangle.wasm


BIN
wasm/triangle3d.wasm


BIN
wasm/triangle3dTex.wasm


BIN
wasm/triangleTex.wasm