|
@@ -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;
|
|
}
|
|
}
|