11 static void fade_buf(SDL_Surface *screen, double fade)
14 unsigned int *buf = screen->pixels;
17 static double threshold = 0;
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;
38 static void clear_buf(SDL_Surface *screen)
41 unsigned int *buf = screen->pixels;
43 for (i = 0; i < screen->pitch * screen->h / 4; i++)
47 /* Return time in microseconds */
49 static unsigned long gettime(void)
53 static unsigned long start = 0;
55 clock_gettime(CLOCK_MONOTONIC, &tp);
57 ret = tp.tv_sec * 1000 * 1000;
58 ret += tp.tv_nsec / 1000;
73 static int poll_events(struct sim_status *status, double time)
75 static double time_scale_rate = 1;
77 struct camera *cam = status->cam;
79 status->time_scale *= pow(time_scale_rate, time);
81 while (SDL_PollEvent(&event)) {
84 switch (event.key.keysym.sym) {
88 cam->speed.x = -CAM_SPEED;
91 cam->speed.x = CAM_SPEED;
94 cam->speed.y = -CAM_SPEED;
97 cam->speed.y = CAM_SPEED;
101 cam->zoom_rate = CAM_ZOOM_RATE;
105 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
108 time_scale_rate = 1.5;
111 time_scale_rate = 1 / 1.5;
114 status->time_scale = 1;
117 status->time_scale = 10;
120 status->time_scale = 20;
123 status->time_scale = 30;
126 status->tracers_enabled =
127 !status->tracers_enabled;
133 switch (event.key.keysym.sym) {
164 case SDL_VIDEORESIZE:
165 status->screen = SDL_SetVideoMode(event.resize.w,
168 status->screen->flags);
177 printf("\nExiting. Good bye!\n");
181 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
184 struct sim_status status;
185 struct planet *planet, *pl1, *pl2;
186 struct camera camera;
188 int framecount = 0, last_fps_time = 0;
189 int last_framecount = 0, last_step_count = 0;
190 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
191 unsigned long step_count = 0;
192 double last_fps = 0, last_sps = 0;
193 double step_time = 0, true_time = 0;
195 init_camera(&camera);
197 status.cam = &camera;
198 status.time_scale = 1;
199 status.screen = screen;
200 status.tracers_enabled = 0;
202 planet = malloc(sizeof(*planet));
204 create_planets(planet, num_of_planets, total_mass, range);
206 ticks = SDL_GetTicks();
210 list_for_each_entry(pl1, &planet->list, list) {
211 pl2 = list_to_planet(pl1->list.next);
212 list_for_each_entry_from(pl2, &planet->list, list) {
214 if (!gravitize_planets(pl1, pl2,
218 ptmp = list_to_planet(pl2->list.prev);
219 merge_planets(pl1, pl2);
223 move_planet(pl1, step_time);
226 move_camera(&camera, true_time);
228 if (poll_events(&status, true_time))
233 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
234 step_time = true_time * status.time_scale;
235 step_time = MIN(step_time, 0.02);
239 * Do not draw to the screen more often than MAS_FPS
242 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
245 last_frame_tick = ticks;
247 SDL_LockSurface(screen);
249 if (status.tracers_enabled &&
250 !camera.speed.x && !camera.speed.y &&
251 camera.zoom_rate == 1) {
252 fade_buf(screen, 10 * true_time);
257 list_for_each_entry(pl1, &planet->list, list) {
258 draw_planet(screen, pl1, &camera);
262 SDL_UnlockSurface(screen);
266 if (last_fps_time + (500 * 1000) < ticks) {
267 last_framecount = framecount - last_framecount;
268 last_fps = last_framecount * 1000 * 1000 /
269 (float)(ticks - last_fps_time);
270 last_framecount = framecount;
272 last_step_count = step_count - last_step_count;
273 last_sps = last_step_count * 1000 * 1000 /
274 (float)(ticks - last_fps_time);
275 last_step_count = step_count;
277 last_fps_time = ticks;
281 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
282 ", scale %.2f, zoom %.2f, step %ld",
283 last_fps, last_sps, planets, status.time_scale,
284 camera.zoom, step_count);
292 int main(int argc, char *argv[])
295 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
296 int planets = 100, xres = 800, yres = 600;
297 double total_mass = 50000;
300 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
301 fprintf(stderr, "Unable to initialize SDL: %s\n",
309 planets = atoi(argv[1]);
312 total_mass = atof(argv[2]);
315 range = atof(argv[3]);
317 screen = SDL_SetVideoMode(xres, yres, 32, flags);
318 if (screen == NULL) {
319 fprintf(stderr, "Unable to set video mode: %s\n",
324 SDL_WM_SetCaption(argv[0], NULL);
326 loop(screen, planets, total_mass, range);