#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <helianthus.h>
#define TILESIZE 512
#define ROWS 8
#define COLS 12
const double cellRatio = 5/6.0;
Animation colTiles[128];
Animation rowTiles[128];
int colTilesCount = 0;
int rowTilesCount = 0;
Framebuffer framebuffer;
Animation fbAnim;
void generate() {
saveState();
target(framebuffer);
background(COLOR_TRANSPARENT);
clear();
zoom(TILESIZE*cellRatio);
double k = 0.1;
fill(colorByRGBA(1, 1, 1, 1));
noStroke();
saveState();
translate(1, 0.5);
if (rowTilesCount > 0)
for(int y = 0; y < ROWS; ++y)
for(int x = 0; x < COLS-1; ++x) {
saveState();
translate(x, y);
zoom(1/cellRatio);
rectTextured(rowTiles[rand()%rowTilesCount], -0.5, -0.5, 1, 1);
restoreState();
}
restoreState();
saveState();
translate(0.5, 1);
if (rowTilesCount > 0)
for(int x = 0; x < COLS; ++x)
for(int y = 0; y < ROWS-1; ++y) {
saveState();
translate(x, y);
zoom(1/cellRatio);
rectTextured(colTiles[rand()%colTilesCount], -0.5, -0.5, 1, 1);
restoreState();
}
restoreState();
viewportSave("data/output/generated-scheme.png");
restoreState();
}
Animation subImage(int width, int height, unsigned char *pixels, int x, int y, int w, int h) {
if (x < 0 || y < 0 || x + w > width || y + h > height || !pixels)
return NULL;
unsigned char *px = calloc(4, w*h);
for(int i = 0; i < h; ++i)
memcpy(px + i*w*4, pixels + ((y+i)*width + x)*4, w*4);
Animation anim = createAnimationFromImage(w, h, px, FALSE);
free(px);
return anim;
}
void init() {
int w = 0;
int h = 0;
unsigned char *px = NULL;
imageLoad("data/scheme.png", &w, &h, &px);
for(int i = 0; i < 4; ++i)
if ((rowTiles[rowTilesCount] = subImage(w, h, px, i*TILESIZE, 0*TILESIZE, TILESIZE, TILESIZE)))
++rowTilesCount;
for(int i = 0; i < 4; ++i)
if ((colTiles[colTilesCount] = subImage(w, h, px, i*TILESIZE, 1*TILESIZE, TILESIZE, TILESIZE)))
++colTilesCount;
free(px);
w = ceil(TILESIZE*COLS*cellRatio - 0.001);
h = ceil(TILESIZE*ROWS*cellRatio - 0.001);
framebuffer = createFramebuffer(w, h);
fbAnim = createAnimationFromFramebuffer(framebuffer);
generate();
}
void draw() {
background(COLOR_WHITE);
double w = windowGetWidth();
double h = windowGetHeight();
if (keyWentDown("space")) generate();
saveState();
translate(w/2, h/2);
zoom(w/COLS);
fill(COLOR_WHITE);
noStroke();
rectTextured(fbAnim, -COLS/2.0, -ROWS/2.0, COLS, ROWS);
restoreState();
}
int main() {
windowSetVariableFrameRate();
windowSetResizable(TRUE);
windowSetInit(&init);
windowSetDraw(&draw);
windowRun();
}