fix build
[gltest.git] / gltest.cpp
index c66ab32c40efd5db2460da248ad8772e41672d75..b8d3d0e6a54dc9ba9f9f1ef68ad5ed5eec257ddb 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 // stdlib includes
+#include <iostream>
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <unistd.h>
 #include <assert.h>
-#include <GL/gl.h>
 #include <boost/program_options.hpp>
+#include <X11/keysym.h>
 
 namespace po = boost::program_options;
 
-// include proper GL connector
+// my GL utility functions and the GL window class
+#include "glutil.h"
 #include "glwindow.h"
-#if defined(USE_GLX)
+// include proper GL backend (windowing system)
+#if defined(WIN_GLX)
 #include "glxbackend.h"
 static GLBackend *createGLBackend()
 {
        return new GLXBackend();
 }
-#elif defined(USE_EGL)
+#elif defined(WIN_EGL)
 #include "eglbackend.h"
 static GLBackend *createGLBackend()
 {
@@ -52,8 +55,8 @@ static const GLfloat boxWidth = 0.045f;
 static const GLfloat boxSpeed = 1.25f; // per second
 
 // profiler
-enum ProfilerState { statePreRender, stateClear, stateDraw, statePresent, statePostRender, stateOutsideRender, numProfilerStates };
-static const char *profilerStateNames[numProfilerStates] = { "Pre-Render", "Clearing", "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()
@@ -63,24 +66,29 @@ static double getTime()
        return tp.tv_sec + 1e-9 * tp.tv_nsec;
 }
 
-static void rectQuad(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat 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, int sleep_time) : GLWindow(XOpenDisplay(0), createGLBackend()),
-               overdraw(overdraw), copy(copy), sleep_time(sleep_time), boxPos(0), boxDirection(1)
+       TearTestWindow() : GLWindow(XOpenDisplay(0), createGLBackend()),
+               overdraw(false), sleep_time(0), boxPos(0), boxDirection(1)
        {}
 
-       void setSwapInterval(int i) {
-               getBackend()->setSwapInterval(i);
-       }
+       void setOverdraw(bool overdraw) { this->overdraw = overdraw; }
+       void setSleepTime(int sleep_time) { this->sleep_time = sleep_time; }
+
+       void setSwapInterval(int i) { getBackend()->setSwapInterval(i); }
+
+private:
+       bool overdraw;
+       int sleep_time;
+       // animation control
+       double lastFrame;
+       GLfloat boxPos, boxDirection;
+       // FPS, profiler
+       double lastDisplay, lastProfile;                 
+       int framect;
+       ProfilerState curState;
+       double stateTime[NumProfilerStates];
 
 protected:
        virtual void initGL()
@@ -93,29 +101,22 @@ protected:
                // initailize profiler
                framect = 0;
                memset(stateTime, 0, sizeof(stateTime));
-               curState = -1;
+               curState = NumProfilerStates;
                lastDisplay = lastProfile = getTime();
        }
        
        virtual void resizeGL(unsigned int width, unsigned int height)
        {
-               /* prevent divide-by-zero */                      
-               if (height == 0)                                  
-                       height = 1;                                   
                glViewport(0, 0, width, height);
-               glMatrixMode(GL_PROJECTION);                      
-               glLoadIdentity();                                 
-               glOrtho (0, 1, 1, 0, 0, 1);
-               glMatrixMode(GL_MODELVIEW);      
-               glClear(GL_COLOR_BUFFER_BIT);                                                 
-               glFlush();
+               initialise2dProjection();
+               glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        }
        
        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;
@@ -123,7 +124,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);
                        }
@@ -137,7 +138,7 @@ protected:
        void renderGL()
        {              
                //////////////////////////////////////////////
-               profilerTick(statePreRender);
+               profilerTick(StatePreRender);
                double time = getTime();
                // anim
                double passedTime = time-lastFrame;
@@ -154,40 +155,27 @@ protected:
                }
                lastFrame = time;
                //////////////////////////////////////////////
-               profilerTick(stateClear);
+               profilerTick(StateClear);
                if (overdraw) {
                        // clear manually
-                       glBegin(GL_QUADS);
-                       glColor3f(0.0f, 0.0f, 0.0f);
-                       rectQuad(0, 0, 1, 1);
-                       glEnd();
+                       drawQuad(/*color*/0.0f, 0.0f, 0.0f, /*coordinates*/0, 0, 1, 1);
                }
                else {
                        glClear(GL_COLOR_BUFFER_BIT);
                }
                //////////////////////////////////////////////
-               profilerTick(stateDraw);
-               glBegin(GL_QUADS);
-               glColor3f(0.8f, 1.0f, 0.75f);
-               rectQuad(boxPos, 0, boxPos+boxWidth, 1);
-               glEnd();
+               profilerTick(StateDraw);
+               drawQuad(/*color*/0.8f, 1.0f, 0.75f, /*coordinates*/boxPos, 0, boxPos+boxWidth, 1);
                usleep(sleep_time*1000);
                //////////////////////////////////////////////
-               profilerTick(statePresent);
-               if (copy) {
-                       glDrawBuffer(GL_FRONT);
-                       glCopyPixels(0, 0, getWidth(), getHeight(), GL_COLOR);
-                       glDrawBuffer(GL_BACK);
-               }
-               else {
-                       getBackend()->swapBuffers();
-               }
+               profilerTick(StatePresent);
+               getBackend()->swapBuffers();
                //////////////////////////////////////////////
-               profilerTick(statePostRender);
-               glFlush();
+               profilerTick(StatePostRender);
+               glFinish();
                ++framect;
                //////////////////////////////////////////////
-               profilerTick(stateOutsideRender);
+               profilerTick(StateOutsideRender);
        }
        
        virtual void handleKeyPress(KeySym key)
@@ -198,17 +186,6 @@ protected:
                        default: break;
                }
        }
-
-private:
-       bool overdraw, copy;
-       int sleep_time;
-       // animation control
-       double lastFrame;
-       GLfloat boxPos, boxDirection;
-       // FPS, profiler
-       double lastDisplay, lastProfile;                 
-       int framect, curState;
-       double stateTime[numProfilerStates];
 };
 
 int main(int argc, char ** argv)
@@ -218,9 +195,8 @@ int main(int argc, char ** argv)
        desc.add_options()
                ("help,h", "produce help message")
                ("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)")
+               ("sleep,s", po::value<int>()->default_value(0), "Number of milliseconds to sleep in each frame (in the drawing phase)")
        ;
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -232,7 +208,9 @@ int main(int argc, char ** argv)
        }
 
        // actual program
-       TearTestWindow w(vm.count("overdraw"), vm.count("copy"), vm["sleep"].as<int>());
+       TearTestWindow w;
+       w.setOverdraw(vm.count("overdraw"));
+       w.setSleepTime(vm["sleep"].as<int>());
        w.open(800, 600);
        if (vm.count("swap-interval"))
                w.setSwapInterval(vm["swap-interval"].as<int>());