Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 1 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | /* |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 3 | * Mesa 3-D graphics library |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 4 | * |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999 Brian Paul All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 23 | */ |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 24 | |
| 25 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 26 | /* |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 27 | * Example of using glXUseXFont(). |
| 28 | * 5 November 1999 |
| 29 | * Brian Paul |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 30 | */ |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 31 | |
| 32 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 33 | #include <GL/gl.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 34 | #include <GL/glx.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 37 | #include <string.h> |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 38 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 39 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 40 | static const char *ProgramName = "xfont"; |
| 41 | |
| 42 | static const char *FontName = "fixed"; |
| 43 | |
| 44 | static GLuint FontBase = 0; |
| 45 | |
| 46 | |
| 47 | |
| 48 | static void redraw( Display *dpy, Window w ) |
| 49 | { |
| 50 | static const char *text = "This is glXUseXFont()"; |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 51 | |
| 52 | glClear( GL_COLOR_BUFFER_BIT ); |
| 53 | |
| 54 | /* triangle */ |
| 55 | glColor3f( 0.2, 0.2, 1.0 ); |
| 56 | glBegin(GL_TRIANGLES); |
| 57 | glVertex2f( 0, 0.8 ); |
| 58 | glVertex2f( -0.8, -0.7 ); |
| 59 | glVertex2f( 0.8, -0.7 ); |
| 60 | glEnd(); |
| 61 | |
| 62 | /* text */ |
| 63 | glColor3f( 1, 1, 1 ); |
| 64 | glRasterPos2f(-0.8, 0); |
| 65 | glListBase(FontBase); |
| 66 | glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text); |
| 67 | |
| 68 | glXSwapBuffers( dpy, w ); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | static void resize( unsigned int width, unsigned int height ) |
| 74 | { |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 75 | glViewport( 0, 0, width, height ); |
| 76 | glMatrixMode( GL_PROJECTION ); |
| 77 | glLoadIdentity(); |
| 78 | glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 ); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | static void setup_font( Display *dpy ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 84 | { |
| 85 | XFontStruct *fontInfo; |
| 86 | Font id; |
| 87 | unsigned int first, last; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 88 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 89 | fontInfo = XLoadQueryFont(dpy, FontName); |
| 90 | if (!fontInfo) { |
| 91 | printf("Error: font %s not found\n", FontName); |
| 92 | exit(0); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | id = fontInfo->fid; |
| 96 | first = fontInfo->min_char_or_byte2; |
| 97 | last = fontInfo->max_char_or_byte2; |
| 98 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 99 | FontBase = glGenLists((GLuint) last + 1); |
| 100 | if (!FontBase) { |
| 101 | printf("Error: unable to allocate display lists\n"); |
| 102 | exit(0); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 103 | } |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 104 | glXUseXFont(id, first, last - first + 1, FontBase + first); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 107 | static Window make_rgb_db_window( Display *dpy, int xpos, int ypos, |
| 108 | unsigned int width, unsigned int height ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 109 | { |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 110 | int attrib[] = { GLX_RGBA, |
| 111 | GLX_RED_SIZE, 1, |
| 112 | GLX_GREEN_SIZE, 1, |
| 113 | GLX_BLUE_SIZE, 1, |
| 114 | GLX_DOUBLEBUFFER, |
| 115 | None }; |
| 116 | int scrnum; |
| 117 | XSetWindowAttributes attr; |
| 118 | unsigned long mask; |
| 119 | Window root; |
| 120 | Window win; |
| 121 | GLXContext ctx; |
| 122 | XVisualInfo *visinfo; |
| 123 | |
| 124 | scrnum = DefaultScreen( dpy ); |
| 125 | root = RootWindow( dpy, scrnum ); |
| 126 | |
| 127 | visinfo = glXChooseVisual( dpy, scrnum, attrib ); |
| 128 | if (!visinfo) { |
| 129 | printf("Error: couldn't get an RGB, Double-buffered visual\n"); |
| 130 | exit(1); |
| 131 | } |
| 132 | |
| 133 | /* window attributes */ |
| 134 | attr.background_pixel = 0; |
| 135 | attr.border_pixel = 0; |
| 136 | attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone); |
| 137 | attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; |
| 138 | mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; |
| 139 | |
| 140 | win = XCreateWindow( dpy, root, 0, 0, width, height, |
| 141 | 0, visinfo->depth, InputOutput, |
| 142 | visinfo->visual, mask, &attr ); |
| 143 | |
| 144 | { |
| 145 | XSizeHints sizehints; |
| 146 | sizehints.x = xpos; |
| 147 | sizehints.y = ypos; |
| 148 | sizehints.width = width; |
| 149 | sizehints.height = height; |
| 150 | sizehints.flags = USSize | USPosition; |
| 151 | XSetNormalHints(dpy, win, &sizehints); |
| 152 | XSetStandardProperties(dpy, win, ProgramName, ProgramName, |
| 153 | None, (char **)NULL, 0, &sizehints); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | ctx = glXCreateContext( dpy, visinfo, NULL, True ); |
| 158 | |
| 159 | glXMakeCurrent( dpy, win, ctx ); |
| 160 | |
| 161 | return win; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 164 | |
| 165 | static void event_loop( Display *dpy ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 166 | { |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 167 | XEvent event; |
| 168 | |
| 169 | while (1) { |
| 170 | XNextEvent( dpy, &event ); |
| 171 | |
| 172 | switch (event.type) { |
| 173 | case Expose: |
| 174 | redraw( dpy, event.xany.window ); |
| 175 | break; |
| 176 | case ConfigureNotify: |
| 177 | resize( event.xconfigure.width, event.xconfigure.height ); |
| 178 | break; |
| 179 | case KeyPress: |
| 180 | exit(0); |
| 181 | default: |
| 182 | ; /* no-op */ |
| 183 | } |
| 184 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | int main( int argc, char *argv[] ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 190 | { |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 191 | Display *dpy; |
| 192 | Window win; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 193 | |
Brian Paul | f1b45ca | 1999-11-05 08:00:49 +0000 | [diff] [blame] | 194 | dpy = XOpenDisplay(NULL); |
| 195 | |
| 196 | win = make_rgb_db_window( dpy, 0, 0, 300, 300 ); |
| 197 | setup_font( dpy ); |
| 198 | |
| 199 | glShadeModel( GL_FLAT ); |
| 200 | glClearColor( 0.5, 0.5, 1.0, 1.0 ); |
| 201 | |
| 202 | XMapWindow( dpy, win ); |
| 203 | |
| 204 | event_loop( dpy ); |
| 205 | return 0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 206 | } |