Merge branch '7.8'
[mesa.git] / progs / redbook / stroke.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 /*
41 * stroke.c
42 * This program demonstrates some characters of a
43 * stroke (vector) font. The characters are represented
44 * by display lists, which are given numbers which
45 * correspond to the ASCII values of the characters.
46 * Use of glCallLists() is demonstrated.
47 */
48 #include <stdlib.h>
49 #include <string.h>
50 #include <GL/glut.h>
51
52 #define PT 1
53 #define STROKE 2
54 #define END 3
55
56 typedef struct charpoint {
57 GLfloat x, y;
58 int type;
59 } CP;
60
61 CP Adata[] = {
62 { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT},
63 {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
64 };
65
66 CP Edata[] = {
67 {5, 0, PT}, {0, 0, PT}, {0, 10, PT}, {5, 10, STROKE},
68 {0, 5, PT}, {4, 5, END}
69 };
70
71 CP Pdata[] = {
72 {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT},
73 {4, 5, PT}, {0, 5, END}
74 };
75
76 CP Rdata[] = {
77 {0, 0, PT}, {0, 10, PT}, {4, 10, PT}, {5, 9, PT}, {5, 6, PT},
78 {4, 5, PT}, {0, 5, STROKE}, {3, 5, PT}, {5, 0, END}
79 };
80
81 CP Sdata[] = {
82 {0, 1, PT}, {1, 0, PT}, {4, 0, PT}, {5, 1, PT}, {5, 4, PT},
83 {4, 5, PT}, {1, 5, PT}, {0, 6, PT}, {0, 9, PT}, {1, 10, PT},
84 {4, 10, PT}, {5, 9, END}
85 };
86
87 /* drawLetter() interprets the instructions from the array
88 * for that letter and renders the letter with line segments.
89 */
90 static void drawLetter(CP *l)
91 {
92 glBegin(GL_LINE_STRIP);
93 for (;;) {
94 switch (l->type) {
95 case PT:
96 glVertex2fv(&l->x);
97 break;
98 case STROKE:
99 glVertex2fv(&l->x);
100 glEnd();
101 glBegin(GL_LINE_STRIP);
102 break;
103 case END:
104 glVertex2fv(&l->x);
105 glEnd();
106 glTranslatef(8.0, 0.0, 0.0);
107 return;
108 }
109 l++;
110 }
111 }
112
113 /* Create a display list for each of 6 characters */
114 static void myinit (void)
115 {
116 GLuint base;
117
118 glShadeModel (GL_FLAT);
119
120 base = glGenLists (128);
121 glListBase(base);
122 glNewList(base+'A', GL_COMPILE); drawLetter(Adata); glEndList();
123 glNewList(base+'E', GL_COMPILE); drawLetter(Edata); glEndList();
124 glNewList(base+'P', GL_COMPILE); drawLetter(Pdata); glEndList();
125 glNewList(base+'R', GL_COMPILE); drawLetter(Rdata); glEndList();
126 glNewList(base+'S', GL_COMPILE); drawLetter(Sdata); glEndList();
127 glNewList(base+' ', GL_COMPILE); glTranslatef(8.0, 0.0, 0.0); glEndList();
128 }
129
130 char *test1 = "A SPARE SERAPE APPEARS AS";
131 char *test2 = "APES PREPARE RARE PEPPERS";
132
133 static void printStrokedString(char *s)
134 {
135 GLsizei len = (GLsizei) strlen(s);
136 glCallLists(len, GL_BYTE, (GLbyte *)s);
137 }
138
139 static void display(void)
140 {
141 glClear(GL_COLOR_BUFFER_BIT);
142 glColor3f(1.0, 1.0, 1.0);
143 glPushMatrix();
144 glScalef(2.0, 2.0, 2.0);
145 glTranslatef(10.0, 30.0, 0.0);
146 printStrokedString(test1);
147 glPopMatrix();
148 glPushMatrix();
149 glScalef(2.0, 2.0, 2.0);
150 glTranslatef(10.0, 13.0, 0.0);
151 printStrokedString(test2);
152 glPopMatrix();
153 glFlush();
154 }
155
156 static void reshape(GLsizei w, GLsizei h)
157 {
158 glViewport(0, 0, w, h);
159 glMatrixMode(GL_PROJECTION);
160 glLoadIdentity();
161 glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
162 glMatrixMode(GL_MODELVIEW);
163 glLoadIdentity();
164 }
165
166 static void
167 key(unsigned char k, int x, int y)
168 {
169 switch (k) {
170 case 27: /* Escape */
171 exit(0);
172 break;
173 default:
174 return;
175 }
176 glutPostRedisplay();
177 }
178
179 /* Main Loop
180 * Open window with initial window size, title bar,
181 * RGBA display mode, and handle input events.
182 */
183 int main(int argc, char** argv)
184 {
185 glutInit(&argc, argv);
186 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
187 glutInitWindowSize (440, 120);
188 glutCreateWindow (argv[0]);
189 myinit ();
190 glutDisplayFunc(display);
191 glutReshapeFunc(reshape);
192 glutKeyboardFunc(key);
193 glutMainLoop();
194 return 0; /* ANSI C requires main to return int. */
195 }