From fbfb9d903fc42fd51ee002d1d3e6415da56f1756 Mon Sep 17 00:00:00 2001 From: Dukantic Date: Tue, 26 May 2026 09:47:36 +0200 Subject: [PATCH] optimisation --- src/game_core.c | 52 ++++++++++++++++++++++--------------------------- src/game_core.h | 7 ++++--- src/main.c | 33 +++++++++++++++++-------------- 3 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/game_core.c b/src/game_core.c index 3af5fcf..de4b9fa 100644 --- a/src/game_core.c +++ b/src/game_core.c @@ -7,40 +7,30 @@ const int directions[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, const uint32_t colors[2] = {BLACK, WHITE}; Board init_board(size_t width, size_t height, int random) { - Board board = {NULL, width, height}; - board.pixels = malloc(height * sizeof(uint32_t*)); + Board board = {NULL, NULL, width, height}; + board.pixels = malloc(width * height * sizeof(uint32_t)); + board.next_pixels = malloc(width * height * sizeof(uint32_t)); for (size_t y = 0; y < height; y++) { - board.pixels[y] = malloc(width * sizeof(uint32_t)); for (size_t x = 0; x < width; x++) { if (random) - board.pixels[y][x] = colors[rand() % 2]; + board.pixels[y * width + x] = colors[rand() % 2]; else - board.pixels[y][x] = BLACK; + board.pixels[y * width + x] = BLACK; } } return board; } void free_board(Board board) { - for (size_t y = 0; y < board.height; y++) - free(board.pixels[y]); free(board.pixels); -} - -Board copy_board(Board board) { - Board copy = init_board(board.width, board.height, 0); - for (size_t x = 0; x < board.width; x++) { - for (size_t y = 0; y < board.height; y++) { - copy.pixels[y][x] = board.pixels[y][x]; - } - } - return copy; + free(board.next_pixels); } void display_board(Board board) { for (size_t y = 0; y < board.height; y++) { for (size_t x = 0; x < board.width; x++) { - printf("%c", board.pixels[y][x] == BLACK ? ' ' : 'X'); + printf("%c", + board.pixels[y * board.width + x] == BLACK ? ' ' : 'X'); } puts(""); } @@ -51,31 +41,35 @@ int count_adjacent(Board board, size_t x, size_t y) { int w, h; uint8_t count = 0; for (i = 0; i < 8; i++) { - w = (int)x + directions[i][0]; - h = (int)y + directions[i][1]; + w = ((int)x + directions[i][0] + board.width) % board.width; + h = ((int)y + directions[i][1] + board.height) % board.height; if (!((w < 0 || w >= (int)board.width) || (h < 0 || h >= (int)board.height))) - if (board.pixels[h][w] == WHITE) + if (board.pixels[h * board.width + w] == WHITE) count++; } return count; } Board process_game(Board board) { - Board new_board = copy_board(board); size_t x, y; uint8_t count = 0; for (y = 0; y < board.height; y++) { for (x = 0; x < board.width; x++) { count = count_adjacent(board, x, y); - if (board.pixels[y][x] == BLACK && count == 3) { - new_board.pixels[y][x] = WHITE; - } else if (board.pixels[y][x] == WHITE && count != 2 && - count != 3) { - new_board.pixels[y][x] = BLACK; + if (board.pixels[y * board.width + x] == BLACK && count == 3) { + board.next_pixels[y * board.width + x] = WHITE; + } else if (board.pixels[y * board.width + x] == WHITE && + count != 2 && count != 3) { + board.next_pixels[y * board.width + x] = BLACK; + } else { + board.next_pixels[y * board.width + x] = + board.pixels[y * board.width + x]; } } } - free_board(board); - return new_board; + uint32_t* tmp = board.next_pixels; + board.next_pixels = board.pixels; + board.pixels = tmp; + return board; } diff --git a/src/game_core.h b/src/game_core.h index 5dce605..ff6c3bf 100644 --- a/src/game_core.h +++ b/src/game_core.h @@ -8,9 +8,10 @@ #define WHITE (0xFFFFFFFF) typedef struct { - uint32_t **pixels; - size_t width; - size_t height; + uint32_t* pixels; + uint32_t* next_pixels; + size_t width; + size_t height; } Board; void free_board(Board board); diff --git a/src/main.c b/src/main.c index 8089c56..b629526 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -39,7 +40,12 @@ int main(int argc, char* argv[]) { srand(time(NULL)); if (flags.graphical) { - Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1); + Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 0); + b.pixels[0 * SIM_WIDTH + 1] = WHITE; + b.pixels[1 * SIM_WIDTH + 2] = WHITE; + b.pixels[2 * SIM_WIDTH + 0] = WHITE; + b.pixels[2 * SIM_WIDTH + 1] = WHITE; + b.pixels[2 * SIM_WIDTH + 2] = WHITE; SDL_Init(SDL_INIT_VIDEO); @@ -53,23 +59,25 @@ int main(int argc, char* argv[]) { int running = 1; SDL_Event e; + int pitch; + void* tex_pixels; while (running) { while (SDL_PollEvent(&e)) if (e.type == SDL_QUIT) running = 0; - uint32_t flat[SIM_HEIGHT * SIM_WIDTH]; - for (size_t y = 0; y < SIM_HEIGHT; y++) - for (size_t x = 0; x < SIM_WIDTH; x++) - flat[y * SIM_WIDTH + x] = b.pixels[y][x]; - SDL_Rect dst = {0, 0, WIDTH, HEIGHT}; - SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t)); - SDL_RenderClear(ren); + SDL_LockTexture(tex, NULL, &tex_pixels, &pitch); + memcpy(tex_pixels, b.pixels, + SIM_WIDTH * SIM_HEIGHT * sizeof(uint32_t)); + SDL_UnlockTexture(tex); + // SDL_UpdateTexture(tex, NULL, b.pixels,SIM_WIDTH * + // sizeof(uint32_t)); + // SDL_RenderClear(ren); SDL_RenderCopy(ren, tex, NULL, &dst); SDL_RenderPresent(ren); b = process_game(b); - SDL_Delay(50); + // SDL_Delay(100); } SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); @@ -78,12 +86,7 @@ int main(int argc, char* argv[]) { free_board(b); } else { - Board b = init_board(6, 6, 0); - b.pixels[0][1] = WHITE; - b.pixels[1][2] = WHITE; - b.pixels[2][0] = WHITE; - b.pixels[2][1] = WHITE; - b.pixels[2][2] = WHITE; + Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1); display_board(b); puts("----------"); b = process_game(b);