Make the state names capital-first
[gltest.git] / gltest.cpp
index 0cae93287a6f021270c0f739a3f9a1a3ad519f9a..ed45ac677a311bad053024b7421b267e1deb3050 100644 (file)
@@ -1,3 +1,21 @@
+/* gltest - small OpenGL tearing test program
+ * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
 // stdlib includes
 #include <time.h>
 #include <stdio.h>
@@ -34,8 +52,8 @@ static const GLfloat boxWidth = 0.045f;
 static const GLfloat boxSpeed = 1.25f; // per second
 
 // profiler
-enum ProfilerState { statePreRender, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
-static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
+enum ProfilerState { StatePreRender, StateClear, StateDraw, StatePresent, StatePostRender, StateOutsideRender, NumProfilerStates };
+static const char *profilerStateNames[NumProfilerStates] = { "Pre-Render", "Clearing", "Drawing", "Presenting", "Post-Render", "Outside renderer"};
 
 // utility functions
 static double getTime()
@@ -45,15 +63,19 @@ static double getTime()
        return tp.tv_sec + 1e-9 * tp.tv_nsec;
 }
 
-static void drawRect(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
+static void rectQuad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
 {
-       glVertex2f(x1, y1); glVertex2f(x2, y1); glVertex2f(x2, y2); glVertex2f(x1, y2);
+       glVertex2f(x1, y1);
+       glVertex2f(x2, y1);
+       glVertex2f(x2, y2);
+       glVertex2f(x1, y2);
 }
 
 // the window
 class TearTestWindow : public GLWindow {
 public:
-       TearTestWindow(bool overdraw, bool copy) : GLWindow(XOpenDisplay(0), createGLBackend()), overdraw(overdraw), copy(copy), boxPos(0), boxDirection(1)
+       TearTestWindow(bool overdraw, bool copy, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
+               overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
        {}
 
        void setSwapInterval(int i) {
@@ -71,7 +93,7 @@ protected:
                // initailize profiler
                framect = 0;
                memset(stateTime, 0, sizeof(stateTime));
-               curState = -1;
+               curState = NumProfilerStates;
                lastDisplay = lastProfile = getTime();
        }
        
@@ -91,9 +113,9 @@ protected:
        
        void profilerTick(ProfilerState nextState)
        {
-               assert (nextState >= 0 && nextState < numProfilerStates);
+               assert (nextState >= 0 && nextState < NumProfilerStates);
                double time = getTime();
-               if (curState >= 0)
+               if (curState >= 0 && curState < NumProfilerStates)
                        stateTime[curState] += time-lastProfile;
                curState = nextState;
                lastProfile = time;
@@ -101,7 +123,7 @@ protected:
                const double elapsed = time-lastDisplay;
                if (elapsed >= 3) {
                        printf("%.1f fps, time spent: ", framect/elapsed);
-                       for (int i = 0; i < numProfilerStates; ++i) {
+                       for (int i = 0; i < NumProfilerStates; ++i) {
                                if (i != 0) printf(", ");
                                printf("%s %.1f%%", profilerStateNames[i], stateTime[i]/elapsed*100);
                        }
@@ -115,7 +137,7 @@ protected:
        void renderGL()
        {              
                //////////////////////////////////////////////
-               profilerTick(statePreRender);
+               profilerTick(StatePreRender);
                double time = getTime();
                // anim
                double passedTime = time-lastFrame;
@@ -132,22 +154,26 @@ protected:
                }
                lastFrame = time;
                //////////////////////////////////////////////
-               profilerTick(stateDraw);
+               profilerTick(StateClear);
                if (overdraw) {
-                       glBegin(GL_QUADS);
                        // clear manually
+                       glBegin(GL_QUADS);
                        glColor3f(0.0f, 0.0f, 0.0f);
-                       drawRect(0, 0, 1, 1);
+                       rectQuad(0, 0, 1, 1);
+                       glEnd();
                }
                else {
                        glClear(GL_COLOR_BUFFER_BIT);
-                       glBegin(GL_QUADS);
                }
+               //////////////////////////////////////////////
+               profilerTick(StateDraw);
+               glBegin(GL_QUADS);
                glColor3f(0.8f, 1.0f, 0.75f);
-               drawRect(boxPos, 0, boxPos+boxWidth, 1);
+               rectQuad(boxPos, 0, boxPos+boxWidth, 1);
                glEnd();
+               usleep(sleep_time*1000);
                //////////////////////////////////////////////
-               profilerTick(statePresent);
+               profilerTick(StatePresent);
                if (copy) {
                        glDrawBuffer(GL_FRONT);
                        glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
@@ -157,11 +183,11 @@ protected:
                        getBackend()->swapBuffers();
                }
                //////////////////////////////////////////////
-               profilerTick(statePostRender);
+               profilerTick(StatePostRender);
                glFlush();
                ++framect;
                //////////////////////////////////////////////
-               profilerTick(stateOutsideRender);
+               profilerTick(StateOutsideRender);
        }
        
        virtual void handleKeyPress(KeySym key)
@@ -175,16 +201,17 @@ protected:
 
 private:
        bool overdraw, copy;
+       int sleep_time;
+       // animation control
        double lastFrame;
        GLfloat boxPos, boxDirection;
-       // FPS
+       // FPS, profiler
        double lastDisplay, lastProfile;                 
-       int framect, curState;
-       double stateTime[numProfilerStates];
+       int framect;
+       ProfilerState curState;
+       double stateTime[NumProfilerStates];
 };
 
-
-
 int main(int argc, char ** argv)
 {
        // program options handling
@@ -194,6 +221,7 @@ int main(int argc, char ** argv)
                ("swap-interval,i", po::value<int>(), "set swap interval")
                ("copy,c", "copy to front buffer (instead of performing a buffer swap)")
                ("overdraw,o", "overdraw previous image (instead of calling glClear)")
+               ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleap in each frame (in the drawing phase)")
        ;
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -205,7 +233,7 @@ int main(int argc, char ** argv)
        }
 
        // actual program
-       TearTestWindow w(vm.count("overdraw"), vm.count("copy"));
+       TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
        w.open(800, 600);
        if (vm.count("swap-interval"))
                w.setSwapInterval(vm["swap-interval"].as<int>());