From 08fcd58893b5ea754ebddbf63b2745dde34d4830 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 05:11:35 +0300 Subject: publish --- src/bitarray.c | 33 +++ src/bitarray.h | 18 ++ src/board.c | 36 +++ src/board.h | 19 ++ src/cellular-automaton.c | 49 ++++ src/cellular-automaton.h | 19 ++ src/config.h | 52 +++++ src/dynamic-array.h | 59 +++++ src/main.c | 576 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 65 ++++++ 10 files changed, 926 insertions(+) create mode 100644 src/bitarray.c create mode 100644 src/bitarray.h create mode 100644 src/board.c create mode 100644 src/board.h create mode 100644 src/cellular-automaton.c create mode 100644 src/cellular-automaton.h create mode 100644 src/config.h create mode 100644 src/dynamic-array.h create mode 100644 src/main.c create mode 100644 src/utils.h (limited to 'src') 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 @@ +#include "board.h" +#include "utils.h" + +void +bitarray_init (struct bitarray *bitarray, size_t bit_count) +{ + bitarray->size = (bit_count + 7)/8; + bitarray->bytes = calloc (bitarray->size, 1); +} + +void +bitarray_set (struct bitarray *bitarray, size_t index, bool value) +{ + ASSERT (bitarray); + ASSERT (bitarray->bytes); + ASSERT (index >> 3 < bitarray->size); + setbit (bitarray->bytes, index, value); +} + +bool +bitarray_get (struct bitarray *bitarray, size_t index) +{ + ASSERT (bitarray); + ASSERT (bitarray->bytes); + ASSERT (index >> 3 < bitarray->size); + return getbit (bitarray->bytes, index); +} + +void +bitarray_free (struct bitarray *bitarray) +{ + FREE (bitarray->bytes); +} 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 @@ +#ifndef INCLUDED_BITARRAY_H +#define INCLUDED_BITARRAY_H + +#include +#include +#include + +struct bitarray { + uint8_t *bytes; + size_t size; +}; + +void bitarray_init (struct bitarray *, size_t bit_count); +void bitarray_free (struct bitarray *); +void bitarray_set (struct bitarray *, size_t index, bool); +bool bitarray_get (struct bitarray *, size_t index); + +#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 @@ +#include "board.h" +#include "utils.h" + +void +board_init (struct board *board, uint32_t rows, uint32_t cols) +{ + bitarray_init (&board->bitarray, rows*cols); + board->rows = rows; + board->cols = cols; +} + +void +board_set (struct board *board, int32_t row, int32_t col, bool value) +{ + ASSERT (board); + + row = MOD (row, (int32_t) board->rows); + col = MOD (col, (int32_t) board->cols); + bitarray_set (&board->bitarray, row*board->cols + col, value); +} + +bool +board_get (struct board *board, int32_t row, int32_t col) +{ + ASSERT (board); + + row = MOD (row, (int32_t) board->rows); + col = MOD (col, (int32_t) board->cols); + return bitarray_get (&board->bitarray, row*board->cols + col); +} + +void +board_free (struct board *board) +{ + bitarray_free (&board->bitarray); +} 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 @@ +#ifndef INCLUDED_BOARD_H +#define INCLUDED_BOARD_H + +#include +#include +#include "bitarray.h" + +struct board { + struct bitarray bitarray; + uint32_t rows; + uint32_t cols; +}; + +void board_init (struct board *, uint32_t rows, uint32_t cols); +void board_free (struct board *); +void board_set (struct board *, int32_t row, int32_t col, bool); +bool board_get (struct board *, int32_t row, int32_t col); + +#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 @@ +#include +#include "cellular-automaton.h" +#include "utils.h" + +void +cellular_automaton_init (struct cellular_automaton *cellular_automaton, + uint32_t function, uint32_t rows, uint32_t cols) +{ + board_init (&cellular_automaton->board, rows, cols); + cellular_automaton->back_board_bitarray = + malloc (cellular_automaton->board.bitarray.size); + cellular_automaton->function = function; +} + +void +cellular_automaton_progress (struct cellular_automaton *cellular_automaton) +{ + // uint32_t alive = 0; + for (int32_t row = 0; row < cellular_automaton->board.rows; ++row) { + for (int32_t col = 0; col < cellular_automaton->board.cols; ++col) { + struct board *board = &cellular_automaton->board; + uint8_t state = + board_get (board, row - 1, col + 0) << 3 | + board_get (board, row + 0, col - 1) << 1 | + board_get (board, row + 0, col + 0) << 0 | + board_get (board, row + 0, col + 1) << 2 | + board_get (board, row + 1, col + 0) << 4; + + // if (board_get (board, row, col)) { + // // printf ("%d/%d, ", row, col); + // ++alive; + // } + + setbit (cellular_automaton->back_board_bitarray, + row*board->cols + col, + cellular_automaton->function >> state & 1); + } + } // printf ("%u\n", alive); + + SWAP (cellular_automaton->back_board_bitarray, + cellular_automaton->board.bitarray.bytes); +} + +void +cellular_automaton_free (struct cellular_automaton *cellular_automaton) +{ + board_free (&cellular_automaton->board); + free (cellular_automaton->back_board_bitarray); +} 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 @@ +#ifndef INCLUDED_CELLULAR_AUTOMATON_H +#define INCLUDED_CELLULAR_AUTOMATON_H + +#include +#include "board.h" + +struct cellular_automaton { + struct board board; + uint8_t *back_board_bitarray; + uint32_t function; +}; + +void cellular_automaton_init (struct cellular_automaton *, + uint32_t function, + uint32_t rows, uint32_t cols); +void cellular_automaton_free (struct cellular_automaton *); +void cellular_automaton_progress (struct cellular_automaton *); + +#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 @@ +#ifndef INCLUDED_CONFIG_H +#define INCLUDED_CONFIG_H + +#define INITIAL_WINDOW_WIDTH 900 +#define INITIAL_WINDOW_HEIGHT 600 +#define INITIAL_WINDOW_TITLE "Двумерный клеточный автомат с окрестностью фон Неймана" + +#define INITIAL_CELLULAR_AUTOMATON_FUNCTION ((uint32_t) 0x7907E8) +#define INITIAL_CELLULAR_AUTOMATON_BOARD_COLS 735 +#define INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS 400 +// #define INITIAL_CELLULAR_AUTOMATON_BOARD_COLS 620 +// #define INITIAL_CELLULAR_AUTOMATON_BOARD_ROWS 400 + +#define INITIAL_IS_RANDOM true + +#define INITIAL_TICK_RATE 0 //1./2 +#define TICK_RATE_CHANGE_SPEED 1./100 +#define TICK_RATE_CHANGE_SPEED_MULTIPLIER 10 + +#define BACKGROUND_COLOR BLACK + +#define INITIAL_IS_PAUSED true +#define INITIAL_IS_GRID_SHOWN false +#define INITIAL_IS_HELP_SHOWN true + +#define AFTER_CLEAR_IS_PAUSED true + +#define GRID_THICKNESS 1 +#define GRID_COLOR GRAY + +#define CELL_COLOR_ALIVE WHITE +#define CELL_COLOR_DEAD BACKGROUND_COLOR + +#define TEXT_SPACING 2 + +#define INFOBAR_FONT_SIZE 20 +#define INFOBAR_HEIGHT 20 +#define INFOBAR_COLOR_TEXT WHITE +#define INFOBAR_COLOR_TEXT_EDITING RED +#define INFOBAR_COLOR_BACKGROUND DARKBLUE +#define INFOBAR_COLOR_OUTLINE BLUE +#define INFOBAR_TEXT_PAUSED "*PAUSED*" + +#define HELP_TITLE "HELP" +#define HELP_FONT_SIZE_HEADING 28 +#define HELP_FONT_SIZE_POINT 22 +#define HELP_PADDING 20 +#define HELP_COLOR_TEXT ((Color) { 0xDF, 0x00, 0x00, 0xFF }) +#define HELP_COLOR_BACKGROUND ((Color) { 0x2F, 0x00, 0x00, 0xDF }) +#define HELP_COLOR_OUTLINE ((Color) { 0xFF, 0x00, 0x00, 0xFF }) + +#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 @@ +#ifndef INCLUDED_DYNAMIC_ARRAY_H +#define INCLUDED_DYNAMIC_ARRAY_H + +#include "utils.h" + +#ifndef DYNAMIC_ARRAY_INIT_CAP +#define DYNAMIC_ARRAY_INIT_CAP 256 +#endif + +#define DYNAMIC_ARRAY_ENSURE_CAPACITY(DYNAMIC_ARRAY, COUNT) \ + ({ \ + if ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) { \ + if ((DYNAMIC_ARRAY).capacity == 0) \ + (DYNAMIC_ARRAY).capacity = DYNAMIC_ARRAY_INIT_CAP; \ + while ((DYNAMIC_ARRAY).count + (COUNT) > (DYNAMIC_ARRAY).capacity) \ + (DYNAMIC_ARRAY).capacity *= 2; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + } \ + }) + +#define DYNAMIC_ARRAY_SHRINK(DYNAMIC_ARRAY) \ + ({ \ + (DYNAMIC_ARRAY).capacity = (DYNAMIC_ARRAY).count; \ + (DYNAMIC_ARRAY).items = REALLOC((DYNAMIC_ARRAY).items, \ + (DYNAMIC_ARRAY).capacity * \ + sizeof (*(DYNAMIC_ARRAY).items)); \ + ASSERT((DYNAMIC_ARRAY).items); \ + \ + }) + +#define DYNAMIC_ARRAY_APPEND(DYNAMIC_ARRAY, ITEM) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), 1); \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count++] = (ITEM); \ + }) + +#define DYNAMIC_ARRAY_APPEND_MANY(DYNAMIC_ARRAY, NEW_ITEMS, NEW_ITEMS_COUNT) \ + ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY((DYNAMIC_ARRAY), (NEW_ITEMS_COUNT)); \ + memcpy ((DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count, (NEW_ITEMS), \ + (NEW_ITEMS_COUNT)*sizeof (*(DYNAMIC_ARRAY).items)); \ + (DYNAMIC_ARRAY).count += (NEW_ITEMS_COUNT); \ + }) + +#define DYNAMIC_ARRAY_LAST(DYNAMIC_ARRAY) \ + (DYNAMIC_ARRAY).items[(DYNAMIC_ARRAY).count - 1] + +#define DYNAMIC_ARRAY_RESET(DYNAMIC_ARRAY) ({ (DYNAMIC_ARRAY).count = 0; }) + +#define DYNAMIC_ARRAY_FREE(DYNAMIC_ARRAY) FREE((DYNAMIC_ARRAY).items) + +#define DYNAMIC_ARRAY_FOREACH(ITEM, DYNAMIC_ARRAY) for ( \ + typeof ((DYNAMIC_ARRAY).items) ITEM = (DYNAMIC_ARRAY).items; \ + ITEM < (DYNAMIC_ARRAY).items + (DYNAMIC_ARRAY).count; ++ITEM) + +#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 @@ +// src/main.c --- entry point and window management +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} 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 @@ +#ifndef INCLUDED_UTILS_H +#define INCLUDED_UTILS_H + +#include +#include +#include +#include +#include + +#define MALLOC malloc +#define REALLOC realloc +#ifdef DEBUG +#define FREE(PTR) ({free (PTR); PTR = NULL;}) +#define ASSERT assert +#else +#define FREE free +#define ASSERT +#endif + +#define CEIL_INT_DIV(A, B) ((A + (B - 1))/B) +// #define CEIL_INT_DIV(A, B) (1 + (A - 1)/B) // A != 0 + +#define MOD(A, B) (((A % B) + B) % B) + +#define SWAP(A, B) \ + ({ \ + typeof (A) tmp = A; \ + A = B; \ + B = tmp; \ + }) + +#define MIN(A, B) \ + ({ \ + typeof (A) a = A; \ + typeof (B) b = B; \ + a < b ? a : b; \ + }) \ + +inline void +setbit (uint8_t *bytes, size_t index, bool value) +{ + uint8_t *byte = &bytes[index >> 3]; + uint8_t offset = index & ((1 << 3) - 1); + uint8_t mask = ~(1 << offset); + *byte = *byte & mask | value << offset; +} + +inline bool +getbit (uint8_t *bytes, size_t index) +{ + uint8_t byte = bytes[index >> 3]; + uint8_t offset = index & ((1 << 3) - 1); + uint8_t mask = 1 << offset; + return (byte & mask) >> offset; +} + +inline void +prompt (char *label, char *fmt, void *where) +{ + printf ("%s: ", label); + scanf (fmt, where); + while (getchar () != '\n'); +} + +#endif -- cgit v1.3.1