1 /*************************************************************************
2 
3                          "I Have No Tomatoes"
4                   Copyright (c) 2004, Mika Halttunen
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute
12  it freely, subject to the following restrictions:
13 
14     1. The origin of this software must not be misrepresented; you must
15     not claim that you wrote the original software. If you use this
16     software in a product, an acknowledgment in the product documentation
17     would be appreciated but is not required.
18 
19     2. Altered source versions must be plainly marked as such, and must
20     not be misrepresented as being the original software.
21 
22     3. This notice may not be removed or altered from any source
23     distribution.
24 
25 
26  Mika Halttunen <lsoft@mbnet.fi>
27 
28 *************************************************************************/
29 
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include "SDL.h"
34 #include "SDL_image.h"
35 #include "SDL_opengl.h"
36 #include "font.h"
37 #include "texture.h"
38 
39 
40 // Spacing of the fonts
41 const float spacing = 0.55f;
42 
43 // Font scale
44 float font_scale_x = 1.0f, font_scale_y = 1.0f;
45 
46 // Display list for bitmapped fonts
47 GLuint font_base = 0;
48 GLuint font_num_base = 0;
49 
50 // Font textures
51 GLuint font1;
52 GLuint font_num;
53 
54 
55 // Return the length of a string
glfont_length(char * string,...)56 float glfont_length(char *string, ...) {
57 
58 	// Format the string arguments
59 	char buf[1024];
60 	va_list args;
61 	va_start(args, string);
62 	vsprintf(buf, string, args);
63 	va_end(args);
64 
65 	float length = (float)strlen(buf)*spacing;
66 	if(font_scale_x != 1.0f)
67 		length *= font_scale_x;
68 
69 	return length;
70 }
71 
72 
73 // Print a string with specified font texture
glprintf(GLuint tex,int set,float x,float y,float z,char * string,...)74 void glprintf(GLuint tex, int set, float x, float y, float z, char *string, ...) {
75 
76 	// Format the string arguments
77 	char buf[1024];
78 	va_list args;
79 	va_start(args, string);
80 	vsprintf(buf, string, args);
81 	va_end(args);
82 
83 	// Print
84 	BIND_TEXTURE(tex);
85 	glPushMatrix();
86 	glLoadIdentity();
87 	glTranslatef(x,y,z);
88 
89 	if(font_scale_x != 1.0f || font_scale_y != 1.0f)
90 		glScalef(font_scale_x, font_scale_y, 1);
91 
92 	glListBase(font_base - 32 + (128*set));
93 	glCallLists(strlen(buf), GL_BYTE, buf);
94 
95 	glPopMatrix();
96 }
97 
98 
99 // Print a centered string with specified font texture
glprintf_center(GLuint tex,int set,float x,float y,float z,char * string,...)100 void glprintf_center(GLuint tex, int set, float x, float y, float z, char *string, ...) {
101 
102 	// Format the string arguments
103 	char buf[1024];
104 	va_list args;
105 	va_start(args, string);
106 	vsprintf(buf, string, args);
107 	va_end(args);
108 
109 	// Print
110 	BIND_TEXTURE(tex);
111 	glPushMatrix();
112 	glLoadIdentity();
113 	float length = (float)strlen(buf)*spacing;
114 	if(font_scale_x != 1.0f)
115 		length *= font_scale_x;
116 	glTranslatef(x-(length * 0.5f),y,z);
117 
118 	if(font_scale_x != 1.0f || font_scale_y != 1.0f)
119 		glScalef(font_scale_x, font_scale_y, 1);
120 
121 	glListBase(font_base - 32 + (128*set));
122 	glCallLists(strlen(buf), GL_BYTE, buf);
123 
124 	glPopMatrix();
125 }
126 
127 
128 // Build a display list for bitmapped fonts (-> font_base)
build_font_list()129 void build_font_list() {
130 
131 	float cx, cy;
132 
133 	font_base = glGenLists(256);
134 	BIND_TEXTURE(0);
135 
136 	// For normal fonts
137 	int f;
138 	for(f=0; f<256; f++) {
139 		cx = float(f%16)/16.0f;
140 		cy = float(f/16)/16.0f;
141 
142 		glNewList(font_base+f, GL_COMPILE);
143 			glBegin(GL_TRIANGLE_STRIP);
144 				glTexCoord2f(cx+0.0625f, 1-cy); glVertex2f(1,1);
145 				glTexCoord2f(cx, 1-cy); glVertex2f(0,1);
146 				glTexCoord2f(cx+0.0625f, 1-cy-0.0625f); glVertex2f(1,0);
147 				glTexCoord2f(cx, 1-cy-0.0625f); glVertex2f(0,0);
148 			glEnd();
149 			glTranslatef(spacing, 0,0);
150 		glEndList();
151 	}
152 
153 	// For the big numbers
154 	font_num_base = glGenLists(10);
155 	int xx = -1, yy = 0;
156 	for(f=0; f<10; f++) {
157 		xx++;
158 		if(xx > 7) {
159 			xx = 0;
160 			yy++;
161 		}
162 
163 		cx = (float)xx / 8.0f;
164 		cy = (float)yy / 2.0f;
165 
166 		glNewList(font_num_base+f, GL_COMPILE);
167 			glBegin(GL_TRIANGLE_STRIP);
168 				glTexCoord2f(cx+0.125f, 1-cy); glVertex2f(1,1);
169 				glTexCoord2f(cx, 1-cy); glVertex2f(0,1);
170 				glTexCoord2f(cx+0.125f, 1-cy-0.5f); glVertex2f(1,0);
171 				glTexCoord2f(cx, 1-cy-0.5f); glVertex2f(0,0);
172 			glEnd();
173 			glTranslatef(0.8f, 0,0);
174 		glEndList();
175 	}
176 
177 }
178 
179 
180 
181 // Print numbers using the big number font
glprint_num(float x,float y,float z,char * string)182 void glprint_num(float x, float y, float z, char *string) {
183 
184 	// Print
185 	BIND_TEXTURE(font_num);
186 	glPushMatrix();
187 	glLoadIdentity();
188 	glTranslatef(x,y,z);
189 
190 	if(font_scale_x != 1.0f || font_scale_y != 1.0f)
191 		glScalef(font_scale_x, font_scale_y, 1);
192 
193 	int len = strlen(string);
194 
195 	for(int f=0; f<len; f++) {
196 		int c = (int)string[f];
197 		if(c == 32) {
198 			// It's a space, skip it
199 			glTranslatef(0.8f, 0,0);
200 			continue;
201 		}
202 
203 		// Check for numbers
204 		if(c >= 48 && c <= 57) {
205 			// Draw the number
206 			c -= 48;
207 			glCallList(c + font_num_base);
208 		}
209 	}
210 
211 	glPopMatrix();
212 }
213 
214 
215