8 static void fade_buf(SDL_Surface *screen, double amount)
11 unsigned char *buf = screen->pixels;
13 for (i = 0; i < screen->pitch * screen->h; i++)
14 buf[i] = (buf[i] < amount) ? 0 : buf[i] - amount;
17 static void clear_buf(SDL_Surface *screen)
20 unsigned int *buf = screen->pixels;
22 for (i = 0; i < screen->pitch * screen->h / 4; i++)
26 static void loop(SDL_Surface *screen, int num_of_planets)
28 struct planet *planet, *pl1, *pl2;
32 int old_ticks, ticks, framecount = 0, last_fps_time = 0;
33 int last_framecount = 0;
34 double time = 0, last_fps = 0;
38 planet = malloc(sizeof(*planet));
39 init_planet(screen, planet);
41 for (i = 1; i < num_of_planets; i++) {
42 struct planet *new_planet;
43 new_planet = malloc(sizeof(*planet));
44 init_planet(screen, new_planet);
45 list_add(&new_planet->list, &planet->list);
48 ticks = SDL_GetTicks();
52 list_for_each_entry(pl1, &planet->list, list) {
53 pl2 = list_to_planet(pl1->list.next);
54 list_for_each_entry_from(pl2, &planet->list, list) {
56 if (!gravitize_planets(pl1, pl2, time))
59 ptmp = list_to_planet(pl2->list.prev);
60 merge_planets(pl1, pl2);
64 move_planet(pl1, time);
67 SDL_LockSurface(screen);
71 list_for_each_entry(pl1, &planet->list, list) {
72 draw_planet(screen, pl1, &camera);
76 SDL_UnlockSurface(screen);
80 move_camera(&camera, time);
82 while (SDL_PollEvent(&event)) {
85 switch (event.key.keysym.sym) {
87 printf("\nExiting. Good bye!\n");
90 camera.speed.x = -CAM_SPEED;
93 camera.speed.x = CAM_SPEED;
96 camera.speed.y = -CAM_SPEED;
99 camera.speed.y = CAM_SPEED;
102 camera.zoom_rate = CAM_ZOOM_RATE;
105 camera.zoom_rate = 1 / CAM_ZOOM_RATE;
112 switch (event.key.keysym.sym) {
114 printf("\nExiting. Good bye!\n");
129 camera.zoom_rate = 1;
132 camera.zoom_rate = 1;
138 case SDL_VIDEORESIZE:
139 screen = SDL_SetVideoMode(event.resize.w,
148 ticks = SDL_GetTicks();
149 time = (ticks - old_ticks) / 1000.0;
151 if (last_fps_time + 500 < ticks) {
152 last_framecount = framecount - last_framecount;
153 last_fps = last_framecount * 1000 /
154 (float)(ticks - last_fps_time);
155 last_framecount = framecount;
156 last_fps_time = ticks;
158 printf(" \rFrames/s: %.2f, Frame: %d, planets: %d",
159 last_fps, framecount++, planets);
163 int main(int argc, char *argv[])
166 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
167 int planets = 100, xres = 800, yres = 600;
169 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
170 fprintf(stderr, "Unable to initialize SDL: %s\n",
178 planets = atoi(argv[1]);
181 xres = atoi(argv[2]);
184 yres = atoi(argv[3]);
186 screen = SDL_SetVideoMode(xres, yres, 32, flags);
187 if (screen == NULL) {
188 fprintf(stderr, "Unable to set video mode: %s\n",
193 loop(screen, planets);