Managed DirectX 9 (Part I) - Initializing the Device


Senior Contributor
May 21, 2003
Boston, Massachusetts
I will be making a set of 5 or 6 tutorials, each of which include source for Visual Basic.NET 2002 and Visual Basic.NET 2003. C# tutorials may or may not follow. These tutorials are to give you guys a jumpstart to some Direct3D. I hope you enjoy these.

This tutorial will teach you how to initialize the device - the first step in learning D3D.

As you can see above - were going to initialize the device. Well what is the device? Youll see. Ill explain more as we code.

Create a new project.



If you dont know how to reference, please see the Managed DirectX 9 - Introduction tutorial.

Now create a new class called GameClass. At the very top of the code, type these 2 lines.

Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX

Why are we importing? So that we can declare the variables easily! Heres an example. Soon, were going to say Dim D3DDev as Device. If we didnt import DirectX and Direct3D, wed have to say Dim D3DDev as Microsoft.DirectX.Direct3D.Device. This just helps us save some time while coding.

Were going to declare 3 more variables: the Device, the PresentationParameters, and the Displaymode.

The Device will be the object that talks to our graphics card, it is the main control of our direct3D application.

The PresentationParameters are basically how were going to present our program to the user.

The Displaymode is... well, our display mode!


Declare the following variables.

Private D3Ddev As Device = Nothing
Short for PresentationParameters
Private D3Dpp As PresentParameters =
Private DP As DisplayMode =

Now were going to create a sub to initialize our device, this gets the device ready for some action(drawing and such) [Broken External Image]: .

Public Sub Initialize(ByVal TargetForm As Form, ByVal FullScreen As Boolean)

End Sub

The first argument takes in the form that were going to draw to. The 2nd argument tells us whether were going to do this in fullscreen or windowed.

Now add the following code to the sub.

If FullScreen Then
800x600 Resolution
DP.Width = 800
DP.Height = 600
R5G6B5 = 16-bit. Visit MSDN to find out what the other ones are.
DP.Format = Format.R5G6B5
If its not fullscreen, use the current display mode!
DP = Manager.Adapters.Default.CurrentDisplayMode
End If

Im sure you guys understand most of that lol. The only tricky part is the Format.R5G6B5. Format.X8R8G8B8 is 32-bit mode. and Format.R5G6B6 is 16-bit mode.

for more information on different formats.

The next thing to do is set up your PresentationParameters.

As usual, we must always instantiate our classes
D3Dpp = New PresentParameters()
Initialize some stuff for the Presentation parameters
D3Dpp.BackBufferFormat = DP.Format
D3Dpp.BackBufferWidth = DP.Width
D3Dpp.BackBufferHeight = DP.Height

Whats the backbuffer you ask? The backbuffer is what directX draws to. It draws to the backbuffer, and then takes the content of the backbuffer and draws it to the screen.

We need to set up more flags for the Presentation Parameters

Theres flip, copy, and discard. Flip and Discard are used most often.
Visit MSDN has more information.
D3Dpp.SwapEffect = SwapEffect.Discard
Present the scene immediately
D3Dpp.PresentationInterval = PresentInterval.Immediate

Ok - let me explain. Swapeffect is a flag which basically tells Direct3D how
to go from one frame to another.

SwapEffect.Discard - This flag deletes the old frame and draws a new one on top of it. Great preformance.
SwapEffect.Flip - Not sure what this does.
Swapeffect.Copy - The new frame is simply copied on top of the old one. This is the most simle of the backbuffer swap operations, but it is the one with the worst performance.

"In particular, the Flip swap effect operates the same whether windowed or full-screen, and the Direct3D runtime guarantees this by creating extra buffers. As a result, it is recommended that applications use Discard whenever possible to avoid any performance penalties, because the current swap effect is always the most efficient in terms of memory consumption and performance."
"An application can use the Discard swap effect to avoid overheads and to enable the display driver to choose the most efficient presentation technique for the swap chain."
- From the DirectX Help file.

Thus, Id recommend you use Discard, typically for FullScreen apps, although flip is common as well. Please consult MSDN for more help as I am not 100% sure on what each does.

Now to explain PresentationLevel.Immediate. Basically it means present the scene immediately when its drawn. It doesnt have to wait for other stuff to happen before presenting - just present immediately. Gets the most FPS out of your game

The next few lines are simple.

Set to Fullscreen or Windowed
If FullScreen Then
D3Dpp.Windowed = False
D3Dpp.Windowed = True
End If

Ah cmon, no need to explain here.

The next line will be a bit tricky though.

Instantiate the device

D3Ddev = New Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, TargetForm.Handle, CreateFlags.SoftwareVertexProcessing, D3Dpp)

Heres an explanation of the arguments

Manager.Adapters.Default.Adapter - Use the current display driver: the one thats displaying your desktop right now
DeviceType.Hardware - If youve worked with GDI+, youll know how slow software devices are
TargetForm.Handle - Draw to the form
CreateFlags.SoftwareVertexProcessing - Processing vertices with Software (Direct3D) is safer than processing it with your hardware. This is becuase different graphics cards may process them differently. You want it to be processed the same universally, so you let Direct3D do the work.

D3Dpp - Well, use the presentation parameters!

Were done with this sub!

Unfortunately, My post can only be 10000 characters long, so Im going to have to split this long post into 2 posts :) . The next post will be done shortly.
Last edited by a moderator:


Senior Contributor
May 21, 2003
Boston, Massachusetts

Now its time to do some rendering.

Heres what a basic rendering loop would look like:

Do While Not GameOver
End While

Were going to do just that. In your GameClass globals,

Public GameOver AsBoolean

Create a new sub:

PublicSub RenderScene()

End Sub

Add the following code to it:


Do While Not GameOver

Try commenting this out . Youll see what it does. It might hang your app depending on your graphics card. In DX 9.0a, the screen would flash in multiple colors if you didnt clear.
D3Ddev.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 225), 0, 0)

Rendering code goes here usually. But were not rendering anything in this program. So dont type anything here.


In a loop, keyboard events are ignored. This means: let them NOT be ignored


Wow. Its kind of simple right? I just have to explain the D3DDev.Clear.

ClearFlags.Target - Clear the form, our "target"
Color.FromArgb(0, 0, 225) - Clear it with this color (Dark blue)
0 - ZDepth. Our app isnt 3D yet, so dont waste memory on ZDepth
0 - Stencil. Has to do with the advanced topic of Stenciling, which I havent gotten into yet

Now were going to create a Terminate sub.

Public Sub Terminate()
Free up mem
DP = Nothing
D3Dpp = Nothing
D3Ddev = Nothing
FORCE an exit if it didnt exit
End Sub

At the very end of RenderScene, type in Terminate(). This insures that when GameOver = True, the application will terminate.

Wow - were done with GameClass! It has 3 basic parts: Initialize, Render and Terminate, simple right? Now its time to put this program into some action.


Go back to form1.
Type in
Dim Game As GameClass

In the form1_load event, type in:

We have to say Me.Show becuase the form doesnt actually get shown until AFTER the load event.
Try commenting everything in this sub, and type in MessageBox.Show("asdf"). Notice how
the MessageBox appears *before* the form shows up?

As usual, we must always instantiate our classes
Game = New
Initialize the game
Me - Render to form1
True - Use fullscreen
Game.Initialize(Me, True

Now go to form1_keydown.
Type this in.

If e.KeyCode = Keys.Escape Then
Game.GameOver =
End If

Now, its time to run your app! Before doing so, save all of your work, just in case it crashes. You should get a blue screen :0!

Thats it for this tutorial. The source code is attached below. Mods: (EXEs in Bin folder and Obj folder are removed). It is compatible with VS.NET 2002. 2003 users will find it easy to open it and hit ok when it asks to convert. [Broken External Image]:

- The Pentium Guy


  • Direct3D
    14.9 KB · Views: 71