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); } } } }
void tick() { double dt = delta_time(); physics_tick(dt); player_move(dt); }
int main(void) { /* Initialize the library */ if (!glfwInit()) return -1; /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } int tiles[4 * 4] = { 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, }; Game game; game.tiles.tiles = tiles; game.tiles.width = 4; game.tiles.height = 4; game.players[0].position = vec2(2.0f, 6.0f); game.players[0].velocity = vec2(0.0f, 0.0f); game.players[0].size = vec2(0.5f, 0.8f); /* Make the window's context current */ glfwMakeContextCurrent(window); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, 10.0f, 0.0f, 10.0f, -1.0f, 1.0f); update_input(&game); physics_tick(&game, 0.016f); render(&game); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; }