]> git.jsancho.org Git - lugaru.git/blob - Source/Text.cpp
First step at cleaning image loading.
[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
24 void Text::LoadFontTexture(const char *fileName)
25 {
26     LOGFUNC;
27
28     LOG(std::string("Loading font texture...") + fileName);
29
30     FontTexture.load(fileName, false, false);
31     if (base) {
32         glDeleteLists(base, 512);
33         base = 0;
34     }
35 }
36
37 void Text::BuildFont() // Build Our Font Display List
38 {
39     float cx; // Holds Our X Character Coord
40     float cy; // Holds Our Y Character Coord
41     int loop;
42
43     LOGFUNC;
44
45     if (base) {
46         glDeleteLists(base, 512);
47         base = 0;
48     }
49
50     base = glGenLists(512); // Creating 256 Display Lists
51     FontTexture.bind();
52     for (loop = 0; loop < 512; loop++) { // Loop Through All 256 Lists
53         if (loop < 256) {
54             cx = float(loop % 16) / 16.0f; // X Position Of Current Character
55             cy = float(loop / 16) / 16.0f; // Y Position Of Current Character
56         } else {
57             cx = float((loop - 256) % 16) / 16.0f; // X Position Of Current Character
58             cy = float((loop - 256) / 16) / 16.0f; // Y Position Of Current Character
59         }
60         glNewList(base + loop, GL_COMPILE); // Start Building A List
61         glBegin(GL_QUADS); // Use A Quad For Each Character
62         glTexCoord2f(cx, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Left)
63         glVertex2i(0, 0); // Vertex Coord (Bottom Left)
64         glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Right)
65         glVertex2i(16, 0); // Vertex Coord (Bottom Right)
66         glTexCoord2f(cx + 0.0625f, 1 - cy - .001); // Texture Coord (Top Right)
67         glVertex2i(16, 16); // Vertex Coord (Top Right)
68         glTexCoord2f(cx, 1 - cy - +.001); // Texture Coord (Top Left)
69         glVertex2i(0, 16); // Vertex Coord (Top Left)
70         glEnd(); // Done Building Our Quad (Character)
71         if (loop < 256)
72             glTranslated(10, 0, 0); // Move To The Right Of The Character
73         else
74             glTranslated(8, 0, 0); // Move To The Right Of The Character
75         glEndList(); // Done Building The Display List
76     } // Loop Until All 256 Are Built
77 }
78
79 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
80 {
81     glPrint(x, y, string, set, size, width, height, 0, strlen(string));
82 }
83
84 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
85 {
86     if (set > 1) {
87         set = 1;
88     }
89     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
90     FontTexture.bind();
91     glDisable(GL_DEPTH_TEST);
92     glDisable(GL_LIGHTING);
93     glEnable(GL_BLEND);
94     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95     glMatrixMode(GL_PROJECTION);
96     glPushMatrix();
97     glLoadIdentity();
98     glOrtho(0, width, 0, height, -100, 100);
99     glMatrixMode(GL_MODELVIEW);
100     glPushMatrix();
101     glLoadIdentity();
102     glTranslated(x, y, 0);
103     glScalef(size, size, 1);
104     glListBase(base - 32 + (128 * set) + offset); // Choose The Font Set (0 or 1)
105     glCallLists(end - start, GL_BYTE, &string[start]); // Write The Text To The Screen
106     glMatrixMode(GL_PROJECTION);
107     glPopMatrix();
108     glMatrixMode(GL_MODELVIEW);
109     glPopMatrix();
110     glEnable(GL_DEPTH_TEST);
111     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
112 }
113
114 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
115 {
116     _glPrint(x, y, string, set, size, width, height, start, end, 0);
117 }
118
119 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
120 {
121     glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
122 }
123
124 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
125 {
126     _glPrint(x, y, string, set, size, width, height, start, end, 256);
127 }
128 void Text::glPrintOutlined(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
129 {
130     glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
131 }
132
133 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
134 {
135     glColor4f(0, 0, 0, 1);
136     glPrintOutline( x - 2 * size,  y - 2 * size, string,  set,  size * 2.5 / 2,  width,  height);
137     glColor4f(r, g, b, 1);
138     glPrint( x,  y, string,  set,  size,  width,  height);
139 }
140
141 Text::Text()
142 {
143     base = 0;
144 }
145 Text::~Text()
146 {
147     if (base) {
148         glDeleteLists(base, 512);
149         base = 0;
150     }
151     FontTexture.destroy();
152 }
153