]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Text.cpp
Sorted all source files in folders
[lugaru.git] / Source / Graphic / Text.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**> HEADER FILES <**/
22 #include "Graphic/Text.h"
23 #include "Game.h"
24
25 void Text::LoadFontTexture(const std::string& fileName)
26 {
27     LOGFUNC;
28
29     LOG(std::string("Loading font texture...") + fileName);
30
31     FontTexture.load(fileName, false);
32     if (base) {
33         glDeleteLists(base, 512);
34         base = 0;
35     }
36 }
37
38 void Text::BuildFont() // Build Our Font Display List
39 {
40     float cx; // Holds Our X Character Coord
41     float cy; // Holds Our Y Character Coord
42     int loop;
43
44     LOGFUNC;
45
46     if (base) {
47         glDeleteLists(base, 512);
48         base = 0;
49     }
50
51     base = glGenLists(512); // Creating 256 Display Lists
52     FontTexture.bind();
53     for (loop = 0; loop < 512; loop++) { // Loop Through All 256 Lists
54         if (loop < 256) {
55             cx = float(loop % 16) / 16.0f; // X Position Of Current Character
56             cy = float(loop / 16) / 16.0f; // Y Position Of Current Character
57         } else {
58             cx = float((loop - 256) % 16) / 16.0f; // X Position Of Current Character
59             cy = float((loop - 256) / 16) / 16.0f; // Y Position Of Current Character
60         }
61         glNewList(base + loop, GL_COMPILE); // Start Building A List
62         glBegin(GL_QUADS); // Use A Quad For Each Character
63         glTexCoord2f(cx, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Left)
64         glVertex2i(0, 0); // Vertex Coord (Bottom Left)
65         glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f + .001); // Texture Coord (Bottom Right)
66         glVertex2i(16, 0); // Vertex Coord (Bottom Right)
67         glTexCoord2f(cx + 0.0625f, 1 - cy - .001); // Texture Coord (Top Right)
68         glVertex2i(16, 16); // Vertex Coord (Top Right)
69         glTexCoord2f(cx, 1 - cy - +.001); // Texture Coord (Top Left)
70         glVertex2i(0, 16); // Vertex Coord (Top Left)
71         glEnd(); // Done Building Our Quad (Character)
72         if (loop < 256)
73             glTranslated(10, 0, 0); // Move To The Right Of The Character
74         else
75             glTranslated(8, 0, 0); // Move To The Right Of The Character
76         glEndList(); // Done Building The Display List
77     } // Loop Until All 256 Are Built
78 }
79
80 void Text::glPrint(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
81 {
82     glPrint(x, y, string, set, size, width, height, 0, strlen(string));
83 }
84
85 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
86 {
87     if (set > 1) {
88         set = 1;
89     }
90     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
91     FontTexture.bind();
92     glDisable(GL_DEPTH_TEST);
93     glDisable(GL_LIGHTING);
94     glEnable(GL_BLEND);
95     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96     glMatrixMode(GL_PROJECTION);
97     glPushMatrix();
98     glLoadIdentity();
99     glOrtho(0, width, 0, height, -100, 100);
100     glMatrixMode(GL_MODELVIEW);
101     glPushMatrix();
102     glLoadIdentity();
103     glTranslated(x, y, 0);
104     glScalef(size, size, 1);
105     glListBase(base - 32 + (128 * set) + offset); // Choose The Font Set (0 or 1)
106     glCallLists(end - start, GL_BYTE, &string[start]); // Write The Text To The Screen
107     glMatrixMode(GL_PROJECTION);
108     glPopMatrix();
109     glMatrixMode(GL_MODELVIEW);
110     glPopMatrix();
111     glEnable(GL_DEPTH_TEST);
112     glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
113 }
114
115 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
116 {
117     _glPrint(x, y, string, set, size, width, height, start, end, 0);
118 }
119
120 void Text::glPrintOutline(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
121 {
122     glPrintOutline(x, y, string, set, size, width, height, 0, strlen(string));
123 }
124
125 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
126 {
127     _glPrint(x, y, string, set, size, width, height, start, end, 256);
128 }
129 void Text::glPrintOutlined(float x, float y, const char *string, int set, float size, float width, float height) // Where The Printing Happens
130 {
131     glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
132 }
133
134 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
135 {
136     glColor4f(0, 0, 0, 1);
137     glPrintOutline( x - 2 * size,  y - 2 * size, string,  set,  size * 2.5 / 2,  width,  height);
138     glColor4f(r, g, b, 1);
139     glPrint( x,  y, string,  set,  size,  width,  height);
140 }
141
142 Text::Text()
143 {
144     base = 0;
145 }
146 Text::~Text()
147 {
148     if (base) {
149         glDeleteLists(base, 512);
150         base = 0;
151     }
152     FontTexture.destroy();
153 }
154