1 //*******************************************************************
2 //glfont2.cpp -- glFont Version 2.0 implementation
3 //Copyright (c) 1998-2002 Brad Fish
4 //See glfont.html for terms of use
5 //May 14, 2002
6 //*******************************************************************
7 
8 //STL headers
9 #include <string>
10 #include <utility>
11 #include <iostream>
12 #include <fstream>
13 using namespace std;
14 
15 //OpenGL headers
16 #ifdef _WINDOWS
17 #include <windows.h>
18 #endif
19 #include <GL/gl.h>
20 
21 //glFont header
22 #include "glfont2.h"
23 
24 //*******************************************************************
25 //GLFont Class Implementation
26 //*******************************************************************
GLFont()27 GLFont::GLFont ()
28 {
29 	//Initialize header to safe state
30 	header.tex = -1;
31 	header.tex_width = 0;
32 	header.tex_height = 0;
33 	header.start_char = 0;
34 	header.end_char = 0;
35 	header.chars = NULL;
36 }
37 //*******************************************************************
~GLFont()38 GLFont::~GLFont ()
39 {
40 	//Destroy the font
41 	Destroy();
42 }
43 //*******************************************************************
Create(const char * file_name,int tex)44 bool GLFont::Create (const char *file_name, int tex)
45 {
46 	ifstream input;
47 	int num_chars, num_tex_bytes;
48 	char *tex_bytes;
49 
50 	//Destroy the old font if there was one, just to be safe
51 	Destroy();
52 
53 	//Open input file
54 	input.open(file_name, ios::in | ios::binary);
55 	if (!input)
56 		return false;
57 
58 	//Read the header from file
59 	input.read((char *)&header, sizeof(header));
60 	header.tex = tex;
61 
62 	//Allocate space for character array
63 	num_chars = header.end_char - header.start_char + 1;
64 	if ((header.chars = new GLFontChar[num_chars]) == NULL)
65 		return false;
66 
67 	//Read character array
68 	input.read((char *)header.chars, sizeof(GLFontChar) *
69 		num_chars);
70 
71 	//Read texture pixel data
72 	num_tex_bytes = header.tex_width * header.tex_height * 2;
73 	tex_bytes = new char[num_tex_bytes];
74 	input.read(tex_bytes, num_tex_bytes);
75 
76 	//Create OpenGL texture
77 	glBindTexture(GL_TEXTURE_2D, tex);
78 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
79 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
80 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
81 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
82 	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
83 	//	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
84 	glTexImage2D(GL_TEXTURE_2D, 0, 2, header.tex_width,
85 		header.tex_height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
86 		(void *)tex_bytes);
87 
88 	//Free texture pixels memory
89 	delete[] tex_bytes;
90 
91 	//Close input file
92 	input.close();
93 
94 	//Return successfully
95 	return true;
96 }
97 //*******************************************************************
Create(const std::string & file_name,int tex)98 bool GLFont::Create (const std::string &file_name, int tex)
99 {
100 	return Create(file_name.c_str(), tex);
101 }
102 //*******************************************************************
Destroy(void)103 void GLFont::Destroy (void)
104 {
105 	//Delete the character array if necessary
106 	if (header.chars)
107 	{
108 		delete[] header.chars;
109 		header.chars = NULL;
110 	}
111 }
112 //*******************************************************************
GetTexSize(std::pair<int,int> * size)113 void GLFont::GetTexSize (std::pair<int, int> *size)
114 {
115 	//Retrieve texture size
116 	size->first = header.tex_width;
117 	size->second = header.tex_height;
118 }
119 //*******************************************************************
GetTexWidth(void)120 int GLFont::GetTexWidth (void)
121 {
122 	//Return texture width
123 	return header.tex_width;
124 }
125 //*******************************************************************
GetTexHeight(void)126 int GLFont::GetTexHeight (void)
127 {
128 	//Return texture height
129 	return header.tex_height;
130 }
131 //*******************************************************************
GetCharInterval(std::pair<int,int> * interval)132 void GLFont::GetCharInterval (std::pair<int, int> *interval)
133 {
134 	//Retrieve character interval
135 	interval->first = header.start_char;
136 	interval->second = header.end_char;
137 }
138 //*******************************************************************
GetStartChar(void)139 int GLFont::GetStartChar (void)
140 {
141 	//Return start character
142 	return header.start_char;
143 }
144 //*******************************************************************
GetEndChar(void)145 int GLFont::GetEndChar (void)
146 {
147 	//Return end character
148 	return header.end_char;
149 }
150 //*******************************************************************
GetCharSize(int c,std::pair<int,int> * size)151 void GLFont::GetCharSize (int c, std::pair<int, int> *size)
152 {
153 	//Make sure character is in range
154 	if (c < header.start_char || c > header.end_char)
155 	{
156 		//Not a valid character, so it obviously has no size
157 		size->first = 0;
158 		size->second = 0;
159 	}
160 	else
161 	{
162 		GLFontChar *glfont_char;
163 
164 		//Retrieve character size
165 		glfont_char = &header.chars[c - header.start_char];
166 		size->first = (int)(glfont_char->dx * header.tex_width);
167 		size->second = (int)(glfont_char->dy *
168 			header.tex_height);
169 	}
170 }
171 //*******************************************************************
GetCharWidth(int c)172 int GLFont::GetCharWidth (int c)
173 {
174 	//Make sure in range
175 	if (c < header.start_char || c > header.end_char)
176 		return 0;
177 	else
178 	{
179 		GLFontChar *glfont_char;
180 
181 		//Retrieve character width
182 		glfont_char = &header.chars[c - header.start_char];
183 		return (int)(glfont_char->dx * header.tex_width);
184 	}
185 }
186 //*******************************************************************
GetCharHeight(int c)187 int GLFont::GetCharHeight (int c)
188 {
189 	//Make sure in range
190 	if (c < header.start_char || c > header.end_char)
191 		return 0;
192 	else
193 	{
194 		GLFontChar *glfont_char;
195 
196 		//Retrieve character height
197 		glfont_char = &header.chars[c - header.start_char];
198 		return (int)(glfont_char->dy * header.tex_height);
199 	}
200 }
201 //*******************************************************************
Begin(void)202 void GLFont::Begin (void)
203 {
204 	//Bind to font texture
205 	glBindTexture(GL_TEXTURE_2D, header.tex);
206 }
207 //*******************************************************************
208 
209 //End of file
210 
211