]> git.friedersdorff.com Git - max/gol.git/commitdiff
Making more progress (hopefully)
authorMaximilian Friedersdorff <max@friedersdorff.com>
Mon, 3 Apr 2017 15:36:08 +0000 (16:36 +0100)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Mon, 3 Apr 2017 15:36:08 +0000 (16:36 +0100)
gol.c

diff --git a/gol.c b/gol.c
index e5e5c4f05072cfe0f374a0d52c0f435bc35faf12..ebdbba0cb680f24a1e2cbf9628b3a54a2edee443 100644 (file)
--- a/gol.c
+++ b/gol.c
@@ -9,9 +9,10 @@ void quit(int e_st)
        exit(e_st);
 }
 
-void init_state(struct gol_board *state); 
+void simulate(struct gol_board *state, SDL_Surface *screen, SDL_Window *window);
 
-void simulate(struct gol_board *state, SDL_Surface *screen);
+int redraw_screen(struct gol_board *state, SDL_Surface *screen, int ppc,
+       int origin_x, int origin_y);
 
 int main(int argc, char* args[])
 {
@@ -24,25 +25,30 @@ int main(int argc, char* args[])
                .max_y = 0,
                .min_y = 0
        };
-       SDL_Windowwindow = NULL;
-       SDL_Surfacescreen = NULL;
+       SDL_Window *window = NULL;
+       SDL_Surface *screen = NULL;
 
+       char finished = 0;
+       char redraw = 1;
 
-       state.live_cells = malloc(10 * sizeof(typeof(*state.live_cells)))
-       if (!state.live_cells) {
-               return 1;
-       }
-       
-       init_state(&state);
-       
-       
+       // 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);
        }
 
-       //screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
-
        window = SDL_CreateWindow("SDL Tutorial",
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED,
@@ -57,72 +63,88 @@ int main(int argc, char* args[])
        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);
+       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 init_state(struct gol_board *state) {
-       // Create the initial state. This is a glider gun (hopefully)
-       gol_vivify(state, 12, 0);
-       gol_vivify(state, 13, 0);
-       gol_vivify(state, 11, 1);
-       gol_vivify(state, 15, 1);
-       gol_vivify(state, 10, 2);
-       gol_vivify(state, 16, 2);
-       gol_vivify(state, 24, 2);
-       gol_vivify(state, 0, 3);
-       gol_vivify(state, 1, 3);
-       gol_vivify(state, 10, 3);
-       gol_vivify(state, 14, 3);
-       gol_vivify(state, 16, 3);
-       gol_vivify(state, 17, 3);
-       gol_vivify(state, 22, 3);
-       gol_vivify(state, 24, 3);
-       gol_vivify(state, 0, 4);
-       gol_vivify(state, 1, 4);
-       gol_vivify(state, 10, 4);
-       gol_vivify(state, 16, 4);
-       gol_vivify(state, 20, 4);
-       gol_vivify(state, 21, 4);
-       gol_vivify(state, 11, 5);
-       gol_vivify(state, 15, 5);
-       gol_vivify(state, 20, 5);
-       gol_vivify(state, 21, 5);
-       gol_vivify(state, 34, 5);
-       gol_vivify(state, 35, 5);
-       gol_vivify(state, 12, 6);
-       gol_vivify(state, 13, 6);
-       gol_vivify(state, 20, 6);
-       gol_vivify(state, 21, 6);
-       gol_vivify(state, 34, 6);
-       gol_vivify(state, 35, 6);
-       gol_vivify(state, 22, 7);
-       gol_vivify(state, 24, 7);
-       gol_vivify(state, 24, 7);
-}
-
-void simulate(struct gol_board *state, SDL_Surface *screen)
+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);
-               for (unsigned int j = 0; j < state.n; j += 2) {
-                       printf("(%d, %d)\n", state.live_cells[j],
-                               state.live_cells[j + 1]);
+               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)) {
-                       //Pannick
+                       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));
+                       }
                }
        }
 }