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)
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 glDeleteLists(base, 512);
83 //LOG("Font already created...");
87 // base=glGenLists(256); // Creating 256 Display Lists
88 base=glGenLists(512); // Creating 256 Display Lists
89 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
90 for (loop=0; loop<512; loop++) // Loop Through All 256 Lists
94 cx=float(loop%16)/16.0f; // X Position Of Current Character
95 cy=float(loop/16)/16.0f; // Y Position Of Current Character
99 cx=float((loop-256)%16)/16.0f; // X Position Of Current Character
100 cy=float((loop-256)/16)/16.0f; // Y Position Of Current Character
102 glNewList(base+loop,GL_COMPILE); // Start Building A List
103 glBegin(GL_QUADS); // Use A Quad For Each Character
104 glTexCoord2f(cx,1-cy-0.0625f+.001); // Texture Coord (Bottom Left)
105 glVertex2i(0,0); // Vertex Coord (Bottom Left)
106 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001); // Texture Coord (Bottom Right)
107 glVertex2i(16,0); // Vertex Coord (Bottom Right)
108 glTexCoord2f(cx+0.0625f,1-cy-.001); // Texture Coord (Top Right)
109 glVertex2i(16,16); // Vertex Coord (Top Right)
110 glTexCoord2f(cx,1-cy-+.001); // Texture Coord (Top Left)
111 glVertex2i(0,16); // Vertex Coord (Top Left)
112 glEnd(); // Done Building Our Quad (Character)
114 glTranslated(10,0,0); // Move To The Right Of The Character
116 glTranslated(8,0,0); // Move To The Right Of The Character
117 glEndList(); // Done Building The Display List
118 } // Loop Until All 256 Are Built
121 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
123 glPrint(x, y, string, set, size, width, height, 0, strlen(string));
126 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
132 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
133 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
134 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
135 glDisable(GL_LIGHTING);
137 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
138 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
139 glPushMatrix(); // Store The Projection Matrix
140 glLoadIdentity(); // Reset The Projection Matrix
141 glOrtho(0,width,0,height,-100,100); // Set Up An Ortho Screen
142 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
143 glPushMatrix(); // Store The Modelview Matrix
145 glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
146 glScalef(size,size,1); // Reset The Modelview Matrix
147 glListBase(base-32+(128*set) + offset); // Choose The Font Set (0 or 1)
148 glCallLists(end-start,GL_BYTE,&string[start]); // Write The Text To The Screen
149 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
150 glPopMatrix(); // Restore The Old Projection Matrix
151 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
152 glPopMatrix(); // Restore The Old Projection Matrix
153 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
154 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
157 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
159 _glPrint(x, y, string, set, size, width, height, start, end, 0);
162 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
164 glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
167 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
169 _glPrint(x, y, string, set, size, width, height, start, end, 256);
171 void Text::glPrintOutlined(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
173 glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
176 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
179 glPrintOutline( x-2*size, y-2*size, string, set, size*2.5/2, width, height);
181 glPrint( x, y, string, set, size, width, height);
193 glDeleteLists(base, 512);
196 glDeleteTextures( 1, &FontTexture );