aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:11:35 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:11:35 +0000
commit08fcd58893b5ea754ebddbf63b2745dde34d4830 (patch)
tree6e121100ec1aceccd2a6032c90bf11d1dd16ef8f /src/main.c
downloadlab-01-08fcd58893b5ea754ebddbf63b2745dde34d4830.tar.gz
publishHEADmaster
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c576
1 files changed, 576 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..89741da
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,576 @@
1// src/main.c --- entry point and window management
2#include <stdlib.h>
3#include <stdio.h>
4#include <stdbool.h>
5#include <inttypes.h>
6#include <assert.h>
7#include <string.h>
8#include <ctype.h>
9#include <time.h>
10#include <raylib.h>
11#include <raymath.h>
12
13#include "config.h"
14#include "dynamic-array.h"
15#include "cellular-automaton.h"
16
17static struct cellular_automaton cellular_automaton;
18
19static float cell_size = 0;
20static Rectangle board_bounding_box = {0};
21static Font font;
22
23static float tick_rate = INITIAL_TICK_RATE;
24static float time_from_last_udpate = 0;
25
26static bool is_paused = INITIAL_IS_PAUSED;
27static bool is_grid_shown = INITIAL_IS_GRID_SHOWN;
28static bool is_help_shown = INITIAL_IS_HELP_SHOWN;
29
30static uint8_t *transient_iteration_bytes;
31static size_t iteration;
32static struct {
33 uint8_t *items;
34 uint32_t count;
35 uint32_t capacity;
36} iterations = {0};
37
38#define TEXT_SIZE_FUNCTION 9
39static char text_function[TEXT_SIZE_FUNCTION];
40static bool text_function_is_editing = false;
41
42#define TEXT_SIZE_COLS 4
43static char text_cols[TEXT_SIZE_COLS];
44static bool text_cols_is_editing = false;
45
46#define TEXT_SIZE_ROWS 4
47static char text_rows[TEXT_SIZE_ROWS];
48static bool text_rows_is_editing = false;
49
50static void
51init (void)
52{
53 srand (time (NULL));
54
55 cellular_automaton_init (&cellular_automaton,
56 INITIAL_CELLULAR_AUTOMATON_FUNCTION,
57 INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS,
58 INITIAL_CELLULAR_AUTOMATON_BOARD_COLS);
59 if (INITIAL_IS_RANDOM) {
60 for (uint32_t row = 0; row < cellular_automaton.board.rows; ++row) {
61 for (uint32_t col = 0; col < cellular_automaton.board.cols; ++col) {
62 board_set (&cellular_automaton.board, row, col, rand () % 2);
63 }
64 }
65 }
66
67 DYNAMIC_ARRAY_APPEND_MANY (iterations,
68 cellular_automaton.board.bitarray.bytes,
69 cellular_automaton.board.bitarray.size);
70 iteration = 0;
71 if (INITIAL_IS_PAUSED)
72 transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
73
74 SetConfigFlags (FLAG_MSAA_4X_HINT |
75 FLAG_WINDOW_RESIZABLE |
76 FLAG_FULLSCREEN_MODE |
77 FLAG_VSYNC_HINT);
78
79 // InitWindow (INITIAL_WINDOW_WIDTH,
80 // INITIAL_WINDOW_HEIGHT,
81 // INITIAL_WINDOW_TITLE);
82 InitWindow (0, 0, INITIAL_WINDOW_TITLE);
83
84 font = GetFontDefault ();
85
86 snprintf (text_function, TEXT_SIZE_FUNCTION, "%08X", cellular_automaton.function);
87 snprintf (text_cols, TEXT_SIZE_COLS, "%u", cellular_automaton.board.cols);
88 snprintf (text_rows, TEXT_SIZE_ROWS, "%u", cellular_automaton.board.rows);
89}
90
91static void
92deinit (void)
93{
94 CloseWindow ();
95 cellular_automaton_free (&cellular_automaton);
96}
97
98static void
99handel_input (void)
100{
101 if (IsKeyPressed (KEY_SPACE)) {
102 if (is_paused) {
103 is_paused = false;
104 cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
105 memcpy (cellular_automaton.board.bitarray.bytes,
106 &iterations.items[iteration*cellular_automaton.board.bitarray.size],
107 cellular_automaton.board.bitarray.size);
108 iterations.count = (iteration + 1)*cellular_automaton.board.bitarray.size;
109 } else {
110 is_paused = true;
111 transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
112 memcpy (&iterations.items[iteration*cellular_automaton.board.bitarray.size],
113 cellular_automaton.board.bitarray.bytes,
114 cellular_automaton.board.bitarray.size);
115 }
116 }
117
118 if (IsKeyPressed (KEY_F1) || IsKeyPressed (KEY_H))
119 is_help_shown = !is_help_shown;
120
121 if (is_help_shown && IsKeyPressed (KEY_Q))
122 is_help_shown = false;
123
124 if (IsKeyPressed (KEY_G))
125 is_grid_shown = !is_grid_shown;
126
127 if (IsKeyPressed (KEY_C) && !text_function_is_editing) {
128 if (is_paused)
129 cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
130 memset (cellular_automaton.board.bitarray.bytes, 0,
131 cellular_automaton.board.bitarray.size);
132 DYNAMIC_ARRAY_RESET (iterations);
133 DYNAMIC_ARRAY_APPEND_MANY (iterations,
134 cellular_automaton.board.bitarray.bytes,
135 cellular_automaton.board.bitarray.size);
136 iteration = 0;
137 is_paused = AFTER_CLEAR_IS_PAUSED;
138 if (is_paused)
139 transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
140 }
141
142 if (is_paused) {
143 int change = 0;
144 change += GetMouseWheelMove () > 0 ||
145 IsKeyPressed (KEY_N) || IsKeyPressed (KEY_RIGHT) ||
146 IsKeyDown (KEY_LEFT_SHIFT) && (IsKeyDown (KEY_N) ||
147 IsKeyDown (KEY_RIGHT));
148 change -= GetMouseWheelMove () < 0 ||
149 IsKeyPressed (KEY_P) || IsKeyPressed (KEY_LEFT) ||
150 IsKeyDown (KEY_LEFT_SHIFT) && (IsKeyDown (KEY_P) ||
151 IsKeyDown (KEY_LEFT));
152 if (change < 0 && iteration >= -change ||
153 change > 0 && (iteration + change <=
154 (iterations.count - 1) /
155 cellular_automaton.board.bitarray.size))
156 iteration += change;
157 cellular_automaton.board.bitarray.bytes =
158 &iterations.items[iteration*cellular_automaton.board.bitarray.size];
159 } else {
160 tick_rate = fmaxf (tick_rate +
161 TICK_RATE_CHANGE_SPEED*GetMouseWheelMove () *
162 (1 + (TICK_RATE_CHANGE_SPEED_MULTIPLIER - 1) *
163 IsKeyDown (KEY_LEFT_SHIFT)), 0);
164 }
165
166 enum cell_action {
167 CELL_ACTION_NONE = 0b00,
168 CELL_ACTION_KILL = 0b01,
169 CELL_ACTION_RESURRECT = 0b10,
170 } cell_action = CELL_ACTION_NONE;
171
172 if (!text_function_is_editing &&
173 !text_cols_is_editing &&
174 !text_rows_is_editing) {
175 if (IsMouseButtonDown (MOUSE_LEFT_BUTTON))
176 cell_action = CELL_ACTION_RESURRECT;
177 else if (IsMouseButtonDown (MOUSE_RIGHT_BUTTON))
178 cell_action = CELL_ACTION_KILL;
179 }
180
181 if (cell_action) {
182 Vector2 mouse = GetMousePosition ();
183 if (CheckCollisionPointRec (mouse, board_bounding_box)) {
184 uint32_t row = (mouse.y - board_bounding_box.y)/cell_size;
185 uint32_t col = (mouse.x - board_bounding_box.x)/cell_size;
186 board_set (&cellular_automaton.board, row, col, cell_action >> 1 & 1);
187 }
188 }
189}
190
191static void
192update (void)
193{
194 cell_size = fminf (
195 (GetScreenHeight () - INFOBAR_HEIGHT)/(float) cellular_automaton.board.rows,
196 (GetScreenWidth () )/(float) cellular_automaton.board.cols);
197 board_bounding_box.width = cell_size*cellular_automaton.board.cols;
198 board_bounding_box.height = cell_size*cellular_automaton.board.rows;
199 board_bounding_box.x = (GetScreenWidth () - board_bounding_box.width )/2.;
200 board_bounding_box.y = (GetScreenHeight () - board_bounding_box.height + INFOBAR_HEIGHT)/2.;
201
202 time_from_last_udpate += GetFrameTime ();
203 if (!is_paused && time_from_last_udpate >= tick_rate) {
204 time_from_last_udpate = 0;
205
206 cellular_automaton_progress (&cellular_automaton);
207 DYNAMIC_ARRAY_APPEND_MANY (iterations,
208 cellular_automaton.board.bitarray.bytes,
209 cellular_automaton.board.bitarray.size);
210 iteration += 1;
211 }
212}
213
214static void
215draw_fps (int x, int y, float font_size)
216{
217 DrawTextEx (font, TextFormat ("%2i FPS", GetFPS ()), (Vector2) { x, y },
218 font_size, TEXT_SPACING, INFOBAR_COLOR_TEXT);
219}
220
221static void
222draw_infobar (void)
223{
224 DrawRectangle (0, 0, GetScreenWidth (), INFOBAR_HEIGHT, INFOBAR_COLOR_BACKGROUND);
225 DrawRectangleLines (0, 0, GetScreenWidth (), INFOBAR_HEIGHT, INFOBAR_COLOR_OUTLINE);
226
227 draw_fps (3, 1, INFOBAR_FONT_SIZE);
228
229 if (is_paused) {
230 Vector2 text_center = Vector2Scale (MeasureTextEx (
231 font, INFOBAR_TEXT_PAUSED,
232 INFOBAR_FONT_SIZE, TEXT_SPACING), .5);
233 text_center.y -= 1;
234 Vector2 infobar_center = { GetScreenWidth ()/2., INFOBAR_HEIGHT/2. };
235 DrawTextEx (font, INFOBAR_TEXT_PAUSED, Vector2Subtract (infobar_center, text_center),
236 INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
237 }
238
239 {
240 Vector2 head = { GetScreenWidth () - 3, 1 };
241 {
242 Vector2 text_size = MeasureTextEx (font, text_function, INFOBAR_FONT_SIZE, TEXT_SPACING);
243 head.x -= text_size.x;
244 if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
245 IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
246 Rectangle bounding_box = {
247 .x = head.x,
248 .y = head.y,
249 .width = text_size.x,
250 .height = text_size.y,
251 };
252 if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
253 text_function_is_editing = true;
254 else
255 text_function_is_editing = false;
256 sscanf (text_function, "%X", &cellular_automaton.function);
257 }
258
259 if (text_function_is_editing) {
260 if (IsKeyPressed (KEY_ENTER)) {
261 text_function_is_editing = false;
262 sscanf (text_function, "%X", &cellular_automaton.function);
263 } else {
264 int key;
265 while (text_function[0] == '0' &&
266 (key = toupper (GetCharPressed ()))) {
267 if ('0' <= key && key <= '9' ||
268 'A' <= key && key <= 'F') {
269 char last_char;
270 for (int i = 0; i < TEXT_SIZE_FUNCTION - 2; ++i) {
271 last_char = text_function[i];
272 text_function[i] = text_function[i + 1];
273 } text_function[TEXT_SIZE_FUNCTION - 2] = key;
274 }
275 }
276
277 if (IsKeyPressed (KEY_BACKSPACE)) {
278 if (IsKeyDown (KEY_CAPS_LOCK)) {
279 memset (text_function, '0', TEXT_SIZE_FUNCTION - 1);
280 } else {
281 char last_char = '0';
282 for (int i = 0; i < TEXT_SIZE_FUNCTION - 1; ++i) {
283 char tmp = text_function[i];
284 text_function[i] = last_char;
285 last_char = tmp;
286 }
287 }
288 }
289 }
290 }
291
292 DrawTextEx (font, text_function, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
293 text_function_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
294 }
295 head.x -= MeasureTextEx (font, " | 0x", INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
296 DrawTextEx (font, " | 0x", head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
297
298 bool resize = false;
299 if ((text_cols_is_editing || text_rows_is_editing) &&
300 IsKeyPressed (KEY_TAB)) {
301 text_cols_is_editing = !text_cols_is_editing;
302 text_rows_is_editing = !text_rows_is_editing;
303 }
304
305 {
306 Vector2 text_size = MeasureTextEx (font, text_rows, INFOBAR_FONT_SIZE, TEXT_SPACING);
307 head.x -= text_size.x;
308 if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
309 IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
310 Rectangle bounding_box = {
311 .x = head.x,
312 .y = head.y,
313 .width = text_size.x,
314 .height = text_size.y,
315 };
316 if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
317 text_rows_is_editing = true;
318 else {
319 if (text_rows_is_editing) {
320 resize = true;
321 text_rows_is_editing = false;
322 }
323 }
324 }
325
326 if (text_rows_is_editing) {
327 if (IsKeyPressed (KEY_ENTER)) {
328 text_rows_is_editing = false;
329 resize = true;
330 } else {
331 int key;
332 size_t len;
333 while ((len = strlen (text_rows)) < TEXT_SIZE_ROWS - 1 &&
334 (key = GetCharPressed ())) {
335 if ('0' <= key && key <= '9') {
336 if (text_rows[0] == '0') {
337 text_rows[0] = key;
338 } else {
339 text_rows[len] = key;
340 text_rows[len + 1] = '\0';
341 }
342 }
343 }
344
345 if (IsKeyPressed (KEY_BACKSPACE)) {
346 if (IsKeyDown (KEY_CAPS_LOCK) || len == 1) {
347 text_rows[0] = '0';
348 text_rows[1] = '\0';
349 } else {
350 text_rows[len - 1] = '\0';
351 }
352 }
353 }
354 }
355
356 DrawTextEx (font, text_rows, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
357 text_rows_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
358 }
359 head.x -= MeasureTextEx (font, "x", INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
360 DrawTextEx (font, "x", head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
361 {
362 Vector2 text_size = MeasureTextEx (font, text_cols, INFOBAR_FONT_SIZE, TEXT_SPACING);
363 head.x -= text_size.x + TEXT_SPACING;
364 if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
365 IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
366 Rectangle bounding_box = {
367 .x = head.x,
368 .y = head.y,
369 .width = text_size.x,
370 .height = text_size.y,
371 };
372 if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
373 text_cols_is_editing = true;
374 else {
375 if (text_cols_is_editing) {
376 resize = true;
377 text_cols_is_editing = false;
378 }
379 }
380 }
381
382 if (text_cols_is_editing) {
383 if (IsKeyPressed (KEY_ENTER)) {
384 text_cols_is_editing = false;
385 resize = true;
386 } else {
387 int key;
388 size_t len;
389 while ((len = strlen (text_cols)) < TEXT_SIZE_COLS - 1 &&
390 (key = GetCharPressed ())) {
391 if ('0' <= key && key <= '9') {
392 if (text_cols[0] == '0') {
393 text_cols[0] = key;
394 } else {
395 text_cols[len] = key;
396 text_cols[len + 1] = '\0';
397 }
398 }
399 }
400
401 if (IsKeyPressed (KEY_BACKSPACE)) {
402 if (IsKeyDown (KEY_CAPS_LOCK) || len == 1) {
403 text_cols[0] = '0';
404 text_cols[1] = '\0';
405 } else {
406 text_cols[len - 1] = '\0';
407 }
408 }
409 }
410 }
411
412 DrawTextEx (font, text_cols, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
413 text_cols_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
414 }
415
416 if (resize) {
417 uint32_t rows, cols;
418 sscanf (text_rows, "%u", &rows);
419 sscanf (text_cols, "%u", &cols);
420
421 if (rows != cellular_automaton.board.rows ||
422 cols != cellular_automaton.board.cols) {
423 if (is_paused)
424 cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
425 board_free (&cellular_automaton.board);
426 board_init (&cellular_automaton.board, rows, cols);
427 DYNAMIC_ARRAY_RESET (iterations);
428 DYNAMIC_ARRAY_APPEND_MANY (iterations,
429 cellular_automaton.board.bitarray.bytes,
430 cellular_automaton.board.bitarray.size);
431 iteration = 0;
432 is_paused = INITIAL_IS_PAUSED;
433 if (is_paused)
434 transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
435 }
436 }
437
438 const char *text_rest = TextFormat (
439 "%u/%u | %.2fs | ",
440 iteration + 1, iterations.count/cellular_automaton.board.bitarray.size,
441 tick_rate,
442 cellular_automaton.board.rows);
443 head.x -= MeasureTextEx (font, text_rest, INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
444 DrawTextEx (font, text_rest, head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
445 }
446}
447
448static void
449draw_heading (Vector2 *head, const char *heading)
450{
451 DrawTextEx (font, heading, *head,
452 HELP_FONT_SIZE_HEADING, TEXT_SPACING, HELP_COLOR_TEXT);
453 head->y += HELP_FONT_SIZE_HEADING;
454}
455
456static void
457draw_point (Vector2 *head, const char *lhs, const char *rhs)
458{
459 DrawTextEx (font, lhs, Vector2Add (*head, (Vector2) { 1*HELP_PADDING, 0 }),
460 HELP_FONT_SIZE_POINT, TEXT_SPACING, HELP_COLOR_TEXT);
461 DrawTextEx (font, rhs, Vector2Add (*head, (Vector2) { 7*HELP_FONT_SIZE_POINT, 0 }),
462 HELP_FONT_SIZE_POINT, TEXT_SPACING, HELP_COLOR_TEXT);
463 head->y += HELP_FONT_SIZE_POINT;
464}
465
466static void
467draw_help (void)
468{
469 Rectangle bounding_box = {
470 .x = .1*GetScreenWidth (),
471 .y = .1*GetScreenHeight () + INFOBAR_HEIGHT,
472 .width = .8*GetScreenWidth (),
473 .height = .8*(GetScreenHeight () - INFOBAR_HEIGHT),
474 };
475
476 DrawRectangleRec (bounding_box, HELP_COLOR_BACKGROUND);
477 DrawRectangleLinesEx (bounding_box, 1, HELP_COLOR_OUTLINE);
478
479 Vector2 head = { bounding_box.x + HELP_PADDING,
480 bounding_box.y + HELP_PADDING - 3 };
481 Vector2 offset_point = { 1*HELP_PADDING, 0 };
482 Vector2 offset_description = { 7*HELP_FONT_SIZE_POINT, 0 };
483 draw_heading (&head, "General:");
484 draw_point (&head, "- F1, H", "Show this help window.");
485 draw_point (&head, "- Q", "Quit any transient window.");
486 draw_point (&head, "- SPC", "Pause/Unpause.");
487 draw_point (&head, "- G", "Toggle grid display.");
488 draw_point (&head, "- C", "Clear automaton's state.");
489 draw_point (&head, "- LMB", "Make cell under cursor alive.");
490 draw_point (&head, "- RMB", "Make cell under cursor dead.");
491 draw_point (&head, "- SHIFT", "Make adjustments faster.");
492 head.y += HELP_FONT_SIZE_HEADING;
493 draw_heading (&head, "Running:");
494 draw_point (&head, "- SCROLL", "Adjust tick rate.");
495 head.y += HELP_FONT_SIZE_HEADING;
496 draw_heading (&head, "Paused:");
497 draw_point (&head, "- SCROLL", "Move between states.");
498 draw_point (&head, "- P, LEFT", "Move to previous state.");
499 draw_point (&head, "- N, RIGHT", "Move to next state.");
500}
501
502static void
503draw (void)
504{
505 BeginDrawing (); {
506 ClearBackground (BACKGROUND_COLOR);
507
508 draw_infobar ();
509
510 Rectangle rec = {
511 .x = board_bounding_box.x,
512 .y = board_bounding_box.y,
513 .width = cell_size,
514 .height = cell_size,
515 };
516 for (uint32_t row = 0; row < cellular_automaton.board.rows; ++row) {
517 for (uint32_t col = 0; col < cellular_automaton.board.cols; ++col) {
518 DrawRectangleRec (rec,
519 board_get (&cellular_automaton.board, row, col)
520 ? CELL_COLOR_ALIVE
521 : CELL_COLOR_DEAD);
522 rec.x += cell_size;
523 }
524 rec.x = board_bounding_box.x;
525 rec.y += cell_size;
526 }
527
528 DrawRectangleLinesEx (board_bounding_box, GRID_THICKNESS, GRID_COLOR);
529 if (is_grid_shown) {
530 Vector2 line_begin;
531 Vector2 line_end;
532
533 line_begin = (Vector2) {
534 board_bounding_box.x,
535 board_bounding_box.y + cell_size,
536 };
537 line_end = (Vector2) {
538 board_bounding_box.x + board_bounding_box.width,
539 board_bounding_box.y + cell_size,
540 };
541 for (uint32_t i = 0; i < cellular_automaton.board.rows - 1; ++i) {
542 DrawLineEx (line_begin, line_end, GRID_THICKNESS, GRID_COLOR);
543 line_begin.y += cell_size;
544 line_end.y += cell_size;
545 }
546
547 line_begin = (Vector2) {
548 board_bounding_box.x + cell_size,
549 board_bounding_box.y,
550 };
551 line_end = (Vector2) {
552 board_bounding_box.x + cell_size,
553 board_bounding_box.y + board_bounding_box.height,
554 };
555 for (uint32_t i = 0; i < cellular_automaton.board.cols - 1; ++i) {
556 DrawLineEx (line_begin, line_end, GRID_THICKNESS, GRID_COLOR);
557 line_begin.x += cell_size;
558 line_end.x += cell_size;
559 }
560 }
561
562 if (is_help_shown) draw_help ();
563 } EndDrawing ();
564}
565
566int
567main (int argc, char *argv[])
568{
569 init ();
570 while (!WindowShouldClose ()) {
571 handel_input ();
572 update ();
573 draw ();
574 } deinit ();
575 return EXIT_SUCCESS;
576}