Debian Codegraph @

utils/fgpanel/FGTextLayer.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2000 David Megginson
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "ApplicationProperties.hxx"
5 #include "GL_utils.hxx"
6 #include "FGTextLayer.hxx"
7 using std::string;
8
9 SGPath FGTextLayer::The_Font_Path;
10
11 FGFontCache FGTextLayer::The_Font_Cache;
12
13 GLuint FGTextLayer::Text_Layer_Program_Object (0);
14 GLint FGTextLayer::Text_Layer_Position_Loc (0);
15 GLint FGTextLayer::Text_Layer_Tex_Coord_Loc (0);
16 GLint FGTextLayer::Text_Layer_MVP_Loc (0);
17 GLint FGTextLayer::Text_Layer_Sampler_Loc (0);
18 GLint FGTextLayer::Text_Layer_Color_Loc (0);
19
20 bool
21 FGTextLayer::Init () {
22 const char V_Text_Layer_Shader_Str[] =
23 #ifdef _RPI
24 "#version 100 \n"
25 "attribute vec4 a_position; \n"
26 "attribute vec2 a_tex_coord; \n"
27 "varying vec2 v_tex_coord; \n"
28 #else
29 "#version 130 \n"
30 "in vec4 a_position; \n"
31 "in vec2 a_tex_coord; \n"
32 "out vec2 v_tex_coord; \n"
33 #endif
34 "uniform mat4 u_mvp_matrix; \n"
35 "void main () { \n"
36 " gl_Position = u_mvp_matrix * a_position; \n"
37 " v_tex_coord = a_tex_coord; \n"
38 "} \n";
39
40 const char F_Text_Layer_Shader_Str[] =
41 #ifdef _RPI
42 "#version 100 \n"
43 "precision mediump float; \n"
44 "varying vec2 v_tex_coord; \n"
45 #else
46 "#version 130 \n"
47 "in vec2 v_tex_coord; \n"
48 #endif
49 "uniform sampler2D u_texture; \n"
50 "uniform vec4 u_color; \n"
51 "void main () { \n"
52 " gl_FragColor = vec4 (1, 1, 1, \n"
53 " texture2D (u_texture, v_tex_coord).a) * \n"
54 " u_color; \n"
55 "} \n";
56
57 Text_Layer_Program_Object = GL_utils::instance ().load_program (V_Text_Layer_Shader_Str,
58 F_Text_Layer_Shader_Str);
59 if (Text_Layer_Program_Object == 0) {
60 return false;
61 }
62
63 // Get the attribute locations
64 Text_Layer_Position_Loc = glGetAttribLocation (Text_Layer_Program_Object, "a_position");
65 Text_Layer_Tex_Coord_Loc = glGetAttribLocation (Text_Layer_Program_Object, "a_tex_coord");
66
67 // Get the uniform locations
68 Text_Layer_MVP_Loc = glGetUniformLocation (Text_Layer_Program_Object, "u_mvp_matrix");
69
70 // Get the sampler location
71 Text_Layer_Sampler_Loc = glGetUniformLocation (Text_Layer_Program_Object, "u_texture");
72
73 // Get the color location
74 Text_Layer_Color_Loc = glGetUniformLocation (Text_Layer_Program_Object, "u_color");
75
76 return true;
77 }
78
79 FGTextLayer::FGTextLayer (const int w, const int h) :
80 FGInstrumentLayer (w, h),
81 m_pointSize (0.0),
82 m_font_name () {
83 m_then.stamp ();
84 m_color[0] = m_color[1] = m_color[2] = 0.0;
85 m_color[3] = 1.0;
86 }
87
88 FGTextLayer::~FGTextLayer () {
89 for (chunk_list::iterator It = m_chunks.begin ();
90 It != m_chunks.end ();
91 ++It) {
92 delete *It;
93 }
94 }
95
96 void
97 FGTextLayer::draw () {
98 if (test ()) {
99 glUseProgram (Text_Layer_Program_Object);
100 glUniform4fv (Text_Layer_Color_Loc, 1, m_color);
101
102 transform ();
103
104 GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION);
105 GL_utils::instance ().glPushMatrix ();
106 GL_utils::instance ().glMultMatrixf
107 (*reinterpret_cast <GLfloat (*)[4][4]>
108 (GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_MODELVIEW)));
109 glUniformMatrix4fv (Text_Layer_MVP_Loc,
110 1,
111 GL_FALSE,
112 GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_PROJECTION));
113 GLuint Glyph_Texture;
114 if (!The_Font_Cache.Set_Font (m_font_name, m_pointSize, Glyph_Texture)) {
115 SG_LOG (SG_COCKPIT, SG_ALERT, "Missing font : " << m_font_name << " " << m_pointSize);
116 }
117
118 m_now.stamp ();
119 const long diff ((m_now - m_then).toUSecs ());
120
121 if (diff > 100000 || diff < 0 ) {
122 // ( diff < 0 ) is a sanity check and indicates our time stamp
123 // difference math probably overflowed. We can handle a max
124 // difference of 35.8 minutes since the returned value is in
125 // usec. So if the panel is left off longer than that we can
126 // over flow the math with it is turned back on. This (diff <
127 // 0) catches that situation, gets us out of trouble, and
128 // back on track.
129 recalc_value ();
130 m_then = m_now;
131 }
132
133 glActiveTexture (GL_TEXTURE0);
134 glBindTexture (GL_TEXTURE_2D, Glyph_Texture);
135 glUniform1i (Text_Layer_Sampler_Loc, 0);
136
137 int X (0);
138 int Y (0);
139 int Previous_X (0);
140 int Previous_Y (0);
141 int Left, Bottom, W, H;
142 double X1, Y1, X2, Y2;
143
144 for (string::iterator It = m_value.begin (); It != m_value.end (); ++It) {
145 if (The_Font_Cache.Get_Char (*It,
146 X, Y,
147 Left, Bottom,
148 W, H,
149 X1, Y1,
150 X2, Y2)) {
151 const GLfloat v_Vertices[] = {
152 float (Previous_X + Left), float (Previous_Y + Bottom), 0.0f, // Position 0 (bottom left corner)
153 float (X1), float (Y2), // TexCoord 0 (bottom left corner)
154 float (Previous_X + Left + W), float (Previous_Y + Bottom), 0.0f, // Position 1 (bottom right corner)
155 float (X2), float (Y2), // TexCoord 1 (bottom right corner)
156 float (Previous_X + Left + W), float (Previous_Y + Bottom + H), 0.0f, // Position 2 (top right corner)
157 float (X2), float (Y1), // TexCoord 2 (top right corner)
158 float (Previous_X + Left), float (Previous_Y + Bottom + H), 0.0f, // Position 3 (top left corner)
159 float (X1), float (Y1) }; // TexCoord 3 (top left corner)
160 glVertexAttribPointer (Text_Layer_Position_Loc,
161 3,
162 GL_FLOAT,
163 GL_FALSE,
164 5 * sizeof (GLfloat),
165 v_Vertices);
166 glVertexAttribPointer (Text_Layer_Tex_Coord_Loc,
167 2,
168 GL_FLOAT,
169 GL_FALSE,
170 5 * sizeof (GLfloat),
171 &v_Vertices[3]);
172
173 glEnableVertexAttribArray (Text_Layer_Position_Loc);
174 glEnableVertexAttribArray (Text_Layer_Tex_Coord_Loc);
175
176 const GLushort indices[] = {0, 1, 2, 0, 2, 3};
177 glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
178 Previous_X = X;
179 Previous_Y = Y;
180 }
181 }
182
183 GL_utils::instance ().glPopMatrix ();
184 GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW);
185 }
186 }
187
188 void
189 FGTextLayer::addChunk (FGTextLayer::Chunk * const chunk) {
190 m_chunks.push_back (chunk);
191 }
192
193 void
194 FGTextLayer::setColor (const float r,
195 const float g,
196 const float b) {
197 m_color[0] = r;
198 m_color[1] = g;
199 m_color[2] = b;
200 m_color[3] = 1.0;
201 }
202
203 void
204 FGTextLayer::setPointSize (const float size) {
205 m_pointSize = size;
206 }
207
208 void
209 FGTextLayer::setFontName (const string &name) {
210 if (The_Font_Path.isNull ()) {
211 char *Env_Path = ::getenv ("FG_FONTS");
212 if (Env_Path != NULL) {
213 The_Font_Path = SGPath::fromEnv (Env_Path);
214 } else {
215 The_Font_Path = ApplicationProperties::GetRootPath ("Fonts");
216 }
217 }
218 m_font_name = The_Font_Path.local8BitStr () + "/" + name + ".ttf";
219 }
220
221 void
222 FGTextLayer::recalc_value () const {
223 m_value = "";
224 for (chunk_list::const_iterator It = m_chunks.begin ();
225 It != m_chunks.end ();
226 ++It) {
227 m_value += (*It)->getValue ();
228 }
229 }
230
231 ////////////////////////////////////////////////////////////////////////
232 // Implementation of FGTextLayer::Chunk.
233 ////////////////////////////////////////////////////////////////////////
234
235 FGTextLayer::Chunk::Chunk (const string &text,
236 const string &fmt) :
237 m_type (FGTextLayer::TEXT),
238 m_text (text),
239 m_fmt (fmt),
240 m_mult (1.0),
241 m_offs (0.0),
242 m_trunc (false) {
243 if (m_fmt.empty ()) {
244 m_fmt = "%s";
245 }
246 }
247
248 FGTextLayer::Chunk::Chunk (const ChunkType type,
249 const SGPropertyNode *node,
250 const string &fmt,
251 const float mult,
252 const float offs,
253 const bool truncation) :
254 m_type (type),
255 m_node (node),
256 m_fmt (fmt),
257 m_mult (mult),
258 m_offs (offs),
259 m_trunc (truncation) {
260 if (m_fmt.empty ()) {
261 if (type == TEXT_VALUE) {
262 m_fmt = "%s";
263 } else {
264 m_fmt = "%.2f";
265 }
266 }
267 }
268
269 const char *
270 FGTextLayer::Chunk::getValue () const {
271 if (test ()) {
272 m_buf[0] = '\0';
273 switch (m_type) {
274 case TEXT:
275 sprintf (m_buf, m_fmt.c_str (), m_text.c_str ());
276 break;
277 case TEXT_VALUE:
278 sprintf (m_buf, m_fmt.c_str (), m_node->getStringValue ());
279 break;
280 case DOUBLE_VALUE:
281 double d (m_offs + m_node->getFloatValue() * m_mult);
282 if (m_trunc) {
283 d = (d < 0) ? -floor (-d) : floor (d);
284 }
285 sprintf (m_buf, m_fmt.c_str(), d);
286 break;
287 }
288 return m_buf;
289 } else {
290 return "";
291 }
292 }