From 27d337ab5c54f86324a536105d656781afc1ea4b Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 21 Apr 2016 15:01:11 +0200 Subject: [PATCH] Added FPS counter --- OpenGL1/Main.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/OpenGL1/Main.cpp b/OpenGL1/Main.cpp index a5eeedd..fad0cd0 100644 --- a/OpenGL1/Main.cpp +++ b/OpenGL1/Main.cpp @@ -8,7 +8,10 @@ void obj_cube(float clr); void obj_cube2(void); void set_view(int); void draw_string(std::string, int, int); +void calculateFPS(void); + +/* Variables */ float rotation = 0; float keyrotationx = 0; float keyrotationy = 0; @@ -19,6 +22,11 @@ bool wireframe = false; bool persp = true; bool rotatec = true; +int frameCount = 0; +int currentTime = 0; +int previousTime = 0; +int fps = 0; + float cameraRot = 0; void display() @@ -65,13 +73,17 @@ void display() //overlay set_view(1); + std::string str0 = "FPS: " + std::to_string(fps); std::string str1 = "Wireframe: " + std::string(wireframe ? "On" : "Off"); std::string str2 = "Projectie: " + std::string(persp ? "Perspective" : "Ortho"); std::string str3 = "Rotation: " + std::string(rotatec ? "On" : "Off"); - draw_string(str1, 10, 30); - draw_string(str2, 10, 60); - draw_string(str3, 10, 90); + draw_string(str0, 10, 30); + draw_string(str1, 10, 60); + draw_string(str2, 10, 90); + draw_string(str3, 10, 120); + + frameCount++; glutSwapBuffers(); } @@ -120,6 +132,7 @@ void idle() { rotation += 0.1f; glutPostRedisplay(); + calculateFPS(); } void keyboard(unsigned char key, int x, int y) @@ -164,6 +177,19 @@ void resize(int w, int h) height = w; } +void calculateFPS() +{ + currentTime = glutGet(GLUT_ELAPSED_TIME); + int timeInterval = currentTime - previousTime; + + if (timeInterval > 1000) + { + fps = frameCount / (timeInterval / 1000.0f); + previousTime = currentTime; + frameCount = 0; + } +} + int main(int argc, char *argv[]) { glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);