File: TextRenderer.cpp

package info (click to toggle)
0ad 0~r11863-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 30,560 kB
  • sloc: cpp: 201,230; ansic: 28,387; sh: 10,593; perl: 4,847; python: 2,240; makefile: 658; java: 412; xml: 243; sql: 40
file content (228 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright (C) 2012 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "TextRenderer.h"

#include "lib/ogl.h"
#include "lib/res/graphics/unifont.h"
#include "ps/Font.h"

extern int g_xres, g_yres;

CTextRenderer::CTextRenderer(const CShaderProgramPtr& shader) :
	m_Shader(shader)
{
	ResetTransform();
	Color(CColor(1.0f, 1.0f, 1.0f, 1.0f));
	Font(L"sans-10");
}

void CTextRenderer::ResetTransform()
{
	m_Transform.SetIdentity();
	m_Transform.Scale(1.0f, -1.f, 1.0f);
	m_Transform.Translate(0.0f, (float)g_yres, -1000.0f);

	CMatrix3D proj;
	proj.SetOrtho(0.f, (float)g_xres, 0.f, (float)g_yres, -1.f, 1000.f);
	m_Transform = proj * m_Transform;
}

CMatrix3D CTextRenderer::GetTransform()
{
	return m_Transform;
}

void CTextRenderer::SetTransform(const CMatrix3D& transform)
{
	m_Transform = transform;
}

void CTextRenderer::Translate(float x, float y, float z)
{
	CMatrix3D m;
	m.SetTranslation(x, y, z);
	m_Transform = m_Transform * m;
}

void CTextRenderer::Color(const CColor& color)
{
	m_Color = color;
}

void CTextRenderer::Color(float r, float g, float b, float a)
{
	m_Color = CColor(r, g, b, a);
}

void CTextRenderer::Font(const CStrW& font)
{
	if (!m_Fonts[font])
		m_Fonts[font] = shared_ptr<CFont>(new CFont(font));

	m_Font = m_Fonts[font];
}

void CTextRenderer::PrintfAdvance(const wchar_t* fmt, ...)
{
	wchar_t buf[1024] = {0};

	va_list args;
	va_start(args, fmt);
	int ret = vswprintf(buf, ARRAY_SIZE(buf)-1, fmt, args);
	va_end(args);

	if (ret < 0)
		debug_printf(L"CTextRenderer::Printf vswprintf failed (buffer size exceeded?) - return value %d, errno %d\n", ret, errno);

	PutAdvance(buf);
}


void CTextRenderer::PrintfAt(float x, float y, const wchar_t* fmt, ...)
{
	wchar_t buf[1024] = {0};

	va_list args;
	va_start(args, fmt);
	int ret = vswprintf(buf, ARRAY_SIZE(buf)-1, fmt, args);
	va_end(args);

	if (ret < 0)
		debug_printf(L"CTextRenderer::PrintfAt vswprintf failed (buffer size exceeded?) - return value %d, errno %d\n", ret, errno);

	Put(x, y, buf);
}

void CTextRenderer::PutAdvance(const wchar_t* buf)
{
	Put(0.0f, 0.0f, buf);

	int w, h;
	m_Font->CalculateStringSize(buf, w, h);
	Translate((float)w, 0.0f, 0.0f);
}

void CTextRenderer::Put(float x, float y, const wchar_t* buf)
{
	if (buf[0] == 0)
		return; // empty string; don't bother storing

	CMatrix3D translate;
	translate.SetTranslation(x, y, 0.0f);

	SBatch batch;
	batch.transform = m_Transform * translate;
	batch.color = m_Color;
	batch.font = m_Font;
	batch.text = buf;
	m_Batches.push_back(batch);
}

struct t2f_v2i
{
	t2f_v2i() : u(0), v(0), x(0), y(0) { }
	float u, v;
	i16 x, y;
};

void CTextRenderer::Render()
{
	std::vector<u16> indexes;
	std::vector<t2f_v2i> vertexes;

	for (size_t i = 0; i < m_Batches.size(); ++i)
	{
		SBatch& batch = m_Batches[i];

		if (batch.text.empty()) // avoid zero-length arrays
			continue;

		const std::map<u16, UnifontGlyphData>& glyphs = batch.font->GetGlyphs();

		m_Shader->BindTexture("tex", batch.font->GetTexture());

		m_Shader->Uniform("transform", batch.transform);

		// ALPHA-only textures will have .rgb sampled as 0, so we need to
		// replace it with white (but not affect RGBA textures)
		if (batch.font->HasRGB())
			m_Shader->Uniform("colorAdd", CColor(0.0f, 0.0f, 0.0f, 0.0f));
		else
			m_Shader->Uniform("colorAdd", CColor(1.0f, 1.0f, 1.0f, 0.0f));

		m_Shader->Uniform("colorMul", batch.color);

		vertexes.clear();
		vertexes.resize(batch.text.size()*4);

		indexes.clear();
		indexes.resize(batch.text.size()*6);

		i16 x = 0;
		for (size_t i = 0; i < batch.text.size(); ++i)
		{
			std::map<u16, UnifontGlyphData>::const_iterator it = glyphs.find(batch.text[i]);

			if (it == glyphs.end())
				it = glyphs.find(0xFFFD); // Use the missing glyph symbol

			if (it == glyphs.end()) // Missing the missing glyph symbol - give up
				continue;

			const UnifontGlyphData& g = it->second;

			vertexes[i*4].u = g.u1;
			vertexes[i*4].v = g.v0;
			vertexes[i*4].x = g.x1 + x;
			vertexes[i*4].y = g.y0;

			vertexes[i*4+1].u = g.u0;
			vertexes[i*4+1].v = g.v0;
			vertexes[i*4+1].x = g.x0 + x;
			vertexes[i*4+1].y = g.y0;

			vertexes[i*4+2].u = g.u0;
			vertexes[i*4+2].v = g.v1;
			vertexes[i*4+2].x = g.x0 + x;
			vertexes[i*4+2].y = g.y1;

			vertexes[i*4+3].u = g.u1;
			vertexes[i*4+3].v = g.v1;
			vertexes[i*4+3].x = g.x1 + x;
			vertexes[i*4+3].y = g.y1;

			indexes[i*6+0] = i*4+0;
			indexes[i*6+1] = i*4+1;
			indexes[i*6+2] = i*4+2;
			indexes[i*6+3] = i*4+2;
			indexes[i*6+4] = i*4+3;
			indexes[i*6+5] = i*4+0;

			x += g.xadvance;
		}

		m_Shader->VertexPointer(2, GL_SHORT, sizeof(t2f_v2i), &vertexes[0].x);
		m_Shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, sizeof(t2f_v2i), &vertexes[0].u);

		glDrawElements(GL_TRIANGLES, indexes.size(), GL_UNSIGNED_SHORT, &indexes[0]);
	}

	m_Batches.clear();
}