This commit is contained in:
2026-05-26 09:07:23 +02:00
parent 638eb5f381
commit b49d9c6ac7
2 changed files with 140 additions and 131 deletions

View File

@@ -8,9 +8,8 @@ const uint32_t colors[2] = {BLACK, WHITE};
Board init_board(size_t width, size_t height, int random) { Board init_board(size_t width, size_t height, int random) {
Board board = {NULL, width, height}; Board board = {NULL, width, height};
board.pixels = malloc(height * sizeof(uint32_t *)); board.pixels = malloc(height * sizeof(uint32_t*));
for (size_t y = 0; y < height; y++) { for (size_t y = 0; y < height; y++) {
board.pixels[y] = malloc(width * sizeof(uint32_t)); board.pixels[y] = malloc(width * sizeof(uint32_t));
for (size_t x = 0; x < width; x++) { for (size_t x = 0; x < width; x++) {
if (random) if (random)
@@ -52,8 +51,8 @@ int count_adjacent(Board board, size_t x, size_t y) {
int w, h; int w, h;
uint8_t count = 0; uint8_t count = 0;
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
w = x + directions[i][1]; w = (int)x + directions[i][0];
h = y + directions[i][0]; h = (int)y + directions[i][1];
if (!((w < 0 || w >= (int)board.width) || if (!((w < 0 || w >= (int)board.width) ||
(h < 0 || h >= (int)board.height))) (h < 0 || h >= (int)board.height)))
if (board.pixels[h][w] == WHITE) if (board.pixels[h][w] == WHITE)
@@ -71,7 +70,8 @@ Board process_game(Board board) {
count = count_adjacent(board, x, y); count = count_adjacent(board, x, y);
if (board.pixels[y][x] == BLACK && count == 3) { if (board.pixels[y][x] == BLACK && count == 3) {
new_board.pixels[y][x] = WHITE; new_board.pixels[y][x] = WHITE;
} else if (board.pixels[y][x] == WHITE && count != 2 && count != 3) { } else if (board.pixels[y][x] == WHITE && count != 2 &&
count != 3) {
new_board.pixels[y][x] = BLACK; new_board.pixels[y][x] = BLACK;
} }
} }

View File

@@ -1,25 +1,25 @@
#include "game_core.h"
#include "utils.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "game_core.h"
#include "utils.h"
#define SEED (5) #define SEED (5)
#define WIDTH (800) #define WIDTH (800)
#define HEIGHT (600) #define HEIGHT (600)
#define SIM_WIDTH (10) #define SIM_WIDTH (800)
#define SIM_HEIGHT (10) #define SIM_HEIGHT (600)
t_flags process_args(int argc, char **argv) { t_flags process_args(int argc, char** argv) {
t_flags output_flags = {0}; t_flags output_flags = {0};
if (argc <= 1) if (argc <= 1)
return output_flags; return output_flags;
if (argv[1][0] != '-') if (argv[1][0] != '-')
return output_flags; return output_flags;
char *flags = argv[1]; char* flags = argv[1];
int i = 1; int i = 1;
char c = flags[i]; char c = flags[i];
while (c) { while (c) {
@@ -34,23 +34,23 @@ t_flags process_args(int argc, char **argv) {
return output_flags; return output_flags;
} }
int main(int argc, char *argv[]) { int main(int argc, char* argv[]) {
t_flags flags = process_args(argc, argv); t_flags flags = process_args(argc, argv);
srand(time(NULL)); srand(time(NULL));
if (flags.graphical) { if (flags.graphical) {
Board b = init_board(WIDTH, HEIGHT, 1); Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
SDL_Window *win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0); SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); SDL_Renderer* ren =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture *tex = SDL_Texture* tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
SDL_TEXTUREACCESS_STREAMING, SIM_WIDTH, SIM_HEIGHT); SIM_WIDTH, SIM_HEIGHT);
uint32_t pixels[HEIGHT][WIDTH];
int running = 1; int running = 1;
SDL_Event e; SDL_Event e;
while (running) { while (running) {
@@ -58,11 +58,10 @@ int main(int argc, char *argv[]) {
if (e.type == SDL_QUIT) if (e.type == SDL_QUIT)
running = 0; running = 0;
// Envoie la matrice à la texture uint32_t flat[SIM_HEIGHT * SIM_WIDTH];
uint32_t flat[HEIGHT * WIDTH]; for (size_t y = 0; y < SIM_HEIGHT; y++)
for (size_t y = 0; y < HEIGHT; y++) for (size_t x = 0; x < SIM_WIDTH; x++)
for (size_t x = 0; x < WIDTH; x++) flat[y * SIM_WIDTH + x] = b.pixels[y][x];
flat[y * WIDTH + x] = b.pixels[y][x];
SDL_Rect dst = {0, 0, WIDTH, HEIGHT}; SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t)); SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t));
@@ -70,7 +69,7 @@ int main(int argc, char *argv[]) {
SDL_RenderCopy(ren, tex, NULL, &dst); SDL_RenderCopy(ren, tex, NULL, &dst);
SDL_RenderPresent(ren); SDL_RenderPresent(ren);
b = process_game(b); b = process_game(b);
SDL_Delay(500); SDL_Delay(50);
} }
SDL_DestroyTexture(tex); SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren); SDL_DestroyRenderer(ren);
@@ -79,8 +78,18 @@ int main(int argc, char *argv[]) {
free_board(b); free_board(b);
} else { } else {
Board b = init_board(6, 6, 0);
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1); 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); display_board(b);
puts("----------"); puts("----------");
b = process_game(b); b = process_game(b);