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 int poll_events(SDL_Surface **screen, struct camera *cam,
27 double *time_scale, double time)
29 static double time_scale_rate = 1;
32 *time_scale *= pow(time_scale_rate, time);
34 while (SDL_PollEvent(&event)) {
37 switch (event.key.keysym.sym) {
39 printf("\nExiting. Good bye!\n");
42 cam->speed.x = -CAM_SPEED;
45 cam->speed.x = CAM_SPEED;
48 cam->speed.y = -CAM_SPEED;
51 cam->speed.y = CAM_SPEED;
55 cam->zoom_rate = CAM_ZOOM_RATE;
59 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
62 time_scale_rate = 1.5;
65 time_scale_rate = 1 / 1.5;
72 switch (event.key.keysym.sym) {
103 case SDL_VIDEORESIZE:
104 *screen = SDL_SetVideoMode(event.resize.w,
115 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
118 struct planet *planet, *pl1, *pl2;
119 struct camera camera;
121 int old_ticks, ticks, framecount = 0, last_fps_time = 0;
122 int last_framecount = 0;
123 double time = 0, last_fps = 0, time_scale = 1;
125 init_camera(&camera);
127 planet = malloc(sizeof(*planet));
129 create_planets(planet, num_of_planets, total_mass, range);
131 ticks = SDL_GetTicks();
135 list_for_each_entry(pl1, &planet->list, list) {
136 pl2 = list_to_planet(pl1->list.next);
137 list_for_each_entry_from(pl2, &planet->list, list) {
139 if (!gravitize_planets(pl1, pl2,
143 ptmp = list_to_planet(pl2->list.prev);
144 merge_planets(pl1, pl2);
148 move_planet(pl1, time * time_scale);
151 SDL_LockSurface(screen);
155 list_for_each_entry(pl1, &planet->list, list) {
156 draw_planet(screen, pl1, &camera);
160 SDL_UnlockSurface(screen);
164 move_camera(&camera, time);
166 if (poll_events(&screen, &camera, &time_scale, time))
170 ticks = SDL_GetTicks();
171 time = (ticks - old_ticks) / 1000.0;
172 time = MIN(time, 0.02);
174 if (last_fps_time + 500 < ticks) {
175 last_framecount = framecount - last_framecount;
176 last_fps = last_framecount * 1000 /
177 (float)(ticks - last_fps_time);
178 last_framecount = framecount;
179 last_fps_time = ticks;
181 printf(" \rFrames/s: %.2f, Frame: %d, planets: %d"
182 ", scale %.2f, zoom %.2f",
183 last_fps, framecount++, planets, time_scale,
188 int main(int argc, char *argv[])
191 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
192 int planets = 100, xres = 800, yres = 600;
193 double total_mass = 50000;
196 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
197 fprintf(stderr, "Unable to initialize SDL: %s\n",
205 planets = atoi(argv[1]);
208 total_mass = atof(argv[2]);
211 range = atof(argv[3]);
213 screen = SDL_SetVideoMode(xres, yres, 32, flags);
214 if (screen == NULL) {
215 fprintf(stderr, "Unable to set video mode: %s\n",
220 SDL_WM_SetCaption(argv[0], NULL);
222 loop(screen, planets, total_mass, range);