]> git.friedersdorff.com Git - max/gol.git/blob - gol.c
Modifications
[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 simulate(struct gol_board *state, SDL_Surface *screen);
15
16 int main(int argc, char* args[])
17 {
18         struct gol_board state = {
19                 .live_cells = NULL,
20                 .n = 0,
21                 .size = 0,
22                 .max_x = 0,
23                 .min_x = 0,
24                 .max_y = 0,
25                 .min_y = 0
26         };
27         SDL_Window* window = NULL;
28         SDL_Surface* screen = NULL;
29
30
31         state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells)))
32         if (!state.live_cells) {
33                 return 1;
34         }
35         
36         init_state(&state);
37         
38         
39
40         if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
41                 quit(1);
42         }
43
44         //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
45
46         window = SDL_CreateWindow("SDL Tutorial",
47                                   SDL_WINDOWPOS_UNDEFINED,
48                                   SDL_WINDOWPOS_UNDEFINED,
49                                   640,
50                                   320,
51                                   SDL_WINDOW_SHOWN);
52         if (!window) {
53                 quit(1);
54         }
55
56         screen = SDL_GetWindowSurface(window);
57         if (!screen) {
58                 quit(1);
59         }
60         
61         // Main loop
62         simulate(&state, screen);
63         
64
65         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
66         SDL_UpdateWindowSurface(window);
67
68         SDL_Delay(2000);
69
70         SDL_DestroyWindow(window);
71         quit(0);
72 }
73
74
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);
113 }
114
115 void simulate(struct gol_board *state, SDL_Surface *screen)
116 {
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]);
122                 }
123                 printf("\n");
124                 if (!gol_tick(state)) {
125                         //Pannick
126                 }
127         }
128 }