Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created June 14, 2011 19:46
Show Gist options
  • Save neuro-sys/1025701 to your computer and use it in GitHub Desktop.
Save neuro-sys/1025701 to your computer and use it in GitHub Desktop.
game of life
#include <stdio.h>
#include <math.h>
#include <SDL/SDL.h>
#include <SDL/SDL_draw.h>
#define SCREEN_W 320
#define SCREEN_H 320
#define CELL_SIZ 10
#define W SCREEN_W / CELL_SIZ
#define H SCREEN_H / CELL_SIZ
SDL_Surface *scr;
int cells[W][H];
int cells2[W][H];
int (*cellsPtr)[H] = cells;
int (*cells2Ptr)[H] = cells2;
int (*cellsTmp)[H];
int done;
int start;
void life();
void handle_keys()
{
int x, y;
static int cx, cy;
SDL_Event event;
while (SDL_PollEvent(&event)) {
SDL_GetMouseState(&x, &y);
cx = x / CELL_SIZ;
cy = y / CELL_SIZ;
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
exit(0);
else if (event.key.keysym.sym == SDLK_SPACE)
start = !start;
else if (event.key.keysym.sym == SDLK_n)
life();
break;
case SDL_MOUSEBUTTONDOWN:
cellsPtr[cx][cy] = !cellsPtr[cx][cy];
break;
case SDL_QUIT:
exit(0);
}
}
/* hover display */
if (SDL_MUSTLOCK(scr))
SDL_LockSurface(scr);
Draw_FillRect(scr, cx * CELL_SIZ, cy * CELL_SIZ, CELL_SIZ, CELL_SIZ, SDL_MapRGB(scr->format, 10, 10, 20));
if (SDL_MUSTLOCK(scr))
SDL_UnlockSurface(scr);
}
void draw_cells()
{
int i, j;
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) {
if (cellsPtr[j][i]) {
if (SDL_MUSTLOCK(scr))
SDL_LockSurface(scr);
Draw_FillRect(scr, j * CELL_SIZ, i * CELL_SIZ, CELL_SIZ, CELL_SIZ, SDL_MapRGB(scr->format, 10, 10, start ? 250 : 150));
if (SDL_MUSTLOCK(scr))
SDL_UnlockSurface(scr);
}
}
}
}
void life()
{
int i, j;
for (i = 0; i < H; i++)
for (j = 0; j < W; j++) {
cells2Ptr[i][j] = 0;
int num_alive_adj = 0;
num_alive_adj += cellsPtr[i][j-1];
num_alive_adj += cellsPtr[i-1][j-1];
num_alive_adj += cellsPtr[i-1][j];
num_alive_adj += cellsPtr[i-1][j+1];
num_alive_adj += cellsPtr[i][j+1];
num_alive_adj += cellsPtr[i+1][j+1];
num_alive_adj += cellsPtr[i+1][j];
num_alive_adj += cellsPtr[i+1][j-1];
if (cellsPtr[i][j]) {
if (num_alive_adj == 2 || num_alive_adj == 3) /* survive */
cells2Ptr[i][j] = 1;
else if (num_alive_adj > 3) /* overpopulation */
cells2Ptr[i][j] = 0;
else if (num_alive_adj <= 2)
cells2Ptr[i][j] = 0;
} else {
if (num_alive_adj == 3) /* birth! */
cells2Ptr[i][j] = 1;
}
}
cellsTmp = cellsPtr;
cellsPtr = cells2Ptr;
cells2Ptr = cellsTmp;
}
int main(int argc, char* argv[])
{
Uint32 second;
int fps = 0;
int life_second;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
scr = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_ShowCursor(SDL_ENABLE);
second = SDL_GetTicks() + 1000;
life_second = SDL_GetTicks() + 300;
while(!done) {
SDL_FillRect(scr, NULL, 0);
handle_keys();
if (start) {
/* slow down a bit */
if (SDL_GetTicks() > life_second) {
life_second = SDL_GetTicks() + 300;
life();
}
}
draw_cells();
SDL_Flip(scr);
if (SDL_GetTicks() > second) {
fps = 0;
second = SDL_GetTicks() + 1000;
}
SDL_Delay(30);
fps++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment