]> git.jsancho.org Git - lugaru.git/blob - Source/Text.cpp
License: Update GPLv2+ header to match current FSF recommendation
[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 modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 Lugaru 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.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**> HEADER FILES <**/
21 #include "Text.h"
22 #include "Game.h"
23 extern TGAImageRec texture;
24
25 void Text::LoadFontTexture(const char *fileName)
26 {
27     LOGFUNC;
28
29     LOG(std::string("Loading font texture...") + fileName);
30
31     FontTexture.load(fileName, false, false);
32     /*
33         //Load Image
34         //LoadTGA( fileName );
35         unsigned char fileNamep[256];
36         CopyCStringToPascal(fileName,fileNamep);
37         //Load Image
38         upload_image( fileNamep ,1);
39
40         //Is it valid?
41         if(1==1){
42             //Alpha channel?
43             if ( texture.bpp == 24 )
44                 type = GL_RGB;
45             else
46                 type = GL_RGBA;
47
48             glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
49
50             if(!FontTexture)glGenTextures( 1, &FontTexture );
51             glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
52
53             glBindTexture( GL_TEXTURE_2D, FontTexture);
54             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
55             glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
56
57             gluBuild2DMipmaps( GL_TEXTURE_2D, type, texture.sizeX, texture.sizeY, type, GL_UNSIGNED_BYTE, texture.data );
58         }
59     */
60     if (base) {
61         glDeleteLists(base, 512);
62         base = 0;
63     }
64 }
65
66 void Text::BuildFont() // Build Our Font Display List
67 {
68     float cx; // Holds Our X Character Coord
69     float cy; // Holds Our Y Character Coord
70     int loop;
71
72     LOGFUNC;
73
74     if (base) {
75         glDeleteLists(base, 512);
76         base = 0;
77         //LOG("Font already created...");
78         //return;
79     }
80
81 //base=glGenLists(256); // Creating 256 Display Lists
82     base = glGenLists(512); // Creating 256 Display Lists
83     FontTexture.bind();
84     for (loop = 0; loop < 512; loop++) { // Loop Through All 256 Lists
85         if (loop < 256) {
86             cx = float(loop % 16) / 16.0f; // X Position Of Current Character
87             cy = float(loop / 16) / 16.0f; // Y Position Of Current Character
88         } else {
89             cx = float((loop - 256) % 16) / 16.0f; // X Position Of Current Character
90             cy = float((loop - 256) / 16) / 16.0f; // Y Position Of Current Character
91         }
92         glNewList(base + loop, GL_COMPILE); // Start Building A List
93         glBegin(GL_QUADS); // Use A Quad For Each Character
94         glTexCoord2f(cx, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Left)
95         glVertex2i(0, 0); // Vertex Coord (Bottom Left)
96         glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Right)
97         glVertex2i(16, 0); // Vertex Coord (Bottom Right)
98         glTexCoord2f(cx + 0.0625f, 1 - cy - .001); // Texture Coord (Top Right)
99         glVertex2i(16, 16); // Vertex Coord (Top Right)
100         glTexCoord2f(cx, 1 - cy - +.001); // Texture Coord (Top Left)
101         glVertex2i(0, 16); // Vertex Coord (Top Left)
102         glEnd(); // Done Building Our Quad (Character)
103         if (loop < 256)
104             glTranslated(10, 0, 0); // Move To The Right Of The Character
105         else
106             glTranslated(8, 0, 0); // Move To The Right Of The Character
107         glEndList(); // Done Building The Display List
108     } // Loop Until All 256 Are Built
109 }
110
111 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
112 {
113     glPrint(x, y, string, set, size, width, height, 0, strlen(string));
114 }
115
116 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
117 {
118     if (set > 1) {
119         set = 1;
120     }
121     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
122     FontTexture.bind();
123     glDisable(GL_DEPTH_TEST);
124     glDisable(GL_LIGHTING);
125     glEnable(GL_BLEND);
126     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
127     glMatrixMode(GL_PROJECTION);
128     glPushMatrix();
129     glLoadIdentity();
130     glOrtho(0, width, 0, height, -100, 100);
131     glMatrixMode(GL_MODELVIEW);
132     glPushMatrix();
133     glLoadIdentity();
134     glTranslated(x, y, 0);
135     glScalef(size, size, 1);
136     glListBase(base - 32 + (128 * set) + offset); // Choose The Font Set (0 or 1)
137     glCallLists(end - start, GL_BYTE, &string[start]); // Write The Text To The Screen
138     glMatrixMode(GL_PROJECTION);
139     glPopMatrix();
140     glMatrixMode(GL_MODELVIEW);
141     glPopMatrix();
142     glEnable(GL_DEPTH_TEST);
143     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
144 }
145
146 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
147 {
148     _glPrint(x, y, string, set, size, width, height, start, end, 0);
149 }
150
151 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
152 {
153     glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
154 }
155
156 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
157 {
158     _glPrint(x, y, string, set, size, width, height, start, end, 256);
159 }
160 void Text::glPrintOutlined(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
161 {
162     glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
163 }
164
165 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
166 {
167     glColor4f(0, 0, 0, 1);
168     glPrintOutline( x - 2 * size,  y - 2 * size, string,  set,  size * 2.5 / 2,  width,  height);
169     glColor4f(r, g, b, 1);
170     glPrint( x,  y, string,  set,  size,  width,  height);
171 }
172
173 Text::Text()
174 {
175     base = 0;
176 }
177 Text::~Text()
178 {
179     if (base) {
180         glDeleteLists(base, 512);
181         base = 0;
182     }
183     FontTexture.destroy();
184 }
185