1 /*
2 * Text.cpp
3 * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20 /**> HEADER FILES <**/
21 #include "Text.h"
22
LoadFontTexture(char * fileName)23 void Text::LoadFontTexture(char *fileName)
24 {
25 TGAImageRec *tempTexture;
26 GLuint type;
27
28 //Load Image
29 tempTexture = LoadTGA(fileName);
30 //Is it valid?
31 if(tempTexture) {
32 //Alpha channel?
33 if(tempTexture->bpp == 24)
34 type = GL_RGB;
35 else
36 type = GL_RGBA;
37
38 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
39
40 glGenTextures(1, &FontTexture);
41 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
42
43 glBindTexture(GL_TEXTURE_2D, FontTexture);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
46
47 gluBuild2DMipmaps(GL_TEXTURE_2D, type, tempTexture->sizeX,
48 tempTexture->sizeY, type, GL_UNSIGNED_BYTE,
49 tempTexture->data);
50 free(tempTexture->data);
51 free(tempTexture);
52 }
53 }
54
BuildFont()55 void Text::BuildFont() // Build Our Font Display List
56 {
57 float cx; // Holds Our X Character Coord
58 float cy; // Holds Our Y Character Coord
59 int loop;
60
61 base = glGenLists(256); // Creating 256 Display Lists
62 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
63 for(loop = 0; loop < 256; loop++) // Loop Through All 256 Lists
64 {
65 cx = float (loop % 16) / 16.0f; // X Position Of Current Character
66 cy = float (loop / 16) / 16.0f; // Y Position Of Current Character
67
68 glNewList(base + loop, GL_COMPILE); // Start Building A List
69 glBegin(GL_QUADS); // Use A Quad For Each Character
70 glTexCoord2f(cx, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Left)
71 glVertex2i(0, 0); // Vertex Coord (Bottom Left)
72 glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Right)
73 glVertex2i(16, 0); // Vertex Coord (Bottom Right)
74 glTexCoord2f(cx + 0.0625f, 1 - cy - .001); // Texture Coord (Top Right)
75 glVertex2i(16, 16); // Vertex Coord (Top Right)
76 glTexCoord2f(cx, 1 - cy - +.001); // Texture Coord (Top Left)
77 glVertex2i(0, 16); // Vertex Coord (Top Left)
78 glEnd(); // Done Building Our Quad (Character)
79 glTranslated(10, 0, 0); // Move To The Right Of The Character
80 glEndList(); // Done Building The Display List
81 } // Loop Until All 256 Are Built
82 }
83
glPrint(GLint x,GLint y,char * string,int set,float size,float width,float height)84 void Text::glPrint(GLint x, GLint y, char *string, int set, float size, float width, float height) // Where The Printing Happens
85 {
86 if(set > 1) {
87 set = 1;
88 }
89 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
90 glBindTexture(GL_TEXTURE_2D, FontTexture); // Select Our Font Texture
91 glDisable(GL_DEPTH_TEST); // Disables Depth Testing
92 glDisable(GL_LIGHTING);
93 glEnable(GL_BLEND);
94 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
96 glPushMatrix(); // Store The Projection Matrix
97 glLoadIdentity(); // Reset The Projection Matrix
98 glOrtho(0, width, 0, height, -100, 100); // Set Up An Ortho Screen
99 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
100 glPushMatrix(); // Store The Modelview Matrix
101 glLoadIdentity();
102 glScalef(size, size, 1); // Reset The Modelview Matrix
103 glTranslated(x, y, 0); // Position The Text (0,0 - Bottom Left)
104 glListBase(base - 32 + (128 * set)); // Choose The Font Set (0 or 1)
105 glCallLists(strlen(string), GL_BYTE, string); // Write The Text To The Screen
106 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
107 glPopMatrix(); // Restore The Old Projection Matrix
108 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
109 glPopMatrix(); // Restore The Old Projection Matrix
110 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
111 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
112 }
113