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