12 void init_state(struct gol_board *state);
14 void simulate(struct gol_board *state, SDL_Surface *screen);
16 int main(int argc, char* args[])
18 struct gol_board state = {
27 SDL_Window* window = NULL;
28 SDL_Surface* screen = NULL;
31 state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells)))
32 if (!state.live_cells) {
40 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
44 //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
46 window = SDL_CreateWindow("SDL Tutorial",
47 SDL_WINDOWPOS_UNDEFINED,
48 SDL_WINDOWPOS_UNDEFINED,
56 screen = SDL_GetWindowSurface(window);
62 simulate(&state, screen);
65 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
66 SDL_UpdateWindowSurface(window);
70 SDL_DestroyWindow(window);
75 void init_state(struct gol_board *state) {
76 // Create the initial state. This is a glider gun (hopefully)
77 gol_vivify(state, 12, 0);
78 gol_vivify(state, 13, 0);
79 gol_vivify(state, 11, 1);
80 gol_vivify(state, 15, 1);
81 gol_vivify(state, 10, 2);
82 gol_vivify(state, 16, 2);
83 gol_vivify(state, 24, 2);
84 gol_vivify(state, 0, 3);
85 gol_vivify(state, 1, 3);
86 gol_vivify(state, 10, 3);
87 gol_vivify(state, 14, 3);
88 gol_vivify(state, 16, 3);
89 gol_vivify(state, 17, 3);
90 gol_vivify(state, 22, 3);
91 gol_vivify(state, 24, 3);
92 gol_vivify(state, 0, 4);
93 gol_vivify(state, 1, 4);
94 gol_vivify(state, 10, 4);
95 gol_vivify(state, 16, 4);
96 gol_vivify(state, 20, 4);
97 gol_vivify(state, 21, 4);
98 gol_vivify(state, 11, 5);
99 gol_vivify(state, 15, 5);
100 gol_vivify(state, 20, 5);
101 gol_vivify(state, 21, 5);
102 gol_vivify(state, 34, 5);
103 gol_vivify(state, 35, 5);
104 gol_vivify(state, 12, 6);
105 gol_vivify(state, 13, 6);
106 gol_vivify(state, 20, 6);
107 gol_vivify(state, 21, 6);
108 gol_vivify(state, 34, 6);
109 gol_vivify(state, 35, 6);
110 gol_vivify(state, 22, 7);
111 gol_vivify(state, 24, 7);
112 gol_vivify(state, 24, 7);
115 void simulate(struct gol_board *state, SDL_Surface *screen)
117 for (unsigned int i = 0; i < 1000; ++i) {
118 printf("Generation %d\n", i);
119 for (unsigned int j = 0; j < state.n; j += 2) {
120 printf("(%d, %d)\n", state.live_cells[j],
121 state.live_cells[j + 1]);
124 if (!gol_tick(state)) {