Blob Blame Raw

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


#define MAXCOUNT 10000
#define STEP     1.001
#define SPEED    2000


typedef struct Star {
  double cx, cy;
  double dx, dy;
  double r;
  int visible;
} Star;

Star stars[MAXCOUNT];
double step;


void updateStar(Star *s, double k, double w, double h) {
  s->dx *= k;
  s->dy *= k;
  s->r *= k;
  s->visible = fabs(s->dx + s->cx) + s->r < w/2
            && fabs(s->dy + s->cy) + s->r < h/2;
}


void update(double dt, double mx, double my, double w, double h) {
  step += SPEED*dt;
  while(step > 1) {
    int found = 0;
    for(int i = 0; i < MAXCOUNT; ++i) {
      if (stars[i].visible) updateStar(&stars[i], STEP, w, h);

      if (!found && !stars[i].visible) {
        stars[i].cx = mx;
        stars[i].cy = my;
        stars[i].dx = (randomFloat() - 0.5)*w - mx;
        stars[i].dy = (randomFloat() - 0.5)*h - my;
        stars[i].r = 0.25*pow(STEP, randomFloat());
        stars[i].visible = TRUE;
        found = 1;
      }
    }
    step -= 1;
  }

  double k = pow(STEP, step);
  for(int i = 0; i < MAXCOUNT; ++i)
    if (stars[i].visible) updateStar(&stars[i], k, w, h);
}


void init() {
  background(COLOR_BLACK);
}


void draw() {
  double w = windowGetWidth();
  double h = windowGetHeight();
  saveState();
  translate(w/2, h/2);

  update(windowGetFrameTime(), mouseTransformedX(), mouseTransformedY(), w, h);

  noStroke();
  fill(COLOR_WHITE);
  for(int i = 0; i < MAXCOUNT; ++i)
    if (stars[i].visible)
      circle(stars[i].cx + stars[i].dx, stars[i].cy + stars[i].dy, stars[i].r);

  restoreState();
}


int main() {
  windowSetResizable(TRUE);
  windowSetVariableFrameRate();
  windowSetInit(&init);
  windowSetDraw(&draw);
  windowRun();
}