Example 3-2 : Simple Xlib Example Program
/* * simplest - simple single buffered RGBA xlib program. */ /* compile: cc -o simplest simplest.c -lGL -lX11 */ #include <GL/glx.h> #include <X11/keysym.h> #include <stdlib.h> #include <stdio.h> static int attributeList[] = { GLX_RGBA, None }; static void draw_scene(void) { glClearColor(0.5, 0.5, 0.5, 1.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glRectf(-.5,-.5,.5,.5); glColor3f(0.0,1.0,0.0); glRectf(-.4,-.4,.4,.4); glColor3f(0.0,0.0,1.0); glRectf(-.3,-.3,.3,.3); glFlush(); } static void process_input(Display *dpy) { XEvent event; Bool redraw = 0; do { char buf[31]; KeySym keysym; XNextEvent(dpy, &event); switch(event.type) { case Expose: redraw = 1; break; case ConfigureNotify: glViewport(0, 0, event.xconfigure.width, event.xconfigure.height); redraw = 1; break; case KeyPress: (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL); switch (keysym) { case XK_Escape: exit(EXIT_SUCCESS); default: break; } default: break; } } while (XPending(dpy)); if (redraw) draw_scene(); } static void error(const char *prog, const char *msg) { fprintf(stderr, "%s: %s\n", prog, msg); exit(EXIT_FAILURE); } int main(int argc, char **argv) { Display *dpy; XVisualInfo *vi; XSetWindowAttributes swa; Window win; GLXContext cx; /* get a connection */ dpy = XOpenDisplay(0); if (!dpy) error(argv[0], "can't open display"); /* get an appropriate visual */ vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList); if (!vi) error(argv[0], "no suitable visual"); /* create a GLX context */ cx = glXCreateContext(dpy, vi, 0, GL_TRUE); /* create a colormap */ swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone); /* create a window */ swa.border_pixel = 0; swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask; win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa); XStoreName(dpy, win, "simplest"); XMapWindow(dpy, win); /* connect the context to the window */ glXMakeCurrent(dpy, win, cx); while (1) process_input(dpy); }