]> git.jsancho.org Git - lugaru.git/blob - Source/Text.cpp
Took off some FORCE markers to allow configuration on OSX
[lugaru.git] / Source / Text.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
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.
10
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.  
14
15 See the GNU General Public License for more details.
16
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.
20 */
21
22 /**> HEADER FILES <**/
23 #include "Text.h"
24 #include "Game.h"
25 extern TGAImageRec texture;
26
27 void Text::LoadFontTexture(const char *fileName)
28 {
29         GLuint          type;
30
31         LOGFUNC;
32
33         LOG(std::string("Loading font texture...") + fileName);
34
35         Game::LoadTexture(fileName, &FontTexture, false, false);
36 /*
37         //Load Image
38         //LoadTGA( fileName ); 
39         unsigned char fileNamep[256];
40         CopyCStringToPascal(fileName,fileNamep);
41         //Load Image
42         upload_image( fileNamep ,1); 
43
44         //Is it valid?
45         if(1==1){
46                 //Alpha channel?
47                 if ( texture.bpp == 24 )
48                         type = GL_RGB;
49                 else
50                         type = GL_RGBA;
51
52                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
53
54                 if(!FontTexture)glGenTextures( 1, &FontTexture );
55                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
56
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 );
60
61                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
62         }
63 */
64         if (base)
65         {
66                 glDeleteLists(base, 512);
67                 base = 0;
68         }
69 }
70
71 void Text::BuildFont()                                                          // Build Our Font Display List
72 {
73         float   cx;                                                                                     // Holds Our X Character Coord
74         float   cy;                                                                                     // Holds Our Y Character Coord
75         int loop;
76
77         LOGFUNC;
78
79         if (base)
80         {
81                 LOG("Font already created...");
82                 return;
83         }
84
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<512; loop++)                                          // Loop Through All 256 Lists
89         {
90                 if (loop < 256)
91                 {
92                         cx=float(loop%16)/16.0f;                                                // X Position Of Current Character
93                         cy=float(loop/16)/16.0f;                                                // Y Position Of Current Character
94                 }
95                 else
96                 {
97                         cx=float((loop-256)%16)/16.0f;                                          // X Position Of Current Character
98                         cy=float((loop-256)/16)/16.0f;                                          // Y Position Of Current Character
99                 }
100                 glNewList(base+loop,GL_COMPILE);                                // Start Building A List
101                 glBegin(GL_QUADS);                                                      // Use A Quad For Each Character
102                 glTexCoord2f(cx,1-cy-0.0625f+.001);                     // Texture Coord (Bottom Left)
103                 glVertex2i(0,0);                                                // Vertex Coord (Bottom Left)
104                 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001);     // Texture Coord (Bottom Right)
105                 glVertex2i(16,0);                                               // Vertex Coord (Bottom Right)
106                 glTexCoord2f(cx+0.0625f,1-cy-.001);                     // Texture Coord (Top Right)
107                 glVertex2i(16,16);                                              // Vertex Coord (Top Right)
108                 glTexCoord2f(cx,1-cy-+.001);                                    // Texture Coord (Top Left)
109                 glVertex2i(0,16);                                               // Vertex Coord (Top Left)
110                 glEnd();                                                                        // Done Building Our Quad (Character)
111                 if (loop < 256)
112                         glTranslated(10,0,0);                                           // Move To The Right Of The Character
113                 else
114                         glTranslated(8,0,0);                                            // Move To The Right Of The Character
115                 glEndList();                                                                    // Done Building The Display List
116         }                                                                                                       // Loop Until All 256 Are Built
117 }
118
119 void Text::glPrint(float x, float y, char *string, int set, float size, float width, float height)      // Where The Printing Happens
120 {
121         glPrint(x, y, string, set, size, width, height, 0, strlen(string));
122 }
123
124 void Text::_glPrint(float x, float y, char *string, int set, float size, float width, float height,int start,int end, int offset)       // Where The Printing Happens
125 {
126         if (set>1)
127         {
128                 set=1;
129         }
130         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
131         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
132         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
133         glDisable(GL_LIGHTING);
134         glEnable(GL_BLEND);
135         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
136         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
137         glPushMatrix();                                                                         // Store The Projection Matrix
138                 glLoadIdentity();                                                                       // Reset The Projection Matrix
139                 glOrtho(0,width,0,height,-100,100);                                             // Set Up An Ortho Screen
140                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
141                 glPushMatrix();                                                                         // Store The Modelview Matrix
142                         glLoadIdentity();
143                         glTranslated(x,y,0);                                                            // Position The Text (0,0 - Bottom Left)
144                         glScalef(size,size,1);                                                                  // Reset The Modelview Matrix
145                         glListBase(base-32+(128*set) + offset);                                         // Choose The Font Set (0 or 1)
146                         glCallLists(end-start,GL_BYTE,&string[start]);                  // Write The Text To The Screen
147                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
148                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
149                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
150         glPopMatrix();                                                                          // Restore The Old Projection Matrix
151         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
152         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
153 }
154
155 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
156 {
157         _glPrint(x, y, string, set, size, width, height, start, end, 0);
158 }
159
160 void Text::glPrintOutline(float x, float y, char *string, int set, float size, float width, float height)       // Where The Printing Happens
161 {
162         glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
163 }
164
165 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
166 {
167         _glPrint(x, y, string, set, size, width, height, start, end, 256);
168 }
169 void Text::glPrintOutlined(float x, float y, char *string, int set, float size, float width, float height)      // Where The Printing Happens
170 {
171         glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
172 }
173
174 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
175 {
176         glColor4f(0,0,0,1);
177         glPrintOutline( x-2*size,  y-2*size, string,  set,  size*2.5/2,  width,  height);
178         glColor4f(r,g,b,1);
179         glPrint( x,  y, string,  set,  size,  width,  height);
180 }
181
182 Text::Text()
183 {
184         base = 0;
185         FontTexture = 0;
186 }
187 Text::~Text()
188 {
189         if (base)
190         {
191                 glDeleteLists(base, 512);
192                 base = 0;
193         }
194         if (FontTexture) glDeleteTextures( 1, &FontTexture );
195 }
196