diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bitarray.c | 33 | ||||
| -rw-r--r-- | src/bitarray.h | 18 | ||||
| -rw-r--r-- | src/board.c | 36 | ||||
| -rw-r--r-- | src/board.h | 19 | ||||
| -rw-r--r-- | src/cellular-automaton.c | 49 | ||||
| -rw-r--r-- | src/cellular-automaton.h | 19 | ||||
| -rw-r--r-- | src/config.h | 52 | ||||
| -rw-r--r-- | src/dynamic-array.h | 59 | ||||
| -rw-r--r-- | src/main.c | 576 | ||||
| -rw-r--r-- | src/utils.h | 65 |
10 files changed, 926 insertions, 0 deletions
diff --git a/src/bitarray.c b/src/bitarray.c new file mode 100644 index 0000000..5d7c1e3 --- /dev/null +++ b/src/bitarray.c | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #include "board.h" | ||
| 2 | #include "utils.h" | ||
| 3 | |||
| 4 | void | ||
| 5 | bitarray_init (struct bitarray *bitarray, size_t bit_count) | ||
| 6 | { | ||
| 7 | bitarray->size = (bit_count + 7)/8; | ||
| 8 | bitarray->bytes = calloc (bitarray->size, 1); | ||
| 9 | } | ||
| 10 | |||
| 11 | void | ||
| 12 | bitarray_set (struct bitarray *bitarray, size_t index, bool value) | ||
| 13 | { | ||
| 14 | ASSERT (bitarray); | ||
| 15 | ASSERT (bitarray->bytes); | ||
| 16 | ASSERT (index >> 3 < bitarray->size); | ||
| 17 | setbit (bitarray->bytes, index, value); | ||
| 18 | } | ||
| 19 | |||
| 20 | bool | ||
| 21 | bitarray_get (struct bitarray *bitarray, size_t index) | ||
| 22 | { | ||
| 23 | ASSERT (bitarray); | ||
| 24 | ASSERT (bitarray->bytes); | ||
| 25 | ASSERT (index >> 3 < bitarray->size); | ||
| 26 | return getbit (bitarray->bytes, index); | ||
| 27 | } | ||
| 28 | |||
| 29 | void | ||
| 30 | bitarray_free (struct bitarray *bitarray) | ||
| 31 | { | ||
| 32 | FREE (bitarray->bytes); | ||
| 33 | } | ||
diff --git a/src/bitarray.h b/src/bitarray.h new file mode 100644 index 0000000..934b84e --- /dev/null +++ b/src/bitarray.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef INCLUDED_BITARRAY_H | ||
| 2 | #define INCLUDED_BITARRAY_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <inttypes.h> | ||
| 7 | |||
| 8 | struct bitarray { | ||
| 9 | uint8_t *bytes; | ||
| 10 | size_t size; | ||
| 11 | }; | ||
| 12 | |||
| 13 | void bitarray_init (struct bitarray *, size_t bit_count); | ||
| 14 | void bitarray_free (struct bitarray *); | ||
| 15 | void bitarray_set (struct bitarray *, size_t index, bool); | ||
| 16 | bool bitarray_get (struct bitarray *, size_t index); | ||
| 17 | |||
| 18 | #endif | ||
diff --git a/src/board.c b/src/board.c new file mode 100644 index 0000000..fe798f7 --- /dev/null +++ b/src/board.c | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | #include "board.h" | ||
| 2 | #include "utils.h" | ||
| 3 | |||
| 4 | void | ||
| 5 | board_init (struct board *board, uint32_t rows, uint32_t cols) | ||
| 6 | { | ||
| 7 | bitarray_init (&board->bitarray, rows*cols); | ||
| 8 | board->rows = rows; | ||
| 9 | board->cols = cols; | ||
| 10 | } | ||
| 11 | |||
| 12 | void | ||
| 13 | board_set (struct board *board, int32_t row, int32_t col, bool value) | ||
| 14 | { | ||
| 15 | ASSERT (board); | ||
| 16 | |||
| 17 | row = MOD (row, (int32_t) board->rows); | ||
| 18 | col = MOD (col, (int32_t) board->cols); | ||
| 19 | bitarray_set (&board->bitarray, row*board->cols + col, value); | ||
| 20 | } | ||
| 21 | |||
| 22 | bool | ||
| 23 | board_get (struct board *board, int32_t row, int32_t col) | ||
| 24 | { | ||
| 25 | ASSERT (board); | ||
| 26 | |||
| 27 | row = MOD (row, (int32_t) board->rows); | ||
| 28 | col = MOD (col, (int32_t) board->cols); | ||
| 29 | return bitarray_get (&board->bitarray, row*board->cols + col); | ||
| 30 | } | ||
| 31 | |||
| 32 | void | ||
| 33 | board_free (struct board *board) | ||
| 34 | { | ||
| 35 | bitarray_free (&board->bitarray); | ||
| 36 | } | ||
diff --git a/src/board.h b/src/board.h new file mode 100644 index 0000000..7ce113e --- /dev/null +++ b/src/board.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #ifndef INCLUDED_BOARD_H | ||
| 2 | #define INCLUDED_BOARD_H | ||
| 3 | |||
| 4 | #include <inttypes.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include "bitarray.h" | ||
| 7 | |||
| 8 | struct board { | ||
| 9 | struct bitarray bitarray; | ||
| 10 | uint32_t rows; | ||
| 11 | uint32_t cols; | ||
| 12 | }; | ||
| 13 | |||
| 14 | void board_init (struct board *, uint32_t rows, uint32_t cols); | ||
| 15 | void board_free (struct board *); | ||
| 16 | void board_set (struct board *, int32_t row, int32_t col, bool); | ||
| 17 | bool board_get (struct board *, int32_t row, int32_t col); | ||
| 18 | |||
| 19 | #endif | ||
diff --git a/src/cellular-automaton.c b/src/cellular-automaton.c new file mode 100644 index 0000000..183684a --- /dev/null +++ b/src/cellular-automaton.c | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include "cellular-automaton.h" | ||
| 3 | #include "utils.h" | ||
| 4 | |||
| 5 | void | ||
| 6 | cellular_automaton_init (struct cellular_automaton *cellular_automaton, | ||
| 7 | uint32_t function, uint32_t rows, uint32_t cols) | ||
| 8 | { | ||
| 9 | board_init (&cellular_automaton->board, rows, cols); | ||
| 10 | cellular_automaton->back_board_bitarray = | ||
| 11 | malloc (cellular_automaton->board.bitarray.size); | ||
| 12 | cellular_automaton->function = function; | ||
| 13 | } | ||
| 14 | |||
| 15 | void | ||
| 16 | cellular_automaton_progress (struct cellular_automaton *cellular_automaton) | ||
| 17 | { | ||
| 18 | // uint32_t alive = 0; | ||
| 19 | for (int32_t row = 0; row < cellular_automaton->board.rows; ++row) { | ||
| 20 | for (int32_t col = 0; col < cellular_automaton->board.cols; ++col) { | ||
| 21 | struct board *board = &cellular_automaton->board; | ||
| 22 | uint8_t state = | ||
| 23 | board_get (board, row - 1, col + 0) << 3 | | ||
| 24 | board_get (board, row + 0, col - 1) << 1 | | ||
| 25 | board_get (board, row + 0, col + 0) << 0 | | ||
| 26 | board_get (board, row + 0, col + 1) << 2 | | ||
| 27 | board_get (board, row + 1, col + 0) << 4; | ||
| 28 | |||
| 29 | // if (board_get (board, row, col)) { | ||
| 30 | // // printf ("%d/%d, ", row, col); | ||
| 31 | // ++alive; | ||
| 32 | // } | ||
| 33 | |||
| 34 | setbit (cellular_automaton->back_board_bitarray, | ||
| 35 | row*board->cols + col, | ||
| 36 | cellular_automaton->function >> state & 1); | ||
| 37 | } | ||
| 38 | } // printf ("%u\n", alive); | ||
| 39 | |||
| 40 | SWAP (cellular_automaton->back_board_bitarray, | ||
| 41 | cellular_automaton->board.bitarray.bytes); | ||
| 42 | } | ||
| 43 | |||
| 44 | void | ||
| 45 | cellular_automaton_free (struct cellular_automaton *cellular_automaton) | ||
| 46 | { | ||
| 47 | board_free (&cellular_automaton->board); | ||
| 48 | free (cellular_automaton->back_board_bitarray); | ||
| 49 | } | ||
diff --git a/src/cellular-automaton.h b/src/cellular-automaton.h new file mode 100644 index 0000000..8be8951 --- /dev/null +++ b/src/cellular-automaton.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #ifndef INCLUDED_CELLULAR_AUTOMATON_H | ||
| 2 | #define INCLUDED_CELLULAR_AUTOMATON_H | ||
| 3 | |||
| 4 | #include <inttypes.h> | ||
| 5 | #include "board.h" | ||
| 6 | |||
| 7 | struct cellular_automaton { | ||
| 8 | struct board board; | ||
| 9 | uint8_t *back_board_bitarray; | ||
| 10 | uint32_t function; | ||
| 11 | }; | ||
| 12 | |||
| 13 | void cellular_automaton_init (struct cellular_automaton *, | ||
| 14 | uint32_t function, | ||
| 15 | uint32_t rows, uint32_t cols); | ||
| 16 | void cellular_automaton_free (struct cellular_automaton *); | ||
| 17 | void cellular_automaton_progress (struct cellular_automaton *); | ||
| 18 | |||
| 19 | #endif | ||
diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..e93a9d4 --- /dev/null +++ b/src/config.h | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | #ifndef INCLUDED_CONFIG_H | ||
| 2 | #define INCLUDED_CONFIG_H | ||
| 3 | |||
| 4 | #define INITIAL_WINDOW_WIDTH 900 | ||
| 5 | #define INITIAL_WINDOW_HEIGHT 600 | ||
| 6 | #define INITIAL_WINDOW_TITLE "Двумерный клеточный автомат с окрестностью фон Неймана" | ||
| 7 | |||
| 8 | #define INITIAL_CELLULAR_AUTOMATON_FUNCTION ((uint32_t) 0x7907E8) | ||
| 9 | #define INITIAL_CELLULAR_AUTOMATON_BOARD_COLS 735 | ||
| 10 | #define INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS 400 | ||
| 11 | // #define INITIAL_CELLULAR_AUTOMATON_BOARD_COLS 620 | ||
| 12 | // #define INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS 400 | ||
| 13 | |||
| 14 | #define INITIAL_IS_RANDOM true | ||
| 15 | |||
| 16 | #define INITIAL_TICK_RATE 0 //1./2 | ||
| 17 | #define TICK_RATE_CHANGE_SPEED 1./100 | ||
| 18 | #define TICK_RATE_CHANGE_SPEED_MULTIPLIER 10 | ||
| 19 | |||
| 20 | #define BACKGROUND_COLOR BLACK | ||
| 21 | |||
| 22 | #define INITIAL_IS_PAUSED true | ||
| 23 | #define INITIAL_IS_GRID_SHOWN false | ||
| 24 | #define INITIAL_IS_HELP_SHOWN true | ||
| 25 | |||
| 26 | #define AFTER_CLEAR_IS_PAUSED true | ||
| 27 | |||
| 28 | #define GRID_THICKNESS 1 | ||
| 29 | #define GRID_COLOR GRAY | ||
| 30 | |||
| 31 | #define CELL_COLOR_ALIVE WHITE | ||
| 32 | #define CELL_COLOR_DEAD BACKGROUND_COLOR | ||
| 33 | |||
| 34 | #define TEXT_SPACING 2 | ||
| 35 | |||
| 36 | #define INFOBAR_FONT_SIZE 20 | ||
| 37 | #define INFOBAR_HEIGHT 20 | ||
| 38 | #define INFOBAR_COLOR_TEXT WHITE | ||
| 39 | #define INFOBAR_COLOR_TEXT_EDITING RED | ||
| 40 | #define INFOBAR_COLOR_BACKGROUND DARKBLUE | ||
| 41 | #define INFOBAR_COLOR_OUTLINE BLUE | ||
| 42 | #define INFOBAR_TEXT_PAUSED "*PAUSED*" | ||
| 43 | |||
| 44 | #define HELP_TITLE "HELP" | ||
| 45 | #define HELP_FONT_SIZE_HEADING 28 | ||
| 46 | #define HELP_FONT_SIZE_POINT 22 | ||
| 47 | #define HELP_PADDING 20 | ||
| 48 | #define HELP_COLOR_TEXT ((Color) { 0xDF, 0x00, 0x00, 0xFF }) | ||
| 49 | #define HELP_COLOR_BACKGROUND ((Color) { 0x2F, 0x00, 0x00, 0xDF }) | ||
| 50 | #define HELP_COLOR_OUTLINE ((Color) { 0xFF, 0x00, 0x00, 0xFF }) | ||
| 51 | |||
| 52 | #endif | ||
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..726fcce --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ | ||
| 13 | if ((DYNAMIC_ARRAY).capacity == 0) \ | ||
| 14 | (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ | ||
| 16 | (DYNAMIC_ARRAY).capacity *= 2; \ | ||
| 17 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 18 | (DYNAMIC_ARRAY).capacity * \ | ||
| 19 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 20 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ | ||
| 25 | ({ \ | ||
| 26 | (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ | ||
| 27 | (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ | ||
| 28 | (DYNAMIC_ARRAY).capacity * \ | ||
| 29 | sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 30 | ASSERT((DYNAMIC_ARRAY).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ | ||
| 37 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ | ||
| 43 | memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ | ||
| 44 | (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ | ||
| 45 | (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ | ||
| 46 | }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ | ||
| 49 | (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] | ||
| 50 | |||
| 51 | #define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) | ||
| 52 | |||
| 53 | #define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) | ||
| 54 | |||
| 55 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ | ||
| 56 | typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ | ||
| 57 | ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) | ||
| 58 | |||
| 59 | #endif | ||
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 | |||
| 17 | static struct cellular_automaton cellular_automaton; | ||
| 18 | |||
| 19 | static float cell_size = 0; | ||
| 20 | static Rectangle board_bounding_box = {0}; | ||
| 21 | static Font font; | ||
| 22 | |||
| 23 | static float tick_rate = INITIAL_TICK_RATE; | ||
| 24 | static float time_from_last_udpate = 0; | ||
| 25 | |||
| 26 | static bool is_paused = INITIAL_IS_PAUSED; | ||
| 27 | static bool is_grid_shown = INITIAL_IS_GRID_SHOWN; | ||
| 28 | static bool is_help_shown = INITIAL_IS_HELP_SHOWN; | ||
| 29 | |||
| 30 | static uint8_t *transient_iteration_bytes; | ||
| 31 | static size_t iteration; | ||
| 32 | static struct { | ||
| 33 | uint8_t *items; | ||
| 34 | uint32_t count; | ||
| 35 | uint32_t capacity; | ||
| 36 | } iterations = {0}; | ||
| 37 | |||
| 38 | #define TEXT_SIZE_FUNCTION 9 | ||
| 39 | static char text_function[TEXT_SIZE_FUNCTION]; | ||
| 40 | static bool text_function_is_editing = false; | ||
| 41 | |||
| 42 | #define TEXT_SIZE_COLS 4 | ||
| 43 | static char text_cols[TEXT_SIZE_COLS]; | ||
| 44 | static bool text_cols_is_editing = false; | ||
| 45 | |||
| 46 | #define TEXT_SIZE_ROWS 4 | ||
| 47 | static char text_rows[TEXT_SIZE_ROWS]; | ||
| 48 | static bool text_rows_is_editing = false; | ||
| 49 | |||
| 50 | static void | ||
| 51 | init (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 | |||
| 91 | static void | ||
| 92 | deinit (void) | ||
| 93 | { | ||
| 94 | CloseWindow (); | ||
| 95 | cellular_automaton_free (&cellular_automaton); | ||
| 96 | } | ||
| 97 | |||
| 98 | static void | ||
| 99 | handel_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 | |||
| 191 | static void | ||
| 192 | update (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 | |||
| 214 | static void | ||
| 215 | draw_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 | |||
| 221 | static void | ||
| 222 | draw_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 | |||
| 448 | static void | ||
| 449 | draw_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 | |||
| 456 | static void | ||
| 457 | draw_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 | |||
| 466 | static void | ||
| 467 | draw_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 | |||
| 502 | static void | ||
| 503 | draw (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 | |||
| 566 | int | ||
| 567 | main (int argc, char *argv[]) | ||
| 568 | { | ||
| 569 | init (); | ||
| 570 | while (!WindowShouldClose ()) { | ||
| 571 | handel_input (); | ||
| 572 | update (); | ||
| 573 | draw (); | ||
| 574 | } deinit (); | ||
| 575 | return EXIT_SUCCESS; | ||
| 576 | } | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..d5a1242 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <inttypes.h> | ||
| 8 | #include <assert.h> | ||
| 9 | |||
| 10 | #define MALLOC malloc | ||
| 11 | #define REALLOC realloc | ||
| 12 | #ifdef DEBUG | ||
| 13 | #define FREE(PTR) ({free (PTR); PTR = NULL;}) | ||
| 14 | #define ASSERT assert | ||
| 15 | #else | ||
| 16 | #define FREE free | ||
| 17 | #define ASSERT | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #define CEIL_INT_DIV(A, B) ((A + (B - 1))/B) | ||
| 21 | // #define CEIL_INT_DIV(A, B) (1 + (A - 1)/B) // A != 0 | ||
| 22 | |||
| 23 | #define MOD(A, B) (((A % B) + B) % B) | ||
| 24 | |||
| 25 | #define SWAP(A, B) \ | ||
| 26 | ({ \ | ||
| 27 | typeof (A) tmp = A; \ | ||
| 28 | A = B; \ | ||
| 29 | B = tmp; \ | ||
| 30 | }) | ||
| 31 | |||
| 32 | #define MIN(A, B) \ | ||
| 33 | ({ \ | ||
| 34 | typeof (A) a = A; \ | ||
| 35 | typeof (B) b = B; \ | ||
| 36 | a < b ? a : b; \ | ||
| 37 | }) \ | ||
| 38 | |||
| 39 | inline void | ||
| 40 | setbit (uint8_t *bytes, size_t index, bool value) | ||
| 41 | { | ||
| 42 | uint8_t *byte = &bytes[index >> 3]; | ||
| 43 | uint8_t offset = index & ((1 << 3) - 1); | ||
| 44 | uint8_t mask = ~(1 << offset); | ||
| 45 | *byte = *byte & mask | value << offset; | ||
| 46 | } | ||
| 47 | |||
| 48 | inline bool | ||
| 49 | getbit (uint8_t *bytes, size_t index) | ||
| 50 | { | ||
| 51 | uint8_t byte = bytes[index >> 3]; | ||
| 52 | uint8_t offset = index & ((1 << 3) - 1); | ||
| 53 | uint8_t mask = 1 << offset; | ||
| 54 | return (byte & mask) >> offset; | ||
| 55 | } | ||
| 56 | |||
| 57 | inline void | ||
| 58 | prompt (char *label, char *fmt, void *where) | ||
| 59 | { | ||
| 60 | printf ("%s: ", label); | ||
| 61 | scanf (fmt, where); | ||
| 62 | while (getchar () != '\n'); | ||
| 63 | } | ||
| 64 | |||
| 65 | #endif | ||
