forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.cpp
More file actions
296 lines (259 loc) · 6.38 KB
/
overlay.cpp
File metadata and controls
296 lines (259 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// overlay.cpp
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <cstring>
#include <iostream>
#include <Eigen/Core>
#include <celmath/geomutil.h>
#include <celttf/truetypefont.h>
#include <celutil/color.h>
#include <celutil/debug.h>
#include <celutil/utf8.h>
#include "overlay.h"
#include "rectangle.h"
#include "render.h"
#include "texture.h"
#include "vecgl.h"
using namespace std;
using namespace Eigen;
using namespace celmath;
Overlay::Overlay(Renderer& r) :
ostream(&sbuf),
renderer(r)
{
sbuf.setOverlay(this);
}
void Overlay::begin()
{
projection = Ortho2D(0.0f, (float)windowWidth, 0.0f, (float)windowHeight);
// ModelView is Identity
renderer.enableBlending();
renderer.setBlendingFactors(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
global.reset();
useTexture = false;
}
void Overlay::end()
{
}
void Overlay::setWindowSize(int w, int h)
{
windowWidth = w;
windowHeight = h;
}
void Overlay::setFont(const std::shared_ptr<TextureFont>& f)
{
if (f != font)
{
if (font != nullptr)
font->flush();
font = f;
fontChanged = true;
}
}
void Overlay::beginText()
{
savePos();
textBlock++;
if (font != nullptr)
{
font->bind();
font->setMVPMatrices(projection);
useTexture = true;
fontChanged = false;
}
}
void Overlay::endText()
{
if (textBlock > 0)
{
textBlock--;
restorePos();
}
font->unbind();
}
void Overlay::print(wchar_t c)
{
if (font != nullptr)
{
if (!useTexture || fontChanged)
{
font->bind();
font->setMVPMatrices(projection);
useTexture = true;
fontChanged = false;
}
switch (c)
{
case '\n':
if (textBlock > 0)
{
restorePos();
global.y -= 1.0f + font->getHeight();
savePos();
}
break;
default:
font->render(c, global.x + xoffset, global.y + yoffset);
xoffset += font->getAdvance(c);
break;
}
}
}
void Overlay::print(char c)
{
if (font != nullptr)
{
if (!useTexture || fontChanged)
{
font->bind();
font->setMVPMatrices(projection);
useTexture = true;
fontChanged = false;
}
switch (c)
{
case '\n':
if (textBlock > 0)
{
restorePos();
global.y -= 1.0f + font->getHeight();
savePos();
}
break;
default:
font->render(c, global.x + xoffset, global.y + yoffset);
xoffset += font->getAdvance(c);
break;
}
}
}
void Overlay::print(const char* s)
{
int length = strlen(s);
bool validChar = true;
int i = 0;
while (i < length && validChar)
{
wchar_t ch = 0;
validChar = UTF8Decode(s, i, length, ch);
i += UTF8EncodedSize(ch);
print(ch);
}
}
void Overlay::drawRectangle(const celestia::Rect& r)
{
if (useTexture && r.tex == nullptr)
useTexture = false;
renderer.drawRectangle(r, ShaderProperties::FisheyeOverrideModeDisabled, projection);
}
void Overlay::setColor(float r, float g, float b, float a)
{
if (font != nullptr)
font->flush();
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex, r, g, b, a);
}
void Overlay::setColor(const Color& c)
{
if (font != nullptr)
font->flush();
glVertexAttrib4f(CelestiaGLProgram::ColorAttributeIndex,
c.red(), c.green(), c.blue(), c.alpha());
}
void Overlay::moveBy(float dx, float dy)
{
global.x += dx;
global.y += dy;
}
void Overlay::savePos()
{
posStack.push_back(global);
}
void Overlay::restorePos()
{
if (!posStack.empty())
{
global = posStack.back();
posStack.pop_back();
}
xoffset = yoffset = 0.0f;
}
//
// OverlayStreamBuf implementation
//
OverlayStreamBuf::OverlayStreamBuf()
{
setbuf(nullptr, 0);
}
void OverlayStreamBuf::setOverlay(Overlay* o)
{
overlay = o;
}
int OverlayStreamBuf::overflow(int c)
{
if (overlay != nullptr)
{
switch (decodeState)
{
case UTF8DecodeStart:
if (c < 0x80)
{
// Just a normal 7-bit character
overlay->print((char) c);
}
else
{
unsigned int count;
if ((c & 0xe0) == 0xc0)
count = 2;
else if ((c & 0xf0) == 0xe0)
count = 3;
else if ((c & 0xf8) == 0xf0)
count = 4;
else if ((c & 0xfc) == 0xf8)
count = 5;
else if ((c & 0xfe) == 0xfc)
count = 6;
else
count = 1; // Invalid byte
if (count > 1)
{
unsigned int mask = (1 << (7 - count)) - 1;
decodeShift = (count - 1) * 6;
decodedChar = (c & mask) << decodeShift;
decodeState = UTF8DecodeMultibyte;
}
else
{
// If the character isn't valid multibyte sequence head,
// silently skip it by leaving the decoder state alone.
}
}
break;
case UTF8DecodeMultibyte:
if ((c & 0xc0) == 0x80)
{
// We have a valid non-head byte in the sequence
decodeShift -= 6;
decodedChar |= (c & 0x3f) << decodeShift;
if (decodeShift == 0)
{
overlay->print(decodedChar);
decodeState = UTF8DecodeStart;
}
}
else
{
// Bad byte in UTF-8 encoded sequence; we'll silently ignore
// it and reset the state of the UTF-8 decoder.
decodeState = UTF8DecodeStart;
}
break;
}
}
return c;
}