diff options
author | Kamil Kaminski <nfm@phenon.localdomain> | 2010-10-03 02:44:51 -0500 |
---|---|---|
committer | Kamil Kaminski <nfm@phenon.localdomain> | 2010-10-03 02:44:51 -0500 |
commit | 2ac02c7158605c65d84983409e38fca175896762 (patch) | |
tree | 3f69ca3e6a6a7b1a39a8dfaa78cda9ba692d0a24 | |
parent | 9165d25bdcba577e53ee8022067c0984484a449b (diff) | |
download | GLPyramid-2ac02c7158605c65d84983409e38fca175896762.tar.gz GLPyramid-2ac02c7158605c65d84983409e38fca175896762.tar.bz2 GLPyramid-2ac02c7158605c65d84983409e38fca175896762.zip |
pyramid: cap to 300 fps
-rw-r--r-- | sdl/math3d.c | 2 | ||||
-rw-r--r-- | sdl/pyramid.c | 14 |
2 files changed, 15 insertions, 1 deletions
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 <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 ) |