backported fixes from deeprunner
[miniglut] / test.c
diff --git a/test.c b/test.c
index 6e68405..57a22e4 100644 (file)
--- a/test.c
+++ b/test.c
@@ -1,3 +1,5 @@
+#include <stdio.h>
+#include <string.h>
 #include <math.h>
 #include "miniglut.h"
 
@@ -23,13 +25,36 @@ int mouse_x, mouse_y;
 int bnstate[8];
 int anim;
 float torus_pos[3], torus_rot[4] = {0, 0, 0, 1};
+int teapot, torus, cone, sphere;
+long nframes;
 
+#ifndef GL_FRAMEBUFFER_SRGB
+#define GL_FRAMEBUFFER_SRGB    0x8db9
+#endif
+
+#ifndef GL_MULTISAMPLE
+#define GL_MULTISAMPLE 0x809d
+#endif
 
 int main(int argc, char **argv)
 {
+       int i, test_aa = 0, test_srgb = 0;
+       unsigned int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
+
+       for(i=1; i<argc; i++) {
+               if(strcmp(argv[i], "-ms") == 0) {
+                       test_aa = 1;
+               } else if(strcmp(argv[i], "-srgb") == 0) {
+                       test_srgb = 1;
+               }
+       }
+
+       if(test_aa) mode |= GLUT_MULTISAMPLE;
+       if(test_srgb) mode |= GLUT_SRGB;
+
        glutInit(&argc, argv);
-       glutInitWindowSize(1024, 768);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
+       glutInitWindowSize(800, 600);
+       glutInitDisplayMode(mode);
        glutCreateWindow("miniglut test");
 
        glutDisplayFunc(display);
@@ -46,6 +71,33 @@ int main(int argc, char **argv)
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
 
+       if(test_aa) {
+               glEnable(GL_MULTISAMPLE);
+       }
+       if(test_srgb) {
+               glEnable(GL_FRAMEBUFFER_SRGB);
+       }
+
+       torus = glGenLists(1);
+       glNewList(torus, GL_COMPILE);
+       glutSolidTorus(0.3, 1, 16, 24);
+       glEndList();
+
+       cone = glGenLists(1);
+       glNewList(cone, GL_COMPILE);
+       glutSolidCone(1.1, 2, 16, 2);
+       glEndList();
+
+       sphere = glGenLists(1);
+       glNewList(sphere, GL_COMPILE);
+       glutSolidSphere(0.4, 16, 8);
+       glEndList();
+
+       teapot = glGenLists(1);
+       glNewList(teapot, GL_COMPILE);
+       glutSolidTeapot(1.0);
+       glEndList();
+
        glutMainLoop();
        return 0;
 }
@@ -77,10 +129,10 @@ void display(void)
                glRotatef(tm / 10.0f, 1, 0, 0);
                glRotatef(tm / 10.0f, 0, 1, 0);
        }
-       glutSolidTorus(0.3, 1, 16, 24);
+       glCallList(torus);
        glPopMatrix();
 
-       glutSolidSphere(0.4, 16, 8);
+       glCallList(sphere);
 
        glPushMatrix();
        glTranslatef(torus_pos[0] - 2.5, torus_pos[1], torus_pos[2]);
@@ -92,7 +144,14 @@ void display(void)
        glPushMatrix();
        glTranslatef(2.5, -1, 0);
        glRotatef(-90, 1, 0, 0);
-       glutSolidCone(1.1, 2, 16, 2);
+       glCallList(cone);
+       glPopMatrix();
+
+       glPushMatrix();
+       glTranslatef(0, -0.5, 2.5);
+       glFrontFace(GL_CW);
+       glCallList(teapot);
+       glFrontFace(GL_CCW);
        glPopMatrix();
 
        glBegin(GL_QUADS);
@@ -104,6 +163,7 @@ void display(void)
        glEnd();
 
        glutSwapBuffers();
+       nframes++;
 }
 
 #define ZNEAR  0.5f
@@ -121,6 +181,7 @@ void keypress(unsigned char key, int x, int y)
 {
        static int fullscr;
        static int prev_xsz, prev_ysz;
+       static long start_msec;
 
        switch(key) {
        case 27:
@@ -132,6 +193,15 @@ void keypress(unsigned char key, int x, int y)
                anim ^= 1;
                glutIdleFunc(anim ? idle : 0);
                glutPostRedisplay();
+
+               if(anim) {
+                       start_msec = glutGet(GLUT_ELAPSED_TIME);
+                       nframes = 0;
+               } else {
+                       long tm = glutGet(GLUT_ELAPSED_TIME) - start_msec;
+                       long fps = (nframes * 100000) / tm;
+                       printf("framerate: %ld.%ld fps\n", fps / 100, fps % 100);
+               }
                break;
 
        case '\n':