diff options
Diffstat (limited to 'sdl/pyramid.c')
-rw-r--r-- | sdl/pyramid.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/sdl/pyramid.c b/sdl/pyramid.c index 90f513c..e2d317c 100644 --- a/sdl/pyramid.c +++ b/sdl/pyramid.c @@ -6,6 +6,8 @@ #include <SDL/SDL_image.h> /* for some reason order of the headers matters */ #include "math3d.h" +#include <sys/time.h> +#define FRAMES_PER_SECOND 300 static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; @@ -293,13 +295,25 @@ int main(int argc, char **argv) Uint32 deltaclock = 0; Uint32 current_fps = 0; #endif + struct timeval m_LastCount; + struct timeval lcurrent; for (;;) { + gettimeofday(&m_LastCount, 0); #ifdef STAT_FPS startclock = SDL_GetTicks(); #endif processEvents(); render(); + + gettimeofday(&lcurrent, 0); + float fSeconds = (float) (lcurrent.tv_sec - m_LastCount.tv_sec); + float fFraction = (float) (lcurrent.tv_usec - m_LastCount.tv_usec) * 0.000001f; + float delta = fSeconds + fFraction; + + if (delta < 1000 / FRAMES_PER_SECOND) + SDL_Delay((1000 / FRAMES_PER_SECOND) - delta); + #ifdef STAT_FPS deltaclock = SDL_GetTicks() - startclock; if (deltaclock != 0 ) |