Line data Source code
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"
22 : #include "graphics/ShaderProgram.h"
23 : #include "maths/Rect.h"
24 : #include "maths/Vector2D.h"
25 : #include "ps/CStrIntern.h"
26 : #include "renderer/backend/IDeviceCommandContext.h"
27 :
28 : #include <list>
29 :
30 : class CFont;
31 : class CMatrix3D;
32 :
33 0 : class CTextRenderer
34 : {
35 : public:
36 : CTextRenderer();
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 0 : 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,
108 : Renderer::Backend::IShaderProgram* shader,
109 : const CVector2D& transformScale, const CVector2D& translation);
110 :
111 : private:
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 : */
118 : struct SBatchRun
119 : {
120 : private:
121 : SBatchRun& operator=(const SBatchRun&);
122 : public:
123 0 : SBatchRun()
124 0 : : text(NULL), owned(false)
125 : {
126 0 : }
127 :
128 0 : SBatchRun(const SBatchRun& str)
129 0 : : x(str.x), y(str.y), owned(str.owned)
130 : {
131 0 : if (owned)
132 0 : text = new std::wstring(*str.text);
133 : else
134 0 : text = str.text;
135 0 : }
136 :
137 0 : ~SBatchRun()
138 0 : {
139 0 : if (owned)
140 0 : delete text;
141 0 : }
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 0 : struct SBatch
154 : {
155 : size_t chars; // sum of runs[i].text->size()
156 : CVector2D translate;
157 : CColor color;
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 :
164 : CVector2D m_Translate;
165 : CRect m_Clipping;
166 :
167 : CColor m_Color;
168 : CStrIntern m_FontName;
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
|