]> git.friedersdorff.com Git - max/gol.git/blob - gol.c
MINIMAL implementation. Single glider gun
[max/gol.git] / gol.c
1 #include <SDL2/SDL.h>
2 #include <stdio.h>
3 #include "array.h"
4 #include "tick.h"
5
6 void quit(int e_st)
7 {
8         SDL_Quit();
9         exit(e_st);
10 }
11
12 void init_state(struct gol_board *state); 
13
14 void init_state(struct gol_board *state) {
15         // Create the initial state. This is a glider gun (hopefully)
16         gol_vivify(state, 12, 0);
17         gol_vivify(state, 13, 0);
18         gol_vivify(state, 11, 1);
19         gol_vivify(state, 15, 1);
20         gol_vivify(state, 10, 2);
21         gol_vivify(state, 16, 2);
22         gol_vivify(state, 24, 2);
23         gol_vivify(state, 0, 3);
24         gol_vivify(state, 1, 3);
25         gol_vivify(state, 10, 3);
26         gol_vivify(state, 14, 3);
27         gol_vivify(state, 16, 3);
28         gol_vivify(state, 17, 3);
29         gol_vivify(state, 22, 3);
30         gol_vivify(state, 24, 3);
31         gol_vivify(state, 0, 4);
32         gol_vivify(state, 1, 4);
33         gol_vivify(state, 10, 4);
34         gol_vivify(state, 16, 4);
35         gol_vivify(state, 20, 4);
36         gol_vivify(state, 21, 4);
37         gol_vivify(state, 11, 5);
38         gol_vivify(state, 15, 5);
39         gol_vivify(state, 20, 5);
40         gol_vivify(state, 21, 5);
41         gol_vivify(state, 34, 5);
42         gol_vivify(state, 35, 5);
43         gol_vivify(state, 12, 6);
44         gol_vivify(state, 13, 6);
45         gol_vivify(state, 20, 6);
46         gol_vivify(state, 21, 6);
47         gol_vivify(state, 34, 6);
48         gol_vivify(state, 35, 6);
49         gol_vivify(state, 22, 7);
50         gol_vivify(state, 24, 7);
51         gol_vivify(state, 24, 7);
52 }
53
54 int main(int argc, char* args[])
55 {
56         struct gol_board state = {
57                 .live_cells = malloc(10 * sizeof(typeof(*state.live_cells))),
58                 .n = 0,
59                 .size = 10,
60                 .max_x = 0,
61                 .min_x = 0,
62                 .max_y = 0,
63                 .min_y = 0
64         };
65         if (!state.live_cells) {
66                 return 1;
67         }
68         
69         init_state(&state);
70         
71         for (unsigned int i = 0; i < 1000; ++i) {
72                 printf("Generation %d\n", i);
73                 for (unsigned int j = 0; j < state.n; j += 2) {
74                         printf("(%d, %d)\n", state.live_cells[j],
75                                 state.live_cells[j + 1]);
76                 }
77                 printf("\n");
78                 gol_tick(&state);
79         }
80
81
82         SDL_Window* window = NULL;
83         SDL_Surface* screen = NULL;
84
85         if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
86                 quit(1);
87         }
88
89         //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
90
91         window = SDL_CreateWindow("SDL Tutorial",
92                                   SDL_WINDOWPOS_UNDEFINED,
93                                   SDL_WINDOWPOS_UNDEFINED,
94                                   640,
95                                   320,
96                                   SDL_WINDOW_SHOWN);
97         if (!window) {
98                 quit(1);
99         }
100
101         screen = SDL_GetWindowSurface(window);
102         if (!screen) {
103                 quit(1);
104         }
105
106         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
107         SDL_UpdateWindowSurface(window);
108
109         SDL_Delay(2000);
110
111         SDL_DestroyWindow(window);
112         quit(0);
113 }
114
115