~l3kn/quad

1002ec965d3de700bfeb91c0001eb7d51db4cc72 — Leon Rische 6 months ago 6074b4e
Add messy menu.h file
1 files changed, 166 insertions(+), 0 deletions(-)

A menu.h
A menu.h => menu.h +166 -0
@@ 0,0 1,166 @@
bool outline = true;
bool expand = true;
int limit = 1;
int currentRule;

SDL_Rect makeRect(int x, int y, int w, int h) {
  SDL_Rect rect;
  rect.x = x;
  rect.y = y;
  rect.w = w;
  rect.h = h;

  return rect;
}

void drawSquare(SDL_Renderer* renderer, int x, int y, int s, Pattern pattern, bool outline);
void drawNumber(SDL_Renderer* renderer, int x, int y, int s, int n);

void drawNode(SDL_Renderer* renderer, int x, int y, Node node);
void drawRule(SDL_Renderer* renderer, int x, int y, Rule* rule, int n);
void drawMenu(SDL_Renderer* renderer,
              int x, int y,
              Pattern patterns[static NPATTERNS],
              Rule rules[static NRULES]);

void expandRule(SDL_Renderer* renderer,
                int x, int y, int s, int n,
                Rule rules[NRULES]);
void expandNode(SDL_Renderer* renderer,
                int x, int y, int s,
                Node node,
                Rule rules[NRULES]);

void drawRect(SDL_Renderer* renderer,
              int x, int y, int w, int h,
              Pattern pattern,
              bool outline) {
  // Offset by 1 so outlines overlap
  SDL_Rect rect = makeRect(x, y, w + 1, h + 1);

  if (pattern) {
    SDL_RenderFillRect(renderer, &rect);
  } else {
    setColor(renderer, colors[0]);
    SDL_RenderFillRect(renderer, &rect);
    setColor(renderer, colors[1]);
  }

  if (outline) {
    SDL_RenderDrawRect(renderer, &rect);
  }
}

void drawSquare(SDL_Renderer* renderer,
                int x, int y, int s,
                Pattern pattern,
                bool outline) {
  drawRect(renderer, x, y, s, s, pattern, outline);
}

void drawNumber(SDL_Renderer* renderer, int x, int y, int s, int n) {
  int h = s / 2;
  int o = s / 4;
  drawSquare(renderer, x + o, y + o, h / 2, (n & 1) > 0, true) ;
  drawSquare(renderer, x + h, y + o, h / 2, (n & 2) > 0, true);
  drawSquare(renderer, x + o, y + h, h / 2, (n & 4) > 0, true);
  drawSquare(renderer, x + h, y + h, h / 2, (n & 8) > 0, true);
}

void drawNode(SDL_Renderer* renderer, int x, int y, Node node) {
  if (node.type == RULE) {
    drawSquare(renderer, x, y, TILE_SIZE, BLACK, outline);
    drawNumber(renderer, x, y, TILE_SIZE, node.index);
  } else {
    drawSquare(renderer, x, y, TILE_SIZE, patterns[node.index], outline);
  }
}

void drawRule(SDL_Renderer* renderer, int x, int y, Rule* rule, int n) {
  drawSquare(renderer, x, y, TILE_SIZE, BLACK, currentRule == n);
  drawNumber(renderer, x, y, TILE_SIZE, n);
  drawNode(renderer, x, y + TILE_SIZE, rule->fallback);
  drawNode(renderer, x + 1 * TILE_SIZE, y, rule->quads[0]);
  drawNode(renderer, x + 2 * TILE_SIZE, y, rule->quads[1]);
  drawNode(renderer, x + 1 * TILE_SIZE, y + TILE_SIZE, rule->quads[2]);
  drawNode(renderer, x + 2 * TILE_SIZE, y + TILE_SIZE, rule->quads[3]);
}

void drawSelector(SDL_Renderer* renderer,
                  int x, int y,
                  Pattern patterns[static NPATTERNS]) {
  drawRect(renderer, x - 4, y - 4,
           TILE_SIZE * 2 + 8,
           TILE_SIZE * 5 + 8,
           false,
           true);
  drawRect(renderer, x, y,
           TILE_SIZE * 2,
           TILE_SIZE * 5,
           false,
           true);
  for (int i = 0; i < NPATTERNS; ++i) {
    int x_ = (i % WPATTERNS) * TILE_SIZE;
    int y_ = (i / WPATTERNS) * TILE_SIZE;
    drawSquare(renderer,
               x + x_, y + y_,
               TILE_SIZE,
               patterns[i],
               true);
  }
  for (int i = 0; i < NRULES; ++i) {
    int x_ = (i % WRULES) * TILE_SIZE;
    int y_ = (i / WRULES) * TILE_SIZE;
    drawSquare(renderer,
               x + x_,
               HPATTERNS * TILE_SIZE + y + y_,
               TILE_SIZE,
               false,
               true);
    drawNumber(renderer,
               x + x_,
               HPATTERNS * TILE_SIZE + y + y_,
               TILE_SIZE, i);
  }
}

void drawMenu(SDL_Renderer* renderer,
              int x, int y,
              Pattern patterns[static NPATTERNS],
              Rule rules[static NRULES]) {
  for (int i = 0; i < NRULES; ++i) {
    int x_ = (i % WEDITRULES) * WEDIT * TILE_SIZE;
    int y_ = (i / WEDITRULES) * HEDIT * TILE_SIZE;
    drawRule(renderer, x + x_, y + y_, &rules[i], i);
  }
}

void expandNode(SDL_Renderer* renderer,
                int x, int y, int s,
                Node node,
                Rule rules[NRULES]) {
  if (node.type == RULE) {
    /* if (!outline) { */
      expandRule(renderer, x, y, s, node.index, rules);
    /* } else { */
      /* drawNumber(renderer, x, y, s, node.index); */
    /* } */
  } else if (node.type == PATTERN) {
    drawSquare(renderer, x, y, s, patterns[node.index], false);
  }
}

void expandRule(SDL_Renderer* renderer,
                int x, int y, int s,
                int n, Rule rules[NRULES]) {
  Rule rule = rules[n];
  int h = s / 2;
  if (s > limit) {
    expandNode(renderer, x, y, h, rule.quads[0], rules);
    expandNode(renderer, x + h, y, h, rule.quads[1], rules);
    expandNode(renderer, x, y + h, h, rule.quads[2], rules);
    expandNode(renderer, x + h, y + h, h, rule.quads[3], rules);
  } else if (rule.fallback.type == PATTERN) {
    expandNode(renderer, x, y, s, rule.fallback, rules);
  }
}