From 6195ba94c8c7ffcd87b07f4371748e8f6598e721 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Fri, 31 Mar 2017 16:05:58 +0100 Subject: [PATCH] Modifications --- gol.c | 117 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 52 deletions(-) diff --git a/gol.c b/gol.c index eba6914..e5e5c4f 100644 --- a/gol.c +++ b/gol.c @@ -11,6 +11,67 @@ void quit(int e_st) void init_state(struct gol_board *state); +void simulate(struct gol_board *state, SDL_Surface *screen); + +int main(int argc, char* args[]) +{ + struct gol_board state = { + .live_cells = NULL, + .n = 0, + .size = 0, + .max_x = 0, + .min_x = 0, + .max_y = 0, + .min_y = 0 + }; + SDL_Window* window = NULL; + SDL_Surface* screen = NULL; + + + state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells))) + if (!state.live_cells) { + return 1; + } + + init_state(&state); + + + + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { + quit(1); + } + + //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); + + window = SDL_CreateWindow("SDL Tutorial", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + 640, + 320, + SDL_WINDOW_SHOWN); + if (!window) { + quit(1); + } + + screen = SDL_GetWindowSurface(window); + if (!screen) { + quit(1); + } + + // Main loop + simulate(&state, screen); + + + SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); + SDL_UpdateWindowSurface(window); + + SDL_Delay(2000); + + SDL_DestroyWindow(window); + quit(0); +} + + void init_state(struct gol_board *state) { // Create the initial state. This is a glider gun (hopefully) gol_vivify(state, 12, 0); @@ -51,23 +112,8 @@ void init_state(struct gol_board *state) { gol_vivify(state, 24, 7); } -int main(int argc, char* args[]) +void simulate(struct gol_board *state, SDL_Surface *screen) { - struct gol_board state = { - .live_cells = malloc(10 * sizeof(typeof(*state.live_cells))), - .n = 0, - .size = 10, - .max_x = 0, - .min_x = 0, - .max_y = 0, - .min_y = 0 - }; - if (!state.live_cells) { - return 1; - } - - init_state(&state); - for (unsigned int i = 0; i < 1000; ++i) { printf("Generation %d\n", i); for (unsigned int j = 0; j < state.n; j += 2) { @@ -75,41 +121,8 @@ int main(int argc, char* args[]) state.live_cells[j + 1]); } printf("\n"); - gol_tick(&state); - } - - - SDL_Window* window = NULL; - SDL_Surface* screen = NULL; - - if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { - quit(1); - } - - //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); - - window = SDL_CreateWindow("SDL Tutorial", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 640, - 320, - SDL_WINDOW_SHOWN); - if (!window) { - quit(1); - } - - screen = SDL_GetWindowSurface(window); - if (!screen) { - quit(1); + if (!gol_tick(state)) { + //Pannick + } } - - SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); - SDL_UpdateWindowSurface(window); - - SDL_Delay(2000); - - SDL_DestroyWindow(window); - quit(0); } - - -- 2.44.0