File: PathRenderer.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (263 lines) | stat: -rw-r--r-- 5,521 bytes parent folder | download | duplicates (2)
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
#include "graphics/paths/PathRenderer.h"

#include "graphics/paths/NanoVGRenderer.h"

namespace {

NVGcolor fsColorToNVG(color* c) {
	return nvgRGBA(c->red, c->green, c->blue, c->is_alphacolor ? c->alpha : 255);
}

}

namespace graphics {
namespace paths {
std::unique_ptr<PathRenderer> PathRenderer::s_instance;

bool PathRenderer::init() {
	s_instance = std::unique_ptr<PathRenderer>(new PathRenderer());

	return true;
}

void PathRenderer::shutdown() {
	s_instance = nullptr;
}

PathRenderer::PathRenderer() : m_inFrame(false) {
	m_context = createNanoVGContext();
}

PathRenderer::~PathRenderer() {
	if (m_context) {
		deleteNanoVGContext(m_context);
		m_context = nullptr;
	}
}

void PathRenderer::beginFrame() {
	Assertion(!m_inFrame,
			  "beginFrame() was called withoug ending previous frame! Check your path renderer calls or get a coder!");

	nvgBeginFrame(m_context, gr_screen.max_w, gr_screen.max_h, 1.0f);
	m_inFrame = true;
}

void PathRenderer::cancelFrame() {
	nvgCancelFrame(m_context);
	m_inFrame = false;
}

void PathRenderer::endFrame() {
	nvgEndFrame(m_context);

	m_inFrame = false;
}

void PathRenderer::scissor(float x, float y, float w, float h) {
	nvgScissor(m_context, x, y, w, h);
}

void PathRenderer::resetScissor() {
	nvgResetScissor(m_context);
}

void PathRenderer::resetTransform() {
	nvgResetTransform(m_context);
}

void PathRenderer::translate(float x, float y) {
	nvgTranslate(m_context, x, y);
}

void PathRenderer::rotate(float rad) {
	nvgRotate(m_context, rad);
}

void PathRenderer::skewX(float rad) {
	nvgSkewX(m_context, rad);
}

void PathRenderer::skewY(float rad) {
	nvgSkewY(m_context, rad);
}

void PathRenderer::scale(float x, float y) {
	nvgScale(m_context, x, y);
}

DrawPaint PathRenderer::createLinearGradient(float sx, float sy, float ex, float ey, color* icol, color* ocol) {
	DrawPaint p;

	p.nvg = nvgLinearGradient(m_context, sx, sy, ex, ey, fsColorToNVG(icol), fsColorToNVG(ocol));

	return p;
}

void PathRenderer::setAlpha(float alpha) {
	nvgGlobalAlpha(m_context, alpha);
}

void PathRenderer::setFillColor(color* color) {
	nvgFillColor(m_context, fsColorToNVG(color));
}

void PathRenderer::setFillPaint(const DrawPaint& paint) {
	nvgFillPaint(m_context, paint.nvg);
}

void PathRenderer::setStrokeColor(::color* color) {
	nvgStrokeColor(m_context, fsColorToNVG(color));
}

void PathRenderer::setStrokePaint(const DrawPaint& paint) {
	nvgStrokePaint(m_context, paint.nvg);
}

void PathRenderer::setStrokeWidth(float width) {
	nvgStrokeWidth(m_context, width);
}

void PathRenderer::beginPath() {
	nvgBeginPath(m_context);
}

void PathRenderer::moveTo(float x, float y) {
	nvgMoveTo(m_context, x, y);
}

void PathRenderer::setSolidity(Solidity solid) {
	int nvgSolid;
	switch (solid) {
	case SOLIDITY_HOLE:
		nvgSolid = NVG_HOLE;
		break;
	case SOLIDITY_SOLID:
		nvgSolid = NVG_SOLID;
		break;
	default:
		nvgSolid = NVG_SOLID;
		break;
	}

	nvgPathWinding(m_context, nvgSolid);
}

void PathRenderer::lineTo(float x, float y) {
	nvgLineTo(m_context, x, y);
}

void PathRenderer::rectangle(float x, float y, float w, float h) {
	nvgRect(m_context, x, y, w, h);
}

void PathRenderer::roundedRectangle(float x, float y, float w, float h, float radius) {
	nvgRoundedRect(m_context, x, y, w, h, radius);
}

void PathRenderer::circle(float x, float y, float r) {
	nvgCircle(m_context, x, y, r);
}

void PathRenderer::ellipse(float x, float y, float rx, float ry) {
	nvgEllipse(m_context, x, y, rx, ry);
}

void PathRenderer::arc(float cx, float cy, float r, float a0, float a1, Direction dir) {
	int nvgDir;
	switch (dir) {
	case DIR_CCW:
		nvgDir = NVG_CCW;
		break;
	case DIR_CW:
		nvgDir = NVG_CW;
		break;
	default:
		nvgDir = NVG_CW;
		break;
	}

	nvgArc(m_context, cx, cy, r, a0, a1, nvgDir);
}

void PathRenderer::closePath() {
	nvgClosePath(m_context);
}

int PathRenderer::createFontMem(const char* name, unsigned char* data, int ndata, int freeData) {
	return nvgCreateFontMem(m_context, name, data, ndata, freeData);
}

void PathRenderer::fontSize(float size) {
	nvgFontSize(m_context, size);
}

void PathRenderer::textLetterSpacing(float spacing) {
	nvgTextLetterSpacing(m_context, spacing);
}

void PathRenderer::fontFaceId(int font) {
	nvgFontFaceId(m_context, font);
}

float PathRenderer::text(float x, float y, const char* string, const char* end) {
	return nvgText(m_context, x, y, string, end);
}

float PathRenderer::textBounds(float x, float y, const char* string, const char* end, float* bounds) {
	return nvgTextBounds(m_context, x, y, string, end, bounds);
}

void PathRenderer::textMetrics(float* ascender, float* descender, float* lineh) {
	nvgTextMetrics(m_context, ascender, descender, lineh);
}

void PathRenderer::textAlign(TextAlign align) {
	int nvgAlign = 0;
	if (align & ALIGN_LEFT) {
		nvgAlign |= NVG_ALIGN_LEFT;
	}
	if (align & ALIGN_CENTER) {
		nvgAlign |= NVG_ALIGN_CENTER;
	}
	if (align & ALIGN_RIGHT) {
		nvgAlign |= NVG_ALIGN_RIGHT;
	}

	if (align & ALIGN_TOP) {
		nvgAlign |= NVG_ALIGN_TOP;
	}
	if (align & ALIGN_MIDDLE) {
		nvgAlign |= NVG_ALIGN_MIDDLE;
	}
	if (align & ALIGN_BOTTOM) {
		nvgAlign |= NVG_ALIGN_BOTTOM;
	}
	if (align & ALIGN_BASELINE) {
		nvgAlign |= NVG_ALIGN_BASELINE;
	}

	nvgTextAlign(m_context, nvgAlign);
}

void PathRenderer::fill() {
	nvgFill(m_context);
}

void PathRenderer::stroke() {
	nvgStroke(m_context);
}

void PathRenderer::saveState() {
	nvgSave(m_context);
}

void PathRenderer::resetState() {
	nvgReset(m_context);
}

void PathRenderer::restoreState() {
	nvgRestore(m_context);
}
}
}