From 2ac02c7158605c65d84983409e38fca175896762 Mon Sep 17 00:00:00 2001 From: Kamil Kaminski Date: Sun, 3 Oct 2010 02:44:51 -0500 Subject: pyramid: cap to 300 fps --- sdl/math3d.c | 2 +- sdl/pyramid.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sdl/math3d.c b/sdl/math3d.c index cdfb8cd..7baad85 100644 --- a/sdl/math3d.c +++ b/sdl/math3d.c @@ -611,7 +611,7 @@ GLbyte *gltLoadTGA(const char *szFileName, GLint *iWidth, GLint *iHeight, if (pBits == NULL) { perror("malloc"); - return 0; + return NULL; } /* read in the bits */ 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 /* for some reason order of the headers matters */ #include "math3d.h" +#include +#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 ) -- cgit v1.2.3