12 void simulate(struct gol_board *state, SDL_Surface *screen, SDL_Window *window);
14 int redraw_screen(struct gol_board *state, SDL_Surface *screen, int ppc,
15 int origin_x, int origin_y);
17 int main(int argc, char* args[])
19 struct gol_board state = {
28 SDL_Window *window = NULL;
29 SDL_Surface *screen = NULL;
34 // The coordinates of the cell to draw in the bottom left corner of the
43 //state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells)));
44 //if (!state.live_cells) {
48 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
52 window = SDL_CreateWindow("SDL Tutorial",
53 SDL_WINDOWPOS_UNDEFINED,
54 SDL_WINDOWPOS_UNDEFINED,
62 screen = SDL_GetWindowSurface(window);
66 SDL_UpdateWindowSurface(window);
69 while (SDL_PollEvent(&e) != 0) {
70 if (e.type == SDL_QUIT) {
72 } else if (e.type == SDL_KEYDOWN) {
73 switch (e.key.keysym.sym) {
94 redraw_screen(&state, screen, ppc, origin_x, origin_y);
99 // simulate(&state, screen, window);
102 SDL_DestroyWindow(window);
107 void simulate(struct gol_board *state, SDL_Surface *screen, SDL_Window *window)
112 for (unsigned int i = 0; i < 1000; ++i) {
113 printf("Generation %d\n", i);
114 ppc_x = SCREEN_WIDTH/(state->max_x - state->min_x);
115 ppc_y = SCREEN_HEIGHT/(state->max_y - state->min_y);
116 for (unsigned int j = 0; j < state->n; j += 2) {
117 printf("(%d, %d)\n", state->live_cells[j],
118 state->live_cells[j + 1]);
122 if (!gol_tick(state)) {
125 SDL_UpdateWindowSurface(window);
129 int redraw_screen(struct gol_board *state, SDL_Surface *screen,
130 int ppc, int origin_x, int origin_y)
135 SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
136 for (i = origin_x; i < (screen->w/ppc) + origin_x; ++i) {
137 for (j = origin_y; j < (screen->h/ppc) + origin_y; ++j) {
138 if (gol_is_live(state, i, j)) {
140 ((i - origin_x) * ppc),
141 ((j - origin_y) * ppc),
145 SDL_FillRect(screen, &rect,
146 SDL_MapRGB(screen->format, 0, 0, 0));