Creating a Line in Managed DirectX
Then I rechecked the forums and Andy the ZMan had already answered the guys question with a short one liner. Use the Line class. What??!? There's a line class? Started looking around and what do you know? There's actually a line class.
I wrote up some sample code using the line class and then posted it to the forums and I casually tossed out that I would wrap all those steps in a line function to make it easier to work with the DirectX Line class.
Now, I've went one step further. Below is the code I've written to wrap the Line class in the simplest sense, to make it very easier to create and render a line.
'Description: Microsoft.DirectX.Direct3D.Line class wrapper to make it easier to
' work with and manipulates Lines
Public Class DXLine
'Line object
Private mLine As Microsoft.DirectX.Direct3D.Line
'Starting point for the line
Private mStartPoint As Point
Public Property StartPoint() As Point
Get
Return mStartPoint
End Get
Set(ByVal Value As Point)
mStartPoint = Value
End Set
End Property
'Ending point for the line
Private mEndPoint As Point
Public Property EndPoint() As Point
Get
Return mEndPoint
End Get
Set(ByVal Value As Point)
mEndPoint = Value
End Set
End Property
'Width of the line
Private mWidth As Integer
Public Property Width() As Integer
Get
Return mWidth
End Get
Set(ByVal Value As Integer)
mWidth = Value
End Set
End Property
'Color for the line
Private mColor As System.Drawing.Color
Public Property Color() As System.Drawing.Color
Get
Return mColor
End Get
Set(ByVal Value As System.Drawing.Color)
mColor = Value
End Set
End Property
'Line class constructor
Public Sub New(ByVal startPoint As Point, ByVal endPoint As Point, ByVal width As Integer, ByVal color As System.Drawing.Color, ByVal dxDevice As Microsoft.DirectX.Direct3D.Device)
'Store the data passed into the class constructor
mStartPoint = startPoint
mEndPoint = endPoint
mWidth = width
mColor = color
'Create the line object
mLine = New Microsoft.DirectX.Direct3D.Line(dxDevice)
'Automatically antialias the line
mLine.Antialias = True
End Sub
'Draw the line using the current class values for starpoint, endpoint, width and color
Public Sub Draw()
'Give the line it's width
mLine.Width = Width
'Render the line
mLine.Begin()
mLine.Draw(LineVectors, Color)
mLine.End()
End Sub
'Construct the Line Vectors based on the Start and End Points
Private Function LineVectors() As Microsoft.DirectX.Vector2()
'Vector object (for passing startpoint and endpoint into Line.Draw function)
Dim aLineVectors(1) As Microsoft.DirectX.Vector2
'Set the starting point of the line
aLineVectors(0).X = StartPoint.X
aLineVectors(0).Y = StartPoint.Y
'Set the end point of the line
aLineVectors(1).X = EndPoint.X
aLineVectors(1).Y = EndPoint.Y
Return aLineVectors
End Function
End Class
So basically to use this class, I first create a line object in my Main module when I'm initializing all my DX objects. Then during my Render code I just call the objects .Draw method and that's it, a Line will be drawn to your scene.
Module Main
Private mLine As DXLine
Public Sub Main
...
....
mLine = New DXLine(New Point(5, 100), New Point(5, 300), 10, System.Drawing.Color.BurlyWood, mDXEngine.myDevice)
..
....
Do While GameOver = False
...
....
Call Render()
Application.DoEvents()
...
....
Loop 'While GameOver = False
...
....
End Sub
Public Sub Render()
...
....
'Draw the line
mLine.Draw
...
....
End Sub
End Module
Hopefully that helps someone, it's a pretty simple implementation, but I think that I will be using it a bit.