]> git.jsancho.org Git - lugaru.git/blob - Source/Graphic/Text.cpp
Added braces to all statements with clang-tidy and ran clang-format again
[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 #include "Graphic/Text.hpp"
22
23 #include "Game.hpp"
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         }
77         glEndList(); // Done Building The Display List
78     }                // Loop Until All 256 Are Built
79 }
80
81 void Text::_glPrint(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end, int offset) // Where The Printing Happens
82 {
83     if (set > 1) {
84         set = 1;
85     }
86     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
87     FontTexture.bind();
88     glDisable(GL_DEPTH_TEST);
89     glDisable(GL_LIGHTING);
90     glEnable(GL_BLEND);
91     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92     glMatrixMode(GL_PROJECTION);
93     glPushMatrix();
94     glLoadIdentity();
95     glOrtho(0, width, 0, height, -100, 100);
96     glMatrixMode(GL_MODELVIEW);
97     glPushMatrix();
98     glLoadIdentity();
99     glTranslated(x, y, 0);
100     glScalef(size, size, 1);
101     glListBase(base - 32 + (128 * set) + offset);      // Choose The Font Set (0 or 1)
102     glCallLists(end - start, GL_BYTE, &string[start]); // Write The Text To The Screen
103     glMatrixMode(GL_PROJECTION);
104     glPopMatrix();
105     glMatrixMode(GL_MODELVIEW);
106     glPopMatrix();
107     glEnable(GL_DEPTH_TEST);
108     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
109 }
110
111 void Text::glPrint(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
112 {
113     if (end < 0) {
114         end = string.size();
115     }
116     _glPrint(x, y, string, set, size, width, height, start, end, 0);
117 }
118
119 void Text::glPrintOutline(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
120 {
121     if (end < 0) {
122         end = string.size();
123     }
124     _glPrint(x, y, string, set, size, width, height, start, end, 256);
125 }
126
127 void Text::glPrintOutlined(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
128 {
129     glPrintOutlined(1, 1, 1, 1, x, y, string, set, size, width, height, start, end);
130 }
131
132 void Text::glPrintOutlined(float r, float g, float b, float a, float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
133 {
134     glColor4f(0, 0, 0, a);
135     glPrintOutline(x - 2 * size, y - 2 * size, string, set, size * 2.5 / 2, width, height, start, end);
136     glColor4f(r, g, b, a);
137     glPrint(x, y, string, set, size, width, height, start, end);
138 }
139
140 Text::Text()
141 {
142     base = 0;
143 }
144
145 Text::~Text()
146 {
147     if (base) {
148         glDeleteLists(base, 512);
149         base = 0;
150     }
151 }