Blame onefile/crypt.c

261920
261920
#include <helianthus.h></helianthus.h>
261920
261920
261920
const int cw = 64;
261920
const int ch = 64;
261920
const int tw = 512;
261920
const int th = 512;
261920
261920
261920
double tx = 0;
261920
double ty = 0;
261920
261920
261920
const char *srcfile = "data/people.png";
261920
const char *dstfile = "data/output/crypt.png";
261920
261920
Animation animChars;
261920
Animation animText;
261920
Framebuffer fb;
261920
261920
261920
void save() {
261920
  saveState();
261920
  target(fb);
261920
  viewportSave(dstfile);
261920
  restoreState();
261920
}
261920
261920
261920
void clearText() {
261920
  saveState();
261920
  resetState();
261920
  target(fb);
261920
  noStroke();
261920
  fill(COLOR_WHITE);
261920
  rect(0, 0, tw, th);
261920
  restoreState();
261920
  save();
261920
}
261920
261920
261920
void put(double x, double y) {
261920
  saveState();
261920
  resetState();
261920
  target(fb);
261920
  noStroke();
261920
  fill(COLOR_WHITE);
261920
  fillTexture(animChars, tx-x, ty-y, tw, th, FALSE);
261920
  rect(tx, ty, cw, ch);
261920
  restoreState();
261920
  save();
261920
}
261920
261920
261920
void erase(double x, double y) {
261920
  saveState();
261920
  resetState();
261920
  target(fb);
261920
  noStroke();
261920
  fill(COLOR_WHITE);
261920
  rect(x, y, cw, ch);
261920
  restoreState();
261920
  save();
261920
}
261920
261920
261920
void move(double dx, double dy) {
261920
  tx += dx;
261920
  ty += dy;
261920
  if (tx > tw - cw) { ty += ch; tx = 0; }
261920
  if (tx < 0) { ty -= ch; tx = tw - cw; }
261920
  if (ty > th - ch) ty = th - ch;
261920
  if (ty < 0) ty = 0;
261920
}
261920
261920
261920
void init() {
261920
  windowSetSize(tw*2, th);
261920
  if (fileExists(dstfile)) {
261920
    fb = createFramebufferFromFile(dstfile);
261920
  } else {
261920
    fb = createFramebuffer(tw, th);
261920
    clearText();
261920
  }
261920
  animText = createAnimationFromFramebuffer(fb);
261920
  animChars = createAnimation(srcfile);
261920
}
261920
261920
261920
void draw() {
261920
  double mx = mouseX();
261920
  double my = mouseY();
261920
261920
  saveState();
261920
261920
  stroke(COLOR_BLACK);
261920
  fill(COLOR_WHITE);
261920
  rectTextured(animChars, 0, 0, tw, th);
261920
  rectTextured(animText, tw, 0, tw, th);
261920
261920
  noFill();
261920
  rect(tw + tx, ty, cw, ch);
261920
261920
  if (mx < tw) {
261920
    mx -= cw/2;
261920
    my -= ch/2;
261920
    saveState();
261920
    cliprect(0, 0, tw, th);
261920
    stroke(COLOR_BLUE);
261920
    rect(mx, my, cw, ch);
261920
    if (mouseWentDown("left")) {
261920
      put(mx, my);
261920
      move(cw, 0);
261920
    }
261920
    restoreState();
261920
  } else {
261920
    mx -= tw + cw/2;
261920
    my -= ch/2;
261920
    saveState();
261920
    translate(tw, 0);
261920
    cliprect(0, 0, tw, th);
261920
    stroke(COLOR_RED);
261920
    rect(mx, my, cw, ch);
261920
    if (mouseWentDown("left")) {
261920
      tx = mx;
261920
      ty = my;
261920
    } else
261920
    if (mouseWentDown("right")) {
261920
      erase(mx, my);
261920
    }
261920
    restoreState();
261920
  }
261920
261920
  if (keyDown("any shift")) {
261920
    if (keyWentDown("delete")) clearText();
261920
  } else {
261920
    if (keyWentDown("return")) move(0, ch);
261920
    if (keyWentDown("delete")) erase(tx, ty);
261920
    if (keyWentDown("backspace")) { move(-cw, 0); erase(tx, ty); }
261920
  }
261920
261920
261920
  restoreState();
261920
}
261920
261920
261920
int main() {
261920
  windowSetVariableFrameRate();
261920
  windowSetInit(&init);
261920
  windowSetDraw(&draw);
261920
  windowRun();
261920
}
261920