LOLWUT: version 6 initial output. May change a bit.

This commit is contained in:
antirez 2019-10-07 18:24:34 +02:00
parent f019f28e7b
commit c10889150e
4 changed files with 33 additions and 13 deletions

View File

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

View File

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

View File

@ -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++) {

View File

@ -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,