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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
// src/main.c --- entry point and window management
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <raylib.h>
#include <raymath.h>
#include "config.h"
#include "dynamic-array.h"
#include "cellular-automaton.h"
static struct cellular_automaton cellular_automaton;
static float cell_size = 0;
static Rectangle board_bounding_box = {0};
static Font font;
static float tick_rate = INITIAL_TICK_RATE;
static float time_from_last_udpate = 0;
static bool is_paused = INITIAL_IS_PAUSED;
static bool is_grid_shown = INITIAL_IS_GRID_SHOWN;
static bool is_help_shown = INITIAL_IS_HELP_SHOWN;
static uint8_t *transient_iteration_bytes;
static size_t iteration;
static struct {
uint8_t *items;
uint32_t count;
uint32_t capacity;
} iterations = {0};
#define TEXT_SIZE_FUNCTION 9
static char text_function[TEXT_SIZE_FUNCTION];
static bool text_function_is_editing = false;
#define TEXT_SIZE_COLS 4
static char text_cols[TEXT_SIZE_COLS];
static bool text_cols_is_editing = false;
#define TEXT_SIZE_ROWS 4
static char text_rows[TEXT_SIZE_ROWS];
static bool text_rows_is_editing = false;
static void
init (void)
{
srand (time (NULL));
cellular_automaton_init (&cellular_automaton,
INITIAL_CELLULAR_AUTOMATON_FUNCTION,
INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS,
INITIAL_CELLULAR_AUTOMATON_BOARD_COLS);
if (INITIAL_IS_RANDOM) {
for (uint32_t row = 0; row < cellular_automaton.board.rows; ++row) {
for (uint32_t col = 0; col < cellular_automaton.board.cols; ++col) {
board_set (&cellular_automaton.board, row, col, rand () % 2);
}
}
}
DYNAMIC_ARRAY_APPEND_MANY (iterations,
cellular_automaton.board.bitarray.bytes,
cellular_automaton.board.bitarray.size);
iteration = 0;
if (INITIAL_IS_PAUSED)
transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
SetConfigFlags (FLAG_MSAA_4X_HINT |
FLAG_WINDOW_RESIZABLE |
FLAG_FULLSCREEN_MODE |
FLAG_VSYNC_HINT);
// InitWindow (INITIAL_WINDOW_WIDTH,
// INITIAL_WINDOW_HEIGHT,
// INITIAL_WINDOW_TITLE);
InitWindow (0, 0, INITIAL_WINDOW_TITLE);
font = GetFontDefault ();
snprintf (text_function, TEXT_SIZE_FUNCTION, "%08X", cellular_automaton.function);
snprintf (text_cols, TEXT_SIZE_COLS, "%u", cellular_automaton.board.cols);
snprintf (text_rows, TEXT_SIZE_ROWS, "%u", cellular_automaton.board.rows);
}
static void
deinit (void)
{
CloseWindow ();
cellular_automaton_free (&cellular_automaton);
}
static void
handel_input (void)
{
if (IsKeyPressed (KEY_SPACE)) {
if (is_paused) {
is_paused = false;
cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
memcpy (cellular_automaton.board.bitarray.bytes,
&iterations.items[iteration*cellular_automaton.board.bitarray.size],
cellular_automaton.board.bitarray.size);
iterations.count = (iteration + 1)*cellular_automaton.board.bitarray.size;
} else {
is_paused = true;
transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
memcpy (&iterations.items[iteration*cellular_automaton.board.bitarray.size],
cellular_automaton.board.bitarray.bytes,
cellular_automaton.board.bitarray.size);
}
}
if (IsKeyPressed (KEY_F1) || IsKeyPressed (KEY_H))
is_help_shown = !is_help_shown;
if (is_help_shown && IsKeyPressed (KEY_Q))
is_help_shown = false;
if (IsKeyPressed (KEY_G))
is_grid_shown = !is_grid_shown;
if (IsKeyPressed (KEY_C) && !text_function_is_editing) {
if (is_paused)
cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
memset (cellular_automaton.board.bitarray.bytes, 0,
cellular_automaton.board.bitarray.size);
DYNAMIC_ARRAY_RESET (iterations);
DYNAMIC_ARRAY_APPEND_MANY (iterations,
cellular_automaton.board.bitarray.bytes,
cellular_automaton.board.bitarray.size);
iteration = 0;
is_paused = AFTER_CLEAR_IS_PAUSED;
if (is_paused)
transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
}
if (is_paused) {
int change = 0;
change += GetMouseWheelMove () > 0 ||
IsKeyPressed (KEY_N) || IsKeyPressed (KEY_RIGHT) ||
IsKeyDown (KEY_LEFT_SHIFT) && (IsKeyDown (KEY_N) ||
IsKeyDown (KEY_RIGHT));
change -= GetMouseWheelMove () < 0 ||
IsKeyPressed (KEY_P) || IsKeyPressed (KEY_LEFT) ||
IsKeyDown (KEY_LEFT_SHIFT) && (IsKeyDown (KEY_P) ||
IsKeyDown (KEY_LEFT));
if (change < 0 && iteration >= -change ||
change > 0 && (iteration + change <=
(iterations.count - 1) /
cellular_automaton.board.bitarray.size))
iteration += change;
cellular_automaton.board.bitarray.bytes =
&iterations.items[iteration*cellular_automaton.board.bitarray.size];
} else {
tick_rate = fmaxf (tick_rate +
TICK_RATE_CHANGE_SPEED*GetMouseWheelMove () *
(1 + (TICK_RATE_CHANGE_SPEED_MULTIPLIER - 1) *
IsKeyDown (KEY_LEFT_SHIFT)), 0);
}
enum cell_action {
CELL_ACTION_NONE = 0b00,
CELL_ACTION_KILL = 0b01,
CELL_ACTION_RESURRECT = 0b10,
} cell_action = CELL_ACTION_NONE;
if (!text_function_is_editing &&
!text_cols_is_editing &&
!text_rows_is_editing) {
if (IsMouseButtonDown (MOUSE_LEFT_BUTTON))
cell_action = CELL_ACTION_RESURRECT;
else if (IsMouseButtonDown (MOUSE_RIGHT_BUTTON))
cell_action = CELL_ACTION_KILL;
}
if (cell_action) {
Vector2 mouse = GetMousePosition ();
if (CheckCollisionPointRec (mouse, board_bounding_box)) {
uint32_t row = (mouse.y - board_bounding_box.y)/cell_size;
uint32_t col = (mouse.x - board_bounding_box.x)/cell_size;
board_set (&cellular_automaton.board, row, col, cell_action >> 1 & 1);
}
}
}
static void
update (void)
{
cell_size = fminf (
(GetScreenHeight () - INFOBAR_HEIGHT)/(float) cellular_automaton.board.rows,
(GetScreenWidth () )/(float) cellular_automaton.board.cols);
board_bounding_box.width = cell_size*cellular_automaton.board.cols;
board_bounding_box.height = cell_size*cellular_automaton.board.rows;
board_bounding_box.x = (GetScreenWidth () - board_bounding_box.width )/2.;
board_bounding_box.y = (GetScreenHeight () - board_bounding_box.height + INFOBAR_HEIGHT)/2.;
time_from_last_udpate += GetFrameTime ();
if (!is_paused && time_from_last_udpate >= tick_rate) {
time_from_last_udpate = 0;
cellular_automaton_progress (&cellular_automaton);
DYNAMIC_ARRAY_APPEND_MANY (iterations,
cellular_automaton.board.bitarray.bytes,
cellular_automaton.board.bitarray.size);
iteration += 1;
}
}
static void
draw_fps (int x, int y, float font_size)
{
DrawTextEx (font, TextFormat ("%2i FPS", GetFPS ()), (Vector2) { x, y },
font_size, TEXT_SPACING, INFOBAR_COLOR_TEXT);
}
static void
draw_infobar (void)
{
DrawRectangle (0, 0, GetScreenWidth (), INFOBAR_HEIGHT, INFOBAR_COLOR_BACKGROUND);
DrawRectangleLines (0, 0, GetScreenWidth (), INFOBAR_HEIGHT, INFOBAR_COLOR_OUTLINE);
draw_fps (3, 1, INFOBAR_FONT_SIZE);
if (is_paused) {
Vector2 text_center = Vector2Scale (MeasureTextEx (
font, INFOBAR_TEXT_PAUSED,
INFOBAR_FONT_SIZE, TEXT_SPACING), .5);
text_center.y -= 1;
Vector2 infobar_center = { GetScreenWidth ()/2., INFOBAR_HEIGHT/2. };
DrawTextEx (font, INFOBAR_TEXT_PAUSED, Vector2Subtract (infobar_center, text_center),
INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
}
{
Vector2 head = { GetScreenWidth () - 3, 1 };
{
Vector2 text_size = MeasureTextEx (font, text_function, INFOBAR_FONT_SIZE, TEXT_SPACING);
head.x -= text_size.x;
if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
Rectangle bounding_box = {
.x = head.x,
.y = head.y,
.width = text_size.x,
.height = text_size.y,
};
if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
text_function_is_editing = true;
else
text_function_is_editing = false;
sscanf (text_function, "%X", &cellular_automaton.function);
}
if (text_function_is_editing) {
if (IsKeyPressed (KEY_ENTER)) {
text_function_is_editing = false;
sscanf (text_function, "%X", &cellular_automaton.function);
} else {
int key;
while (text_function[0] == '0' &&
(key = toupper (GetCharPressed ()))) {
if ('0' <= key && key <= '9' ||
'A' <= key && key <= 'F') {
char last_char;
for (int i = 0; i < TEXT_SIZE_FUNCTION - 2; ++i) {
last_char = text_function[i];
text_function[i] = text_function[i + 1];
} text_function[TEXT_SIZE_FUNCTION - 2] = key;
}
}
if (IsKeyPressed (KEY_BACKSPACE)) {
if (IsKeyDown (KEY_CAPS_LOCK)) {
memset (text_function, '0', TEXT_SIZE_FUNCTION - 1);
} else {
char last_char = '0';
for (int i = 0; i < TEXT_SIZE_FUNCTION - 1; ++i) {
char tmp = text_function[i];
text_function[i] = last_char;
last_char = tmp;
}
}
}
}
}
DrawTextEx (font, text_function, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
text_function_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
}
head.x -= MeasureTextEx (font, " | 0x", INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
DrawTextEx (font, " | 0x", head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
bool resize = false;
if ((text_cols_is_editing || text_rows_is_editing) &&
IsKeyPressed (KEY_TAB)) {
text_cols_is_editing = !text_cols_is_editing;
text_rows_is_editing = !text_rows_is_editing;
}
{
Vector2 text_size = MeasureTextEx (font, text_rows, INFOBAR_FONT_SIZE, TEXT_SPACING);
head.x -= text_size.x;
if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
Rectangle bounding_box = {
.x = head.x,
.y = head.y,
.width = text_size.x,
.height = text_size.y,
};
if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
text_rows_is_editing = true;
else {
if (text_rows_is_editing) {
resize = true;
text_rows_is_editing = false;
}
}
}
if (text_rows_is_editing) {
if (IsKeyPressed (KEY_ENTER)) {
text_rows_is_editing = false;
resize = true;
} else {
int key;
size_t len;
while ((len = strlen (text_rows)) < TEXT_SIZE_ROWS - 1 &&
(key = GetCharPressed ())) {
if ('0' <= key && key <= '9') {
if (text_rows[0] == '0') {
text_rows[0] = key;
} else {
text_rows[len] = key;
text_rows[len + 1] = '\0';
}
}
}
if (IsKeyPressed (KEY_BACKSPACE)) {
if (IsKeyDown (KEY_CAPS_LOCK) || len == 1) {
text_rows[0] = '0';
text_rows[1] = '\0';
} else {
text_rows[len - 1] = '\0';
}
}
}
}
DrawTextEx (font, text_rows, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
text_rows_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
}
head.x -= MeasureTextEx (font, "x", INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
DrawTextEx (font, "x", head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
{
Vector2 text_size = MeasureTextEx (font, text_cols, INFOBAR_FONT_SIZE, TEXT_SPACING);
head.x -= text_size.x + TEXT_SPACING;
if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT) ||
IsMouseButtonPressed (MOUSE_BUTTON_RIGHT)) {
Rectangle bounding_box = {
.x = head.x,
.y = head.y,
.width = text_size.x,
.height = text_size.y,
};
if (CheckCollisionPointRec (GetMousePosition (), bounding_box))
text_cols_is_editing = true;
else {
if (text_cols_is_editing) {
resize = true;
text_cols_is_editing = false;
}
}
}
if (text_cols_is_editing) {
if (IsKeyPressed (KEY_ENTER)) {
text_cols_is_editing = false;
resize = true;
} else {
int key;
size_t len;
while ((len = strlen (text_cols)) < TEXT_SIZE_COLS - 1 &&
(key = GetCharPressed ())) {
if ('0' <= key && key <= '9') {
if (text_cols[0] == '0') {
text_cols[0] = key;
} else {
text_cols[len] = key;
text_cols[len + 1] = '\0';
}
}
}
if (IsKeyPressed (KEY_BACKSPACE)) {
if (IsKeyDown (KEY_CAPS_LOCK) || len == 1) {
text_cols[0] = '0';
text_cols[1] = '\0';
} else {
text_cols[len - 1] = '\0';
}
}
}
}
DrawTextEx (font, text_cols, head, INFOBAR_FONT_SIZE, TEXT_SPACING,
text_cols_is_editing ? INFOBAR_COLOR_TEXT_EDITING : INFOBAR_COLOR_TEXT);
}
if (resize) {
uint32_t rows, cols;
sscanf (text_rows, "%u", &rows);
sscanf (text_cols, "%u", &cols);
if (rows != cellular_automaton.board.rows ||
cols != cellular_automaton.board.cols) {
if (is_paused)
cellular_automaton.board.bitarray.bytes = transient_iteration_bytes;
board_free (&cellular_automaton.board);
board_init (&cellular_automaton.board, rows, cols);
DYNAMIC_ARRAY_RESET (iterations);
DYNAMIC_ARRAY_APPEND_MANY (iterations,
cellular_automaton.board.bitarray.bytes,
cellular_automaton.board.bitarray.size);
iteration = 0;
is_paused = INITIAL_IS_PAUSED;
if (is_paused)
transient_iteration_bytes = cellular_automaton.board.bitarray.bytes;
}
}
const char *text_rest = TextFormat (
"%u/%u | %.2fs | ",
iteration + 1, iterations.count/cellular_automaton.board.bitarray.size,
tick_rate,
cellular_automaton.board.rows);
head.x -= MeasureTextEx (font, text_rest, INFOBAR_FONT_SIZE, TEXT_SPACING).x + TEXT_SPACING;
DrawTextEx (font, text_rest, head, INFOBAR_FONT_SIZE, TEXT_SPACING, INFOBAR_COLOR_TEXT);
}
}
static void
draw_heading (Vector2 *head, const char *heading)
{
DrawTextEx (font, heading, *head,
HELP_FONT_SIZE_HEADING, TEXT_SPACING, HELP_COLOR_TEXT);
head->y += HELP_FONT_SIZE_HEADING;
}
static void
draw_point (Vector2 *head, const char *lhs, const char *rhs)
{
DrawTextEx (font, lhs, Vector2Add (*head, (Vector2) { 1*HELP_PADDING, 0 }),
HELP_FONT_SIZE_POINT, TEXT_SPACING, HELP_COLOR_TEXT);
DrawTextEx (font, rhs, Vector2Add (*head, (Vector2) { 7*HELP_FONT_SIZE_POINT, 0 }),
HELP_FONT_SIZE_POINT, TEXT_SPACING, HELP_COLOR_TEXT);
head->y += HELP_FONT_SIZE_POINT;
}
static void
draw_help (void)
{
Rectangle bounding_box = {
.x = .1*GetScreenWidth (),
.y = .1*GetScreenHeight () + INFOBAR_HEIGHT,
.width = .8*GetScreenWidth (),
.height = .8*(GetScreenHeight () - INFOBAR_HEIGHT),
};
DrawRectangleRec (bounding_box, HELP_COLOR_BACKGROUND);
DrawRectangleLinesEx (bounding_box, 1, HELP_COLOR_OUTLINE);
Vector2 head = { bounding_box.x + HELP_PADDING,
bounding_box.y + HELP_PADDING - 3 };
Vector2 offset_point = { 1*HELP_PADDING, 0 };
Vector2 offset_description = { 7*HELP_FONT_SIZE_POINT, 0 };
draw_heading (&head, "General:");
draw_point (&head, "- F1, H", "Show this help window.");
draw_point (&head, "- Q", "Quit any transient window.");
draw_point (&head, "- SPC", "Pause/Unpause.");
draw_point (&head, "- G", "Toggle grid display.");
draw_point (&head, "- C", "Clear automaton's state.");
draw_point (&head, "- LMB", "Make cell under cursor alive.");
draw_point (&head, "- RMB", "Make cell under cursor dead.");
draw_point (&head, "- SHIFT", "Make adjustments faster.");
head.y += HELP_FONT_SIZE_HEADING;
draw_heading (&head, "Running:");
draw_point (&head, "- SCROLL", "Adjust tick rate.");
head.y += HELP_FONT_SIZE_HEADING;
draw_heading (&head, "Paused:");
draw_point (&head, "- SCROLL", "Move between states.");
draw_point (&head, "- P, LEFT", "Move to previous state.");
draw_point (&head, "- N, RIGHT", "Move to next state.");
}
static void
draw (void)
{
BeginDrawing (); {
ClearBackground (BACKGROUND_COLOR);
draw_infobar ();
Rectangle rec = {
.x = board_bounding_box.x,
.y = board_bounding_box.y,
.width = cell_size,
.height = cell_size,
};
for (uint32_t row = 0; row < cellular_automaton.board.rows; ++row) {
for (uint32_t col = 0; col < cellular_automaton.board.cols; ++col) {
DrawRectangleRec (rec,
board_get (&cellular_automaton.board, row, col)
? CELL_COLOR_ALIVE
: CELL_COLOR_DEAD);
rec.x += cell_size;
}
rec.x = board_bounding_box.x;
rec.y += cell_size;
}
DrawRectangleLinesEx (board_bounding_box, GRID_THICKNESS, GRID_COLOR);
if (is_grid_shown) {
Vector2 line_begin;
Vector2 line_end;
line_begin = (Vector2) {
board_bounding_box.x,
board_bounding_box.y + cell_size,
};
line_end = (Vector2) {
board_bounding_box.x + board_bounding_box.width,
board_bounding_box.y + cell_size,
};
for (uint32_t i = 0; i < cellular_automaton.board.rows - 1; ++i) {
DrawLineEx (line_begin, line_end, GRID_THICKNESS, GRID_COLOR);
line_begin.y += cell_size;
line_end.y += cell_size;
}
line_begin = (Vector2) {
board_bounding_box.x + cell_size,
board_bounding_box.y,
};
line_end = (Vector2) {
board_bounding_box.x + cell_size,
board_bounding_box.y + board_bounding_box.height,
};
for (uint32_t i = 0; i < cellular_automaton.board.cols - 1; ++i) {
DrawLineEx (line_begin, line_end, GRID_THICKNESS, GRID_COLOR);
line_begin.x += cell_size;
line_end.x += cell_size;
}
}
if (is_help_shown) draw_help ();
} EndDrawing ();
}
int
main (int argc, char *argv[])
{
init ();
while (!WindowShouldClose ()) {
handel_input ();
update ();
draw ();
} deinit ();
return EXIT_SUCCESS;
}
|