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"
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
);
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
);
55 * Set the color for subsequent print calls.
57 void SetCurrentColor(const CColor
& color
);
60 * Set the font for subsequent print calls.
62 void SetCurrentFont(CStrIntern font
);
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
, ...);
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
, ...);
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
);
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
);
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
);
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.
107 Renderer::Backend::IDeviceCommandContext
* deviceCommandContext
,
108 Renderer::Backend::IShaderProgram
* shader
,
109 const CVector2D
& transformScale
, const CVector2D
& translation
);
112 friend struct SBatchCompare
;
115 * A string (optionally owned by this object, or else pointing to an
116 * externally-owned string) with a position.
121 SBatchRun
& operator=(const SBatchRun
&);
124 : text(NULL
), owned(false)
128 SBatchRun(const SBatchRun
& str
)
129 : x(str
.x
), y(str
.y
), owned(str
.owned
)
132 text
= new std::wstring(*str
.text
);
145 const std::wstring
* text
;
150 * A list of SBatchRuns, with a single font/color/transform,
151 * to be rendered in a single GL call.
155 size_t chars
; // sum of runs[i].text->size()
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
;
168 CStrIntern m_FontName
;
169 std::shared_ptr
<CFont
> m_Font
;
173 std::list
<SBatch
> m_Batches
;
176 #endif // INCLUDED_TEXTRENDERER