home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / demos / glxdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  2.3 KB  |  122 lines

  1. /* glxdemo.c */
  2.  
  3.  
  4. /*
  5.  * A demonstration of using the GLX functions.
  6.  */
  7.  
  8.  
  9. #include <GL/gl.h>
  10. #include <GL/glx.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14.  
  15.  
  16. static void redraw( Display *dpy, Window w )
  17. {
  18.    printf("Redraw event\n");
  19.  
  20.    glClear( GL_COLOR_BUFFER_BIT );
  21.  
  22.    glColor3f( 1.0, 1.0, 0.0 );
  23.    glRectf( -0.8, -0.8, 0.8, 0.8 );
  24.  
  25.    glXSwapBuffers( dpy, w );
  26. }
  27.  
  28.  
  29.  
  30. static void resize( unsigned int width, unsigned int height )
  31. {
  32.    printf("Resize event\n");
  33.    glViewport( 0, 0, width, height );
  34.    glMatrixMode( GL_PROJECTION );
  35.    glLoadIdentity();
  36.    glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  37. }
  38.  
  39.  
  40.  
  41. static Window make_rgb_db_window( Display *dpy,
  42.                   unsigned int width, unsigned int height )
  43. {
  44.    int attrib[] = { GLX_RGBA,
  45.             GLX_RED_SIZE, 1,
  46.             GLX_GREEN_SIZE, 1,
  47.             GLX_BLUE_SIZE, 1,
  48.             GLX_DOUBLEBUFFER,
  49.             None };
  50.    int scrnum;
  51.    XSetWindowAttributes attr;
  52.    unsigned long mask;
  53.    Window root;
  54.    Window win;
  55.    GLXContext ctx;
  56.    XVisualInfo *visinfo;
  57.  
  58.    scrnum = DefaultScreen( dpy );
  59.    root = RootWindow( dpy, scrnum );
  60.  
  61.    visinfo = glXChooseVisual( dpy, scrnum, attrib );
  62.    if (!visinfo) {
  63.       printf("Error: couldn't get an RGB, Double-buffered visual\n");
  64.       exit(1);
  65.    }
  66.  
  67.    /* window attributes */
  68.    attr.background_pixel = 0;
  69.    attr.border_pixel = 0;
  70.    attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  71.    attr.event_mask = StructureNotifyMask | ExposureMask;
  72.    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  73.  
  74.    win = XCreateWindow( dpy, root, 0, 0, width, height,
  75.                 0, visinfo->depth, InputOutput,
  76.                 visinfo->visual, mask, &attr );
  77.  
  78.    ctx = glXCreateContext( dpy, visinfo, NULL, True );
  79.  
  80.    glXMakeCurrent( dpy, win, ctx );
  81.  
  82.    return win;
  83. }
  84.  
  85.  
  86. static void event_loop( Display *dpy )
  87. {
  88.    XEvent event;
  89.  
  90.    while (1) {
  91.       XNextEvent( dpy, &event );
  92.  
  93.       switch (event.type) {
  94.      case Expose:
  95.         redraw( dpy, event.xany.window );
  96.         break;
  97.      case ConfigureNotify:
  98.         resize( event.xconfigure.width, event.xconfigure.height );
  99.         break;
  100.       }
  101.    }
  102. }
  103.  
  104.  
  105.  
  106. main( int argc, char *argv[] )
  107. {
  108.    Display *dpy;
  109.    Window win;
  110.  
  111.    dpy = XOpenDisplay(NULL);
  112.  
  113.    win = make_rgb_db_window( dpy, 300, 300 );
  114.  
  115.    glShadeModel( GL_FLAT );
  116.    glClearColor( 0.5, 0.5, 0.5, 1.0 );
  117.  
  118.    XMapWindow( dpy, win );
  119.  
  120.    event_loop( dpy );
  121. }
  122.