1 /*
2  * Font.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 #include "Font.h"
21 
LoadFontTexture(char * fileName)22 void Font::LoadFontTexture(char *fileName)
23 {
24   TGAImageRec *tempTexture;
25   GLuint type;
26 
27   //Load Image
28   tempTexture = LoadTGA(fileName);
29   //Is it valid?
30   if(tempTexture) {
31     //Alpha channel?
32     if(tempTexture->bpp == 24)
33       type = GL_RGB;
34     else
35       type = GL_RGBA;
36 
37     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
38 
39     glGenTextures(1, &FontTexture);
40     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
41 
42     glBindTexture(GL_TEXTURE_2D, FontTexture);
43     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
44     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
45 
46     gluBuild2DMipmaps(GL_TEXTURE_2D, type, tempTexture->sizeX,
47                       tempTexture->sizeY, type, GL_UNSIGNED_BYTE,
48                       tempTexture->data);
49     free(tempTexture->data);
50     free(tempTexture);
51   }
52 }
53 
BuildFont()54 void Font::BuildFont()          // Build Our Font Display List
55 {
56   float cx;                     // Holds Our X Character Coord
57   float cy;                     // Holds Our Y Character Coord
58   int loop;
59 
60   base = glGenLists(256);       // Creating 256 Display Lists
61   glBindTexture(GL_TEXTURE_2D, FontTexture);    // Select Our Font Texture
62   for(loop = 0; loop < 256; loop++)     // Loop Through All 256 Lists
63   {
64     cx = float (loop % 16) / 16.0f;     // X Position Of Current Character
65     cy = float (loop / 16) / 16.0f;     // Y Position Of Current Character
66 
67     glNewList(base + loop, GL_COMPILE); // Start Building A List
68     glBegin(GL_QUADS);          // Use A Quad For Each Character
69     glTexCoord2f(cx, 1 - cy - 0.0625f + .001);  // Texture Coord (Bottom Left)
70     glVertex2i(0, 0);           // Vertex Coord (Bottom Left)
71     glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001);        // Texture Coord (Bottom Right)
72     glVertex2i(16, 0);          // Vertex Coord (Bottom Right)
73     glTexCoord2f(cx + 0.0625f, 1 - cy - .001);  // Texture Coord (Top Right)
74     glVertex2i(16, 16);         // Vertex Coord (Top Right)
75     glTexCoord2f(cx, 1 - cy - +.001);   // Texture Coord (Top Left)
76     glVertex2i(0, 16);          // Vertex Coord (Top Left)
77     glEnd();                    // Done Building Our Quad (Character)
78     glTranslated(10, 0, 0);     // Move To The Right Of The Character
79     glEndList();                // Done Building The Display List
80   }                             // Loop Until All 256 Are Built
81 }
82