From 58f31d22973f28a27adaa0e9182650cd78111b1e Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 7 Oct 2019 18:24:34 +0200 Subject: [PATCH] LOLWUT: version 6 initial output. May change a bit. --- src/lolwut.c | 4 ++-- src/lolwut.h | 2 +- src/lolwut5.c | 2 +- src/lolwut6.c | 38 +++++++++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/lolwut.c b/src/lolwut.c index 7e2ceca78..0e1552ba0 100644 --- a/src/lolwut.c +++ b/src/lolwut.c @@ -90,12 +90,12 @@ void lolwutCommand(client *c) { * canvas implementation that can be reused. */ /* Allocate and return a new canvas of the specified size. */ -lwCanvas *lwCreateCanvas(int width, int height) { +lwCanvas *lwCreateCanvas(int width, int height, int bgcolor) { lwCanvas *canvas = zmalloc(sizeof(*canvas)); canvas->width = width; canvas->height = height; canvas->pixels = zmalloc(width*height); - memset(canvas->pixels,0,width*height); + memset(canvas->pixels,bgcolor,width*height); return canvas; } diff --git a/src/lolwut.h b/src/lolwut.h index c049ac907..38c0de423 100644 --- a/src/lolwut.h +++ b/src/lolwut.h @@ -41,7 +41,7 @@ typedef struct lwCanvas { } lwCanvas; /* Drawing functions implemented inside lolwut.c. */ -lwCanvas *lwCreateCanvas(int width, int height); +lwCanvas *lwCreateCanvas(int width, int height, int bgcolor); void lwFreeCanvas(lwCanvas *canvas); void lwDrawPixel(lwCanvas *canvas, int x, int y, int color); int lwGetPixel(lwCanvas *canvas, int x, int y); diff --git a/src/lolwut5.c b/src/lolwut5.c index 0200fece6..5a9348800 100644 --- a/src/lolwut5.c +++ b/src/lolwut5.c @@ -74,7 +74,7 @@ lwCanvas *lwDrawSchotter(int console_cols, int squares_per_row, int squares_per_ int padding = canvas_width > 4 ? 2 : 0; float square_side = (float)(canvas_width-padding*2) / squares_per_row; int canvas_height = square_side * squares_per_col + padding*2; - lwCanvas *canvas = lwCreateCanvas(canvas_width, canvas_height); + lwCanvas *canvas = lwCreateCanvas(canvas_width, canvas_height, 0); for (int y = 0; y < squares_per_col; y++) { for (int x = 0; x < squares_per_row; x++) { diff --git a/src/lolwut6.c b/src/lolwut6.c index 8cc8d6e04..44c933629 100644 --- a/src/lolwut6.c +++ b/src/lolwut6.c @@ -114,14 +114,34 @@ void generateSkyscraper(lwCanvas *canvas, struct skyscraper *si) { /* Generate a skyline inspired by the parallax backgrounds of 8 bit games. */ void generateSkyline(lwCanvas *canvas) { - struct skyscraper si = { - .xoff = 4, - .width = 13, - .height = 15, - .windows = 1, - .color = 1 - }; - generateSkyscraper(canvas, &si); + struct skyscraper si; + + /* First draw the background skyscraper without windows, using the + * two different grays. */ + si.color = 1; + for (int offset = -10; offset < canvas->width;) { + offset += rand() % 8; + si.xoff = offset; + si.width = 10 + rand()%9; + si.height = canvas->height/2 + rand()%canvas->height/2; + si.windows = 0; + si.color = si.color == 1 ? 2 : 1; + generateSkyscraper(canvas, &si); + offset += si.width/2; + } + + /* Now draw the foreground skyscraper with the windows. */ + si.color = 0; + for (int offset = -10; offset < canvas->width;) { + offset += rand() % 8; + si.xoff = offset; + si.width = 5 + rand()%14; + if (si.width % 4) si.width += (si.width % 3); + si.height = canvas->height/2 + rand()%canvas->height/2; + si.windows = 1; + generateSkyscraper(canvas, &si); + offset += si.width+1; + } } /* The LOLWUT 6 command: @@ -152,7 +172,7 @@ void lolwut6Command(client *c) { if (rows > 1000) rows = 1000; /* Generate the city skyline and reply. */ - lwCanvas *canvas = lwCreateCanvas(cols,rows); + lwCanvas *canvas = lwCreateCanvas(cols,rows,3); generateSkyline(canvas); sds rendered = renderCanvas(canvas); rendered = sdscat(rendered,