WIP - port to Mali EGL
[mesa-demos/mali.git] / src / xdemos / xfont.c
blob2585aa6447c2cf36abd67783e4e3b0fc4243da31
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Example of using glXUseXFont().
28 * 5 November 1999
29 * Brian Paul
33 #include <GL/gl.h>
34 #include <GL/glx.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
40 static const char *ProgramName = "xfont";
42 static const char *FontName = "fixed";
44 static GLuint FontBase = 0;
48 static void redraw( Display *dpy, Window w )
50 static const char *text = "This is glXUseXFont()";
52 glClear( GL_COLOR_BUFFER_BIT );
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();
62 /* text */
63 glColor3f( 1, 1, 1 );
64 glRasterPos2f(-0.8, 0);
65 glListBase(FontBase);
66 glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text);
68 glXSwapBuffers( dpy, w );
73 static void resize( unsigned int width, unsigned int height )
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 );
83 static void setup_font( Display *dpy )
85 XFontStruct *fontInfo;
86 Font id;
87 unsigned int first, last;
89 fontInfo = XLoadQueryFont(dpy, FontName);
90 if (!fontInfo) {
91 printf("Error: font %s not found\n", FontName);
92 exit(0);
95 id = fontInfo->fid;
96 first = fontInfo->min_char_or_byte2;
97 last = fontInfo->max_char_or_byte2;
99 FontBase = glGenLists((GLuint) last + 1);
100 if (!FontBase) {
101 printf("Error: unable to allocate display lists\n");
102 exit(0);
104 glXUseXFont(id, first, last - first + 1, FontBase + first);
107 static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
108 unsigned int width, unsigned int height )
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;
124 scrnum = DefaultScreen( dpy );
125 root = RootWindow( dpy, scrnum );
127 visinfo = glXChooseVisual( dpy, scrnum, attrib );
128 if (!visinfo) {
129 printf("Error: couldn't get an RGB, Double-buffered visual\n");
130 exit(1);
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;
140 win = XCreateWindow( dpy, root, 0, 0, width, height,
141 0, visinfo->depth, InputOutput,
142 visinfo->visual, mask, &attr );
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);
157 ctx = glXCreateContext( dpy, visinfo, NULL, True );
159 glXMakeCurrent( dpy, win, ctx );
161 return win;
165 static void event_loop( Display *dpy )
167 XEvent event;
169 while (1) {
170 XNextEvent( dpy, &event );
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 */
189 int main( int argc, char *argv[] )
191 Display *dpy;
192 Window win;
194 dpy = XOpenDisplay(NULL);
196 win = make_rgb_db_window( dpy, 0, 0, 300, 300 );
197 setup_font( dpy );
199 glShadeModel( GL_FLAT );
200 glClearColor( 0.5, 0.5, 1.0, 1.0 );
202 XMapWindow( dpy, win );
204 event_loop( dpy );
205 return 0;