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;
28 static void clear_buf(SDL_Surface *screen)
31 unsigned int *buf = screen->pixels;
33 for (i = 0; i < screen->pitch * screen->h / 4; i++)
37 /* Return time in microseconds */
39 static unsigned long gettime(void)
43 static unsigned long start = 0;
45 clock_gettime(CLOCK_MONOTONIC, &tp);
47 ret = tp.tv_sec * 1000 * 1000;
48 ret += tp.tv_nsec / 1000;
63 static int poll_events(struct sim_status *status, double time)
65 static double time_scale_rate = 1;
67 struct camera *cam = status->cam;
69 status->time_scale *= pow(time_scale_rate, time);
71 while (SDL_PollEvent(&event)) {
74 switch (event.key.keysym.sym) {
78 cam->speed.x = -CAM_SPEED;
81 cam->speed.x = CAM_SPEED;
84 cam->speed.y = -CAM_SPEED;
87 cam->speed.y = CAM_SPEED;
91 cam->zoom_rate = CAM_ZOOM_RATE;
95 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
98 time_scale_rate = 1.5;
101 time_scale_rate = 1 / 1.5;
104 status->tracers_enabled =
105 !status->tracers_enabled;
111 switch (event.key.keysym.sym) {
142 case SDL_VIDEORESIZE:
143 status->screen = SDL_SetVideoMode(event.resize.w,
146 status->screen->flags);
155 printf("\nExiting. Good bye!\n");
159 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
162 struct sim_status status;
163 struct planet *planet, *pl1, *pl2;
164 struct camera camera;
166 int framecount = 0, last_fps_time = 0;
167 int last_framecount = 0, last_step_count = 0;
168 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
169 unsigned long step_count = 0;
170 double last_fps = 0, last_sps = 0;
171 double step_time = 0, true_time = 0;
173 init_camera(&camera);
175 status.cam = &camera;
176 status.time_scale = 1;
177 status.screen = screen;
178 status.tracers_enabled = 0;
180 planet = malloc(sizeof(*planet));
182 create_planets(planet, num_of_planets, total_mass, range);
184 ticks = SDL_GetTicks();
188 list_for_each_entry(pl1, &planet->list, list) {
189 pl2 = list_to_planet(pl1->list.next);
190 list_for_each_entry_from(pl2, &planet->list, list) {
192 if (!gravitize_planets(pl1, pl2,
196 ptmp = list_to_planet(pl2->list.prev);
197 merge_planets(pl1, pl2);
201 move_planet(pl1, step_time);
204 move_camera(&camera, true_time);
206 if (poll_events(&status, true_time))
211 true_time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
212 step_time = true_time * status.time_scale;
213 step_time = MIN(step_time, 0.02);
217 * Do not draw to the screen more often than MAS_FPS
220 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
223 last_frame_tick = ticks;
225 SDL_LockSurface(screen);
227 if (status.tracers_enabled &&
228 !camera.speed.x && !camera.speed.y &&
229 camera.zoom_rate == 1) {
230 fade_buf(screen, 10 * true_time);
235 list_for_each_entry(pl1, &planet->list, list) {
236 draw_planet(screen, pl1, &camera);
240 SDL_UnlockSurface(screen);
244 if (last_fps_time + (500 * 1000) < ticks) {
245 last_framecount = framecount - last_framecount;
246 last_fps = last_framecount * 1000 * 1000 /
247 (float)(ticks - last_fps_time);
248 last_framecount = framecount;
250 last_step_count = step_count - last_step_count;
251 last_sps = last_step_count * 1000 * 1000 /
252 (float)(ticks - last_fps_time);
253 last_step_count = step_count;
255 last_fps_time = ticks;
259 printf(" \rFrames/s: %.2f, steps/s: %.2f, planets: %d"
260 ", scale %.2f, zoom %.2f, step %ld",
261 last_fps, last_sps, planets, status.time_scale,
262 camera.zoom, step_count);
270 int main(int argc, char *argv[])
273 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
274 int planets = 100, xres = 800, yres = 600;
275 double total_mass = 50000;
278 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
279 fprintf(stderr, "Unable to initialize SDL: %s\n",
287 planets = atoi(argv[1]);
290 total_mass = atof(argv[2]);
293 range = atof(argv[3]);
295 screen = SDL_SetVideoMode(xres, yres, 32, flags);
296 if (screen == NULL) {
297 fprintf(stderr, "Unable to set video mode: %s\n",
302 SDL_WM_SetCaption(argv[0], NULL);
304 loop(screen, planets, total_mass, range);