~l3kn/quad

f326c6fcb8e84d38fbd5956e19fb35ca90fda247 — Leon Rische 6 months ago 10a66fd
Extract key handler functions
1 files changed, 72 insertions(+), 82 deletions(-)

M quad.c
M quad.c => quad.c +72 -82
@@ 100,21 100,20 @@ void draw() {
  target.h = SIZE;

  SDL_RenderCopy(mainRenderer, grammarTexture, &src, &target);

  /* SDL_BlitScaled(grammarSurface, &src, winSurface, &target); */
  SDL_RenderPresent(mainRenderer);

}

void reset() {
  struct node pat0 = { PATTERN, 0 };
  for (int i = 0; i < 8; ++i) {
    rules[i].quads[0] = pat0;
    rules[i].quads[1] = pat0;
    rules[i].quads[2] = pat0;
    rules[i].quads[3] = pat0;
  Node pat0 = { PATTERN, 0 };
  for (int i = 0; i < NRULES; ++i) {
    for (int q = 0; q < 4; ++q) {
      rules[i].quads[q] = pat0;
    }
    rules[i].fallback = pat0;
  }
  renderRule(grammarRenderer);
  draw();
}

void zoom() {


@@ 130,6 129,55 @@ void zoom() {
  draw();
}

void invertColors() {
  int tmp = colors[0];
  colors[0] = colors[1];
  colors[1] = tmp;
  renderRule(grammarRenderer);
  draw();
}

void toggleZooming() {
  if (zooming) {
    zooming = false;
    zoomFactor = 1.0;
  } else {
    zooming = true;
  }
  draw();
}

void toggleOutline() {
  outline = !outline;
  renderRule(grammarRenderer);
  draw();
}

void decreaseLimit() {
  if (limit < SIZE / 2) {
    limit <<= 1;
    renderRule(grammarRenderer);
    draw();
  }
}

void increaseLimit() {
  if (limit > 1) {
    limit >>= 1;
    draw();
    renderRule(grammarRenderer);
    draw();
  }
}

void selectRule(int n) {
  if (n >= 0 && n < NRULES) {
    currentRule = n;
    renderRule(grammarRenderer);
    draw();
  }
}

void handleKey(SDL_Event* event) {
  switch (event->key.keysym.sym) {
  case SDLK_q:


@@ 139,91 187,33 @@ void handleKey(SDL_Event* event) {
    saveBMP();
    break;
  case SDLK_o:
    outline = !outline;
    renderRule(grammarRenderer);
    draw();
    toggleOutline();
    break;
  case SDLK_c:
    reset();
    renderRule(grammarRenderer);
    draw();
    break;
  case SDLK_v: {
    int tmp = colors[0];
    colors[0] = colors[1];
    colors[1] = tmp;
    renderRule(grammarRenderer);
    draw();
  case SDLK_v:
    invertColors();
    break;
  }
  case SDLK_z:
    if (zooming) {
      zooming = false;
      zoomFactor = 1.0;
    } else {
      zooming = true;
    }
    draw();
    break;
  case SDLK_d: {
    if (limit < SIZE / 2) {
      limit <<= 1;
      renderRule(grammarRenderer);
      draw();
    }
    break;
  }
  case SDLK_i: {
    if (limit > 1) {
      limit >>= 1;
      draw();
      renderRule(grammarRenderer);
      draw();
    }
    break;
  }
  case SDLK_1: {
    currentRule = 0;
    draw();
    break;
  }
  case SDLK_2: {
    currentRule = 1;
    draw();
    break;
  }
  case SDLK_3: {
    currentRule = 2;
    draw();
    break;
  }
  case SDLK_4: {
    currentRule = 3;
    draw();
    toggleZooming();
    break;
  }
  case SDLK_5: {
    currentRule = 4;
    draw();
  case SDLK_d:
    decreaseLimit();
    break;
  }
  case SDLK_6: {
    currentRule = 5;
    draw();
  case SDLK_i:
    increaseLimit();
    break;
  }
  case SDLK_7: {
    currentRule = 6;
    draw();
    break;
  }
  case SDLK_8: {
    currentRule = 7;
    draw();
    break;
  }
  default: {
    // Handle number keys
    if (event->key.keysym.sym >= SDLK_1 && \
        event->key.keysym.sym <= SDLK_9) {
      selectRule(event->key.keysym.sym - SDLK_1);
    }
  }
}
}


void handleMouse(SDL_Event* event) {
  int tx = (event->motion.x - PADDING) / TILE_SIZE;