From 08fcd58893b5ea754ebddbf63b2745dde34d4830 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 05:11:35 +0300 Subject: publish --- src/main.c | 576 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 576 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') 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; +} -- cgit v1.3.1