int main(int argc, char** argv) { if (!setup()) { return 1; } map map(64, 64); map.cell(10, 10) = 1; map.cell(10, 11) = 1; map.cell(10, 12) = 1; map.cell(10, 13) = 1; map.cell(10, 14) = 1; map.cell(10, 15) = 1; player player(vec2(5.0f, 5.0f)); rendering_init(); float old_time = glfwGetTime(); while (glfwGetWindowParam(GLFW_OPENED)) { float time = glfwGetTime(); float elapsed = time - old_time; old_time = time; player.update(elapsed); map.raycast(player.pos, vec2(cos(player.rot), sin(player.rot)).normalized() * 2.0f); rendering_update(); glfwSwapBuffers(); } rendering_close(); }
int main(int argc, const char ** argv) { struct timeval start_time, end_time; struct timespec tspec = {0, 0}; struct timespec left = {0, 0}; int error; long long elapsed; float cam_center_x, cam_center_y; if (swiftsure_log_init() < 0) { printf("Bad times, we couldn't open our log file =(\n"); return -1; } rendering_init(); if (input_init() < 0) { printf("Bad times, we couldn't initialize the input subsystem\n"); return -1; } action_init(); world.width = WORLD_SIZE; world.height = WORLD_SIZE; world_init(&world, 1); game_init(); swiftsure_log(INFO, "Startin engines\n"); frame = 0; elapsed = 16666; while (1) { gettimeofday(&start_time, NULL); game_get_avg_pos(&cam_center_x, &cam_center_y); render_set_camera(-cam_center_x, -cam_center_y, 4); handle_events(); action_perform(); render_start_frame(); render_world(&world); game_render_players(); render_end_frame(); ++frame; physics_tick(&world, 10. / elapsed); //Rate limiting gettimeofday(&end_time, NULL); elapsed = (end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_usec - start_time.tv_usec); if (elapsed < 16666) { tspec.tv_nsec = (16666 - elapsed) * 1000; error = nanosleep(&tspec, &left); if (error < 0) { swiftsure_log(DEBUG, "We had %d seconds and %d nanoseconds left to sleep, errno %d\n", left.tv_sec, left.tv_nsec, errno); } } } }