]> git.jsancho.org Git - lugaru.git/blob - Source/Text.cpp
cc8ba278a13a3751607cc8bdef8f56bd9c932a62
[lugaru.git] / Source / Text.cpp
1 /**> HEADER FILES <**/
2 #include "Text.h"
3 #include "Game.h"
4 extern TGAImageRec texture;
5
6 void Text::LoadFontTexture(char *fileName)
7 {
8         GLuint          type;
9
10         LOGFUNC;
11
12         LOG(std::string("Loading font texture...") + fileName);
13
14         Game::LoadTexture(fileName, &FontTexture, false, false);
15 /*
16         //Load Image
17         //LoadTGA( fileName ); 
18         unsigned char fileNamep[256];
19         CopyCStringToPascal(fileName,fileNamep);
20         //Load Image
21         upload_image( fileNamep ,1); 
22
23         //Is it valid?
24         if(1==1){
25                 //Alpha channel?
26                 if ( texture.bpp == 24 )
27                         type = GL_RGB;
28                 else
29                         type = GL_RGBA;
30
31                 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
32
33                 if(!FontTexture)glGenTextures( 1, &FontTexture );
34                 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
35
36                 glBindTexture( GL_TEXTURE_2D, FontTexture);
37                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
38                 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
39
40                 gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
41         }
42 */
43         if (base)
44         {
45                 glDeleteLists(base, 512);
46                 base = 0;
47         }
48 }
49
50 void Text::BuildFont()                                                          // Build Our Font Display List
51 {
52         float   cx;                                                                                     // Holds Our X Character Coord
53         float   cy;                                                                                     // Holds Our Y Character Coord
54         int loop;
55
56         LOGFUNC;
57
58         if (base)
59         {
60                 LOG("Font already created...");
61                 return;
62         }
63
64 //      base=glGenLists(256);                                                           // Creating 256 Display Lists
65         base=glGenLists(512);                                                           // Creating 256 Display Lists
66         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
67         for (loop=0; loop<256; loop++)                                          // Loop Through All 256 Lists
68         {
69                 cx=float(loop%16)/16.0f;                                                // X Position Of Current Character
70                 cy=float(loop/16)/16.0f;                                                // Y Position Of Current Character
71
72                 glNewList(base+loop,GL_COMPILE);                                // Start Building A List
73                 glBegin(GL_QUADS);                                                      // Use A Quad For Each Character
74                 glTexCoord2f(cx,1-cy-0.0625f+.001);                     // Texture Coord (Bottom Left)
75                 glVertex2i(0,0);                                                // Vertex Coord (Bottom Left)
76                 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001);     // Texture Coord (Bottom Right)
77                 glVertex2i(16,0);                                               // Vertex Coord (Bottom Right)
78                 glTexCoord2f(cx+0.0625f,1-cy-.001);                     // Texture Coord (Top Right)
79                 glVertex2i(16,16);                                              // Vertex Coord (Top Right)
80                 glTexCoord2f(cx,1-cy-+.001);                                    // Texture Coord (Top Left)
81                 glVertex2i(0,16);                                               // Vertex Coord (Top Left)
82                 glEnd();                                                                        // Done Building Our Quad (Character)
83                 glTranslated(10,0,0);                                           // Move To The Right Of The Character
84                 glEndList();                                                                    // Done Building The Display List
85         }                                                                                                       // Loop Until All 256 Are Built
86         for (loop=256; loop<512; loop++)                                                // Loop Through All 256 Lists
87         {
88                 cx=float((loop-256)%16)/16.0f;                                          // X Position Of Current Character
89                 cy=float((loop-256)/16)/16.0f;                                          // Y Position Of Current Character
90
91                 glNewList(base+loop,GL_COMPILE);                                // Start Building A List
92                 glBegin(GL_QUADS);                                                      // Use A Quad For Each Character
93                 glTexCoord2f(cx,1-cy-0.0625f+.001);                     // Texture Coord (Bottom Left)
94                 glVertex2i(0,0);                                                // Vertex Coord (Bottom Left)
95                 glTexCoord2f(cx+0.0625f,1-cy-0.0625f+.001);     // Texture Coord (Bottom Right)
96                 glVertex2i(16,0);                                               // Vertex Coord (Bottom Right)
97                 glTexCoord2f(cx+0.0625f,1-cy-.001);                     // Texture Coord (Top Right)
98                 glVertex2i(16,16);                                              // Vertex Coord (Top Right)
99                 glTexCoord2f(cx,1-cy-+.001);                                    // Texture Coord (Top Left)
100                 glVertex2i(0,16);                                               // Vertex Coord (Top Left)
101                 glEnd();                                                                        // Done Building Our Quad (Character)
102                 glTranslated(8,0,0);                                            // Move To The Right Of The Character
103                 glEndList();                                                                    // Done Building The Display List
104         }                                                                                                       // Loop Until All 256 Are Built
105 }
106
107 void Text::glPrint(float x, float y, char *string, int set, float size, float width, float height)      // Where The Printing Happens
108 {
109         if (set>1)
110         {
111                 set=1;
112         }
113         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
114         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
115         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
116         glDisable(GL_LIGHTING);
117         glEnable(GL_BLEND);
118         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
119         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
120         glPushMatrix();                                                                         // Store The Projection Matrix
121                 glLoadIdentity();                                                                       // Reset The Projection Matrix
122                 glOrtho(0,width,0,height,-100,100);                                             // Set Up An Ortho Screen
123                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
124                 glPushMatrix();                                                                         // Store The Modelview Matrix
125                         glLoadIdentity();
126                         glTranslated(x,y,0);                                                            // Position The Text (0,0 - Bottom Left)
127                         glScalef(size,size,1);                                                                  // Reset The Modelview Matrix
128                         glListBase(base-32+(128*set));                                          // Choose The Font Set (0 or 1)
129                         glCallLists(strlen(string),GL_BYTE,string);                     // Write The Text To The Screen
130                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
131                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
132                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
133         glPopMatrix();                                                                          // Restore The Old Projection Matrix
134         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
135         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
136 }
137
138 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
139 {
140         if (set>1)
141         {
142                 set=1;
143         }
144         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
145         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
146         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
147         glDisable(GL_LIGHTING);
148         glEnable(GL_BLEND);
149         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
150         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
151         glPushMatrix();                                                                         // Store The Projection Matrix
152                 glLoadIdentity();                                                                       // Reset The Projection Matrix
153                 glOrtho(0,width,0,height,-100,100);                                             // Set Up An Ortho Screen
154                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
155                 glPushMatrix();                                                                         // Store The Modelview Matrix
156                         glLoadIdentity();
157                         glTranslated(x,y,0);                                                            // Position The Text (0,0 - Bottom Left)
158                         glScalef(size,size,1);                                                                  // Reset The Modelview Matrix
159                         glListBase(base-32+(128*set));                                          // Choose The Font Set (0 or 1)
160                         glCallLists(end-start,GL_BYTE,&string[start]);                  // Write The Text To The Screen
161                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
162                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
163                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
164         glPopMatrix();                                                                          // Restore The Old Projection Matrix
165         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
166         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
167 }
168
169
170 void Text::glPrintOutline(float x, float y, char *string, int set, float size, float width, float height)       // Where The Printing Happens
171 {
172         if (set>1)
173         {
174                 set=1;
175         }
176         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
177         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
178         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
179         glDisable(GL_LIGHTING);
180         glEnable(GL_BLEND);
181         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
182         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
183         glPushMatrix();                                                                         // Store The Projection Matrix
184                 glLoadIdentity();                                                                       // Reset The Projection Matrix
185                 glOrtho(0,width,0,height,-100,100);                                             // Set Up An Ortho Screen
186                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
187                 glPushMatrix();                                                                         // Store The Modelview Matrix
188                         glLoadIdentity();
189                         glTranslated(x,y,0);                                                            // Position The Text (0,0 - Bottom Left)
190                         glScalef(size,size,1);                                                                  // Reset The Modelview Matrix
191                         glListBase(base-32+(128*set)+256);                                              // Choose The Font Set (0 or 1)
192                         glCallLists(strlen(string),GL_BYTE,string);                     // Write The Text To The Screen
193                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
194                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
195                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
196         glPopMatrix();                                                                          // Restore The Old Projection Matrix
197         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
198         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
199 }
200
201 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
202 {
203         if (set>1)
204         {
205                 set=1;
206         }
207         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
208         glBindTexture(GL_TEXTURE_2D, FontTexture);                      // Select Our Font Texture
209         glDisable(GL_DEPTH_TEST);                                                       // Disables Depth Testing
210         glDisable(GL_LIGHTING);
211         glEnable(GL_BLEND);
212         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
213         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
214         glPushMatrix();                                                                         // Store The Projection Matrix
215                 glLoadIdentity();                                                                       // Reset The Projection Matrix
216                 glOrtho(0,width,0,height,-100,100);                                             // Set Up An Ortho Screen
217                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
218                 glPushMatrix();                                                                         // Store The Modelview Matrix
219                         glLoadIdentity();
220                         glTranslated(x,y,0);                                                            // Position The Text (0,0 - Bottom Left)
221                         glScalef(size,size,1);                                                                  // Reset The Modelview Matrix
222                         glListBase(base-32+(128*set)+256);                                              // Choose The Font Set (0 or 1)
223                         glCallLists(end-start,GL_BYTE,&string[start]);                  // Write The Text To The Screen
224                         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
225                 glPopMatrix();                                                                          // Restore The Old Projection Matrix
226                 glMatrixMode(GL_MODELVIEW);                                                     // Select The Modelview Matrix
227         glPopMatrix();                                                                          // Restore The Old Projection Matrix
228         glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
229         glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
230 }
231
232 void Text::glPrintOutlined(float x, float y, char *string, int set, float size, float width, float height)      // Where The Printing Happens
233 {
234         glColor4f(0,0,0,1);
235         glPrintOutline( x-2*size,  y-2*size, string,  set,  size*2.5/2,  width,  height);
236         glColor4f(1,1,1,1);
237         glPrint( x,  y, string,  set,  size,  width,  height);
238 }
239
240 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
241 {
242         glColor4f(0,0,0,1);
243         glPrintOutline( x-2*size,  y-2*size, string,  set,  size*2.5/2,  width,  height);
244         glColor4f(r,g,b,1);
245         glPrint( x,  y, string,  set,  size,  width,  height);
246 }
247
248 Text::Text()
249 {
250         base = 0;
251         FontTexture = 0;
252 }
253 Text::~Text()
254 {
255         if (base)
256         {
257                 glDeleteLists(base, 512);
258                 base = 0;
259         }
260         if (FontTexture) glDeleteTextures( 1, &FontTexture );
261 }
262