Blob Blame Raw

#include <helianthus.h>


const int cw = 64;
const int ch = 64;
const int tw = 512;
const int th = 512;


double tx = 0;
double ty = 0;


const char *srcfile = "data/people.png";
const char *dstfile = "data/output/crypt.png";

Animation animChars;
Animation animText;
Framebuffer fb;


void save() {
  saveState();
  target(fb);
  viewportSave(dstfile);
  restoreState();
}


void clearText() {
  saveState();
  resetState();
  target(fb);
  noStroke();
  fill(COLOR_WHITE);
  rect(0, 0, tw, th);
  restoreState();
  save();
}


void put(double x, double y) {
  saveState();
  resetState();
  target(fb);
  noStroke();
  fill(COLOR_WHITE);
  fillTexture(animChars, tx-x, ty-y, tw, th, FALSE);
  rect(tx, ty, cw, ch);
  restoreState();
  save();
}


void erase(double x, double y) {
  saveState();
  resetState();
  target(fb);
  noStroke();
  fill(COLOR_WHITE);
  rect(x, y, cw, ch);
  restoreState();
  save();
}


void move(double dx, double dy) {
  tx += dx;
  ty += dy;
  if (tx > tw - cw) { ty += ch; tx = 0; }
  if (tx < 0) { ty -= ch; tx = tw - cw; }
  if (ty > th - ch) ty = th - ch;
  if (ty < 0) ty = 0;
}


void init() {
  windowSetSize(tw*2, th);
  if (fileExists(dstfile)) {
    fb = createFramebufferFromFile(dstfile);
  } else {
    fb = createFramebuffer(tw, th);
    clearText();
  }
  animText = createAnimationFromFramebuffer(fb);
  animChars = createAnimation(srcfile);
}


void draw() {
  double mx = mouseX();
  double my = mouseY();

  saveState();

  stroke(COLOR_BLACK);
  fill(COLOR_WHITE);
  rectTextured(animChars, 0, 0, tw, th);
  rectTextured(animText, tw, 0, tw, th);

  noFill();
  rect(tw + tx, ty, cw, ch);

  if (mx < tw) {
    mx -= cw/2;
    my -= ch/2;
    saveState();
    cliprect(0, 0, tw, th);
    stroke(COLOR_BLUE);
    rect(mx, my, cw, ch);
    if (mouseWentDown("left")) {
      put(mx, my);
      move(cw, 0);
    }
    restoreState();
  } else {
    mx -= tw + cw/2;
    my -= ch/2;
    saveState();
    translate(tw, 0);
    cliprect(0, 0, tw, th);
    stroke(COLOR_RED);
    rect(mx, my, cw, ch);
    if (mouseWentDown("left")) {
      tx = mx;
      ty = my;
    } else
    if (mouseWentDown("right")) {
      erase(mx, my);
    }
    restoreState();
  }

  if (keyDown("any shift")) {
    if (keyWentDown("delete")) clearText();
  } else {
    if (keyWentDown("return")) move(0, ch);
    if (keyWentDown("delete")) erase(tx, ty);
    if (keyWentDown("backspace")) { move(-cw, 0); erase(tx, ty); }
  }


  restoreState();
}


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