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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
Rem
bbdoc: noddybox.vectorgfx
EndRem
Module noddybox.vectorgfx
ModuleInfo "Framework: 2D Vector Graphics classes"
ModuleInfo "Copyright: Public Domain"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"
' $Id$
Strict
Import brl.linkedlist
Import brl.math
Import brl.endianstream
Import brl.max2d
Import noddybox.vector
Import noddybox.algorithm
Rem
bbdoc: Defines a method for drawing vector lines
EndRem
Type TVectorGfxLineStyle Abstract
Rem
bbdoc: Draws a vector line.
returns: The or'ed mask of all collision IDs overwritten in @colmap by this line.
about: This routine must draw a line from @x1, @y1 to @x2, @y2 using the colour @r,@g,@b. @id must be written into the collision map @colmap.
EndRem
Method Draw:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int, colmap:TVectorGfxCollisionMap) Abstract
End Type
Rem
bbdoc: Set to use solid lines for drawing.
about: This mode draws vector lines as solid colour lines using the current alpha, and is the default mode.
EndRem
Function VectorGfxSetSolid()
Static.linedraw=New LineSolid
End Function
Rem
bbdoc: Set to use alpha lines for drawing.
about: This mode draws vector lines using a @line_alpha for the lines, except for the end-points drawn with @end_alpha.
EndRem
Function VectorGfxSetAlpha(line_alpha:Double,end_alpha:Double)
Local l:LineAlpha=New LineAlpha
l.la=line_alpha
l.ea=end_alpha
Static.linedraw=l
End Function
Rem
bbdoc: Set to use thick lines for drawing.
about: This mode draws vector lines using solid line, surrounded by points of alpha @line_alpha.
EndRem
Function VectorGfxSetThickAlpha(line_alpha:Double)
Local l:ThickAlpha=New ThickAlpha
l.a=line_alpha
Static.linedraw=l
End Function
Rem
bbdoc: Set to use a custom drawing method.
EndRem
Function VectorGfxSetCustom(linedraw:TVectorGfxLineStyle)
Static.linedraw=linedraw
End Function
Rem
bbdoc: Defines a vector graphics collision map.
about: Collisions in the library a defined using a bitmask (allowing 32 collision masks).
EndRem
Type TVectorGfxCollisionMap
Field width:Int
Field height:Int
Field xoff:Int
Field yoff:Int
Field data:Int[,]
Rem
bbdoc: Create a collision map.
returns: The created collision map.
about: @width and @height are the size of the collision map.
EndRem
Function Create:TVectorGfxCollisionMap(width:Int, height:Int)
Local o:TVectorGfxCollisionMap=New TVectorGfxCollisionMap
o.width=width
o.height=height
o.xoff=0
o.yoff=0
o.data=New Int[width,height]
Return o
End Function
Rem
bbdoc: Set the offset of the collision map.
about: @x and @y are the offsets into the co-ordinate space where the collision map lives.
EndRem
Method SetOffset(x:Int, y:Int)
xoff=x
yoff=y
End Method
Rem
bbdoc: Set a collision point.
returns: The current collision mask for that point, or zero if the point is out of the map's range.
about: @x and @y are the co-ordinates (in the objects co-ordinate space, not the collision map space)
EndRem
Method SetCollision(x:Int, y:Int, id:Int)
If x<xoff Or x>=xoff+width Or y<yoff Or y>=yoff+height
Return 0
EndIf
x:-xoff
y:-yoff
Local cur:Int=data[x,y]
data[x,y]:|id
Return cur
End Method
Rem
bbdoc: Clear the collision map
EndRem
Method Clear()
For Local x:Int=0 Until width
For Local y:Int=0 Until height
data[x,y]=0
Next
Next
End Method
End Type
Rem
bbdoc: Defines a 2D vector graphics object
EndRem
Type TVectorGfxObject
Rem
bbdoc: The lines making up this object
EndRem
Field lines:TList
Rem
bbdoc: The X co-ordinate of the object
EndRem
Field x:Int
Rem
bbdoc: The Y co-ordinate of the object
EndRem
Field y:Int
Rem
bbdoc: The angle of the object, as degrees*10. Must be within the range 0-3599 inclusive.
EndRem
Field ang:Int
Rem
bbdoc: Creates a new empty object
returns: The created object.
EndRem
Method New()
lines=CreateList()
x=0
y=0
ang=0
End Method
Rem
bbdoc: Adds a line to the object.
about: @line is the line to add.
EndRem
Method AddLine(line:TVectorGfxLine)
line.parent=Self
lines.AddLast(line)
End Method
Rem
bbdoc: Removes a line from the object.
about: @line is the line to remove.
EndRem
Method RemoveLine(line:TVectorGfxLine)
line.parent=Null
lines.Remove(line)
End Method
Rem
bbdoc: Loads the object from the supplied @filename.
returns: The loaded object, or null if the file could not be opened.
EndRem
Function Load:TVectorGfxObject(filename:String)
Local s:TStream=ReadStream(filename)
If Not s
Return Null
EndIf
s=LittleEndianStream(s)
Local o:TVectorGfxObject=New TVectorGfxObject
o.x=s.ReadInt()
o.y=s.ReadInt()
o.ang=s.ReadInt()
Local i:Int=s.ReadInt()
For Local f=0 Until i
o.AddLine(TVectorGfxLine.Load(s))
Next
s.Close()
Return o
End Function
Rem
bbdoc: Saves the object to the supplied @filename.
EndRem
Method Save(filename:String)
Local s:TStream=WriteStream(filename)
If Not s
Return Null
EndIf
s=LittleEndianStream(s)
s.WriteInt(x)
s.WriteInt(y)
s.WriteInt(ang)
s.WriteInt(lines.Count())
For Local l:TVectorGfxLine=EachIn lines
l.Save(s)
Next
s.Close()
End Method
Rem
bbdoc: Draw the object.
returns: A bitmask of collisions.
about: Draws the object. @colmap <u>cannot</u> be null - use a small collision map instead if you're uninterested in collisions
EndRem
Method Draw:Int(colmap:TVectorGfxCollisionMap)
Local col:Int=0
For Local l:TVectorGfxLine=EachIn lines
Local p:TAlgoPoint[]=l.AdjustedCoords()
col:|Static.linedraw.draw(p[0].x,p[0].y,p[1].x,p[1].y,l.r,l.g,l.b,l.id,colmap)
Next
Return col
End Method
End Type
Rem
bbdoc: Defines a line
EndRem
Type TVectorGfxLine
Field parent:TVectorGfxObject
Field x1:Int
Field y1:Int
Field x2:Int
Field y2:Int
Field id:Int
Field r:Int
Field g:Int
Field b:Int
Rem
bbdoc: Create a line.
returns: The created line.
about: @x1, @y1, @x2 and @y2 are the co-ordinates of the line. @r, @g and @b is the line's colour. @id is the collision map mask for the line.
EndRem
Function Create:TVectorGfxLine(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int)
Local o:TVectorGfxLine=New TVectorGfxLine
o.x1=x1
o.y1=y1
o.x2=x2
o.y2=y2
o.r=r
o.g=g
o.b=b
o.id=id
Return o
End Function
Rem
bbdoc: The line's normal.
returns: The normal of the line.
about: When this line is assigned to an object, this will return the normal due to the rotation of the object
EndRem
Method Normal:TVector()
Local p:TAlgoPoint[]=AdjustedCoords()
Local v:TVector=TVector.Create(p[0].y-p[1].y,p[1].x-p[0].x)
v.Normalise()
Return v
End Method
Rem
bbdoc: The line's rotated and adjusted co-ords.
returns: The two points defining the screen co-ordinates used.
about: This won't really return anything useful unless the line is assigned to an object.
EndRem
Method AdjustedCoords:TAlgoPoint[]()
Local p:TAlgoPoint[]=New TAlgoPoint[2]
If parent<>Null
p[0]=DoRotate(x1,y1,parent.ang)
p[1]=DoRotate(x2,y2,parent.ang)
p[0].x:+parent.x
p[0].y:+parent.y
p[1].x:+parent.x
p[1].y:+parent.y
Else
p[0]=TAlgoPoint.Create(x1,y1)
p[1]=TAlgoPoint.Create(x2,y2)
EndIf
Return p
End Method
Rem
bbdoc: This is for internal library use.
EndRem
Function Load:TVectorGfxLine(s:TStream)
Local o:TVectorGfxLine
o=New TVectorGfxLine
o.x1=s.ReadInt()
o.y1=s.ReadInt()
o.x2=s.ReadInt()
o.y2=s.ReadInt()
o.r=s.ReadInt()
o.g=s.ReadInt()
o.b=s.ReadInt()
o.id=s.ReadInt()
Return o
End Function
Rem
bbdoc: This is for internal library use.
EndRem
Method Save:TVectorGfxLine(s:TStream)
s.WriteInt(x1)
s.WriteInt(y1)
s.WriteInt(x2)
s.WriteInt(y2)
s.WriteInt(r)
s.WriteInt(g)
s.WriteInt(b)
s.WriteInt(id)
End Method
End Type
Private
Type Static
Global linedraw:TVectorGfxLineStyle
Function Init()
linedraw=New LineSolid
End Function
End Type
Type LineSolid Extends TVectorGfxLineStyle
Method Draw:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int, colmap:TVectorGfxCollisionMap)
Local mask:Int=0
Local lp:TList=DoLine(x1,y1,x2,y2)
SetColor(r,g,b)
For Local p:TAlgoPoint=EachIn lp
mask:|colmap.SetCollision(p.x,p.y,id)
Plot(p.x,p.y)
Next
Return mask
End Method
End Type
Type LineAlpha Extends TVectorGfxLineStyle
Field la:Double
Field ea:Double
Method Draw:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int, colmap:TVectorGfxCollisionMap)
Local mask:Int=0
Local lp:TList=DoLine(x1,y1,x2,y2)
Local p:TAlgoPoint
Local a:Double=GetAlpha()
SetColor(r,g,b)
SetAlpha(ea)
p=TAlgoPoint(lp.RemoveFirst())
mask:|colmap.SetCollision(p.x,p.y,id)
Plot(p.x,p.y)
If lp.Count()=0
Return id
EndIf
p=TAlgoPoint(lp.RemoveLast())
mask:|colmap.SetCollision(p.x,p.y,id)
Plot(p.x,p.y)
SetAlpha(la)
For Local p:TAlgoPoint=EachIn lp
mask:|colmap.SetCollision(p.x,p.y,id)
Plot(p.x,p.y)
Next
SetAlpha(a)
Return mask
End Method
End Type
Type ThickAlpha Extends TVectorGfxLineStyle
Field a:Double
Method PlotPoint(x,y)
SetAlpha(a)
DrawLine(x-1,y-1,x+1,y-1,False)
DrawLine(x+1,y-1,x+1,y+1,False)
DrawLine(x+1,y+1,x-1,y+1,False)
DrawLine(x-1,y+1,x-1,y-1,False)
SetAlpha(1.0)
Plot(x,y)
End Method
Method Draw:Int(x1:Int, y1:Int, x2:Int, y2:Int, r:Int, g:Int, b:Int, id:Int, colmap:TVectorGfxCollisionMap)
Local mask:Int=0
Local lp:TList=DoLine(x1,y1,x2,y2)
SetColor(r,g,b)
For Local p:TAlgoPoint=EachIn lp
mask:|colmap.SetCollision(p.x,p.y,id)
PlotPoint(p.x,p.y)
Next
Return mask
End Method
End Type
Static.Init()
|