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);
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);
}
}