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, *planet_root;
188 struct camera camera;
189 struct planet_search_iterator itr;
193 int framecount = 0, last_fps_time = 0;
194 int last_framecount = 0, last_step_count = 0;
195 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
196 unsigned long step_count = 0;
197 double last_fps = 0, last_sps = 0, fade_amount;
198 double step_time = 0, true_time = 0;
201 init_camera(&camera);
203 status.cam = &camera;
204 status.time_scale = 1;
205 status.screen = screen;
206 status.tracers_enabled = 0;
210 itr.qt_iterator.direction = planet_search_rectangular;
211 itr.qt_iterator.callback = planet_draw_iterator;
213 planet = malloc(sizeof(*planet));
215 create_planets(planet, num_of_planets, total_mass, range);
217 ticks = SDL_GetTicks();
221 list_for_each_entry(pl1, &planet->list, list) {
222 pl2 = list_to_planet(pl1->list.next);
223 list_for_each_entry_from(pl2, &planet->list, list) {
225 if (!gravitize_planets(pl1, pl2,
229 ptmp = list_to_planet(pl2->list.prev);
230 merge_planets(pl1, pl2);
234 planet_root = move_planet(pl1, step_time);
238 move_camera(&camera, true_time);
240 if (poll_events(&status, true_time))
245 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
246 step_time = true_time * status.time_scale;
247 step_time = MIN(step_time, 0.02);
251 * Do not draw to the screen more often than MAX_FPS
254 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
257 SDL_LockSurface(screen);
259 if (status.tracers_enabled &&
260 !camera.speed.x && !camera.speed.y &&
261 camera.zoom_rate == 1) {
262 fade_amount = (ticks - last_frame_tick) /
263 (1000 * 1000.0) * 20;
264 fade_buf(screen, fade_amount);
269 itr.limit[0] = camera.pos;
270 vect.x = screen->w / 2;
271 vect.y = screen->h / 2;
272 vector_scale(&vect, 1 / camera.zoom, &vect);
273 vector_add(&itr.limit[0], &vect, &itr.limit[1]);
274 vector_sub(&itr.limit[0], &vect, &itr.limit[0]);
276 itr.qt_iterator.head = &planet_root->tree;
278 visible_planets = walk_quadtree(&itr.qt_iterator);
280 SDL_UnlockSurface(screen);
284 last_frame_tick = ticks;
286 if (last_fps_time + (500 * 1000) < ticks) {
287 last_framecount = framecount - last_framecount;
288 last_fps = last_framecount * 1000 * 1000 /
289 (float)(ticks - last_fps_time);
290 last_framecount = framecount;
292 last_step_count = step_count - last_step_count;
293 last_sps = last_step_count * 1000 * 1000 /
294 (float)(ticks - last_fps_time);
295 last_step_count = step_count;
297 last_fps_time = ticks;
300 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
301 ", scale %.2f, zoom %.2f, step %ld, visible %d",
302 last_fps, last_sps, planets, status.time_scale,
303 camera.zoom, step_count, visible_planets);
311 int main(int argc, char *argv[])
314 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
315 int planets = 100, xres = 800, yres = 600;
316 double total_mass = 50000;
319 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
320 fprintf(stderr, "Unable to initialize SDL: %s\n",
328 planets = atoi(argv[1]);
331 total_mass = atof(argv[2]);
334 range = atof(argv[3]);
336 screen = SDL_SetVideoMode(xres, yres, 32, flags);
337 if (screen == NULL) {
338 fprintf(stderr, "Unable to set video mode: %s\n",
343 SDL_WM_SetCaption(argv[0], NULL);
345 loop(screen, planets, total_mass, range);