diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.c | 2 | ||||
-rw-r--r-- | src/main.c | 24 | ||||
-rw-r--r-- | src/physics.c | 5 | ||||
-rw-r--r-- | src/render.c | 6 | ||||
-rw-r--r-- | src/types.h | 4 |
5 files changed, 30 insertions, 11 deletions
@@ -239,6 +239,8 @@ Game *game_new(int width, int height) { g->view_dist = 5.0; g->paused = 0; g->pause_rel = 1; + g->frame_delay = 8000; + g->frame_delay_fiddled = 0; /* Renderer setup */ g->render = render_setup(width, height); @@ -24,6 +24,7 @@ #include "game.h" #include "render.h" #include "physics.h" +#include "utils.h" int main(int argc, char *argv[]) { @@ -42,7 +43,8 @@ int main(int argc, char *argv[]) { SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); width = 1680; height = 1050; - screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL | SDL_FULLSCREEN); + width=640;height=480; + screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL);// | SDL_FULLSCREEN); if ( screen == NULL ) { width = 1280; height = 1024; screen = SDL_SetVideoMode(width, height, 16, SDL_OPENGL | SDL_FULLSCREEN); @@ -99,8 +101,24 @@ int main(int argc, char *argv[]) { render_draw(game); } - /* Sleep for around 8ms to avoid hogging the CPU. This value gives about 100fps on my (rather modest) machine. */ - usleep(8000); + printf("%+7.4f %+7.4f %+7.4f %+5.1f deg %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i %3i fps\r", + game->lander->x, game->lander->y, game->lander->z, + rad2deg(game->lander->yaw), game->lander->vx, game->lander->vy, game->lander->vz, + game->cur_room_x, game->cur_room_y, game->cur_room_z, game->num_rooms, game->render->fps); + fflush(stdout); + + /* Attempt to hold output FPS close to 100 */ + if ( (!game->frame_delay_fiddled) && (game->render->fps < 90) ) { + game->frame_delay -= game->frame_delay/20; + game->frame_delay_fiddled = 1; + } + if ( (!game->frame_delay_fiddled) && (game->render->fps > 110) ) { + game->frame_delay += game->frame_delay/20; + game->frame_delay_fiddled = 1; + } + + /* Sleep for a bit to avoid hogging the CPU. */ + usleep(game->frame_delay); } diff --git a/src/physics.c b/src/physics.c index 555e13b..4c1ac86 100644 --- a/src/physics.c +++ b/src/physics.c @@ -115,11 +115,6 @@ void physics_step(Game *game) { } physics_process(game->lander, dt); game_check_handoff(game); - printf("%+7.4f %+7.4f %+7.4f %+5.1f deg %+7.5f %+7.5f %+7.5f %2i %2i %2i %3i %3i fps\r", - game->lander->x, game->lander->y, game->lander->z, - rad2deg(game->lander->yaw), game->lander->vx, game->lander->vy, game->lander->vz, - game->cur_room_x, game->cur_room_y, game->cur_room_z, game->num_rooms, game->render->fps); - fflush(stdout); for ( j=0; j<game->num_rooms; j++ ) { diff --git a/src/render.c b/src/render.c index 81d5ba3..97b839f 100644 --- a/src/render.c +++ b/src/render.c @@ -352,10 +352,12 @@ void render_draw(Game *game) { SDL_GL_SwapBuffers(); game->render->frames++; - if ( t - game->render->t_fps > 1000 ) { - game->render->fps = (1000*game->render->frames) / (t - game->render->t_fps); + /* Calculate FPS every half a second */ + if ( t - game->render->t_fps > 500 ) { + game->render->fps = (500*game->render->frames) / (t - game->render->t_fps); game->render->t_fps = t; game->render->frames = 0; + game->frame_delay_fiddled = 0; } } diff --git a/src/types.h b/src/types.h index deb083a..5c1ba53 100644 --- a/src/types.h +++ b/src/types.h @@ -150,7 +150,9 @@ typedef struct { unsigned int forward; unsigned int reverse; - Uint32 tlast; /* Time at which the last physics step was performed */ + Uint32 tlast; /* Time at which the last physics step was performed */ + int frame_delay; /* Delay between frames */ + int frame_delay_fiddled; /* frame_delay has been fiddled since the last FPS calculation */ ModelInstance *lander; |