]> git.itanic.dy.fi Git - sdl-planets/commitdiff
Store the planets in a linked list
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 7 Mar 2010 11:14:41 +0000 (13:14 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 7 Mar 2010 11:14:41 +0000 (13:14 +0200)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index 09fc059e9deffc6c7689356dc0082d498cfbeee8..420552c206df5275fc16f151ec233c2ba1de7c6d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -29,31 +29,44 @@ static void clear_buf(SDL_Surface *screen)
 static void loop(SDL_Surface *screen)
 {
        int num_of_planets = 100;
-       struct planet planet[num_of_planets];
+       struct planet *planet, *pl1, *pl2;
        SDL_Event event;
-       int i, j;
+       int i, planets;
        int old_ticks, ticks, framecount = 0, last_fps_time = 0;
        int last_framecount = 0;
        double time = 0, last_fps = 0;
 
-       for (i = 0; i < num_of_planets; i++)
-               init_planet(screen, &planet[i]);
+       planet = malloc(sizeof(*planet));
+       init_planet(screen, planet);
+
+       for (i = 1; i < num_of_planets; i++) {
+               struct planet *new_planet;
+               new_planet = malloc(sizeof(*planet));
+               init_planet(screen, new_planet);
+               list_add(&new_planet->list, &planet->list);
+       }
 
        ticks = SDL_GetTicks();
        while (1) {
-               for (i = 0; i < num_of_planets; i++) {
-                       for (j = i + 1; j < num_of_planets; j++)
-                               gravitize_planets(&planet[i], &planet[j], time);
-
-                       move_planet(&planet[i], time);
+               struct list_head *l;
+               planets = 0;
+               list_for_each_entry(pl1, &planet->list, list) {
+                       l = pl1->list.next;
+                       pl2 = list_to_planet(l);
+                       list_for_each_entry_from(pl2, &planet->list, list)
+                               gravitize_planets(pl1, pl2, time);
+
+                       move_planet(pl1, time);
                }
 
                SDL_LockSurface(screen);
 
                clear_buf(screen);
 
-               for (i = 0; i < num_of_planets; i++)
-                       draw_planet(screen, &planet[i]);
+               list_for_each_entry(pl1, &planet->list, list) {
+                       draw_planet(screen, pl1);
+                       planets++;
+               }
 
                SDL_UnlockSurface(screen);
 
@@ -78,7 +91,8 @@ static void loop(SDL_Surface *screen)
                        last_framecount = framecount;
                        last_fps_time = ticks;
                }
-               printf("\rFrames/s: %.2f, Frame: %d", last_fps, framecount++);
+               printf("\rFrames/s: %.2f, Frame: %d, planets: %d",
+                      last_fps, framecount++, planets);
        }
 }