home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / xlib / pixmap.c.z / pixmap.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.0 KB  |  124 lines

  1. /* compile: cc -o pixmap pixmap.c -lGL -lX11 */
  2.  
  3. #include <GL/glx.h>
  4. #include <X11/keysym.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. static int attributeList[] = { GLX_RGBA, None };
  9. static Display* dpy;
  10. static GC xgc;
  11. static Pixmap pixmap;
  12. static GLXPixmap glxPixmap;
  13. static Window win;
  14. static int width = 300;
  15. static int height = 300;
  16.  
  17. static void refresh(void);
  18. static void draw_scene(void);
  19. static void process_input(void);
  20. static void error(const char* prog, const char* msg);
  21.  
  22. static void
  23. refresh(void) {
  24.     /* don't redraw; just copy pixmap to window */
  25.     XCopyArea(dpy, pixmap, win, xgc, 0, 0, width, height, 0, 0);
  26.     }
  27.  
  28. static void
  29. draw_scene(void) {
  30.     glClearColor(0.5, 0.5, 0.5, 1.0);
  31.     glClear(GL_COLOR_BUFFER_BIT);
  32.     glColor3f(1.0,0.0,0.0);
  33.     glRectf(-.5,-.5,.5,.5);
  34.     glColor3f(0.0,1.0,0.0);
  35.     glRectf(-.4,-.4,.4,.4);
  36.     glColor3f(0.0,0.0,1.0);
  37.     glRectf(-.3,-.3,.3,.3);
  38.     glFlush();
  39. }
  40.  
  41. static void
  42. process_input(void) {
  43.     XEvent event;
  44.     Bool redraw = 0;
  45.  
  46.     do {
  47.     char buf[31];
  48.     KeySym keysym;
  49.  
  50.     XNextEvent(dpy, &event);
  51.     switch(event.type) {
  52.     case Expose:
  53.     case ConfigureNotify:
  54.         redraw = 1;
  55.         break;
  56.     case KeyPress:
  57.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  58.         switch (keysym) {
  59.         case XK_Escape:
  60.         exit(EXIT_SUCCESS);
  61.         default:
  62.         break;
  63.         }
  64.     default:
  65.         break;
  66.     }
  67.     } while (XPending(dpy));
  68.     if (redraw) refresh();
  69. }
  70.  
  71. static void
  72. error(const char *prog, const char *msg) {
  73.     fprintf(stderr, "%s: %s\n", prog, msg);
  74.     exit(EXIT_FAILURE);
  75. }
  76.  
  77. int
  78. main(int argc, char **argv) {
  79.     XVisualInfo *vi;
  80.     XSetWindowAttributes swa;
  81.     GLXContext cx;
  82.  
  83.     /* get a connection */
  84.     dpy = XOpenDisplay(0);
  85.     if (!dpy) error(argv[0], "can't open display");
  86.  
  87.     /* get an appropriate visual */
  88.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  89.     if (!vi) error(argv[0], "no suitable visual");
  90.  
  91.     /* create a GLX context */
  92.     cx = glXCreateContext(dpy, vi, 0, GL_FALSE);
  93.  
  94.     /* create a color map */
  95.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  96.                                    vi->visual, AllocNone);
  97.  
  98.     /* create a window */
  99.     swa.border_pixel = 0;
  100.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  101.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height,
  102.             0, vi->depth, InputOutput, vi->visual,
  103.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  104.     XStoreName(dpy, win, "pixmap");
  105.     XMapWindow(dpy, win);
  106.  
  107.     /* create the pixmap */
  108.     xgc = XCreateGC(dpy, win, 0, NULL);
  109.     pixmap = XCreatePixmap(dpy, win, width, height, vi->depth);
  110.     glxPixmap = glXCreateGLXPixmap(dpy, vi, pixmap);
  111.  
  112.     /* connect the context to the pixmap, and set up transforms to match */
  113.     glXMakeCurrent(dpy, glxPixmap, cx);
  114.     glViewport(0, 0, width, height);
  115.     glOrtho(-1, 1, -1, 1, -1, 1);
  116.  
  117.     /* generate the image in the pixmap */
  118.     draw_scene();
  119.     glXWaitGL();
  120.  
  121.     /* handle events directed to the window */
  122.     while (1) process_input();
  123. }
  124.