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