diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | fps.c | 27 | ||||
-rw-r--r-- | fps.h | 9 | ||||
-rw-r--r-- | glpong.c | 14 | ||||
-rw-r--r-- | text.c | 1 | ||||
-rw-r--r-- | text.h | 2 |
6 files changed, 44 insertions, 11 deletions
@@ -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} @@ -0,0 +1,27 @@ +#include <time.h> + +#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); +} @@ -0,0 +1,9 @@ +#ifndef FPS_H +#define FPS_H + +void GLPong_FPSInit(); +void GLPong_FPSCount(); +void GLPong_FPSPrint(); + +#endif + @@ -5,9 +5,11 @@ #include "SDL.h" #include "SDL_opengl.h" #include "SDL_image.h" +#include <GL/glc.h> #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); } @@ -1,4 +1,3 @@ - #include <GL/glc.h> #include "text.h" @@ -1,8 +1,6 @@ #ifndef TEXT_H #define TEXT_H -#include <GL/glc.h> - int GLPong_TextInit(); void GLPong_TextDeinit(); void GLPong_TextDrawFPS(GLfloat fps); |