2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /**> HEADER FILES <**/
25 extern TGAImageRec texture;
27 void Text::LoadFontTexture(char *fileName)
33 LOG(std::string("Loading font texture...") + fileName);
35 Game::LoadTexture(fileName, &FontTexture, false, false);
38 //LoadTGA( fileName );
39 unsigned char fileNamep[256];
40 CopyCStringToPascal(fileName,fileNamep);
42 upload_image( fileNamep ,1);
47 if ( texture.bpp == 24 )
52 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
54 if(!FontTexture)glGenTextures( 1, &FontTexture );
55 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
57 glBindTexture( GL_TEXTURE_2D, FontTexture);
58 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
59 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
61 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
66 glDeleteLists(base, 512);
71 void Text::BuildFont() // Build Our Font Display List
73 float cx; // Holds Our X Character Coord
74 float cy; // Holds Our Y Character Coord
81 LOG("Font already created...");
85 // base=glGenLists(256); // Creating 256 Display Lists
86 base=glGenLists(512); // Creating 256 Display Lists
87 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
88 for (loop=0; loop<256; loop++) // Loop Through All 256 Lists
90 cx=float(loop%16)/16.0f; // X Position Of Current Character
91 cy=float(loop/16)/16.0f; // Y Position Of Current Character
93 glNewList(base+loop,GL_COMPILE); // Start Building A List
94 glBegin(GL_QUADS); // Use A Quad For Each Character
95 glTexCoord2f(cx,1-cy-0.0625f+.001); // Texture Coord (Bottom Left)
96 glVertex2i(0,0); // Vertex Coord (Bottom Left)
97 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001); // Texture Coord (Bottom Right)
98 glVertex2i(16,0); // Vertex Coord (Bottom Right)
99 glTexCoord2f(cx+0.0625f,1-cy-.001); // Texture Coord (Top Right)
100 glVertex2i(16,16); // Vertex Coord (Top Right)
101 glTexCoord2f(cx,1-cy-+.001); // Texture Coord (Top Left)
102 glVertex2i(0,16); // Vertex Coord (Top Left)
103 glEnd(); // Done Building Our Quad (Character)
104 glTranslated(10,0,0); // Move To The Right Of The Character
105 glEndList(); // Done Building The Display List
106 } // Loop Until All 256 Are Built
107 for (loop=256; loop<512; loop++) // Loop Through All 256 Lists
109 cx=float((loop-256)%16)/16.0f; // X Position Of Current Character
110 cy=float((loop-256)/16)/16.0f; // Y Position Of Current Character
112 glNewList(base+loop,GL_COMPILE); // Start Building A List
113 glBegin(GL_QUADS); // Use A Quad For Each Character
114 glTexCoord2f(cx,1-cy-0.0625f+.001); // Texture Coord (Bottom Left)
115 glVertex2i(0,0); // Vertex Coord (Bottom Left)
116 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001); // Texture Coord (Bottom Right)
117 glVertex2i(16,0); // Vertex Coord (Bottom Right)
118 glTexCoord2f(cx+0.0625f,1-cy-.001); // Texture Coord (Top Right)
119 glVertex2i(16,16); // Vertex Coord (Top Right)
120 glTexCoord2f(cx,1-cy-+.001); // Texture Coord (Top Left)
121 glVertex2i(0,16); // Vertex Coord (Top Left)
122 glEnd(); // Done Building Our Quad (Character)
123 glTranslated(8,0,0); // Move To The Right Of The Character
124 glEndList(); // Done Building The Display List
125 } // Loop Until All 256 Are Built
128 void Text::glPrint(float x, float y, char *string, int set, float size, float width, float height) // Where The Printing Happens
134 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
135 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
136 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
137 glDisable(GL_LIGHTING);
139 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
140 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
141 glPushMatrix(); // Store The Projection Matrix
142 glLoadIdentity(); // Reset The Projection Matrix
143 glOrtho(0,width,0,height,-100,100); // Set Up An Ortho Screen
144 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
145 glPushMatrix(); // Store The Modelview Matrix
147 glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
148 glScalef(size,size,1); // Reset The Modelview Matrix
149 glListBase(base-32+(128*set)); // Choose The Font Set (0 or 1)
150 glCallLists(strlen(string),GL_BYTE,string); // Write The Text To The Screen
151 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
152 glPopMatrix(); // Restore The Old Projection Matrix
153 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
154 glPopMatrix(); // Restore The Old Projection Matrix
155 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
156 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
159 void Text::glPrint(float x, float y, char *string, int set, float size, float width, float height,int start,int end) // Where The Printing Happens
165 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
166 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
167 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
168 glDisable(GL_LIGHTING);
170 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
171 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
172 glPushMatrix(); // Store The Projection Matrix
173 glLoadIdentity(); // Reset The Projection Matrix
174 glOrtho(0,width,0,height,-100,100); // Set Up An Ortho Screen
175 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
176 glPushMatrix(); // Store The Modelview Matrix
178 glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
179 glScalef(size,size,1); // Reset The Modelview Matrix
180 glListBase(base-32+(128*set)); // Choose The Font Set (0 or 1)
181 glCallLists(end-start,GL_BYTE,&string[start]); // Write The Text To The Screen
182 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
183 glPopMatrix(); // Restore The Old Projection Matrix
184 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
185 glPopMatrix(); // Restore The Old Projection Matrix
186 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
187 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
191 void Text::glPrintOutline(float x, float y, char *string, int set, float size, float width, float height) // Where The Printing Happens
197 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
198 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
199 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
200 glDisable(GL_LIGHTING);
202 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
203 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
204 glPushMatrix(); // Store The Projection Matrix
205 glLoadIdentity(); // Reset The Projection Matrix
206 glOrtho(0,width,0,height,-100,100); // Set Up An Ortho Screen
207 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
208 glPushMatrix(); // Store The Modelview Matrix
210 glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
211 glScalef(size,size,1); // Reset The Modelview Matrix
212 glListBase(base-32+(128*set)+256); // Choose The Font Set (0 or 1)
213 glCallLists(strlen(string),GL_BYTE,string); // Write The Text To The Screen
214 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
215 glPopMatrix(); // Restore The Old Projection Matrix
216 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
217 glPopMatrix(); // Restore The Old Projection Matrix
218 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
219 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
222 void Text::glPrintOutline(float x, float y, char *string, int set, float size, float width, float height,int start,int end) // Where The Printing Happens
228 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
229 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
230 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
231 glDisable(GL_LIGHTING);
233 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
234 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
235 glPushMatrix(); // Store The Projection Matrix
236 glLoadIdentity(); // Reset The Projection Matrix
237 glOrtho(0,width,0,height,-100,100); // Set Up An Ortho Screen
238 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
239 glPushMatrix(); // Store The Modelview Matrix
241 glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
242 glScalef(size,size,1); // Reset The Modelview Matrix
243 glListBase(base-32+(128*set)+256); // Choose The Font Set (0 or 1)
244 glCallLists(end-start,GL_BYTE,&string[start]); // Write The Text To The Screen
245 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
246 glPopMatrix(); // Restore The Old Projection Matrix
247 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
248 glPopMatrix(); // Restore The Old Projection Matrix
249 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
250 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
253 void Text::glPrintOutlined(float x, float y, char *string, int set, float size, float width, float height) // Where The Printing Happens
256 glPrintOutline( x-2*size, y-2*size, string, set, size*2.5/2, width, height);
258 glPrint( x, y, string, set, size, width, height);
261 void Text::glPrintOutlined(float r, float g, float b, float x, float y, char *string, int set, float size, float width, float height) // Where The Printing Happens
264 glPrintOutline( x-2*size, y-2*size, string, set, size*2.5/2, width, height);
266 glPrint( x, y, string, set, size, width, height);
278 glDeleteLists(base, 512);
281 if (FontTexture) glDeleteTextures( 1, &FontTexture );