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