1 #ifndef NO_OGL
2 
3 //*********************************************************
4 //GLFONT.CPP -- glFont routines
5 //Copyright (c) 1998 Brad Fish
6 //See glFont.txt for terms of use
7 //November 10, 1998
8 //*********************************************************
9 
10 #include <windows.h>
11 #include <stdio.h>
12 #include <malloc.h>
13 #include <string.h>
14 #include <gl\gl.h>
15 #include "glfont.h"
16 
17 //*********************************************************
18 //Variables
19 //*********************************************************
20 
21 //Current font
22 GLFONT *glFont;
23 
24 //*********************************************************
25 //Functions
26 //*********************************************************
glFontCreate(GLFONT * Font,char * Buffer,int Tex)27 int glFontCreate (GLFONT *Font, char *Buffer, int Tex)
28 {
29 	char *TexBytes;
30 	int Num;
31 
32 	//Read glFont structure
33 	memcpy(Font, Buffer, sizeof(GLFONT));
34 	Buffer+=sizeof(GLFONT);
35 
36 	//Save texture number
37 	Font->Tex = Tex;
38 
39 	//Get number of characters
40 	Num = Font->IntEnd - Font->IntStart + 1;
41 
42 	//Allocate memory for characters
43 	if ((Font->Char = (GLFONTCHAR *)malloc(
44 		sizeof(GLFONTCHAR) * Num)) == NULL)
45 		return FALSE;
46 
47 	//Read glFont characters
48 	memcpy(Font->Char, Buffer, sizeof(GLFONTCHAR)*Num);
49 	Buffer+=sizeof(GLFONTCHAR)*Num;
50 
51 	//Get texture size
52 	Num = Font->TexWidth * Font->TexHeight * 2;
53 
54 	//Allocate memory for texture data
55 	if ((TexBytes = (char *)malloc(Num)) == NULL)
56 		return FALSE;
57 
58 	//Read texture data
59 	memcpy(TexBytes, Buffer, sizeof(char)*Num);
60 	Buffer+=sizeof(char)*Num;
61 
62 	//Set texture attributes
63 	glBindTexture(GL_TEXTURE_2D, Font->Tex);
64 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
65 		GL_CLAMP);
66 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
67 		GL_CLAMP);
68 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
69 		GL_LINEAR);
70 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
71 		GL_LINEAR);
72 	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
73 		GL_MODULATE);
74 
75 	//Create texture
76 	glTexImage2D(GL_TEXTURE_2D, 0, 2, Font->TexWidth,
77 		Font->TexHeight, 0, GL_LUMINANCE_ALPHA,
78 		GL_UNSIGNED_BYTE, (void *)TexBytes);
79 
80 	//Clean up
81 	free(TexBytes);
82 
83 	//Return pointer to new font
84 	return TRUE;
85 }
86 //*********************************************************
glFontDestroy(GLFONT * Font)87 void glFontDestroy (GLFONT *Font)
88 {
89 	//Free character memory
90 	free(Font->Char);
91 }
92 //*********************************************************
glFontBegin(GLFONT * Font)93 void glFontBegin (GLFONT *Font)
94 {
95 	//Save pointer to font structure
96 	if (Font->Char != NULL)
97 		glFont = Font;
98 	else
99 		glFont = NULL;
100 
101 	//Bind to font texture
102 	glBindTexture(GL_TEXTURE_2D, Font->Tex);
103 }
104 //*********************************************************
glFontEnd(void)105 void glFontEnd (void)
106 {
107 	//Font no longer current
108 	glFont = NULL;
109 }
110 //*********************************************************
glFontTextOut(char * String,float x,float y,float z)111 void glFontTextOut (char *String, float x, float y,
112 	float z)
113 {
114 	int Length, i;
115 	GLFONTCHAR *Char;
116 
117 	//Return if we don't have a valid glFont
118 	if (glFont == NULL)
119 		return;
120 
121 	//Get length of string
122 	Length = strlen(String);
123 
124 	//Begin rendering quads
125 	glBegin(GL_QUADS);
126 
127 	//Loop through characters
128 	for (i = 0; i < Length; i++)
129 	{
130 		//Get pointer to glFont character
131 		Char = &glFont->Char[(int)String[i] -
132 			glFont->IntStart];
133 
134 		//Specify vertices and texture coordinates
135 		glTexCoord2f(Char->tx1, Char->ty1);
136 		glVertex3f(x, y - Char->dy, z);
137 		glTexCoord2f(Char->tx1, Char->ty2);
138 		glVertex3f(x, y, z);
139 		glTexCoord2f(Char->tx2, Char->ty2);
140 		glVertex3f(x + Char->dx, y, z);
141 		glTexCoord2f(Char->tx2, Char->ty1);
142 		glVertex3f(x + Char->dx, y - Char->dy, z);
143 
144 		//Move to next character
145 		x += Char->dx;
146 	}
147 
148 	//Stop rendering quads
149 	glEnd();
150 }
151 //*********************************************************
152 
153 //End of file
154 
155 
156 #endif // NO_OGL
157