1
2/*
3 * Mesa 3-D graphics library
4 *
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.
23 */
24
25
26/*
27 * Example of using glXUseXFont().
28 * 5 November 1999
29 * Brian Paul
30 */
31
32
33#include <GL/gl.h>
34#include <GL/glx.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39
40static const char *ProgramName = "xfont";
41
42static const char *FontName = "fixed";
43
44static GLuint FontBase = 0;
45
46
47
48static void redraw( Display *dpy, Window w )
49{
50   static const char *text = "This is glXUseXFont()";
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
73static void resize( unsigned int width, unsigned int height )
74{
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
83static void setup_font( Display *dpy )
84{
85    XFontStruct *fontInfo;
86    Font id;
87    unsigned int first, last;
88
89    fontInfo = XLoadQueryFont(dpy, FontName);
90    if (!fontInfo) {
91        printf("Error: font %s not found\n", FontName);
92	exit(0);
93    }
94
95    id = fontInfo->fid;
96    first = fontInfo->min_char_or_byte2;
97    last = fontInfo->max_char_or_byte2;
98
99    FontBase = glGenLists((GLuint) last + 1);
100    if (!FontBase) {
101        printf("Error: unable to allocate display lists\n");
102	exit(0);
103    }
104    glXUseXFont(id, first, last - first + 1, FontBase + first);
105}
106
107static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
108				  unsigned int width, unsigned int height )
109{
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;
162}
163
164
165static void event_loop( Display *dpy )
166{
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   }
185}
186
187
188
189int main( int argc, char *argv[] )
190{
191   Display *dpy;
192   Window win;
193
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;
206}
207