blob: e38265b41bbdf2fa71d587b3190c6e12b79c0385 [file] [log] [blame]
Brian Paulf1b45ca1999-11-05 08:00:49 +00001/* $Id: xfont.c,v 1.2 1999/11/05 08:00:49 brianp Exp $ */
2
jtgafb833d1999-08-19 00:55:39 +00003/*
Brian Paulf1b45ca1999-11-05 08:00:49 +00004 * Mesa 3-D graphics library
jtgafb833d1999-08-19 00:55:39 +00005 *
Brian Paulf1b45ca1999-11-05 08:00:49 +00006 * 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.
jtgafb833d1999-08-19 00:55:39 +000024 */
Brian Paulf1b45ca1999-11-05 08:00:49 +000025
26
jtgafb833d1999-08-19 00:55:39 +000027/*
Brian Paulf1b45ca1999-11-05 08:00:49 +000028 * Example of using glXUseXFont().
29 * 5 November 1999
30 * Brian Paul
jtgafb833d1999-08-19 00:55:39 +000031 */
Brian Paulf1b45ca1999-11-05 08:00:49 +000032
33
jtgafb833d1999-08-19 00:55:39 +000034#include <GL/gl.h>
jtgafb833d1999-08-19 00:55:39 +000035#include <GL/glx.h>
jtgafb833d1999-08-19 00:55:39 +000036#include <stdio.h>
Brian Paulf1b45ca1999-11-05 08:00:49 +000037#include <stdlib.h>
jtgafb833d1999-08-19 00:55:39 +000038#include <string.h>
jtgafb833d1999-08-19 00:55:39 +000039
jtgafb833d1999-08-19 00:55:39 +000040
Brian Paulf1b45ca1999-11-05 08:00:49 +000041static const char *ProgramName = "xfont";
42
43static const char *FontName = "fixed";
44
45static GLuint FontBase = 0;
46
47
48
49static void redraw( Display *dpy, Window w )
50{
51 static const char *text = "This is glXUseXFont()";
52 printf("Redraw event\n");
53
54 glClear( GL_COLOR_BUFFER_BIT );
55
56 /* triangle */
57 glColor3f( 0.2, 0.2, 1.0 );
58 glBegin(GL_TRIANGLES);
59 glVertex2f( 0, 0.8 );
60 glVertex2f( -0.8, -0.7 );
61 glVertex2f( 0.8, -0.7 );
62 glEnd();
63
64 /* text */
65 glColor3f( 1, 1, 1 );
66 glRasterPos2f(-0.8, 0);
67 glListBase(FontBase);
68 glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text);
69
70 glXSwapBuffers( dpy, w );
71}
72
73
74
75static void resize( unsigned int width, unsigned int height )
76{
77 printf("Resize event\n");
78 glViewport( 0, 0, width, height );
79 glMatrixMode( GL_PROJECTION );
80 glLoadIdentity();
81 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
82}
83
84
85
86static void setup_font( Display *dpy )
jtgafb833d1999-08-19 00:55:39 +000087{
88 XFontStruct *fontInfo;
89 Font id;
90 unsigned int first, last;
jtgafb833d1999-08-19 00:55:39 +000091
Brian Paulf1b45ca1999-11-05 08:00:49 +000092 fontInfo = XLoadQueryFont(dpy, FontName);
93 if (!fontInfo) {
94 printf("Error: font %s not found\n", FontName);
95 exit(0);
jtgafb833d1999-08-19 00:55:39 +000096 }
97
98 id = fontInfo->fid;
99 first = fontInfo->min_char_or_byte2;
100 last = fontInfo->max_char_or_byte2;
101
Brian Paulf1b45ca1999-11-05 08:00:49 +0000102 FontBase = glGenLists((GLuint) last + 1);
103 if (!FontBase) {
104 printf("Error: unable to allocate display lists\n");
105 exit(0);
jtgafb833d1999-08-19 00:55:39 +0000106 }
Brian Paulf1b45ca1999-11-05 08:00:49 +0000107 glXUseXFont(id, first, last - first + 1, FontBase + first);
jtgafb833d1999-08-19 00:55:39 +0000108}
109
Brian Paulf1b45ca1999-11-05 08:00:49 +0000110static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
111 unsigned int width, unsigned int height )
jtgafb833d1999-08-19 00:55:39 +0000112{
Brian Paulf1b45ca1999-11-05 08:00:49 +0000113 int attrib[] = { GLX_RGBA,
114 GLX_RED_SIZE, 1,
115 GLX_GREEN_SIZE, 1,
116 GLX_BLUE_SIZE, 1,
117 GLX_DOUBLEBUFFER,
118 None };
119 int scrnum;
120 XSetWindowAttributes attr;
121 unsigned long mask;
122 Window root;
123 Window win;
124 GLXContext ctx;
125 XVisualInfo *visinfo;
126
127 scrnum = DefaultScreen( dpy );
128 root = RootWindow( dpy, scrnum );
129
130 visinfo = glXChooseVisual( dpy, scrnum, attrib );
131 if (!visinfo) {
132 printf("Error: couldn't get an RGB, Double-buffered visual\n");
133 exit(1);
134 }
135
136 /* window attributes */
137 attr.background_pixel = 0;
138 attr.border_pixel = 0;
139 attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
140 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
141 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
142
143 win = XCreateWindow( dpy, root, 0, 0, width, height,
144 0, visinfo->depth, InputOutput,
145 visinfo->visual, mask, &attr );
146
147 {
148 XSizeHints sizehints;
149 sizehints.x = xpos;
150 sizehints.y = ypos;
151 sizehints.width = width;
152 sizehints.height = height;
153 sizehints.flags = USSize | USPosition;
154 XSetNormalHints(dpy, win, &sizehints);
155 XSetStandardProperties(dpy, win, ProgramName, ProgramName,
156 None, (char **)NULL, 0, &sizehints);
157 }
158
159
160 ctx = glXCreateContext( dpy, visinfo, NULL, True );
161
162 glXMakeCurrent( dpy, win, ctx );
163
164 return win;
jtgafb833d1999-08-19 00:55:39 +0000165}
166
Brian Paulf1b45ca1999-11-05 08:00:49 +0000167
168static void event_loop( Display *dpy )
jtgafb833d1999-08-19 00:55:39 +0000169{
Brian Paulf1b45ca1999-11-05 08:00:49 +0000170 XEvent event;
171
172 while (1) {
173 XNextEvent( dpy, &event );
174
175 switch (event.type) {
176 case Expose:
177 redraw( dpy, event.xany.window );
178 break;
179 case ConfigureNotify:
180 resize( event.xconfigure.width, event.xconfigure.height );
181 break;
182 case KeyPress:
183 exit(0);
184 default:
185 ; /* no-op */
186 }
187 }
jtgafb833d1999-08-19 00:55:39 +0000188}
189
Brian Paulf1b45ca1999-11-05 08:00:49 +0000190
191
192int main( int argc, char *argv[] )
jtgafb833d1999-08-19 00:55:39 +0000193{
Brian Paulf1b45ca1999-11-05 08:00:49 +0000194 Display *dpy;
195 Window win;
jtgafb833d1999-08-19 00:55:39 +0000196
Brian Paulf1b45ca1999-11-05 08:00:49 +0000197 dpy = XOpenDisplay(NULL);
198
199 win = make_rgb_db_window( dpy, 0, 0, 300, 300 );
200 setup_font( dpy );
201
202 glShadeModel( GL_FLAT );
203 glClearColor( 0.5, 0.5, 1.0, 1.0 );
204
205 XMapWindow( dpy, win );
206
207 event_loop( dpy );
208 return 0;
jtgafb833d1999-08-19 00:55:39 +0000209}