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(const char *fileName)
31 LOG(std::string("Loading font texture...") + fileName);
33 FontTexture.load(fileName, false, false);
36 //LoadTGA( fileName );
37 unsigned char fileNamep[256];
38 CopyCStringToPascal(fileName,fileNamep);
40 upload_image( fileNamep ,1);
45 if ( texture.bpp == 24 )
50 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
52 if(!FontTexture)glGenTextures( 1, &FontTexture );
53 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
55 glBindTexture( GL_TEXTURE_2D, FontTexture);
56 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
57 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
59 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
63 glDeleteLists(base, 512);
68 void Text::BuildFont() // Build Our Font Display List
70 float cx; // Holds Our X Character Coord
71 float cy; // Holds Our Y Character Coord
77 glDeleteLists(base, 512);
79 //LOG("Font already created...");
83 //base=glGenLists(256); // Creating 256 Display Lists
84 base = glGenLists(512); // Creating 256 Display Lists
86 for (loop = 0; loop < 512; loop++) { // Loop Through All 256 Lists
88 cx = float(loop % 16) / 16.0f; // X Position Of Current Character
89 cy = float(loop / 16) / 16.0f; // Y Position Of Current Character
91 cx = float((loop - 256) % 16) / 16.0f; // X Position Of Current Character
92 cy = float((loop - 256) / 16) / 16.0f; // Y Position Of Current Character
94 glNewList(base + loop, GL_COMPILE); // Start Building A List
95 glBegin(GL_QUADS); // Use A Quad For Each Character
96 glTexCoord2f(cx, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Left)
97 glVertex2i(0, 0); // Vertex Coord (Bottom Left)
98 glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Right)
99 glVertex2i(16, 0); // Vertex Coord (Bottom Right)
100 glTexCoord2f(cx + 0.0625f, 1 - cy - .001); // Texture Coord (Top Right)
101 glVertex2i(16, 16); // Vertex Coord (Top Right)
102 glTexCoord2f(cx, 1 - cy - +.001); // Texture Coord (Top Left)
103 glVertex2i(0, 16); // Vertex Coord (Top Left)
104 glEnd(); // Done Building Our Quad (Character)
106 glTranslated(10, 0, 0); // Move To The Right Of The Character
108 glTranslated(8, 0, 0); // Move To The Right Of The Character
109 glEndList(); // Done Building The Display List
110 } // Loop Until All 256 Are Built
113 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
115 glPrint(x, y, string, set, size, width, height, 0, strlen(string));
118 void Text::_glPrint(float x, float y, const char *string, int set, float size, float width, float height, int start, int end, int offset) // Where The Printing Happens
123 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
125 glDisable(GL_DEPTH_TEST);
126 glDisable(GL_LIGHTING);
128 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129 glMatrixMode(GL_PROJECTION);
132 glOrtho(0, width, 0, height, -100, 100);
133 glMatrixMode(GL_MODELVIEW);
136 glTranslated(x, y, 0);
137 glScalef(size, size, 1);
138 glListBase(base - 32 + (128 * set) + offset); // Choose The Font Set (0 or 1)
139 glCallLists(end - start, GL_BYTE, &string[start]); // Write The Text To The Screen
140 glMatrixMode(GL_PROJECTION);
142 glMatrixMode(GL_MODELVIEW);
144 glEnable(GL_DEPTH_TEST);
145 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
148 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height, int start, int end) // Where The Printing Happens
150 _glPrint(x, y, string, set, size, width, height, start, end, 0);
153 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
155 glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
158 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height, int start, int end) // Where The Printing Happens
160 _glPrint(x, y, string, set, size, width, height, start, end, 256);
162 void Text::glPrintOutlined(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
164 glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
167 void Text::glPrintOutlined(float r, float g, float b, float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
169 glColor4f(0, 0, 0, 1);
170 glPrintOutline( x - 2 * size, y - 2 * size, string, set, size * 2.5 / 2, width, height);
171 glColor4f(r, g, b, 1);
172 glPrint( x, y, string, set, size, width, height);
182 glDeleteLists(base, 512);
185 FontTexture.destroy();