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