2020-06-13 04:06:33 +00:00
|
|
|
#include "demo.h"
|
|
|
|
|
|
|
|
// lame as fuck lol
|
|
|
|
static bool done;
|
|
|
|
static uint32_t pixel;
|
|
|
|
static int y, x, dy, dx;
|
|
|
|
|
|
|
|
static int
|
|
|
|
dragonmayer(struct ncvisual* ncv, const char* str, int iters){
|
2020-06-14 02:24:50 +00:00
|
|
|
int total = 0;
|
2020-06-13 04:06:33 +00:00
|
|
|
char c;
|
|
|
|
int r;
|
|
|
|
while( (c = *str++) ){
|
|
|
|
switch(c){
|
|
|
|
case 'X':
|
|
|
|
if(iters > 1){
|
2020-06-14 02:24:50 +00:00
|
|
|
if((r = dragonmayer(ncv, "X+YF+", iters - 1)) < 0){
|
2020-06-13 04:06:33 +00:00
|
|
|
return r;
|
|
|
|
}
|
2020-06-14 02:24:50 +00:00
|
|
|
total += r;
|
2020-06-13 04:06:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
if(iters > 1){
|
2020-06-14 02:24:50 +00:00
|
|
|
if((r = dragonmayer(ncv, "-FX-Y", iters - 1)) < 0){
|
2020-06-13 04:06:33 +00:00
|
|
|
return r;
|
|
|
|
}
|
2020-06-14 02:24:50 +00:00
|
|
|
total += r;
|
2020-06-13 04:06:33 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '+': { int tmp = dy; dy = -dx; dx = tmp; break; }
|
|
|
|
case '-': { int tmp = -dy; dy = dx; dx = tmp; break; }
|
|
|
|
case 'F': // FIXME want a line
|
2020-06-14 08:31:00 +00:00
|
|
|
ncpixel_set_r(&pixel, 0xb0 - (iters * 0x10));
|
2020-09-02 21:41:42 +00:00
|
|
|
ncpixel_set_a(&pixel, 0xff);
|
2020-06-14 02:24:50 +00:00
|
|
|
if(ncvisual_set_yx(ncv, y, x, pixel) == 0){
|
|
|
|
++total;
|
2020-06-13 04:06:33 +00:00
|
|
|
}
|
2020-09-02 21:41:42 +00:00
|
|
|
pixel = 0;
|
2020-06-13 04:06:33 +00:00
|
|
|
x += dx;
|
|
|
|
y += dy;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 02:24:50 +00:00
|
|
|
return total;
|
2020-06-13 04:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int dragon_demo(struct notcurses* nc){
|
2020-06-13 04:13:09 +00:00
|
|
|
done = false;
|
2020-06-13 04:06:33 +00:00
|
|
|
int dimy, dimx;
|
|
|
|
struct ncplane* n = notcurses_stddim_yx(nc, &dimy, &dimx);
|
2020-06-16 06:24:27 +00:00
|
|
|
--dimy; // don't disturb the menu bar
|
2020-06-13 04:06:33 +00:00
|
|
|
// we use a Lindenmayer string rewriting system. the classic dragon curve
|
|
|
|
// system is X -> X+YF+, Y -> -FX-Y, where F is forward, - is turn left, and
|
|
|
|
// + is turn right.
|
|
|
|
const char LINDENSTART[] = "FX";
|
2021-07-01 09:28:30 +00:00
|
|
|
const int vscale = 3;
|
|
|
|
dimy *= vscale;
|
|
|
|
dimy += (vscale - 1);
|
2020-06-13 04:06:33 +00:00
|
|
|
dimx *= 2;
|
|
|
|
int dxstart, dystart;
|
|
|
|
if(dimy > dimx){
|
|
|
|
dystart = 0;
|
2020-06-16 06:24:27 +00:00
|
|
|
dxstart = 1;
|
2020-06-13 04:06:33 +00:00
|
|
|
}else{
|
2020-06-16 06:24:27 +00:00
|
|
|
dystart = 1;
|
2020-06-13 04:06:33 +00:00
|
|
|
dxstart = 0;
|
|
|
|
}
|
|
|
|
size_t fbbytes = sizeof(uint32_t) * dimy * dimx;
|
|
|
|
uint32_t* rgba = malloc(fbbytes);
|
|
|
|
if(rgba == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(rgba, 0, fbbytes);
|
|
|
|
struct ncvisual* ncv = ncvisual_from_rgba(rgba, dimy, dimx * sizeof(uint32_t), dimx);
|
2020-09-02 21:41:42 +00:00
|
|
|
free(rgba);
|
2020-06-13 04:06:33 +00:00
|
|
|
if(ncv == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct timespec scaled;
|
2020-06-14 08:31:00 +00:00
|
|
|
timespec_div(&demodelay, 8, &scaled);
|
2020-06-14 02:24:50 +00:00
|
|
|
int lasttotal = 0;
|
2020-06-13 04:06:33 +00:00
|
|
|
int iters = 0;
|
2020-06-14 02:24:50 +00:00
|
|
|
int r = 0;
|
2020-06-13 04:06:33 +00:00
|
|
|
do{
|
|
|
|
++iters;
|
2020-06-14 02:24:50 +00:00
|
|
|
lasttotal = r;
|
2020-06-13 04:06:33 +00:00
|
|
|
dx = dxstart;
|
|
|
|
dy = dystart;
|
|
|
|
x = dimx / 2;
|
2021-07-01 09:28:30 +00:00
|
|
|
y = dimy / vscale;
|
2020-06-14 02:24:50 +00:00
|
|
|
r = dragonmayer(ncv, LINDENSTART, iters);
|
|
|
|
if(r < 0){
|
2020-06-13 04:06:33 +00:00
|
|
|
ncvisual_destroy(ncv);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
struct ncvisual_options vopts = {
|
|
|
|
.n = n,
|
2020-06-16 06:24:27 +00:00
|
|
|
.y = 1,
|
2020-12-26 00:47:05 +00:00
|
|
|
.scaling = NCSCALE_STRETCH,
|
2020-06-13 04:06:33 +00:00
|
|
|
};
|
|
|
|
if(ncvisual_render(nc, ncv, &vopts) == NULL){
|
|
|
|
ncvisual_destroy(ncv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
DEMO_RENDER(nc);
|
|
|
|
demo_nanosleep(nc, &scaled);
|
2020-06-14 02:24:50 +00:00
|
|
|
}while(lasttotal != r);
|
2020-06-13 04:06:33 +00:00
|
|
|
ncvisual_destroy(ncv);
|
|
|
|
return 0;
|
|
|
|
}
|