#include #include #include "array.h" #include "tick.h" void quit(int e_st) { SDL_Quit(); exit(e_st); } void simulate(struct gol_board *state, SDL_Surface *screen, SDL_Window *window); int redraw_screen(struct gol_board *state, SDL_Surface *screen, int ppc, int origin_x, int origin_y); 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; char finished = 0; char redraw = 1; // The coordinates of the cell to draw in the bottom left corner of the // canvas int origin_x = 0; int origin_y = 0; int ppc = 10; SDL_Event e; //state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells))); //if (!state.live_cells) { // return 1; //} if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { quit(1); } 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); } SDL_UpdateWindowSurface(window); while (!finished) { while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { finished = 1; } else if (e.type == SDL_KEYDOWN) { switch (e.key.keysym.sym) { case SDLK_q: case SDLK_x: finished = 1; break; case SDLK_PLUS: case SDLK_EQUALS: ppc *= 1.2; redraw = 1; break; case SDLK_MINUS: case SDLK_UNDERSCORE: ppc /= 1.2; redraw = 1; break; default: break; } } } if (redraw) { redraw_screen(&state, screen, ppc, origin_x, origin_y); } } // Main loop // simulate(&state, screen, window); SDL_DestroyWindow(window); quit(0); } void simulate(struct gol_board *state, SDL_Surface *screen, SDL_Window *window) { SDL_Rect cell; double ppc_x; double ppc_y; for (unsigned int i = 0; i < 1000; ++i) { printf("Generation %d\n", i); ppc_x = SCREEN_WIDTH/(state->max_x - state->min_x); ppc_y = SCREEN_HEIGHT/(state->max_y - state->min_y); for (unsigned int j = 0; j < state->n; j += 2) { printf("(%d, %d)\n", state->live_cells[j], state->live_cells[j + 1]); } printf("\n"); if (!gol_tick(state)) { return; } SDL_UpdateWindowSurface(window); } } int redraw_screen(struct gol_board *state, SDL_Surface *screen, int ppc, int origin_x, int origin_y) { SDL_Rect rect; int i, j; SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); for (i = origin_x; i < (screen->w/ppc) + origin_x; ++i) { for (j = origin_y; j < (screen->h/ppc) + origin_y; ++j) { if (gol_is_live(state, i, j)) { rect = (SDL_Rect) { ((i - origin_x) * ppc), ((j - origin_y) * ppc), ppc, ppc }; SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0)); } } } }