11 static void fade_buf(SDL_Surface *screen, double fade)
14 unsigned int *buf = screen->pixels;
17 static double threshold;
27 for (i = 0; i < screen->pitch * screen->h / sizeof(*buf); i++) {
30 b = (unsigned char *)&buf[i];
31 *b = *b >= amount ? (*b - amount) : *b;
33 *b = *b >= amount ? (*b - amount) : *b;
35 *b = *b >= amount ? (*b - amount) : *b;
39 static void clear_buf(SDL_Surface *screen)
42 unsigned int *buf = screen->pixels;
44 for (i = 0; i < screen->pitch * screen->h / 4; i++)
48 /* Return time in microseconds */
50 static unsigned long gettime(void)
54 static unsigned long start;
56 clock_gettime(CLOCK_MONOTONIC, &tp);
58 ret = tp.tv_sec * 1000 * 1000;
59 ret += tp.tv_nsec / 1000;
74 static int poll_events(struct sim_status *status, double time)
76 static double time_scale_rate = 1;
78 struct camera *cam = status->cam;
80 status->time_scale *= pow(time_scale_rate, time);
82 while (SDL_PollEvent(&event)) {
85 switch (event.key.keysym.sym) {
89 cam->speed.x = -CAM_SPEED;
92 cam->speed.x = CAM_SPEED;
95 cam->speed.y = -CAM_SPEED;
98 cam->speed.y = CAM_SPEED;
102 cam->zoom_rate = CAM_ZOOM_RATE;
106 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
109 time_scale_rate = 1.5;
112 time_scale_rate = 1 / 1.5;
115 status->time_scale = 1;
118 status->time_scale = 10;
121 status->time_scale = 20;
124 status->time_scale = 30;
127 status->tracers_enabled =
128 !status->tracers_enabled;
134 switch (event.key.keysym.sym) {
165 case SDL_VIDEORESIZE:
167 SDL_SetVideoMode(event.resize.w,
170 status->screen->flags);
179 printf("\nExiting. Good bye!\n");
183 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
186 struct sim_status status;
187 struct planet *planet, *pl1, *pl2;
188 struct camera camera;
190 int framecount = 0, last_fps_time = 0;
191 int last_framecount = 0, last_step_count = 0;
192 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
193 unsigned long step_count = 0;
194 double last_fps = 0, last_sps = 0, fade_amount;
195 double step_time = 0, true_time = 0;
197 init_camera(&camera);
199 status.cam = &camera;
200 status.time_scale = 1;
201 status.screen = screen;
202 status.tracers_enabled = 0;
204 planet = malloc(sizeof(*planet));
206 create_planets(planet, num_of_planets, total_mass, range);
208 ticks = SDL_GetTicks();
212 list_for_each_entry(pl1, &planet->list, list) {
213 pl2 = list_to_planet(pl1->list.next);
214 list_for_each_entry_from(pl2, &planet->list, list) {
216 if (!gravitize_planets(pl1, pl2,
220 ptmp = list_to_planet(pl2->list.prev);
221 merge_planets(pl1, pl2);
225 move_planet(pl1, step_time);
228 move_camera(&camera, true_time);
230 if (poll_events(&status, true_time))
235 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
236 step_time = true_time * status.time_scale;
237 step_time = MIN(step_time, 0.02);
241 * Do not draw to the screen more often than MAX_FPS
244 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
247 SDL_LockSurface(screen);
249 if (status.tracers_enabled &&
250 !camera.speed.x && !camera.speed.y &&
251 camera.zoom_rate == 1) {
252 fade_amount = (ticks - last_frame_tick) /
253 (1000 * 1000.0) * 20;
254 fade_buf(screen, fade_amount);
259 list_for_each_entry(pl1, &planet->list, list) {
260 draw_planet(screen, pl1, &camera);
264 SDL_UnlockSurface(screen);
268 last_frame_tick = ticks;
270 if (last_fps_time + (500 * 1000) < ticks) {
271 last_framecount = framecount - last_framecount;
272 last_fps = last_framecount * 1000 * 1000 /
273 (float)(ticks - last_fps_time);
274 last_framecount = framecount;
276 last_step_count = step_count - last_step_count;
277 last_sps = last_step_count * 1000 * 1000 /
278 (float)(ticks - last_fps_time);
279 last_step_count = step_count;
281 last_fps_time = ticks;
285 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
286 ", scale %.2f, zoom %.2f, step %ld",
287 last_fps, last_sps, planets, status.time_scale,
288 camera.zoom, step_count);
296 int main(int argc, char *argv[])
299 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
300 int planets = 100, xres = 800, yres = 600;
301 double total_mass = 50000;
304 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
305 fprintf(stderr, "Unable to initialize SDL: %s\n",
313 planets = atoi(argv[1]);
316 total_mass = atof(argv[2]);
319 range = atof(argv[3]);
321 screen = SDL_SetVideoMode(xres, yres, 32, flags);
322 if (screen == NULL) {
323 fprintf(stderr, "Unable to set video mode: %s\n",
328 SDL_WM_SetCaption(argv[0], NULL);
330 loop(screen, planets, total_mass, range);