Created
September 6, 2021 17:02
-
-
Save eloidrai/1fd866f3c13cb24944a7396a6e3a5ff8 to your computer and use it in GitHub Desktop.
Une implémentation du jeu de la vie en C avec la SDL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#include <time.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <SDL.h> | |
#define GRID_WIDTH 500 | |
#define GRID_HEIGHT 500 | |
#define ALIVE 0 | |
#define DEAD 255 | |
void applyConway (uint8_t * cells); | |
void applyRandom(uint8_t * cells); | |
void copyCellsToTexture(uint8_t * cells, SDL_Texture * gridTexture); | |
int main(int argc, char ** argv) { | |
SDL_Window * window; | |
SDL_Renderer * renderer; | |
SDL_Texture * gridTexture; | |
SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGB332; | |
uint8_t cells[GRID_HEIGHT * GRID_WIDTH]; | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_CreateWindowAndRenderer(1000, 1000, 0, &window, &renderer); | |
SDL_SetWindowTitle(window, "Jeu de la vie"); | |
gridTexture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, GRID_WIDTH, GRID_HEIGHT); | |
applyRandom(cells); | |
copyCellsToTexture(cells, gridTexture); | |
SDL_RenderCopy(renderer, gridTexture, NULL, NULL); | |
SDL_RenderPresent(renderer); | |
while (true) { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT) { | |
SDL_DestroyWindow(window); | |
SDL_DestroyRenderer(renderer); | |
SDL_Quit(); | |
return EXIT_SUCCESS; | |
} | |
if (event.type == SDL_MOUSEBUTTONDOWN) { | |
applyConway(cells); | |
copyCellsToTexture(cells, gridTexture); | |
SDL_RenderCopy(renderer, gridTexture, NULL, NULL); | |
SDL_RenderPresent(renderer); | |
} | |
} | |
} | |
return EXIT_SUCCESS; | |
} | |
void applyConway (uint8_t * cells) { | |
uint8_t * born[GRID_WIDTH*GRID_HEIGHT]; | |
uint8_t * dead[GRID_WIDTH*GRID_HEIGHT]; | |
int birthCount = 0, deathCount = 0; | |
for (int y=0; y<GRID_HEIGHT; y++) for (int x=0; x<GRID_WIDTH; x++) { | |
int neightboorsCount = 0; | |
for (int yn = y-1; yn <= y+1; yn++) for (int xn = x-1; xn <= x+1; xn++) { | |
if (x==xn && y==yn) continue; | |
if (xn < 0 || xn >= GRID_WIDTH) continue; | |
if (yn < 0 || yn >= GRID_HEIGHT) continue; | |
neightboorsCount += cells[yn * GRID_WIDTH + xn] == ALIVE; | |
} | |
if (cells[y * GRID_WIDTH + x] == DEAD && neightboorsCount == 3) { | |
born[birthCount] = cells + y * GRID_WIDTH + x; | |
birthCount++; | |
} | |
if (cells[y * GRID_WIDTH + x] == ALIVE && (neightboorsCount < 2 || neightboorsCount > 3)) { | |
dead[deathCount] = cells + y * GRID_WIDTH + x; | |
deathCount++; | |
} | |
} | |
for (int i = 0; i<deathCount; i++) *(dead[i]) = DEAD; | |
for (int i = 0; i<birthCount; i++) *(born[i]) = ALIVE; | |
} | |
void applyRandom(uint8_t * cells) { | |
srand(time(0)); | |
for (int y=0; y<GRID_HEIGHT; y++) for (int x=0; x<GRID_WIDTH; x++) { | |
cells[y * GRID_WIDTH + x] = (random() % 2) ? ALIVE: DEAD; | |
} | |
} | |
void copyCellsToTexture(uint8_t * cells, SDL_Texture * gridTexture) { | |
uint8_t * pixels; | |
int pitch; | |
SDL_LockTexture(gridTexture, NULL, (void**)&pixels, &pitch); | |
for (int y=0; y<GRID_HEIGHT; y++) for (int x=0; x<GRID_WIDTH; x++) { | |
pixels[y * pitch + x] = cells[y * GRID_WIDTH + x]; | |
} | |
SDL_UnlockTexture(gridTexture); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: main.c | |
gcc -o $@ $^ `sdl2-config --cflags --libs` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment