Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TextRenderer.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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"
22#include "maths/Rect.h"
23#include "maths/Vector2D.h"
24#include "ps/CStrIntern.h"
27
28#include <cstddef>
29#include <list>
30#include <memory>
31#include <string>
32
33class CFont;
34namespace Renderer::Backend { class IDeviceCommandContext; }
35namespace Renderer::Backend { class IShaderProgram; }
36
38{
39public:
41
42 /**
43 * Reset the text transform to the default, with (0,0) in the top-left corner.
44 */
45 void ResetTranslate(const CVector2D& translate = CVector2D{});
46
47 const CVector2D& GetTranslate() const { return m_Translate; }
48 void Translate(float x, float y);
49
50 /**
51 * Set clipping rectangle, in pre-transform coordinates (i.e. text is clipped against
52 * this rect based purely on the x,y values passed into Put()). Text fully outside the
53 * clipping rectangle may not be rendered. Should be used in conjunction with SetScissors
54 * for precise clipping - this is just an optimisation.
55 */
56 void SetClippingRect(const CRect& rect);
57
58 /**
59 * Set the color for subsequent print calls.
60 */
61 void SetCurrentColor(const CColor& color);
62
63 /**
64 * Set the font for subsequent print calls.
65 */
66 void SetCurrentFont(CStrIntern font, CStrIntern locale);
67
68 /**
69 * Print formatted text at (0,0) under the current transform,
70 * and advance the transform by the width of the text.
71 */
72 void PrintfAdvance(const wchar_t* fmt, ...);
73
74 /**
75 * Print formatted text at (x,y) under the current transform.
76 * Does not alter the current transform.
77 */
78 void PrintfAt(float x, float y, const wchar_t* fmt, ...);
79
80 /**
81 * Print text at (0,0) under the current transform,
82 * and advance the transform by the width of the text.
83 */
84 void PutAdvance(const wchar_t* buf);
85
86 /**
87 * Print text at (x,y) under the current transform.
88 * Does not alter the current transform.
89 */
90 void Put(float x, float y, const wchar_t* buf);
91
92 /**
93 * Print text at (x,y) under the current transform.
94 * Does not alter the current transform.
95 * @p buf must be a UTF-8 string.
96 */
97 void Put(float x, float y, const char* buf);
98
99 /**
100 * Print text at (x,y) under the current transform.
101 * Does not alter the current transform.
102 * @p buf must remain valid until Render() is called.
103 * (This should be used to minimise memory copies when possible.)
104 */
105 void Put(float x, float y, const std::wstring* buf);
106
107 /**
108 * Render all of the previously printed text calls.
109 */
110 void Render(
111 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
113 const CVector2D& transformScale, const CVector2D& translation,
114 const bool debugFontBox, const CColor& debugBoxColor);
115
116private:
117 friend struct SBatchCompare;
118
119 /**
120 * A string (optionally owned by this object, or else pointing to an
121 * externally-owned string) with a position.
122 */
124 {
125 private:
127 public:
129 : text(NULL), owned(false)
130 {
131 }
132
134 : x(str.x), y(str.y), owned(str.owned)
135 {
136 if (owned)
137 text = new std::wstring(*str.text);
138 else
139 text = str.text;
140 }
141
143 {
144 if (owned)
145 delete text;
146 }
147
148 float x, y;
149
150 const std::wstring* text;
151 bool owned;
152 };
153
154 /**
155 * A list of SBatchRuns, with a single font/color/transform,
156 * to be rendered in a single GL call.
157 */
158 struct SBatch
159 {
160 size_t chars; // sum of runs[i].text->size()
164 using SBatchRunList = std::list<SBatchRun, PS::Memory::AllocatorAdapter<SBatchRun, PS::Memory::ScopedLinearAllocator>>;
166
168 };
169
170 void PutString(float x, float y, const std::wstring* buf, bool owned);
171
173
176
180
181 bool m_Dirty = true;
182
183 using SBatchList = std::list<SBatch, PS::Memory::AllocatorAdapter<SBatch, PS::Memory::ScopedLinearAllocator>>;
185};
186
187#endif // INCLUDED_TEXTRENDERER
Storage for a bitmap font.
Definition: Font.h:50
Rectangle class used for screen rectangles.
Definition: Rect.h:31
Interned 8-bit strings.
Definition: CStrIntern.h:42
Definition: TextRenderer.h:38
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:97
void SetClippingRect(const CRect &rect)
Set clipping rectangle, in pre-transform coordinates (i.e.
Definition: TextRenderer.cpp:73
CVector2D m_Translate
Definition: TextRenderer.h:174
SBatchList m_Batches
Definition: TextRenderer.h:184
CRect m_Clipping
Definition: TextRenderer.h:175
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:128
const CVector2D & GetTranslate() const
Definition: TextRenderer.h:47
void Put(float x, float y, const wchar_t *buf)
Print text at (x,y) under the current transform.
Definition: TextRenderer.cpp:137
void SetCurrentColor(const CColor &color)
Set the color for subsequent print calls.
Definition: TextRenderer.cpp:78
void Render(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IShaderProgram *shader, const CVector2D &transformScale, const CVector2D &translation, const bool debugFontBox, const CColor &debugBoxColor)
Render all of the previously printed text calls.
Definition: TextRenderer.cpp:207
CFont * m_Font
Definition: TextRenderer.h:179
PS::Memory::ScopedLinearAllocator m_ScopedLinearAllocator
Definition: TextRenderer.h:172
void SetCurrentFont(CStrIntern font, CStrIntern locale)
Set the font for subsequent print calls.
Definition: TextRenderer.cpp:87
void PrintfAt(float x, float y, const wchar_t *fmt,...)
Print formatted text at (x,y) under the current transform.
Definition: TextRenderer.cpp:113
CColor m_Color
Definition: TextRenderer.h:177
void PutString(float x, float y, const std::wstring *buf, bool owned)
Definition: TextRenderer.cpp:161
void Translate(float x, float y)
Definition: TextRenderer.cpp:67
void ResetTranslate(const CVector2D &translate=CVector2D{})
Reset the text transform to the default, with (0,0) in the top-left corner.
Definition: TextRenderer.cpp:61
CTextRenderer()
Definition: TextRenderer.cpp:53
std::list< SBatch, PS::Memory::AllocatorAdapter< SBatch, PS::Memory::ScopedLinearAllocator > > SBatchList
Definition: TextRenderer.h:183
bool m_Dirty
Definition: TextRenderer.h:181
CStrIntern m_FontName
Definition: TextRenderer.h:178
Definition: Vector2D.h:32
Definition: LinearAllocator.h:132
Definition: IDeviceCommandContext.h:46
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:85
Definition: Canvas2D.h:30
Definition: Color.h:46
A string (optionally owned by this object, or else pointing to an externally-owned string) with a pos...
Definition: TextRenderer.h:124
~SBatchRun()
Definition: TextRenderer.h:142
float x
Definition: TextRenderer.h:148
bool owned
Definition: TextRenderer.h:151
const std::wstring * text
Definition: TextRenderer.h:150
SBatchRun()
Definition: TextRenderer.h:128
SBatchRun(const SBatchRun &str)
Definition: TextRenderer.h:133
float y
Definition: TextRenderer.h:148
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:159
std::list< SBatchRun, PS::Memory::AllocatorAdapter< SBatchRun, PS::Memory::ScopedLinearAllocator > > SBatchRunList
Definition: TextRenderer.h:164
CVector2D translate
Definition: TextRenderer.h:161
CColor color
Definition: TextRenderer.h:162
SBatchRunList runs
Definition: TextRenderer.h:165
SBatch(PS::Memory::ScopedLinearAllocator &allocator)
Definition: TextRenderer.h:167
size_t chars
Definition: TextRenderer.h:160
CFont * font
Definition: TextRenderer.h:163
Definition: TextRenderer.cpp:197