#include #include #include #include #include #include #include #include static double timeOfDay() { struct timeval t; gettimeofday( &t, NULL ); return (double)(t.tv_sec) + (double)( t.tv_usec )/1000000.0; } int main(void) { Display *display = XOpenDisplay( NULL ); int screen = DefaultScreen( display ); int width = DisplayWidth( display, screen ); int height = DisplayHeight( display, screen ); if( !glXQueryExtension( display, NULL, NULL ) ) { fprintf( stderr, "No GLX extension\n" ); return 1; } int attrib_list[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 0, GLX_STENCIL_SIZE, 0, GLX_ALPHA_SIZE, 0, None }; XVisualInfo *visual_info = glXChooseVisual( display, screen, attrib_list ); int dummy; XCompositeQueryVersion( display, &dummy, &dummy ); Window window = XCompositeGetOverlayWindow( display, RootWindow( display, screen ) ); GLXContext glContext = glXCreateContext( display, visual_info, NULL, True ); if( !glXIsDirect( display, glContext ) ) { fprintf( stderr, "Not direct\n" ); return 1; } if( !glXMakeCurrent( display, window, glContext ) ) { fprintf( stderr, "Could not make context current\n" ); return 2; } glClearColor( 0.0, 0.0, 0.0, 0.0 ); glShadeModel( GL_FLAT ); glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D( 0, width, height, 0 ); glFinish(); double start_time = timeOfDay(); double lastTime = timeOfDay(); int last_dropped_frames = 0; int i=0; for( ;; ++i ) { glClear( GL_COLOR_BUFFER_BIT ); /* Animated rectangle */ { const int rect_width = 256; int x = (i * 6) % (width - rect_width); glColor4f( 1.0, 1.0, 1.0, 1.0 ); glRecti( x, 0, x + rect_width, height ); } /* Signal here - back buffer is drawn, request it be flipped * to front buffer. Note that the glFinish above will stall us * until this glXSwapBuffers and all drawing has happened. */ glXSwapBuffers( display, window ); double now = timeOfDay(); double run_time = now - start_time; int expected_frame = (int)(run_time*60.); int dropped_frames = expected_frame - i; double interval = (now - lastTime) * 1000; if( dropped_frames != last_dropped_frames ) { printf( "Delta %7.3lfms error %7.3lf frame %6d expected_frame %6d dropped_frames %4d (1 dropped every %.3fs)\n", interval, interval - (1000./60.), i, expected_frame, dropped_frames, ((double)expected_frame)/((double)dropped_frames)/60. ); last_dropped_frames = dropped_frames; } lastTime = now; } return 0; }