-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit: this looks interesting and gives me a headache
there's still a lot to be done with this code, but hey, it displays an animation that looks interesting, so i decided to do a git init and conserve it here for posterity.
- Loading branch information
nex
committed
Mar 10, 2010
0 parents
commit 0856842
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
CC = gcc | ||
COMPILERFLAGS = -Wall -std=c99 | ||
|
||
COMPILERFLAGS += `sdl-config --cflags` | ||
LIBPATH = `sdl-config --libs` | ||
|
||
All: main | ||
|
||
%: %.c | ||
$(CC) $(COMPILERFLAGS) $(LIBPATH) $(FRAMEWORK) $^ -o $@ | ||
|
||
main: main.c | ||
|
||
run: main | ||
./main | ||
|
||
clean: | ||
rm main |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <SDL.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
const int CFG_WIDTH = 512; | ||
const int CFG_HEIGHT = 512; | ||
|
||
SDL_Surface* screen; | ||
|
||
static bool init_sdl() { | ||
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | ||
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | ||
return false; | ||
} | ||
screen = SDL_SetVideoMode(CFG_WIDTH, CFG_HEIGHT, 32, SDL_HWSURFACE); | ||
if (!screen) { | ||
fprintf(stderr, "Unable set SDL video mode: %s\n", SDL_GetError()); | ||
SDL_Quit(); | ||
return false; | ||
} | ||
SDL_WM_SetCaption("cell", NULL); | ||
return true; | ||
} | ||
|
||
static void teardown_sdl() { | ||
SDL_Quit(); | ||
} | ||
|
||
static bool process_events() { | ||
SDL_Event event; | ||
while (SDL_PollEvent(&event)) { | ||
if (event.type == SDL_QUIT) return false; | ||
if (event.type == SDL_KEYDOWN) { | ||
switch(event.key.keysym.sym) { | ||
case SDLK_ESCAPE: | ||
return false; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
static void display(int cell[CFG_WIDTH][CFG_HEIGHT], SDL_Surface* screen) { | ||
Uint32 black = SDL_MapRGB(screen->format, 0, 0, 0); | ||
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255); | ||
if (SDL_MUSTLOCK(screen)) { | ||
if (SDL_LockSurface(screen) < 0) return; | ||
} | ||
for (int x = 0; x < CFG_WIDTH; x++) { | ||
for (int y = 0; y < CFG_HEIGHT; y++) { | ||
Uint32* pixPos = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; | ||
*pixPos = cell[x][y]? black : white; | ||
} | ||
} | ||
if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); | ||
SDL_Flip(screen); | ||
} | ||
|
||
static void update(int cell[CFG_WIDTH][CFG_HEIGHT]) { | ||
for (int x = 1; x < CFG_WIDTH - 1; x++) { | ||
for (int y = 1; y < CFG_HEIGHT - 1; y++) { | ||
int sum = 0; | ||
for (int i = -1; i < 2; i++) { | ||
for (int j = -1; j < 2; j++) { | ||
sum += cell[x + i][y + j]; | ||
} | ||
} | ||
if ((sum < 2) || (sum > 4)) { | ||
cell[x][y] = 0; | ||
} else if (sum == 4) { | ||
cell[x][y] = 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
if (!init_sdl()) return EXIT_FAILURE; | ||
int cell[CFG_WIDTH][CFG_HEIGHT]; // TODO typedef that? | ||
srand(0); | ||
for (int x = 0; x < CFG_WIDTH; x++) { | ||
for (int y = 0; y < CFG_HEIGHT; y++) { | ||
cell[x][y] = rand() % 2; //(x + y) % 2; | ||
} | ||
} | ||
while (process_events()) { | ||
display(cell, screen); | ||
update(cell); | ||
usleep(80000); | ||
} | ||
teardown_sdl(); | ||
return EXIT_SUCCESS; | ||
} |