summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--fps.c27
-rw-r--r--fps.h9
-rw-r--r--glpong.c14
-rw-r--r--text.c1
-rw-r--r--text.h2
6 files changed, 44 insertions, 11 deletions
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 <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);
+}
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 <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);
}
diff --git a/text.c b/text.c
index cd90efe..2694e4b 100644
--- a/text.c
+++ b/text.c
@@ -1,4 +1,3 @@
-
#include <GL/glc.h>
#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 <GL/glc.h>
-
int GLPong_TextInit();
void GLPong_TextDeinit();
void GLPong_TextDrawFPS(GLfloat fps);