Pyrogenesis  trunk
TextRenderer.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 
22 #include "maths/Matrix3D.h"
23 #include "ps/CStrIntern.h"
24 #include "ps/Shapes.h"
25 
26 class CFont;
27 
29 {
30 public:
31  CTextRenderer(const CShaderProgramPtr& shader);
32 
33  /**
34  * Reset the text transform to the default, with (0,0) in the top-left of the screen.
35  */
36  void ResetTransform();
37 
39 
40  void SetTransform(const CMatrix3D& transform);
41 
42  void Translate(float x, float y, float z);
43 
44  /**
45  * Set clipping rectangle, in pre-transform coordinates (i.e. text is clipped against
46  * this rect based purely on the x,y values passed into Put()). Text fully outside the
47  * clipping rectangle may not be rendered. Should be used in conjunction with glScissor
48  * for precise clipping - this is just an optimisation.
49  */
50  void SetClippingRect(const CRect& rect);
51 
52  /**
53  * Set the color for subsequent print calls.
54  */
55  void Color(const CColor& color);
56 
57  /**
58  * Set the color for subsequent print calls.
59  */
60  void Color(float r, float g, float b, float a = 1.0);
61 
62  /**
63  * Set the font for subsequent print calls.
64  */
65  void Font(CStrIntern font);
66 
67  /**
68  * Print formatted text at (0,0) under the current transform,
69  * and advance the transform by the width of the text.
70  */
71  void PrintfAdvance(const wchar_t* fmt, ...);
72 
73  /**
74  * Print formatted text at (x,y) under the current transform.
75  * Does not alter the current transform.
76  */
77  void PrintfAt(float x, float y, const wchar_t* fmt, ...);
78 
79  /**
80  * Print text at (0,0) under the current transform,
81  * and advance the transform by the width of the text.
82  */
83  void PutAdvance(const wchar_t* buf);
84 
85  /**
86  * Print text at (x,y) under the current transform.
87  * Does not alter the current transform.
88  */
89  void Put(float x, float y, const wchar_t* buf);
90 
91  /**
92  * Print text at (x,y) under the current transform.
93  * Does not alter the current transform.
94  * @p buf must be a UTF-8 string.
95  */
96  void Put(float x, float y, const char* buf);
97 
98  /**
99  * Print text at (x,y) under the current transform.
100  * Does not alter the current transform.
101  * @p buf must remain valid until Render() is called.
102  * (This should be used to minimise memory copies when possible.)
103  */
104  void Put(float x, float y, const std::wstring* buf);
105 
106  /**
107  * Render all of the previously printed text calls.
108  */
109  void Render();
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:
124  : text(NULL), owned(false)
125  {
126  }
127 
128  SBatchRun(const SBatchRun& str)
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  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 
165 
168 
171  shared_ptr<CFont> m_Font;
172 
173  bool m_Dirty;
174 
175  std::list<SBatch> m_Batches;
176 };
177 
178 #endif // INCLUDED_TEXTRENDERER
shared_ptr< CFont > m_Font
Definition: TextRenderer.h:171
void PrintfAt(float x, float y, const wchar_t *fmt,...)
Print formatted text at (x,y) under the current transform.
Definition: TextRenderer.cpp:117
void Translate(float x, float y, float z)
Definition: TextRenderer.cpp:64
void Font(CStrIntern font)
Set the font for subsequent print calls.
Definition: TextRenderer.cpp:91
void PutString(float x, float y, const std::wstring *buf, bool owned)
Definition: TextRenderer.cpp:165
void Render()
Render all of the previously printed text calls.
Definition: TextRenderer.cpp:223
CMatrix3D GetTransform()
Definition: TextRenderer.cpp:53
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:132
CMatrix3D transform
Definition: TextRenderer.h:156
Definition: Shapes.h:36
A list of SBatchRuns, with a single font/color/transform, to be rendered in a single GL call...
Definition: TextRenderer.h:153
bool m_Dirty
Definition: TextRenderer.h:173
CTextRenderer(const CShaderProgramPtr &shader)
Definition: TextRenderer.cpp:30
std::list< SBatchRun > runs
Definition: TextRenderer.h:159
CColor m_Color
Definition: TextRenderer.h:169
Definition: TextRenderer.h:28
CMatrix3D m_Transform
Definition: TextRenderer.h:166
float x
Definition: TextRenderer.h:143
Definition: Matrix3D.h:33
shared_ptr< CFont > font
Definition: TextRenderer.h:158
CShaderProgramPtr m_Shader
Definition: TextRenderer.h:164
CColor color
Definition: TextRenderer.h:157
SBatchRun(const SBatchRun &str)
Definition: TextRenderer.h:128
float y
Definition: TextRenderer.h:143
const std::wstring * text
Definition: TextRenderer.h:145
void Put(float x, float y, const wchar_t *buf)
Print text at (x,y) under the current transform.
Definition: TextRenderer.cpp:141
void Color(const CColor &color)
Set the color for subsequent print calls.
Definition: TextRenderer.cpp:77
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:101
Interned 8-bit strings.
Definition: CStrIntern.h:37
void ResetTransform()
Reset the text transform to the default, with (0,0) in the top-left of the screen.
Definition: TextRenderer.cpp:38
size_t chars
Definition: TextRenderer.h:155
CRect m_Clipping
Definition: TextRenderer.h:167
Storage for a bitmap font.
Definition: Font.h:32
void SetTransform(const CMatrix3D &transform)
Definition: TextRenderer.cpp:58
Definition: TextRenderer.cpp:210
SBatchRun()
Definition: TextRenderer.h:123
bool owned
Definition: TextRenderer.h:146
SBatchRun & operator=(const SBatchRun &)
std::list< SBatch > m_Batches
Definition: TextRenderer.h:175
A string (optionally owned by this object, or else pointing to an externally-owned string) with a pos...
Definition: TextRenderer.h:118
Definition: format.h:119
void SetClippingRect(const CRect &rect)
Set clipping rectangle, in pre-transform coordinates (i.e.
Definition: TextRenderer.cpp:72
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
CStrIntern m_FontName
Definition: TextRenderer.h:170
~SBatchRun()
Definition: TextRenderer.h:137
Rectangle class used for screen rectangles.
Definition: Shapes.h:73