[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / TextRenderer.h
blob0bf9d6488dba30cdba4f25d373dfd48d097f0ae7
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_TEXTRENDERER
19 #define INCLUDED_TEXTRENDERER
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"
28 #include <list>
30 class CFont;
31 class CMatrix3D;
33 class CTextRenderer
35 public:
36 CTextRenderer();
38 /**
39 * Reset the text transform to the default, with (0,0) in the top-left corner.
41 void ResetTranslate(const CVector2D& translate = CVector2D{});
43 const CVector2D& GetTranslate() const { return m_Translate; }
44 void Translate(float x, float y);
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.
52 void SetClippingRect(const CRect& rect);
54 /**
55 * Set the color for subsequent print calls.
57 void SetCurrentColor(const CColor& color);
59 /**
60 * Set the font for subsequent print calls.
62 void SetCurrentFont(CStrIntern font);
64 /**
65 * Print formatted text at (0,0) under the current transform,
66 * and advance the transform by the width of the text.
68 void PrintfAdvance(const wchar_t* fmt, ...);
70 /**
71 * Print formatted text at (x,y) under the current transform.
72 * Does not alter the current transform.
74 void PrintfAt(float x, float y, const wchar_t* fmt, ...);
76 /**
77 * Print text at (0,0) under the current transform,
78 * and advance the transform by the width of the text.
80 void PutAdvance(const wchar_t* buf);
82 /**
83 * Print text at (x,y) under the current transform.
84 * Does not alter the current transform.
86 void Put(float x, float y, const wchar_t* buf);
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.
93 void Put(float x, float y, const char* buf);
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.)
101 void Put(float x, float y, const std::wstring* buf);
104 * Render all of the previously printed text calls.
106 void Render(
107 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
108 Renderer::Backend::IShaderProgram* shader,
109 const CVector2D& transformScale, const CVector2D& translation);
111 private:
112 friend struct SBatchCompare;
115 * A string (optionally owned by this object, or else pointing to an
116 * externally-owned string) with a position.
118 struct SBatchRun
120 private:
121 SBatchRun& operator=(const SBatchRun&);
122 public:
123 SBatchRun()
124 : text(NULL), owned(false)
128 SBatchRun(const SBatchRun& str)
129 : x(str.x), y(str.y), owned(str.owned)
131 if (owned)
132 text = new std::wstring(*str.text);
133 else
134 text = str.text;
137 ~SBatchRun()
139 if (owned)
140 delete text;
143 float x, y;
145 const std::wstring* text;
146 bool owned;
150 * A list of SBatchRuns, with a single font/color/transform,
151 * to be rendered in a single GL call.
153 struct SBatch
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;
162 void PutString(float x, float y, const std::wstring* buf, bool owned);
164 CVector2D m_Translate;
165 CRect m_Clipping;
167 CColor m_Color;
168 CStrIntern m_FontName;
169 std::shared_ptr<CFont> m_Font;
171 bool m_Dirty = true;
173 std::list<SBatch> m_Batches;
176 #endif // INCLUDED_TEXTRENDERER