Tuesday, October 25, 2005

Creating a Line in Managed DirectX

Well, I finally saw a question I thought I could answer in the Microsoft forums. A guy was asking how to easily create a Line in DirectX and I thought for sure I knew the answer. I thought you would just create a square from two triangles. I was going to make a really slick line class and deliver the solution too him. I had even began the development.

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.

Friday, October 14, 2005

My Background is Scrolling like a Champ

Well, I quickly created my background graphics last night. Made some changes to my Background class and I now have a successfully scrolling background.

One thing I like about my background class is that I really tried to make an effot to make it very reusable for future projects. It will take any number of background images that comprise your background and scroll through them properly.

The code isn't really that impressive, but I like that fact that I can change what backgrounds are scrolling just by passing in new images to the class.

Looking the the DevExpress metrics for my class though, I do have 3 functions that have unacceptable complexity (I try to keep everything below 100 and these three are above 300) so I want to work on lowering their complexity before I post my class code.

Image hosted by Photobucket.com

Wednesday, October 12, 2005

Getting back to development...

Well, in an effort to get back to doing some actual development and quit monkeying around with Paintshop so much, I've laid out some simple goals so that I don't overextend myself creating oodles of art and not doing any actual development.

1. Create animations for the main character,
This will consist of 2 different sets of animation, approximately 5 frames each.

a. Walking left to right
b. Attacking

2. Create the Level 1 background.

I had this finished at one point, but I created it in basic paint so the quality was definitely not there. So I plan on recreating this using Paint Shop pro (and take advantage of its WONDERFUL WONDERFUL layers!!!)

So I have approximately, 11 graphics to create. Then, once those graphics are done, I have the following plans.

3. Have the game start with loading the background without the character and displaying the "Level 1 (press any key to start)" text in black (using DrawText I'm guessing)

4. After user presses key, make a game start sound and draw the character on the screen.

5. Begin playing a background track (most likely a free one from the web for now)

6. User can walk to the right (and back up by moving left)

7. Allow the user to walk and begin scrolling the background when appropriate.

8. Display "Success!" once the user has walked to the end and off the level.

With these plans, I'm hoping to limit the amount of time spent creating character animations (later I'll want to add Jump, Take Damage, Duck, etc) so that I can get back to actually doing some more Managed DirectX development.

I'll post some character shots after I get them done as well so you can see when I'm moving from the initial graphic stage into tackling some new development.

I'm really looking forward to doing the music and text pieces as well since I haven't actually coded anything for those pieces yet in Managed DirectX.

Then once, I get that level 1 initial round of coding done, I'll try and post some more complete examples of how my classes and code actually work.

Saturday, October 01, 2005

Ye Olde Weapon Shop

I've started spending a lot of time on the intro and first level of my game. In doing so I've had to spend a lot of time learning how to use Jasc Paint Shop Pro 8 (I've never used anything other than the default paint program before). At first I got so frustrated I felt like I should just draw everything by hand, scan it in and then edit my scanned images, but I think I'm starting to get the hang of things now.

Image hosted by Photobucket.com

I'm pretty happy with the way this turned out. The Weapon Shop looks exactly how I was picturing it in my head and I was able to create it in the same graphic style I want to use for the whole game.

I'll try and post some other graphic samples as I get them completed and then hopefully I can get back to the coding. I only plan on doing the graphics needed for the first level and for the game introduction (and both are rather small in scale) so soon I will be able to focus once again on 2D game development with Managed DirectX.