Browse Source

Unhardcode demo resolution for TERM_PLATFORM

rexim 3 years ago
parent
commit
87080ea53b
4 changed files with 47 additions and 30 deletions
  1. 2 2
      demos/3d.c
  2. 4 4
      demos/squish.c
  3. 2 2
      demos/triangle.c
  4. 39 22
      demos/vc.c

+ 2 - 2
demos/3d.c

@@ -38,7 +38,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;
 
 
-uint32_t *render(float dt)
+Olivec_Canvas render(float dt)
 {
 {
     angle += 0.25*PI*dt;
     angle += 0.25*PI*dt;
 
 
@@ -82,7 +82,7 @@ uint32_t *render(float dt)
     size_t size = 8;
     size_t size = 8;
     olivec_text(oc, "aboba", ABOBA_PADDING, HEIGHT - ABOBA_PADDING - default_font.height*size, default_font, size, 0xFFFFFFFF);
     olivec_text(oc, "aboba", ABOBA_PADDING, HEIGHT - ABOBA_PADDING - default_font.height*size, default_font, size, 0xFFFFFFFF);
 
 
-    return pixels;
+    return oc;
 }
 }
 
 
 #include "vc.c"
 #include "vc.c"

+ 4 - 4
demos/squish.c

@@ -14,16 +14,16 @@ float global_time = 0;
 
 
 #define SRC_SCALE 3
 #define SRC_SCALE 3
 
 
-uint32_t *render(float dt)
+Olivec_Canvas render(float dt)
 {
 {
     global_time += dt;
     global_time += dt;
 
 
     float t = sinf(10*global_time);
     float t = sinf(10*global_time);
 
 
-    olivec_fill(olivec_canvas(dst, WIDTH, HEIGHT, WIDTH), 0xFF181818);
-
     Olivec_Canvas dst_canvas = olivec_canvas(dst, WIDTH, HEIGHT, WIDTH);
     Olivec_Canvas dst_canvas = olivec_canvas(dst, WIDTH, HEIGHT, WIDTH);
 
 
+    olivec_fill(dst_canvas, 0xFF181818);
+
     int factor = 100;
     int factor = 100;
     int w = png_width*SRC_SCALE - t*factor;
     int w = png_width*SRC_SCALE - t*factor;
     int h = png_height*SRC_SCALE + t*factor;
     int h = png_height*SRC_SCALE + t*factor;
@@ -32,7 +32,7 @@ uint32_t *render(float dt)
         olivec_canvas(png, png_width, png_height, png_width),
         olivec_canvas(png, png_width, png_height, png_width),
         olivec_subcanvas(dst_canvas, WIDTH/2 - w/2, HEIGHT - h, w, h));
         olivec_subcanvas(dst_canvas, WIDTH/2 - w/2, HEIGHT - h, w, h));
 
 
-    return dst;
+    return dst_canvas;
 }
 }
 
 
 #include "vc.c"
 #include "vc.c"

+ 2 - 2
demos/triangle.c

@@ -35,7 +35,7 @@ static inline void rotate_point(float *x, float *y)
     *y = sinf(dir)*mag + HEIGHT/2;
     *y = sinf(dir)*mag + HEIGHT/2;
 }
 }
 
 
-uint32_t *render(float dt)
+Olivec_Canvas render(float dt)
 {
 {
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
 
 
@@ -73,7 +73,7 @@ uint32_t *render(float dt)
         olivec_circle(oc, circle_x, circle_y, CIRCLE_RADIUS, CIRCLE_COLOR);
         olivec_circle(oc, circle_x, circle_y, CIRCLE_RADIUS, CIRCLE_COLOR);
     }
     }
 
 
-    return pixels;
+    return oc;
 }
 }
 
 
 #include "vc.c"
 #include "vc.c"

+ 39 - 22
demos/vc.c

@@ -3,18 +3,17 @@
 // # Usage
 // # Usage
 // ```c
 // ```c
 // // demo.c
 // // demo.c
