~l3kn/quad

ref: bw-only quad/menu.h -rw-r--r-- 5.1 KiB
feeba87e — Leon Rische Print screen size 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
bool outline = true;
bool expand = true;
size_t limit = 1;
size_t 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, size_t n);

void drawNode(SDL_Renderer* renderer, int x, int y, Node node);
void drawRule(SDL_Renderer* renderer, int x, int y, Rule* rule, size_t 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, size_t 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, size_t 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 (size_t 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 (size_t 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 (size_t 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);
  }
}