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;
48 static int poll_events(SDL_Surface **screen, struct camera *cam,
49 double *time_scale, double time)
51 static double time_scale_rate = 1;
54 *time_scale *= pow(time_scale_rate, time);
56 while (SDL_PollEvent(&event)) {
59 switch (event.key.keysym.sym) {
63 cam->speed.x = -CAM_SPEED;
66 cam->speed.x = CAM_SPEED;
69 cam->speed.y = -CAM_SPEED;
72 cam->speed.y = CAM_SPEED;
76 cam->zoom_rate = CAM_ZOOM_RATE;
80 cam->zoom_rate = 1 / CAM_ZOOM_RATE;
83 time_scale_rate = 1.5;
86 time_scale_rate = 1 / 1.5;
93 switch (event.key.keysym.sym) {
124 case SDL_VIDEORESIZE:
125 *screen = SDL_SetVideoMode(event.resize.w,
137 printf("\nExiting. Good bye!\n");
141 static void loop(SDL_Surface *screen, int num_of_planets, double total_mass,
144 struct planet *planet, *pl1, *pl2;
145 struct camera camera;
147 int framecount = 0, last_fps_time = 0;
148 int last_framecount = 0, last_step_count = 0;
149 unsigned long old_ticks, ticks = 0, last_frame_tick = 0;
150 unsigned long step_count = 0;
151 double time = 0, last_fps = 0, time_scale = 1, last_sps = 0;
153 init_camera(&camera);
155 planet = malloc(sizeof(*planet));
157 create_planets(planet, num_of_planets, total_mass, range);
159 ticks = SDL_GetTicks();
163 list_for_each_entry(pl1, &planet->list, list) {
164 pl2 = list_to_planet(pl1->list.next);
165 list_for_each_entry_from(pl2, &planet->list, list) {
167 if (!gravitize_planets(pl1, pl2,
171 ptmp = list_to_planet(pl2->list.prev);
172 merge_planets(pl1, pl2);
176 move_planet(pl1, time * time_scale);
179 move_camera(&camera, time);
181 if (poll_events(&screen, &camera, &time_scale, time))
186 time = (ticks - old_ticks) / (1000.0 * 1000.0) ;
187 time = MIN(time, 0.02);
191 * Do not draw to the screen more often than MAS_FPS
194 if (last_frame_tick + (1000 * 1000) / MAX_FPS > ticks)
197 last_frame_tick = ticks;
199 SDL_LockSurface(screen);
202 list_for_each_entry(pl1, &planet->list, list) {
203 draw_planet(screen, pl1, &camera);
207 SDL_UnlockSurface(screen);
211 if (last_fps_time + (500 * 1000) < ticks) {
212 last_framecount = framecount - last_framecount;
213 last_fps = last_framecount * 1000 * 1000 /
214 (float)(ticks - last_fps_time);
215 last_framecount = framecount;
217 last_step_count = step_count - last_step_count;
218 last_sps = last_step_count * 1000 * 1000 /
219 (float)(ticks - last_fps_time);
220 last_step_count = step_count;
222 last_fps_time = ticks;
226 printf(" \rFrames/s: %.2f, planets: %d"
227 ", scale %.2f, zoom %.2f, steps/s: %.2f, %ld",
228 last_fps, planets, time_scale,
229 camera.zoom, last_sps, step_count);
236 int main(int argc, char *argv[])
239 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
240 int planets = 100, xres = 800, yres = 600;
241 double total_mass = 50000;
244 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
245 fprintf(stderr, "Unable to initialize SDL: %s\n",
253 planets = atoi(argv[1]);
256 total_mass = atof(argv[2]);
259 range = atof(argv[3]);
261 screen = SDL_SetVideoMode(xres, yres, 32, flags);
262 if (screen == NULL) {
263 fprintf(stderr, "Unable to set video mode: %s\n",
268 SDL_WM_SetCaption(argv[0], NULL);
270 loop(screen, planets, total_mass, range);