ThePentiumGuy
Well-known member
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.
Reference:
Microsoft.DirectX
Microsoft.DirectX.Direct3D
Microsoft.DirectX.Direct3D.D3DX
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
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 = Nothing
Private DP As DisplayMode = Nothing
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]:http://www.computerhelp.forum/x_images/images/smilies/smile.gif .
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
Else
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.
Visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/direct3d/gettingstarted/direct3dsurfaces/surfaceformats.asp
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 http://www.computerhelp.forum/x_images/images/smilies/smile.gif.
The next few lines are simple.
Set to Fullscreen or Windowed
If FullScreen Then
D3Dpp.Windowed = False
Else
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.
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.
Reference:
Microsoft.DirectX
Microsoft.DirectX.Direct3D
Microsoft.DirectX.Direct3D.D3DX
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
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 = Nothing
Private DP As DisplayMode = Nothing
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]:http://www.computerhelp.forum/x_images/images/smilies/smile.gif .
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
Else
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.
Visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/direct3d/gettingstarted/direct3dsurfaces/surfaceformats.asp
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 http://www.computerhelp.forum/x_images/images/smilies/smile.gif.
The next few lines are simple.
Set to Fullscreen or Windowed
If FullScreen Then
D3Dpp.Windowed = False
Else
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: