DirectX:Tutorials:VBNET:DX9:Introduction
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] WelcomeWelcome to the first tutorial for using DirectX 9 with VB.NET. This tutorial will cover setting up a project to allow it to use DX9, the basic code necessary for getting DX to run, and displaying a sprite (and doing something with it) This tutorial assumes one is using VB.NET 2003 and DirectX 9. There are a couple of alternatives however. You can download the .NET Framework and use a free IDE such as SharpDevelop. You can also download VB.NET 2005 Express Edition. As of summer of 2006, all microsoft express editions are free - with registration. [edit] Setting Up Your ProjectStart up VB.NET (I'm assuming you know how to do this Click the OK button and you'll be in the project with a default form. Click the Project | References menu and add the references shown: We're adding the DX3DX reference since it contains a number of helpful functions that we'll see later. [edit] Let's Start CodingGo to the code for the form (hit F7 or click the View Code button for the form in the Solution Explorer) and add the following before the form's class definition: Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports Microsoft.DirectX.DXHelp Using the Imports statement allows us to reference elements in the namespace without using the namespaces. For example, without the Imports Microsoft.DirectX.Direct3D statement the following could possibly give us an error: Dim _device As Device We would have to use the full path to the namespace containing the class: Dim _device As Microsoft.DirectX.Direct3D.Device Having to do this for every declaration would be tiring after awhile. Moving on, add the following declaration to the form: Private _dxDevice As DXDevice What is DXDevice? Good question. It's a class that we'll use to wrap up all the functionality of working with a Direct3D device, which is used to handle all our graphics rendering. Add a class to the project, either as a separate file or in the same code window as the form. I've chosen to do the latter, since this is a short tutorial with a small amount of code, but feel free to make it a separate file. You'll want to do this with larger projects for organization's sake and to make it easy to reuse the class, which is the purpose of having it. Here's the skeleton for the class: Public Class DXDevice Private _device As Device Public Sub New(ByRef frmHandle As IntPtr) End Sub Public Sub Render() End Sub End Class The constructor to the class takes a pointer to the form's handle upon which all rendering will be done. The Render procedure will do the actual displaying of our output to the screen. Right now this will be nothing more than filling with form with a color to let us know that the code works. Later on we'll draw a sprite. Add the following code to the constructor: Dim presentParams As New PresentParameters presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard _device = New Device(0, DeviceType.Hardware, frmHandle, _ CreateFlags.SoftwareVertexProcessing, presentParams) I'm not going to take the time to explain all this in detail. Basically this tells DirectX how our graphics are going to be set up and displayed and creates the device. Feel free to look up all the options for the PresentParameters class. Add the following to the Render procedure: If _device Is Nothing Then Return _device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0F, 0) _device.BeginScene() _device.EndScene() _device.Present() If for some reason our device hasn't been created or has been lost we don't want to try to render anything as we'll get an error. We then clear the screen to blue and tell DirectX to display it on the screen. We'll get into the BeginScene and EndScene later, but basically all the code to display things on the screen goes between these two functions. Now that we have a class to display stuff to the screen, albeit just filling the form with a color, we need to add the code to use the class. In the form's Load event put the following: _dxDevice = New DXDevice(Me.Handle) This creates an instance of our class. Telling the class to render is one line of code since we've encapsulated everything. In the Paint event of the form put: _dxDevice.Render() Save everything (you'll want to save often, just in case you hose something) and run the project. If you've done everything correctly it'll compile and you'll see something like the following:
[edit] Where's The Cool StuffOk, ok, admittedly this isn't very impressive. Let's actually display something on the screen. Here's what we're going to be showing:
Add the following to the DXDevice class: Private _sprite As Sprite Private _texture As Texture The Sprite class is one of the helper things I was talking about earlier. It makes drawing sprites fairly easy. Add the following to the DXDevice constructor: _sprite = New Sprite(_device) This creates our instance of the Sprite class. We have to pass it the DirectX Device member so it is able to draw, otherwise it wouldn't have access to the memory location that is used to render the stuff that is displayed to the screen. Add the following to the Render procedure of the DXDevice class, after the call to BeginScene (remember above where we talked about how stuff would go between the calls to BeginScene and EndScene? Well, here it is): _sprite.Begin(SpriteFlags.AlphaBlend) _sprite.Draw(_texture, Rectangle.Empty, New Vector3(0, 0, 0), _ New Vector3(5.0F, 5.0F, 0.0F), Color.White) _sprite.End() Now we need a way to load the sprite. Add the following to the class: Public Sub LoadSprite(ByVal filename As String) _texture = TextureLoader.FromFile(_device, filename, 256, 256, _ D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, _ Filter.Point, Color.Magenta.ToArgb) End Sub Again, I'm not going to explain all the parameters. One we're interested in though is the last one, which tells us what color will be transparent. I tend to use magenta because who in their right mind would normally use it :) OK, so the class is done, but we need to add call the function to load the sprite from our form. Add the following after the line that's already there to the Load event for the form: _dxDevice.LoadSprite(Application.StartupPath & "\sprite.bmp") Save your work and run the application. If you've done everything correct you should see something like the following:
[edit] ConclusionI was going to cover things like sprite rotation, but I think I'll hold off until another tutorial (if there's interest). Using the techniques we've covered you should be able to do a lot more with not much more code - for example creating a tiled map for a side scroller. I may cover that in a future tutorial if there's interest. Microsoft has made it very easy to do Direct3D game using managed code. Doing the same thing we've done here with unmanaged code would probably have taken about 10 times the amount of code. Download the project to this point here from: agent_arcangel@yahoo.com Make sure to clean up your garbage Public Sub dispose() _device.Dispose() _sprite.Dispose() _texture.Dispose() End Sub and add this to your form: Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.Closed _dxDevice.dispose() End Sub Also need to add this to your class: Public Sub LoadSprite(ByVal filename As String) _texture = TextureLoader.FromFile(_device, filename, 256, 256, D3DX.Default, _ 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, Color.Magenta.ToArgb) End Sub |


