~l3kn/quad

ref: 6633fe32b851e8b53cb2adbad785a1dc24393f33 quad/quad.c -rw-r--r-- 5.8 KiB
6633fe32 — Leon Rische Add config file 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <SDL2/SDL.h>

#include "config.h"
#include "quad.h"
#include "menu.h"

SDL_Window* window = NULL;
SDL_Renderer* windowRenderer = NULL;

size_t currentRule = 0;
Node* selNode = NULL;

Rule rules[NRULES];

bool quit = false;

bool selector = false;
int selectorX = PADDING;
int selectorY = PADDING;

void saveBMP() {
  time_t currentTime;
  struct tm* info;
  time(&currentTime);
  info = localtime(&currentTime);

  char path[50];
  strftime(path, 50, "quad_%F_%T.bmp", info);

  SDL_Surface* surface = SDL_GetWindowSurface(window);
	SDL_RenderReadPixels(windowRenderer,
	                     NULL,
	                     SDL_PIXELFORMAT_ARGB8888,
	                     surface->pixels,
	                     surface->pitch);

  SDL_SaveBMP(surface, path);
  printf("BMP saved to %s\n", path);
  SDL_FreeSurface(surface);
}

void draw(bool expand) {
  SDL_Rect rect = makeRect(0, 0, CANVAS_X, HEIGHT);
  setColor(windowRenderer, colors[0]);
  SDL_RenderFillRect(windowRenderer, &rect);
  setColor(windowRenderer, colors[1]);

  if (expand) {
    drawSquare(windowRenderer, CANVAS_X-1, CANVAS_Y-1, SIZE+2, BLACK, true);
    expandRule(windowRenderer, CANVAS_X, CANVAS_Y, SIZE, currentRule, rules);
  }

  drawMenu(windowRenderer, MENU_X, MENU_Y, patterns, rules);

  if (selector) {
    drawSelector(windowRenderer, selectorX, selectorY, patterns);
  }

  SDL_RenderPresent(windowRenderer);
}

void reset() {
  Node pat0 = { PATTERN, 0 };
  for (size_t i = 0; i < NRULES; ++i) {
    for (size_t q = 0; q < 4; ++q) {
      rules[i].quads[q] = pat0;
    }
    rules[i].fallback = pat0;
  }
  draw(true);
}

void invertColors() {

  int tmp = colors[0];
  colors[0] = colors[1];
  colors[1] = tmp;

  setColor(windowRenderer, colors[0]);
  SDL_RenderClear(windowRenderer);
  setColor(windowRenderer, colors[1]);

  draw(true);
}

void toggleOutline() {
  outline = !outline;
  draw(true);
}

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

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

void selectRule(size_t n) {
  if (n >= 0 && n < NRULES) {
    currentRule = n;
    draw(true);
  }
}

void handleKey(SDL_Event event[static 1]) {
  switch (event->key.keysym.sym) {
  case SDLK_q:
    quit = 1;
    break;
  case SDLK_r:
    saveBMP();
    break;
  case SDLK_o:
    toggleOutline();
    break;
  case SDLK_c:
    reset();
    break;
  case SDLK_v:
    invertColors();
    break;
  case SDLK_d:
    decreaseLimit();
    break;
  case SDLK_i:
    increaseLimit();
    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[static 1]) {
  int x = event->motion.x;
  int y = event->motion.y;

  if (selector) {
    selector = false;
    if (x >= selectorX && x < selectorX + (WPATTERNS * TILE_SIZE) && \
        y >= selectorY && y < selectorY + (HPATTERNS * TILE_SIZE)) {
      int tx = (x - selectorX) / TILE_SIZE;
      int ty = (y - selectorY) / TILE_SIZE;
      selNode->type = PATTERN;
      selNode->index = tx + WPATTERNS * ty;
    } else if (x >= selectorX && x < selectorX + WRULES * TILE_SIZE && \
               y >= selectorY + HPATTERNS * TILE_SIZE && \
               y < selectorY + (HPATTERNS + HRULES) * TILE_SIZE) {
      int tx = (x - selectorX) / TILE_SIZE;
      int ty = (y - selectorY - HPATTERNS * TILE_SIZE) / TILE_SIZE;
      selNode->type = RULE;
      selNode->index = tx + (WRULES * ty);
    }
    draw(true);
  } else if (x >= MENU_X && x < MENU_X + MENU_WIDTH * TILE_SIZE && \
             y >= MENU_Y && y < MENU_Y + MENU_HEIGHT * TILE_SIZE) {
    int mx = (x - MENU_X);
    int my = (y - MENU_Y);
    int tx = mx / TILE_SIZE;
    int ty = my / TILE_SIZE;
    size_t index = (ty / HEDIT) * 2 + (tx / WEDIT);
    int rx = tx % WEDIT;
    int ry = ty % HEDIT;

    if (rx == 0 && ry == 0) {
      currentRule = index;
      draw(true);
    } else if (rx == 0 && ry == 1) {
      Rule* rule = &rules[index];
      rule->fallback.index = !rule->fallback.index;
      draw(true);
    } else if (rx >= 1 && rx < 3 && ry >= 0 && ry < 2) {
      int quad = ry * 2 + (rx - 1);
      selNode = &(rules[index].quads[quad]);
      selector = true;
      selectorX = MENU_X + mx - (mx % TILE_SIZE);
      selectorY = MENU_Y + my - (my % TILE_SIZE);
      draw(true);
    }
  }
}

void printNode(Node node[static 1]) {
  if (node->type == RULE) {
    printf("R%02d", node->index);
  } else {
    printf("P%02d", node->index);
  }
}

void printRules() {
  for (size_t i = 0; i < NRULES; ++i) {
    Rule* rule = &rules[i];
    for (size_t j = 0; j < 4; ++j) {
      printNode(&(rule->quads[j]));
    }
    printNode(&(rule->fallback));
    printf("\n");
  }
}

int main(void) {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    printf("error initializing SDL: %s\n", SDL_GetError());
    return 1;
  }

  if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0,
                                  &window, &windowRenderer) != 0) {
    printf("Error creating window & renderer: %s\n", SDL_GetError());
    return 1;
  }

  reset();
  draw(true);

  while (!quit) {
    SDL_Event event;

    while (SDL_PollEvent(&event)) {
      switch (event.type) {
      case SDL_QUIT:
        quit = true;
        break;
      case SDL_KEYDOWN:
        handleKey(&event);
        break;
      case SDL_MOUSEBUTTONDOWN:
        handleMouse(&event);
        break;
      case SDL_WINDOWEVENT:
        if (event.window.event == SDL_WINDOWEVENT_EXPOSED) {
          draw(true);
        }
      }
    }

    // Render at 60 fps
    SDL_Delay(1000 / 60);
  }

  printRules();

  SDL_DestroyRenderer(windowRenderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return EXIT_SUCCESS;
}