Blob Blame Raw

#include <math.h>
#include <helianthus.h>

#define WIDTH  1024
#define HEIGHT  768
#define SIZE    100
#define COUNT   100


typedef struct {
  double x, y, s, r, rk;
} Rect;


Framebuffer fb;
Animation anim;
Rect rects[COUNT];
int id;


void generateRect(Rect *r) {
  r->x = randomFloat();
  r->y = randomFloat();
  r->s = randomFloat();
  r->r = randomFloat();
  r->rk = randomFloat()*2 - 1;
}


void drawRect(Rect *r, double a) {
  a = (1 - cos(a*PI))/2;
  double s = r->s * a * SIZE * ((mouseX() - WIDTH / 2) + (mouseY() - HEIGHT / 2))/100;
  double rot = a*r->rk*180 + r->r*360;

  saveState();
  //stroke(colorByRGBA(0, 0, 0, a));
  fill(colorByHSVA(r->x*360, 1, 1, a));
  translate(r->x*WIDTH, r->y*HEIGHT);
  rotate(rot);
  rect(-s/2, -s/2, s, s);
  restoreState();
}


void init() {
  fb = createFramebuffer(WIDTH, HEIGHT);
  anim = createAnimationFromFramebuffer(fb);
  for(int i = 0; i < COUNT; ++i)
    generateRect(&rects[i]);
  saveState();
  target(fb);
  clear();
  restoreState();
}


void draw() {
  saveState();
  noStroke();

  rectTextured(anim, 0, 0, WIDTH, HEIGHT);
  for(int i = 0; i < COUNT; ++i)
    drawRect(&rects[(id + i)%COUNT], 1.0 - (double)i/COUNT);

  target(fb);
  drawRect(&rects[id], 1.0);
  generateRect(&rects[id]);
  id = (id + 1)%COUNT;

  restoreState();
}


int main() {
  windowSetVariableFrameRate();
  windowSetSize(WIDTH, HEIGHT);
  windowSetInit(&init);
  windowSetDraw(&draw);
  windowRun();
}