-// // NOTE: Must be always 800x600
-// // TODO: unhardcode the demos resolution
 // #define WIDTH 800
 // #define WIDTH 800
 // #define HEIGHT 600
 // #define HEIGHT 600
 // static uint32_t pixels[WIDTH*HEIGHT];
 // static uint32_t pixels[WIDTH*HEIGHT];
 //
 //
-// uint32_t *render(float dt)
+// Olivec_Canvas render(float dt)
 // {
 // {
+//     Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
 //     // ...
 //     // ...
-//     // ... render into pixels ...
+//     // ... render into oc ...
 //     // ...
 //     // ...
-//     return pixels;
+//     return oc;
 // }
 // }
 //
 //
 // // vc.c expectes render() to be defined an also supplies it's own entry point
 // // vc.c expectes render() to be defined an also supplies it's own entry point
@@ -114,12 +113,11 @@ defer:
 #include <time.h>
 #include <time.h>
 #include <unistd.h>
 #include <unistd.h>
 
 
-static_assert(WIDTH%SCALE_DOWN_FACTOR == 0, "WIDTH must be divisible by the SCALE_DOWN_FACTOR");
-#define SCALED_DOWN_WIDTH (WIDTH/SCALE_DOWN_FACTOR)
-static_assert(HEIGHT%SCALE_DOWN_FACTOR == 0, "HEIGHT must be divisible by the SCALE_DOWN_FACTOR");
-#define SCALED_DOWN_HEIGHT (HEIGHT/SCALE_DOWN_FACTOR)
-
-char char_canvas[SCALED_DOWN_WIDTH*SCALED_DOWN_HEIGHT];
+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;
 
 
 char color_to_char(uint32_t pixel)
 char color_to_char(uint32_t pixel)
 {
 {
@@ -161,14 +159,31 @@ uint32_t compress_pixels_chunk(Olivec_Canvas oc)
     return OLIVEC_RGBA(r, g, b, a);
     return OLIVEC_RGBA(r, g, b, a);
 }
 }
 
 
-void compress_pixels(uint32_t *pixels)
+void resize_char_canvas(size_t new_width, size_t new_height)
+{
+    // TODO: can we just do something so the divisibility is not important?
+    // 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);
+}
+
+void compress_pixels(Olivec_Canvas oc)
 {
 {
-    Olivec_Canvas oc = olivec_canvas(pixels, WIDTH, HEIGHT, WIDTH);
-    for (size_t y = 0; y < SCALED_DOWN_HEIGHT; ++y) {
-        for (size_t x = 0; x < SCALED_DOWN_WIDTH; ++x) {
+    if (actual_width != oc.width || actual_height != oc.height) {
+        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, 
             Olivec_Canvas soc = olivec_subcanvas(oc, x*SCALE_DOWN_FACTOR, y*SCALE_DOWN_FACTOR, SCALE_DOWN_FACTOR, 
 SCALE_DOWN_FACTOR);
 SCALE_DOWN_FACTOR);
-            char_canvas[y*SCALED_DOWN_WIDTH + x] = color_to_char(compress_pixels_chunk(soc));
+            char_canvas[y*scaled_down_width + x] = color_to_char(compress_pixels_chunk(soc));
         }
         }
     }
     }
 }
 }
@@ -177,17 +192,19 @@ int main(void)
 {
 {
     for (;;) {
     for (;;) {
         compress_pixels(render(1.f/60.f));
         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) {
-                putc(char_canvas[y*SCALED_DOWN_WIDTH + x], stdout);
-                putc(char_canvas[y*SCALED_DOWN_WIDTH + x], stdout);
+        for (size_t y = 0; y < scaled_down_height; ++y) {
+            for (size_t x = 0; x < scaled_down_width; ++x) {
+                // TODO: different halfs of the double pixels
+                // 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('\n', stdout);
             putc('\n', stdout);
         }
         }
 
 
         usleep(1000*1000/60);
         usleep(1000*1000/60);
-        printf("\033[%dA", SCALED_DOWN_HEIGHT);
-        printf("\033[%dD", SCALED_DOWN_WIDTH);
+        printf("\033[%zuA", scaled_down_height);
+        printf("\033[%zuD", scaled_down_width);
     }
     }
     return 0;
     return 0;
 }
 }