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
76 glEndList(); // Done Building The Display List
77 } // Loop Until All 256 Are Built
80 void Text::glPrint(float x, float y, const std::string& string, int set, float size, float width, float height) // Where The Printing Happens
82 glPrint(x, y, string, set, size, width, height, 0, string.size());
85 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
90 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
92 glDisable(GL_DEPTH_TEST);
93 glDisable(GL_LIGHTING);
95 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96 glMatrixMode(GL_PROJECTION);
99 glOrtho(0, width, 0, height, -100, 100);
100 glMatrixMode(GL_MODELVIEW);
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);
109 glMatrixMode(GL_MODELVIEW);
111 glEnable(GL_DEPTH_TEST);
112 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
115 void Text::glPrint(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end) // Where The Printing Happens
117 _glPrint(x, y, string, set, size, width, height, start, end, 0);
120 void Text::glPrintOutline(float x, float y, const std::string& string, int set, float size, float width, float height) // Where The Printing Happens
122 glPrintOutline(x, y, string, set, size, width, height, 0, string.size());
125 void Text::glPrintOutline(float x, float y, const std::string& string, int set, float size, float width, float height, int start, int end) // Where The Printing Happens
127 _glPrint(x, y, string, set, size, width, height, start, end, 256);
129 void Text::glPrintOutlined(float x, float y, const std::string& string, int set, float size, float width, float height) // Where The Printing Happens
131 glPrintOutlined(1, 1, 1, x, y, string, set, size, width, height);
134 void Text::glPrintOutlined(float r, float g, float b, float x, float y, const std::string& string, int set, float size, float width, float height) // Where The Printing Happens
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);
149 glDeleteLists(base, 512);
152 FontTexture.destroy();