Skip to content

Instantly share code, notes, and snippets.

@itarato
Created December 6, 2019 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/54119ec6c4c448d8645fe5dbe2ea44f7 to your computer and use it in GitHub Desktop.
Save itarato/54119ec6c4c448d8645fe5dbe2ea44f7 to your computer and use it in GitHub Desktop.
Game of Life (in C/SDL)
#include "SDL.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define SIZE 1000
struct cell {
bool is_alive;
int8_t nbc_prev;
int8_t nbc_next;
};
void sync_neighbors(struct cell *board) {
for (int i = 0; i < (SIZE * SIZE); i++) {
board[i].nbc_prev = board[i].nbc_next;
}
}
void update_neightbors(struct cell *board, int idx, int delta) {
int offs[] = {-SIZE-1, -SIZE, -SIZE+1, -1, +1, SIZE-1, SIZE, SIZE+1};
for (int offs_i = 0; offs_i < 8; offs_i++) {
board[(idx + offs[offs_i]) % (SIZE * SIZE)].nbc_next += delta;
}
}
void step(struct cell *board) {
int cellsize = sizeof(struct cell);
for (int i = 0; i < (SIZE * SIZE); i++) {
struct cell *c = board + i;
if (c->is_alive) {
if (c->nbc_prev <= 1 || c->nbc_prev >= 4) {
c->is_alive = false;
update_neightbors(board, i, -1);
}
} else {
if (c->nbc_prev == 3) {
c->is_alive = true;
update_neightbors(board, i, 1);
}
}
}
sync_neighbors(board);
}
void randomize(struct cell *board) {
srand(time(NULL));
for (int i = 0; i < (SIZE * SIZE); i++) {
// int r = rand() % 20;
// if (r < 1) {
// board[i].is_alive = true;
// update_neightbors(board, i, 1);
// }
if (i % 3 == 0) {
board[i].is_alive = true;
update_neightbors(board, i, 1);
}
}
sync_neighbors(board);
}
void draw(struct cell *board, SDL_Renderer *renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
int i = y * SIZE + x;
if (!board[i].is_alive) continue;
SDL_Rect point = {x, y, 1, 1};
SDL_RenderFillRect(renderer, &point);
}
}
}
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL Init Error\n");
exit(EXIT_FAILURE);
}
SDL_Window *win = SDL_CreateWindow("Game Of Life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SIZE, SIZE, SDL_WINDOW_SHOWN);
if (win == NULL) {
printf("Window creation error\n");
exit(EXIT_FAILURE);
}
SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) {
printf("Renderer creation error\n");
exit(EXIT_FAILURE);
}
struct cell *board = malloc(sizeof(struct cell) * (SIZE * SIZE));
if (board == NULL) {
printf("Malloc error\n");
exit(EXIT_FAILURE);
}
memset(board, 0, sizeof(struct cell) * (SIZE * SIZE));
randomize(board);
bool quit = false;
while (!quit) {
step(board);
draw(board, renderer);
SDL_RenderPresent(renderer);
SDL_Delay(10);
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_MOUSEBUTTONDOWN) quit = true;
}
}
SDL_Quit();
free(board);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment