2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
21 #include "Graphic/Text.hpp"
25 void Text::LoadFontTexture(const std::string& fileName)
29 LOG(std::string("Loading font texture...") + fileName);
31 FontTexture.load(fileName, false);
33 glDeleteLists(base, 512);
38 void Text::BuildFont() // Build Our Font Display List
40 float cx; // Holds Our X Character Coord
41 float cy; // Holds Our Y Character Coord
47 glDeleteLists(base, 512);
51 base = glGenLists(512); // Creating 256 Display Lists
53 for (loop = 0; loop < 512; loop++) { // Loop Through All 256 Lists
55 cx = float(loop % 16) / 16.0f; // X Position Of Current Character
56 cy = float(loop / 16) / 16.0f; // Y Position Of Current Character
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
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)
73 glTranslated(10, 0, 0); // Move To The Right Of The Character
75 glTranslated(8, 0, 0); // Move To The Right Of The Character
77 glEndList(); // Done Building The Display List
78 } // Loop Until All 256 Are Built
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
86 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
88 glDisable(GL_DEPTH_TEST);
89 glDisable(GL_LIGHTING);
91 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92 glMatrixMode(GL_PROJECTION);
95 glOrtho(0, width, 0, height, -100, 100);
96 glMatrixMode(GL_MODELVIEW);
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);
105 glMatrixMode(GL_MODELVIEW);
107 glEnable(GL_DEPTH_TEST);
108 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
111 void Text::glPrint(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
116 _glPrint(x, y, string, set, size, width, height, start, end, 0);
119 void Text::glPrintOutline(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
124 _glPrint(x, y, string, set, size, width, height, start, end, 256);
127 void Text::glPrintOutlined(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end)
129 glPrintOutlined(1, 1, 1, 1, x, y, string, set, size, width, height, start, end);
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)
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);
148 glDeleteLists(base, 512);