Blame onefile/gen-scheme.c

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