Pyrogenesis trunk
TextRenderer.h
Go to the documentation of this file.
1/* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_TEXTRENDERER
19#define INCLUDED_TEXTRENDERER
20
21#include "graphics/Color.h"
23#include "maths/Rect.h"
24#include "maths/Vector2D.h"
25#include "ps/CStrIntern.h"
27
28#include <list>
29
30class CFont;
31class CMatrix3D;
32
34{
35public:
37
38 /**
39 * Reset the text transform to the default, with (0,0) in the top-left corner.
40 */
41 void ResetTranslate(const CVector2D& translate = CVector2D{});
42
43 const CVector2D& GetTranslate() const { return m_Translate; }
44 void Translate(float x, float y);
45
46 /**
47 * Set clipping rectangle, in pre-transform coordinates (i.e. text is clipped against
48 * this rect based purely on the x,y values passed into Put()). Text fully outside the
49 * clipping rectangle may not be rendered. Should be used in conjunction with SetScissors
50 * for precise clipping - this is just an optimisation.
51 */
52 void SetClippingRect(const CRect& rect);
53
54 /**
55 * Set the color for subsequent print calls.
56 */
57 void SetCurrentColor(const CColor& color);
58
59 /**
60 * Set the font for subsequent print calls.
61 */
62 void SetCurrentFont(CStrIntern font);
63
64 /**
65 * Print formatted text at (0,0) under the current transform,
66 * and advance the transform by the width of the text.
67 */
68 void PrintfAdvance(const wchar_t* fmt, ...);
69
70 /**
71 * Print formatted text at (x,y) under the current transform.
72 * Does not alter the current transform.
73 */
74 void PrintfAt(float x, float y, const wchar_t* fmt, ...);
75
76 /**
77 * Print text at (0,0) under the current transform,
78 * and advance the transform by the width of the text.
79 */
80 void PutAdvance(const wchar_t* buf);
81
82 /**
83 * Print text at (x,y) under the current transform.
84 * Does not alter the current transform.
85 */
86 void Put(float x, float y, const wchar_t* buf);
87
88 /**
89 * Print text at (x,y) under the current transform.
90 * Does not alter the current transform.
91 * @p buf must be a UTF-8 string.
92 */
93 void Put(float x, float y, const char* buf);
94
95 /**
96 * Print text at (x,y) under the current transform.
97 * Does not alter the current transform.
98 * @p buf must remain valid until Render() is called.
99 * (This should be used to minimise memory copies when possible.)
100 */
101 void Put(float x, float y, const std::wstring* buf);
102
103 /**
104 * Render all of the previously printed text calls.
105 */
106 void Render(
107 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
109 const CVector2D& transformScale, const CVector2D& translation);
110
111private:
112 friend struct SBatchCompare;
113
114 /**
115 * A string (optionally owned by this object, or else pointing to an
116 * externally-owned string) with a position.
117 */
119 {
120 private:
122 public:
124 : text(NULL), owned(false)
125 {
126 }
127
129 : x(str.x), y(str.y), owned(str.owned)
130 {
131 if (owned)
132 text = new std::wstring(*str.text);
133 else
134 text = str.text;
135 }
136
138 {
139 if (owned)
140 delete text;
141 }
142
143 float x, y;
144
145 const std::wstring* text;
146 bool owned;
147 };
148
149 /**
150 * A list of SBatchRuns, with a single font/color/transform,
151 * to be rendered in a single GL call.
152 */
153 struct SBatch
154 {
155 size_t chars; // sum of runs[i].text->size()
158 std::shared_ptr<CFont> font;
159 std::list<SBatchRun> runs;
160 };
161
162 void PutString(float x, float y, const std::wstring* buf, bool owned);
163
166
169 std::shared_ptr<CFont> m_Font;
170
171 bool m_Dirty = true;
172
173 std::list<SBatch> m_Batches;
174};
175
176#endif // INCLUDED_TEXTRENDERER
Storage for a bitmap font.
Definition: Font.h:27
Definition: Matrix3D.h:34
Rectangle class used for screen rectangles.
Definition: Rect.h:31
Interned 8-bit strings.
Definition: CStrIntern.h:38
Definition: TextRenderer.h:34
void PrintfAdvance(const wchar_t *fmt,...)
Print formatted text at (0,0) under the current transform, and advance the transform by the width of ...
Definition: TextRenderer.cpp:84
void Render(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IShaderProgram *shader, const CVector2D &transformScale, const CVector2D &translation)
Render all of the previously printed text calls.
Definition: TextRenderer.cpp:196
void SetClippingRect(const CRect &rect)
Set clipping rectangle, in pre-transform coordinates (i.e.
Definition: TextRenderer.cpp:60
CVector2D m_Translate
Definition: TextRenderer.h:164
CRect m_Clipping
Definition: TextRenderer.h:165
void PutAdvance(const wchar_t *buf)
Print text at (0,0) under the current transform, and advance the transform by the width of the text.
Definition: TextRenderer.cpp:115
const CVector2D & GetTranslate() const
Definition: TextRenderer.h:43
void Put(float x, float y, const wchar_t *buf)
Print text at (x,y) under the current transform.
Definition: TextRenderer.cpp:124
void SetCurrentColor(const CColor &color)
Set the color for subsequent print calls.
Definition: TextRenderer.cpp:65
void SetCurrentFont(CStrIntern font)
Set the font for subsequent print calls.
Definition: TextRenderer.cpp:74
std::shared_ptr< CFont > m_Font
Definition: TextRenderer.h:169
void PrintfAt(float x, float y, const wchar_t *fmt,...)
Print formatted text at (x,y) under the current transform.
Definition: TextRenderer.cpp:100
CColor m_Color
Definition: TextRenderer.h:167
void PutString(float x, float y, const std::wstring *buf, bool owned)
Definition: TextRenderer.cpp:148
void Translate(float x, float y)
Definition: TextRenderer.cpp:54
void ResetTranslate(const CVector2D &translate=CVector2D{})
Reset the text transform to the default, with (0,0) in the top-left corner.
Definition: TextRenderer.cpp:48
CTextRenderer()
Definition: TextRenderer.cpp:41
std::list< SBatch > m_Batches
Definition: TextRenderer.h:173
bool m_Dirty
Definition: TextRenderer.h:171
CStrIntern m_FontName
Definition: TextRenderer.h:168
Definition: Vector2D.h:32
Definition: IDeviceCommandContext.h:42
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:81
Definition: Color.h:43
A string (optionally owned by this object, or else pointing to an externally-owned string) with a pos...
Definition: TextRenderer.h:119
~SBatchRun()
Definition: TextRenderer.h:137
float x
Definition: TextRenderer.h:143
bool owned
Definition: TextRenderer.h:146
const std::wstring * text
Definition: TextRenderer.h:145
SBatchRun()
Definition: TextRenderer.h:123
SBatchRun(const SBatchRun &str)
Definition: TextRenderer.h:128
float y
Definition: TextRenderer.h:143
SBatchRun & operator=(const SBatchRun &)
A list of SBatchRuns, with a single font/color/transform, to be rendered in a single GL call.
Definition: TextRenderer.h:154
CVector2D translate
Definition: TextRenderer.h:156
std::list< SBatchRun > runs
Definition: TextRenderer.h:159
CColor color
Definition: TextRenderer.h:157
size_t chars
Definition: TextRenderer.h:155
std::shared_ptr< CFont > font
Definition: TextRenderer.h:158
Definition: TextRenderer.cpp:186