11 static void fade_buf(SDL_Surface *screen, double amount)
14 unsigned char *buf = screen->pixels;
16 for (i = 0; i < screen->pitch * screen->h; i++)
17 buf[i] = (buf[i] < amount) ? 0 : buf[i] - amount;
20 static void clear_buf(SDL_Surface *screen)
23 unsigned int *buf = screen->pixels;
25 for (i = 0; i < screen->pitch * screen->h / 4; i++)
29 /* Return time in microseconds */
31 static unsigned long gettime(void)
35 static unsigned long start = 0;
37 clock_gettime(CLOCK_MONOTONIC, &tp);
39 ret = tp.tv_sec * 1000 * 1000;
40 ret += tp.tv_nsec / 1000;
55 static int poll_events(struct sim_status *status, double time)
57 static double time_scale_rate = 1;
59 struct camera *cam = status->cam;
61 status->time_scale *= pow(time_scale_rate, time);
63 while (SDL_PollEvent(&event)) {
66 switch (event.key.keysym.sym) {
70 cam->speed.x = -CAM_SPEED;
73 cam->speed.x = CAM_SPEED;
76 cam->speed.y = -CAM_SPEED;
79 cam->speed.y = CAM_SPEED;
83 cam->zoom_rate = CAM_ZOOM_RATE;
87 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
90 time_scale_rate = 1.5;
93 time_scale_rate = 1 / 1.5;
96 status->tracers_enabled =
97 !status->tracers_enabled;
103 switch (event.key.keysym.sym) {
134 case SDL_VIDEORESIZE:
135 status->screen = SDL_SetVideoMode(event.resize.w,
138 status->screen->flags);
147 printf("\nExiting. Good bye!\n");
151 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
154 struct sim_status status;
155 struct planet *planet, *pl1, *pl2;
156 struct camera camera;
158 int framecount = 0, last_fps_time = 0;
159 int last_framecount = 0, last_step_count = 0;
160 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
161 unsigned long step_count = 0;
162 double last_fps = 0, last_sps = 0;
163 double step_time = 0, true_time = 0;
165 init_camera(&camera);
167 status.cam = &camera;
168 status.time_scale = 1;
169 status.screen = screen;
170 status.tracers_enabled = 0;
172 planet = malloc(sizeof(*planet));
174 create_planets(planet, num_of_planets, total_mass, range);
176 ticks = SDL_GetTicks();
180 list_for_each_entry(pl1, &planet->list, list) {
181 pl2 = list_to_planet(pl1->list.next);
182 list_for_each_entry_from(pl2, &planet->list, list) {
184 if (!gravitize_planets(pl1, pl2,
188 ptmp = list_to_planet(pl2->list.prev);
189 merge_planets(pl1, pl2);
193 move_planet(pl1, step_time);
196 move_camera(&camera, true_time);
198 if (poll_events(&status, true_time))
203 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
204 step_time = true_time * status.time_scale;
205 step_time = MIN(step_time, 0.02);
209 * Do not draw to the screen more often than MAS_FPS
212 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
215 last_frame_tick = ticks;
217 SDL_LockSurface(screen);
219 if (status.tracers_enabled &&
220 !camera.speed.x && !camera.speed.y &&
221 camera.zoom_rate == 1) {
222 fade_buf(screen, 10 * true_time);
227 list_for_each_entry(pl1, &planet->list, list) {
228 draw_planet(screen, pl1, &camera);
232 SDL_UnlockSurface(screen);
236 if (last_fps_time + (500 * 1000) < ticks) {
237 last_framecount = framecount - last_framecount;
238 last_fps = last_framecount * 1000 * 1000 /
239 (float)(ticks - last_fps_time);
240 last_framecount = framecount;
242 last_step_count = step_count - last_step_count;
243 last_sps = last_step_count * 1000 * 1000 /
244 (float)(ticks - last_fps_time);
245 last_step_count = step_count;
247 last_fps_time = ticks;
251 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
252 ", scale %.2f, zoom %.2f, step %ld",
253 last_fps, last_sps, planets, status.time_scale,
254 camera.zoom, step_count);
262 int main(int argc, char *argv[])
265 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
266 int planets = 100, xres = 800, yres = 600;
267 double total_mass = 50000;
270 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
271 fprintf(stderr, "Unable to initialize SDL: %s\n",
279 planets = atoi(argv[1]);
282 total_mass = atof(argv[2]);
285 range = atof(argv[3]);
287 screen = SDL_SetVideoMode(xres, yres, 32, flags);
288 if (screen == NULL) {
289 fprintf(stderr, "Unable to set video mode: %s\n",
294 SDL_WM_SetCaption(argv[0], NULL);
296 loop(screen, planets, total_mass, range);