From 7bbc44c79a2f7a745584106cddaa28c052e26d25 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 11 Oct 2005 23:38:22 +0000 Subject: Changed vector objects so they work on a point list and line index basis --- vectorgfx.mod/vectorgfx.bmx | 205 ++++++++++++++++++++++++++++---------------- 1 file changed, 132 insertions(+), 73 deletions(-) diff --git a/vectorgfx.mod/vectorgfx.bmx b/vectorgfx.mod/vectorgfx.bmx index a79b79c..00e3959 100644 --- a/vectorgfx.mod/vectorgfx.bmx +++ b/vectorgfx.mod/vectorgfx.bmx @@ -68,7 +68,8 @@ End Function Rem bbdoc: Defines a vector graphics collision map. -about: Collisions in the library a defined using a bitmask (allowing 32 collision masks). +about: Collisions in the library a defined using a bitmask (allowing 32 collision masks). Note that with the way that lines are drawn in an object you are likely +about: to receive collisions for the IDs used by the lines in the object. EndRem Type TVectorGfxCollisionMap @@ -141,10 +142,14 @@ Rem bbdoc: Defines a 2D vector graphics object EndRem Type TVectorGfxObject + Rem + bbdoc: The points making up this object + EndRem + Field points:TVectorGfxPoint[] Rem bbdoc: The lines making up this object EndRem - Field lines:TList + Field lines:TVectorGfxLine[] Rem bbdoc: The X co-ordinate of the object EndRem @@ -157,34 +162,69 @@ Type TVectorGfxObject bbdoc: The angle of the object, as degrees*10. Must be within the range 0-3599 inclusive. EndRem Field ang:Int + + Field rotated:TVectorGfxPoint[] Rem bbdoc: Creates a new empty object returns: The created object. EndRem Method New() - lines=CreateList() + points=Null + lines=Null + rotated=Null x=0 y=0 ang=0 End Method Rem - bbdoc: Adds a line to the object. - about: @line is the line to add. + bbdoc: Sets the points for this object + about: @pa is an array of points for this object. EndRem - Method AddLine(line:TVectorGfxLine) - line.parent=Self - lines.AddLast(line) + Method SetPoints(pa:Object[]) + points=New TVectorGfxPoint[pa.length] + For Local f:Int=0 Until pa.length + points[f]=TVectorGfxPoint(pa[f]) + Next End Method Rem - bbdoc: Removes a line from the object. - about: @line is the line to remove. + bbdoc: Sets the lines for this object. + about: @la is the array of lines for this object. EndRem - Method RemoveLine(line:TVectorGfxLine) - line.parent=Null - lines.Remove(line) + Method SetLines(la:Object[]) + lines=New TVectorGfxLine[la.length] + For Local f:Int=0 Until la.length + lines[f]=TVectorGfxLine(la[f]) + Next + End Method + + Rem + bbdoc: A line's adjusted co-ordinates. + returns: The rotated points (in object co-ordinates) of the line. + about: @i is the index of the line to return the points for. + about: Calling it before drawing then will cause runtime errors. + EndRem + Method AdjustedCoords:TVectorGfxPoint[](i:Int) + Local p:TVectorGfxPoint[]=New TVectorGfxPoint[2] + Local l:TVectorGfxLine=lines[i] + p[0]=rotated[l.i1] + p[1]=rotated[l.i2] + Return p + End Method + + Rem + bbdoc: A line's normal. + returns: The normal of the selected line. + about: @i is the index of the line to return the normal of. This will return the normal due to the rotation of the object after the object is drawn. + about: Calling it before drawing then will cause runtime errors. + EndRem + Method Normal:TVector(i:Int) + Local l:TVectorGfxLine=lines[i] + Local v:TVector=TVector.Create(rotated[l.i2].y-rotated[l.i1].y,rotated[l.i1].x-rotated[l.i2].x) + v.Normalise() + Return v End Method Rem @@ -205,12 +245,25 @@ Type TVectorGfxObject o.y=s.ReadInt() o.ang=s.ReadInt() + Local l:TList=CreateList() Local i:Int=s.ReadInt() For Local f=0 Until i - o.AddLine(TVectorGfxLine.Load(s)) + l.AddLast(TVectorGfxPoint.Load(s)) + Next + + o.SetPoints(l.ToArray()) + + l.Clear() + + i=s.ReadInt() + + For Local f=0 Until i + l.AddLast(TVectorGfxLine.Load(s)) Next + o.SetLines(l.ToArray()) + s.Close() Return o @@ -231,8 +284,13 @@ Type TVectorGfxObject s.WriteInt(x) s.WriteInt(y) s.WriteInt(ang) - s.WriteInt(lines.Count()) + + s.WriteInt(points.length) + For Local p:TVectorGfxPoint=EachIn points + p.Save(s) + Next + s.WriteInt(lines.length) For Local l:TVectorGfxLine=EachIn lines l.Save(s) Next @@ -243,29 +301,71 @@ Type TVectorGfxObject Rem bbdoc: Draw the object. returns: A bitmask of collisions. - about: Draws the object. @colmap cannot be null - use a small collision map instead if you're uninterested in collisions + about: Draws the object. @colmap cannot be null - use a small collision map instead if you're uninterested in collisions. EndRem Method Draw:Int(colmap:TVectorGfxCollisionMap) Local col:Int=0 + + rotated=New TVectorGfxPoint[points.length] + + For Local f:Int=0 Until points.length + Local p:TAlgoPoint=DoRotate(points[f].x,points[f].y,ang) + rotated[f]=TVectorGfxPoint.Create(p.x,p.y) + Next + 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) + col:|Static.linedraw.draw(x+rotated[l.i1].x,y+rotated[l.i1].y,x+rotated[l.i2].x,y+rotated[l.i2].y,l.r,l.g,l.b,l.id,colmap) Next Return col End Method End Type +Rem +bbdoc: Defines a point +EndRem +Type TVectorGfxPoint + Field x:Int + Field y:Int + + Rem + bbdoc: Create a point. + returns: The created point. + about: @x and @y are the co-ordinates of the point. + EndRem + Function Create:TVectorGfxPoint(x:Int, y:Int) + Local o:TVectorGfxPoint=New TVectorGfxPoint + o.x=x + o.y=y + Return o + End Function + + Rem + bbdoc: This is for internal library use. + EndRem + Function Load:TVectorGfxPoint(s:TStream) + Local o:TVectorGfxPoint + o=New TVectorGfxPoint + o.x=s.ReadInt() + o.y=s.ReadInt() + Return o + End Function + + Rem + bbdoc: This is for internal library use. + EndRem + Method Save(s:TStream) + s.WriteInt(x) + s.WriteInt(y) + 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 i1:Int + Field i2:Int Field id:Int Field r:Int Field g:Int @@ -274,15 +374,13 @@ Type TVectorGfxLine 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. + about: @i1 and @i2 are the indexes into the point list. @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) + Function Create:TVectorGfxLine(i1:Int, i2: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.i1=i1 + o.i2=i2 o.r=r o.g=g o.b=b @@ -291,51 +389,14 @@ Type TVectorGfxLine 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.i1=s.ReadInt() + o.i2=s.ReadInt() o.r=s.ReadInt() o.g=s.ReadInt() o.b=s.ReadInt() @@ -347,10 +408,8 @@ Type TVectorGfxLine 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(i1) + s.WriteInt(i2) s.WriteInt(r) s.WriteInt(g) s.WriteInt(b) -- cgit v1.2.3