diff --git a/src/game_core.c b/src/game_core.c index 875f23c..3af5fcf 100644 --- a/src/game_core.c +++ b/src/game_core.c @@ -7,75 +7,75 @@ 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 *)); - 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]; - else - board.pixels[y][x] = BLACK; + Board board = {NULL, width, height}; + board.pixels = malloc(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]; + else + board.pixels[y][x] = BLACK; + } } - } - return board; + return board; } void free_board(Board board) { - for (size_t y = 0; y < board.height; y++) - free(board.pixels[y]); - free(board.pixels); + 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]; + 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; + return copy; } 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'); + 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'); + } + puts(""); } - puts(""); - } } int count_adjacent(Board board, size_t x, size_t y) { - size_t i; - int w, h; - uint8_t count = 0; - for (i = 0; i < 8; i++) { - w = x + directions[i][1]; - h = y + directions[i][0]; - if (!((w < 0 || w >= (int)board.width) || - (h < 0 || h >= (int)board.height))) - if (board.pixels[h][w] == WHITE) - count++; - } - return count; + size_t i; + 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]; + if (!((w < 0 || w >= (int)board.width) || + (h < 0 || h >= (int)board.height))) + if (board.pixels[h][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; - } + 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; + } + } } - } - free_board(board); - return new_board; + free_board(board); + return new_board; } diff --git a/src/main.c b/src/main.c index 796d046..8089c56 100644 --- a/src/main.c +++ b/src/main.c @@ -1,92 +1,101 @@ -#include "game_core.h" -#include "utils.h" #include #include #include #include #include +#include "game_core.h" +#include "utils.h" #define SEED (5) #define WIDTH (800) #define HEIGHT (600) -#define SIM_WIDTH (10) -#define SIM_HEIGHT (10) +#define SIM_WIDTH (800) +#define SIM_HEIGHT (600) -t_flags process_args(int argc, char **argv) { - t_flags output_flags = {0}; - if (argc <= 1) - return output_flags; - if (argv[1][0] != '-') - return output_flags; +t_flags process_args(int argc, char** argv) { + t_flags output_flags = {0}; + if (argc <= 1) + return output_flags; + if (argv[1][0] != '-') + return output_flags; - char *flags = argv[1]; - int i = 1; - char c = flags[i]; - while (c) { - if (c == 'g') - output_flags.graphical = 1; - if (c == 'm') - output_flags.multithread = 1; - if (c == 'o') - output_flags.opti = 1; - c = flags[++i]; - } - return output_flags; -} - -int main(int argc, char *argv[]) { - t_flags flags = process_args(argc, argv); - srand(time(NULL)); - - if (flags.graphical) { - Board b = init_board(WIDTH, HEIGHT, 1); - - SDL_Init(SDL_INIT_VIDEO); - - SDL_Window *win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0); - SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); - - SDL_Texture *tex = - SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, SIM_WIDTH, SIM_HEIGHT); - - uint32_t pixels[HEIGHT][WIDTH]; - int running = 1; - SDL_Event e; - while (running) { - while (SDL_PollEvent(&e)) - if (e.type == SDL_QUIT) - running = 0; - - // Envoie la matrice à la texture - uint32_t flat[HEIGHT * WIDTH]; - for (size_t y = 0; y < HEIGHT; y++) - for (size_t x = 0; x < WIDTH; x++) - flat[y * 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_RenderCopy(ren, tex, NULL, &dst); - SDL_RenderPresent(ren); - b = process_game(b); - SDL_Delay(500); + char* flags = argv[1]; + int i = 1; + char c = flags[i]; + while (c) { + if (c == 'g') + output_flags.graphical = 1; + if (c == 'm') + output_flags.multithread = 1; + if (c == 'o') + output_flags.opti = 1; + c = flags[++i]; } - SDL_DestroyTexture(tex); - SDL_DestroyRenderer(ren); - SDL_DestroyWindow(win); - SDL_Quit(); - free_board(b); - - } else { - - Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1); - display_board(b); - puts("----------"); - b = process_game(b); - display_board(b); - free_board(b); - } - - return EXIT_SUCCESS; + return output_flags; +} + +int main(int argc, char* argv[]) { + t_flags flags = process_args(argc, argv); + srand(time(NULL)); + + if (flags.graphical) { + Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1); + + SDL_Init(SDL_INIT_VIDEO); + + SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0); + SDL_Renderer* ren = + SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); + + SDL_Texture* tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + SIM_WIDTH, SIM_HEIGHT); + + int running = 1; + SDL_Event e; + 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_RenderCopy(ren, tex, NULL, &dst); + SDL_RenderPresent(ren); + b = process_game(b); + SDL_Delay(50); + } + SDL_DestroyTexture(tex); + SDL_DestroyRenderer(ren); + SDL_DestroyWindow(win); + SDL_Quit(); + 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; + display_board(b); + puts("----------"); + b = process_game(b); + display_board(b); + puts("----------"); + b = process_game(b); + display_board(b); + puts("----------"); + b = process_game(b); + display_board(b); + free_board(b); + } + + return EXIT_SUCCESS; }