From a8921d2baf7fe632839ec4c1a6f55db9c78f1c1c Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sat, 1 Mar 2008 19:37:00 +0000 Subject: Move frames per second stuff into separate files git-svn-id: svn://mattst88.com/svn/glpong/trunk@14 449aeecc-241a-0410-943f-e2f883e2d7a2 --- Makefile | 2 +- fps.c | 27 +++++++++++++++++++++++++++ fps.h | 9 +++++++++ glpong.c | 14 +++++++------- text.c | 1 - text.h | 2 -- 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 fps.c create mode 100644 fps.h diff --git a/Makefile b/Makefile index 94d9c57..4259e2c 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ EXT=.exe EXE=glpong${EXT} -OBJS=glpong.o ball.o text.o #paddle.o +OBJS=glpong.o ball.o text.o fps.o #paddle.o all: ${OBJS} ${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES} diff --git a/fps.c b/fps.c new file mode 100644 index 0000000..4379e38 --- /dev/null +++ b/fps.c @@ -0,0 +1,27 @@ +#include + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "text.h" +#include "fps.h" + +static clock_t start; +static GLfloat fps; + +void +GLPong_FPSInit() { + start = SDL_GetTicks(); +} + +void +GLPong_FPSCount() { + // 10.0f is the number of frames we let draw before calculating FPS + fps = 10.0f / (SDL_GetTicks() - start) * 1000; // 1 sec / 1 millisec == 1000 + start = SDL_GetTicks(); +} + +void +GLPong_FPSPrint() { + printf("%.2f frames per second\n", fps); +} diff --git a/fps.h b/fps.h new file mode 100644 index 0000000..4b965b9 --- /dev/null +++ b/fps.h @@ -0,0 +1,9 @@ +#ifndef FPS_H +#define FPS_H + +void GLPong_FPSInit(); +void GLPong_FPSCount(); +void GLPong_FPSPrint(); + +#endif + diff --git a/glpong.c b/glpong.c index cca1238..a1d9b53 100644 --- a/glpong.c +++ b/glpong.c @@ -5,9 +5,11 @@ #include "SDL.h" #include "SDL_opengl.h" #include "SDL_image.h" +#include #include "glpong.h" #include "text.h" +#include "fps.h" static int GLPong_Init(GLPong_t * GLPong); static void GLPong_Deinit(GLPong_t * GLPong); @@ -25,8 +27,6 @@ int main(int argc, char * argv[]) { GLPONG_HEIGHT }; - clock_t start; - GLfloat fps; unsigned int frames = 0; int action; int done = 0; @@ -48,7 +48,7 @@ int main(int argc, char * argv[]) { printf("%d balls\n", length(GLPong.balls)); #endif - start = SDL_GetTicks(); + GLPong_FPSInit(); while ( !done ) { action = GLPong_HandleEvents(); @@ -69,14 +69,14 @@ int main(int argc, char * argv[]) { SDL_GL_SwapBuffers(); /* end drawing */ - + frames++; if (frames == 10) { - fps = (GLfloat) frames / (SDL_GetTicks() - start) * 1000; + GLPong_FPSCount(); frames = 0; - start = SDL_GetTicks(); - //printf("%.2f frames per second\n", GLPong.fps); + GLPong_FPSPrint(); } + SDL_Delay(10); } diff --git a/text.c b/text.c index cd90efe..2694e4b 100644 --- a/text.c +++ b/text.c @@ -1,4 +1,3 @@ - #include #include "text.h" diff --git a/text.h b/text.h index dfc3458..456fb8b 100644 --- a/text.h +++ b/text.h @@ -1,8 +1,6 @@ #ifndef TEXT_H #define TEXT_H -#include - int GLPong_TextInit(); void GLPong_TextDeinit(); void GLPong_TextDrawFPS(GLfloat fps); -- cgit v1.2.3