11 static void fade_buf(SDL_Surface *screen, int amount)
14 unsigned int *buf = screen->pixels;
17 for (i = 0; i < screen->pitch * screen->h / sizeof(*buf); i ++) {
20 b = (unsigned char *)&buf[i];
21 *b = *b >= amount ? (*b - amount) : *b;
23 *b = *b >= amount ? (*b - amount) : *b;
25 *b = *b >= amount ? (*b - amount) : *b;
29 static void clear_buf(SDL_Surface *screen)
32 unsigned int *buf = screen->pixels;
34 for (i = 0; i < screen->pitch * screen->h / 4; i++)
38 /* Return time in microseconds */
40 static unsigned long gettime(void)
44 static unsigned long start = 0;
46 clock_gettime(CLOCK_MONOTONIC, &tp);
48 ret = tp.tv_sec * 1000 * 1000;
49 ret += tp.tv_nsec / 1000;
64 static int poll_events(struct sim_status *status, double time)
66 static double time_scale_rate = 1;
68 struct camera *cam = status->cam;
70 status->time_scale *= pow(time_scale_rate, time);
72 while (SDL_PollEvent(&event)) {
75 switch (event.key.keysym.sym) {
79 cam->speed.x = -CAM_SPEED;
82 cam->speed.x = CAM_SPEED;
85 cam->speed.y = -CAM_SPEED;
88 cam->speed.y = CAM_SPEED;
92 cam->zoom_rate = CAM_ZOOM_RATE;
96 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
99 time_scale_rate = 1.5;
102 time_scale_rate = 1 / 1.5;
105 status->tracers_enabled =
106 !status->tracers_enabled;
112 switch (event.key.keysym.sym) {
143 case SDL_VIDEORESIZE:
144 status->screen = SDL_SetVideoMode(event.resize.w,
147 status->screen->flags);
156 printf("\nExiting. Good bye!\n");
160 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
163 struct sim_status status;
164 struct planet *planet, *pl1, *pl2;
165 struct camera camera;
167 int framecount = 0, last_fps_time = 0;
168 int last_framecount = 0, last_step_count = 0;
169 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
170 unsigned long step_count = 0;
171 double last_fps = 0, last_sps = 0, fade_amount;
172 double step_time = 0, true_time = 0;
174 init_camera(&camera);
176 status.cam = &camera;
177 status.time_scale = 1;
178 status.screen = screen;
179 status.tracers_enabled = 0;
181 planet = malloc(sizeof(*planet));
183 create_planets(planet, num_of_planets, total_mass, range);
185 ticks = SDL_GetTicks();
189 list_for_each_entry(pl1, &planet->list, list) {
190 pl2 = list_to_planet(pl1->list.next);
191 list_for_each_entry_from(pl2, &planet->list, list) {
193 if (!gravitize_planets(pl1, pl2,
197 ptmp = list_to_planet(pl2->list.prev);
198 merge_planets(pl1, pl2);
202 move_planet(pl1, step_time);
205 move_camera(&camera, true_time);
207 if (poll_events(&status, true_time))
212 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
213 step_time = true_time * status.time_scale;
214 step_time = MIN(step_time, 0.02);
218 * Do not draw to the screen more often than MAS_FPS
221 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
224 SDL_LockSurface(screen);
226 if (status.tracers_enabled &&
227 !camera.speed.x && !camera.speed.y &&
228 camera.zoom_rate == 1) {
229 fade_amount = (ticks - last_frame_tick) /
230 (1000 * 1000.0) * 20;
231 fade_buf(screen, fade_amount);
236 list_for_each_entry(pl1, &planet->list, list) {
237 draw_planet(screen, pl1, &camera);
241 SDL_UnlockSurface(screen);
245 last_frame_tick = ticks;
247 if (last_fps_time + (500 * 1000) < ticks) {
248 last_framecount = framecount - last_framecount;
249 last_fps = last_framecount * 1000 * 1000 /
250 (float)(ticks - last_fps_time);
251 last_framecount = framecount;
253 last_step_count = step_count - last_step_count;
254 last_sps = last_step_count * 1000 * 1000 /
255 (float)(ticks - last_fps_time);
256 last_step_count = step_count;
258 last_fps_time = ticks;
262 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
263 ", scale %.2f, zoom %.2f, step %ld",
264 last_fps, last_sps, planets, status.time_scale,
265 camera.zoom, step_count);
273 int main(int argc, char *argv[])
276 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
277 int planets = 100, xres = 800, yres = 600;
278 double total_mass = 50000;
281 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
282 fprintf(stderr, "Unable to initialize SDL: %s\n",
290 planets = atoi(argv[1]);
293 total_mass = atof(argv[2]);
296 range = atof(argv[3]);
298 screen = SDL_SetVideoMode(xres, yres, 32, flags);
299 if (screen == NULL) {
300 fprintf(stderr, "Unable to set video mode: %s\n",
305 SDL_WM_SetCaption(argv[0], NULL);
307 loop(screen, planets, total_mass, range);