-
Notifications
You must be signed in to change notification settings - Fork 3
/
Drawing.lua
310 lines (240 loc) · 7.07 KB
/
Drawing.lua
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
local ffi = require "ffi"
local bit = require "bit"
local bor = bit.bor
local EGL = require "egl_utils"
local OpenVG = require "OpenVG"
local OpenVGUtils = require "OpenVG_Utils"
-- Color functions
--
--
-- RGB returns a solid color from a RGB triple
function RGB(r, g, b, color)
return RGBA(r, g, b, 1.0, color);
end
-- RGBA fills a color vectors from a RGBA quad.
function RGBA(r, g, b, a, color)
if (r > 255) then
r = 0;
end
if (g > 255) then
g = 0;
end
if (b > 255) then
b = 0;
end
if (a < 0.0 or a > 1.0) then
a = 1.0;
end
color[0] = r / 255.0;
color[1] = g / 255.0;
color[2] = b / 255.0;
color[3] = a;
return color
end
local Renderer = {}
local Renderer_mt = {
__index = Renderer,
}
function Renderer.new(display, width, height)
local self = {
Display = display;
Width = width;
Height = height;
TransformStack = {};
}
setmetatable(self, Renderer_mt);
--[[
self.SansTypeface = loadfont(DejaVuSans_glyphPoints,
DejaVuSans_glyphPointIndices,
DejaVuSans_glyphInstructions,
DejaVuSans_glyphInstructionIndices,
DejaVuSans_glyphInstructionCounts,
DejaVuSans_glyphAdvances, DejaVuSans_characterMap, DejaVuSans_glyphCount);
self.SerifTypeface = loadfont(DejaVuSerif_glyphPoints,
DejaVuSerif_glyphPointIndices,
DejaVuSerif_glyphInstructions,
DejaVuSerif_glyphInstructionIndices,
DejaVuSerif_glyphInstructionCounts,
DejaVuSerif_glyphAdvances, DejaVuSerif_characterMap, DejaVuSerif_glyphCount);
self.MonoTypeface = loadfont(DejaVuSansMono_glyphPoints,
DejaVuSansMono_glyphPointIndices,
DejaVuSansMono_glyphInstructions,
DejaVuSansMono_glyphInstructionIndices,
DejaVuSansMono_glyphInstructionCounts,
DejaVuSansMono_glyphAdvances, DejaVuSansMono_characterMap, DejaVuSansMono_glyphCount);
--]]
return self
end
-- Start begins the picture, clearing a rectangular region with a specified color
function Renderer:Begin()
local color = ffi.new("VGfloat[4]", 255, 255, 255, 1);
EGL.Lib.vgSetfv(ffi.C.VG_CLEAR_COLOR, 4, color);
EGL.Lib.vgClear(0, 0, self.Width, self.Height);
color[0] = 0;
color[1] = 0;
color[2] = 0;
self:SetFill(color);
self:SetStrokeColor(color);
self:StrokeWidth(0);
EGL.Lib.vgLoadIdentity();
end
-- End checks for errors, and renders to the display
function Renderer:End()
--assert(EGL.Lib.vgGetError() == ffi.C.VG_NO_ERROR);
self.Display:SwapBuffers();
--assert(EGL.Lib.eglGetError() == EGL.EGL_SUCCESS);
end
function Renderer:Flush()
EGL.Lib.vgFlush();
end
function Renderer:Finish()
EGL.Lib.vgFinish();
end
--
-- Transformations
--
function Renderer:PushTransform()
local mTrans = ffi.new("VGfloat[9]");
EGL.Lib.vgGetMatrix(mTrans);
table.insert(self.TransformStack, mTrans);
end
function Renderer:PopTransform()
if #self.TransformStack < 1 then
return
end
local mTrans = self.TransformStack[#self.TransformStack];
table.remove(self.TransformStack, #self.TransformStack);
EGL.Lib.vgLoadMatrix(mTrans);
end
-- Translate the coordinate system to x,y
function Renderer:Translate(x, y)
EGL.Lib.vgTranslate(x, y);
end
-- Rotate around angle r
function Renderer:Rotate(r)
EGL.Lib.vgRotate(r);
end
-- Shear shears the x coordinate by x degrees, the y coordinate by y degrees
function Renderer:Shear(x, y)
EGL.Lib.vgShear(x, y)
end
-- Scale scales by x, y
function Renderer:Scale(x, y)
EGL.Lib.vgScale(x, y);
end
--
-- Style functions
--
-- setfill sets the fill color
function Renderer:SetFill(color)
local fillPaint, err = OpenVGUtils.Paint();
if not fillPaint then
print("SetFill Failed: ", err);
return false;
end
fillPaint:SetType(ffi.C.VG_PAINT_TYPE_COLOR);
fillPaint:SetColor(color);
fillPaint:SetModes(ffi.C.VG_FILL_PATH);
end
-- setstroke sets the stroke color
function Renderer:SetStrokeColor(color)
local strokePaint = OpenVGUtils.Paint();
strokePaint:SetType(ffi.C.VG_PAINT_TYPE_COLOR);
strokePaint:SetColor(color);
strokePaint:SetModes(ffi.C.VG_STROKE_PATH);
end
-- StrokeWidth sets the stroke width
function Renderer:StrokeWidth(width)
EGL.Lib.vgSetf(ffi.C.VG_STROKE_LINE_WIDTH, width);
EGL.Lib.vgSeti(ffi.C.VG_STROKE_CAP_STYLE, ffi.C.VG_CAP_BUTT);
EGL.Lib.vgSeti(ffi.C.VG_STROKE_JOIN_STYLE, ffi.C.VG_JOIN_MITER);
end
-- Fill sets the fillcolor, defined as a RGBA quad.
function Renderer:Fill(r, g, b, a)
local color = ffi.new("VGfloat[4]")
RGBA(r, g, b, a, color);
self:SetFill(color);
end
function Renderer:SetStroke(r,g,b,a)
local color = ffi.new("VGfloat[4]")
RGBA(r, g, b, a, color);
self:SetStrokeColor(color);
end
-- clear the screen to a background color
function Renderer:Background(r, g, b)
self:Fill(r, g, b, 1);
self:Rect(0, 0,self.Width, self.Height);
end
--[[
Shape drawing functions
--]]
-- Line makes a line from (x1,y1) to (x2,y2)
function Renderer:Line(x1, y1, x2, y2)
OpenVGUtils.Paths.Line(x1, y1, x2, y2):Draw();
end
-- interleave interleaves arrays of x, y into a single array
function interleave(x, y, n, points)
while (n>0) do
points[0] = x[0];
points = points + 1;
points[0] = y[0];
points = points + 1;
x = x + 1;
y = y + 1;
n = n - 1
end
end
-- poly makes either a polygon or polyline
function poly(x, y, n, flag)
flag = flag or bor(ffi.C.VG_FILL_PATH, ffi.C.VG_STROKE_PATH);
local points = ffi.new("VGfloat[?]", n * 2);
interleave(x, y, n, points);
OpenVGUtils.Paths.Poly(points, n):Draw(flag);
end
-- Polygon makes a filled polygon with vertices in x, y arrays
function Renderer:Polygon(x, y, n)
poly(x, y, n, ffi.C.VG_FILL_PATH);
end
-- Polyline makes a polyline with vertices at x, y arrays
function Renderer:Polyline(x, y, n)
poly(x, y, n, ffi.C.VG_STROKE_PATH);
end
-- makecurve makes path data using specified segments and coordinates
function drawcurve(segments, coords)
local path = OpenVGUtils.Path();
EGL.Lib.vgAppendPathData(path, 2, segments, coords);
path:Draw();
end
-- CBezier makes a quadratic bezier curve
function Renderer:Cbezier(sx, sy, cx, cy, px, py, ex, ey)
local segments = ffi.new("VGubyte[2]", ffi.C.VG_MOVE_TO_ABS, ffi.C.VG_CUBIC_TO);
local coords = ffi.new("VGfloat[8]", sx, sy, cx, cy, px, py, ex, ey);
drawcurve(segments, coords);
end
-- QBezier makes a quadratic bezier curve
function Renderer:Qbezier(sx, sy, cx, cy, ex, ey)
local segments = ffi.new("VGubyte[2]", ffi.C.VG_MOVE_TO_ABS, ffi.C.VG_QUAD_TO);
local coords = ffi.new("VGfloat[6]", sx, sy, cx, cy, ex, ey);
drawcurve(segments, coords);
end
-- Rect makes a rectangle at the specified location and dimensions
function Renderer:Rect(x, y, w, h)
OpenVGUtils.Paths.Rect(x,y,w,h):Draw();
end
-- Roundrect makes an rounded rectangle at the specified location and dimensions
function Renderer:Roundrect(x, y, w, h, rw, rh)
OpenVGUtils.Paths.Roundrect(x,y,w,h, rw, rh):Draw();
end
-- Ellipse makes an ellipse at the specified location and dimensions
function Renderer:Ellipse(x, y, w, h)
OpenVGUtils.Paths.Ellipse(x,y,w,h):Draw();
end
-- Circle makes a circle at the specified location and dimensions
function Renderer:Circle(x, y, r)
self:Ellipse(x, y, r, r);
end
-- Arc makes an elliptical arc at the specified location and dimensions
function Arc(x, y, w, h, sa, aext)
OpenVGUtils.Paths.Arc(x,y,w,h,sa,aext):Draw();
end
return Renderer;