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