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