gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / xdemos / xrotfontdemo.c
blob58cd0286cc6486243e0b12a93e5690c220b01004
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
26 * Example of using glXUseRotatedXFontMESA().
27 * 24 Jan 2004
28 * Brian Paul
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"
40 static const char *ProgramName = "xfont";
42 static const char *FontName = "fixed";
44 static GLuint FontBase[4];
47 static void redraw( Display *dpy, Window w )
49 static const char *text = " Rotated bitmap text";
50 int i;
52 glClear( GL_COLOR_BUFFER_BIT );
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();
62 /* marker */
63 glColor3f( 0, 1, 0 );
64 glBegin(GL_POINTS);
65 glVertex2f(0, 0);
66 glEnd();
68 /* text */
69 glColor3f( 1, 1, 1 );
71 for (i = 0; i < 4; i++) {
72 glRasterPos2f(0, 0);
73 glListBase(FontBase[i]);
74 glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text);
77 glXSwapBuffers( dpy, w );
82 static void resize( unsigned int width, unsigned int height )
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 );
92 static void setup_font( Display *dpy )
94 XFontStruct *fontInfo;
95 Font id;
96 unsigned int first, last;
97 int i;
99 fontInfo = XLoadQueryFont(dpy, FontName);
100 if (!fontInfo) {
101 printf("Error: font %s not found\n", FontName);
102 exit(0);
105 id = fontInfo->fid;
106 first = fontInfo->min_char_or_byte2;
107 last = fontInfo->max_char_or_byte2;
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);
115 glXUseRotatedXFontMESA(id, first, last - first + 1, FontBase[i] + first,
116 i * 90);
121 static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
122 unsigned int width, unsigned int height )
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;
138 scrnum = DefaultScreen( dpy );
139 root = RootWindow( dpy, scrnum );
141 visinfo = glXChooseVisual( dpy, scrnum, attrib );
142 if (!visinfo) {
143 printf("Error: couldn't get an RGB, Double-buffered visual\n");
144 exit(1);
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;
154 win = XCreateWindow( dpy, root, 0, 0, width, height,
155 0, visinfo->depth, InputOutput,
156 visinfo->visual, mask, &attr );
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);
171 ctx = glXCreateContext( dpy, visinfo, NULL, True );
173 glXMakeCurrent( dpy, win, ctx );
175 return win;
179 static void event_loop( Display *dpy )
181 XEvent event;
183 while (1) {
184 XNextEvent( dpy, &event );
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 */
203 int main( int argc, char *argv[] )
205 Display *dpy;
206 Window win;
208 dpy = XOpenDisplay(NULL);
210 win = make_rgb_db_window( dpy, 0, 0, 300, 300 );
211 setup_font( dpy );
213 glShadeModel( GL_FLAT );
214 glClearColor( 0.5, 0.5, 1.0, 1.0 );
216 XMapWindow( dpy, win );
218 event_loop( dpy );
219 return 0;