home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / demos / glxdemo.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  2KB  |  125 lines

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