diff --git a/main.c b/main.c index a3fdb8d21ba34a00696e5a7da8d0af8e3ce4eeb7..bcd78bb5e4bd52c66546f7cf05ee816f7ab39312 100644 --- a/main.c +++ b/main.c @@ -21,41 +21,31 @@ int main() { return EXIT_FAILURE; } - int32_t time_elapsing_per_second = 3600 * 24 * 365; - int32_t update_per_second = 1000; - printf("%d\n", time_elapsing_per_second / update_per_second); - SolarSystem *solar_system = solar_system_create(time_elapsing_per_second / update_per_second); - int32_t hz = 240; - int32_t a = 0; - int32_t b = 0; - int32_t elapsed_seconds = 0; + int32_t time_elapsing_per_second = 3600 * 24 * 100; + int32_t refresh_rate = 240; + int32_t elapsed_time = 0; + double sleep_duration = 1.0 / refresh_rate * 1E9; + SolarSystem *solar_system = solar_system_create(time_elapsing_per_second / refresh_rate); + + int64_t tmp = time(NULL) % 1000; while (true) { solar_system_update(solar_system); - b += 1; - - if ((double)b >= (double)update_per_second / hz * (a + 1)) { - a += 1; - - gfx_present(context); - solar_system_draw(solar_system, context); - - if (b >= update_per_second) { - b = 0; - elapsed_seconds += 1; - char title[100]; - sprintf(title, "Solar System (%d)", elapsed_seconds); - SDL_SetWindowTitle(context->window, title); - } - if (a % hz == 0) - a = 0; + gfx_present(context); + solar_system_draw(solar_system, context); + + if (elapsed_time % refresh_rate == 0) { + char title[100]; + sprintf(title, "Solar System (%d : %ld)", elapsed_time / refresh_rate, (time(NULL) - tmp) % 1000); + SDL_SetWindowTitle(context->window, title); } if (gfx_keypressed() == SDLK_ESCAPE) break; - struct timespec t = {.tv_sec = 0, .tv_nsec = 1.0 / update_per_second * 1E9}; + struct timespec t = {.tv_sec = 0, .tv_nsec = sleep_duration}; nanosleep(&t, NULL); + elapsed_time += 1; } gfx_destroy(context);