Search Tools Links Login

DirectX BASICS Tutorial part 2.0


This time I will try and teach Enumeration. Read my first article first before you read this (link given in this tutorial to first one). And learn something. Oh and post lots of feedback (and a vote or two ouldnt go amiss ;) )

Original Author: Coding Genius

Code

xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">






DirectX 8





style='font-weight:normal'>DirectX 8.0


 


style='font-size:14.0pt;mso-bidi-font-size:12.0pt;font-weight:normal'>Contents:


style='font-size:14.0pt;mso-bidi-font-size:12.0pt;font-weight:normal'> 


style='font-size:12.0pt;font-family:Wingdings;color:windowtext;font-weight:
normal;text-decoration:none;text-underline:none'>        
Introduction


style='font-size:12.0pt;font-family:Wingdings;color:windowtext;font-weight:
normal;text-decoration:none;text-underline:none'>        
Enumerate adaptors


style='font-size:12.0pt;font-family:Wingdings;color:windowtext;font-weight:
normal;text-decoration:none;text-underline:none'>        
Hardware
enumeration


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'> 


 


Introduction


 


Before we begin,


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'>1.     
If you haven’t
read my last tutorial then click href="http://www.planetsourcecode.com/xq/ASP/txtCodeId.24086/lngWId.1/qx/vb/scripts/ShowCode.htm">style='text-decoration:none;text-underline:none'>here
and read it
first. It will tell you how to begin and initialise DirectX.


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'>2.     
I will assume a
reasonable knowledge of visual basic so if you don’t have a sound knowledge
then learn something easier and come back in a while.


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'>3.     
So you think you
have a reasonable knowledge of Visual Basic eh! Fine. Reference DirectX and
initialise it using the same set up as my last tutorial (see last tutorial.
DUH!).


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'>4.     
Ok ready to begin
now


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'> 


If you like making games in
Visual Basic, like making cool effects for your apps, wanna make cool 3D
graphics, or are just interested then you have come to the right place, eh
site, eh tutorial, whatever it is. I’m not goanna go on, see the last
tutorial’s intro if you are interested. Infact, you should have already, even
just for the variable names and all.


style='font-size:12.0pt;font-weight:normal;text-decoration:none;text-underline:
none'> 


Enumeration
of the adaptors


style='font-size:12.0pt;font-weight:normal;text-decoration:none;text-underline:
none'> 


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'> Ok
basically, enumeration is a big part of making a DirectX app. It enables you to
find out programmatically what type of graphics and sound cards your users
have, what they can handle, the drivers they use, the software available, how
fast a PC they have and the resolution of the screen. All this is essential to
making a distributable app. After all DirectX (8.0. especially) relies much on
the hardware and driver capabilities.


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'> 


style='font-size:12.0pt;color:windowtext;font-weight:normal;text-decoration:
none;text-underline:none'> First and
most important is to determine what display mode set-up the hardware supports.
This is only concerned with full screen, not windowed mode. First you must
query the available adaptor-device combination for the resolutions it supports and
the hardware capabilities. So, first off I'll show you how to enumerate the
adapters.


style='font-size:12.0pt;mso-bidi-font-size:10.0pt;color:windowtext;font-weight:
normal;text-decoration:none;text-underline:none'> 


'D3D should be a valid reference to a Direct3D8 object.
style='text-align:justify'>'AdapterInfo should be a defined D3DADAPTER_IDENTIFIER8 variablestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='text-align:justify'>Privatestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Sub EnumerateAdapters()style='text-align:justify'> style='text-align:justify'>Dimstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> I style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As Integerstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>, sTemp style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As Stringstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>, J style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As Integerstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='text-align:justify'>'This'll either be 1 or 2style='text-align:justify'> nAdapters = D3D.GetAdapterCountstyle='text-align:justify'> style='text-align:justify'>style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>For I = 0 Tostyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> nAdapters - 1style='text-align:justify'> 'Get the relevant Detailsstyle='text-align:justify'> D3D.GetAdapterIdentifier I, 0, AdapterInfostyle='text-align:justify'> style='text-align:justify'>'Get the name of the current adapter - it's stored as a longstyle='text-align:justify'> 'list of character codes that we need to parse into a string - Don’t ask me why they did it like this.style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> sTemp = "" style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#339966'>'Reset the string ready for our usestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#99CC00'>
style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> 
style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>For J = 0 To 511style='text-align:justify'> style='mso-tab-count:1'> sTemp = sTemp & Chr$(AdapterInfo.Description(J))style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Next Jstyle='text-align:justify'> style='mso-tab-count:1'> style='text-align:justify'> sTemp = Replace(sTemp, Chr$(0), " ")style='text-align:justify'> style='mso-tab-count:1'> cmbAdapters.AddItem sTempstyle='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Next I
End Sub

style='font-size:12.0pt;mso-bidi-font-size:10.0pt;mso-fareast-font-family:"Arial Unicode MS";
color:black;text-decoration:none;text-underline:none'> 


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>Now that we can
identify the adapter we can look at what devices it'll support. Software
renderers are an advanced topic and are unlikely to be relevent yet so we'll
ignore them for now. Instead we'll check if the hardware supports HAL (Hardware
Accelaration). The reference rasterizer should always be present so we'll just
assume that we can access it.


Privatestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Sub EnumerateDevices()
style='text-align:justify'>Onstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Local Errorstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Resume Nextstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#006600'> 'We want to handle the errors...style='text-align:justify'>Dimstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Caps style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As D3DCAPS8style='text-align:justify'> style='text-align:justify'>D3D.GetDeviceCaps cmbAdapters.ListIndex, D3DDEVTYPE_HAL, Capsstyle='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If Err.Number = D3DERR_NOTAVAILABLE style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 'There is no hardware acceleration
style='text-align:justify'> cmbDevice.AddItem "Reference Rasterizer (REF)"style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#006600'> 'Reference device will always be availablestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Else
 cmbDevice.AddItem "Hardware Acceleration (HAL)"
style='text-align:justify'> cmbDevice.AddItem "Reference Rasterizer (REF)"style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#006600'> 'Reference device will always be availablestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>End Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>

style='font-family:"Times New Roman";color:blue'>Endstyle='font-family:"Times New Roman"'> Sub


style='font-family:"Times New Roman"'>

Above
is a slightly sloppy way of detecting hardware support - but it works and is
very simple. If we attempt to read stuff from a HAL device and it doesn't exist
then we'll get the error code "D3DERR_NOTAVAILABLE", at which point
we know that only the REF device is okay to use. The code outputs the results
to a combobox, which you can change if you want...
style='font-family:"Times New Roman"'>


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>The last thing
that we want to be able to check is what display modes are supported. This can
only be done when we've selected an adapter and a device (Add a combobox called
“Cmbres”.


Privatestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Sub EnumerateDispModes(Renderer style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As Longstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>)
style='text-align:justify'>cmbRes.Clearstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#006600'> 'Remove any existing entries...style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='text-align:justify'>Dimstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> I style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As Integerstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>, ModeTemp style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>As D3DDISPLAYMODEstyle='text-align:justify'> style='text-align:justify'>nModes = D3D.GetAdapterModeCount(cmbAdapters.ListIndex)style='text-align:justify'> style='text-align:justify'>Forstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> I = 0 To nModes - 1style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#339966'> 'Cycle through them and collect the data...style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Call D3D.EnumAdapterModes(cmbAdapters.ListIndex, I, ModeTemp)style='text-align:justify'> style='text-align:justify'>'First we parse the modes into two catergories - 16bit and 32bitstyle='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If ModeTemp.Format = D3DFMT_R8G8B8 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Or ModeTemp.Format = D3DFMT_X8R8G8B8 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Or ModeTemp.Format = D3DFMT_A8R8G8B8 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:#339966'>'Check that the device is acceptable and valid...
style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If D3D.CheckDeviceType(cmbAdapters.ListIndex, Renderer, ModeTemp.Format, ModeTemp.Format, style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>False) >= 0 Thenstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> 'then add it to the displayed liststyle='text-align:justify'> cmbRes.AddItem ModeTemp.Width & "x" & ModeTemp.Height & " 32 bit" & " [FMT: " & ModeTemp.Format & "]"style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>End Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Else
 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If D3D.CheckDeviceType(cmbAdapters.ListIndex, Renderer, ModeTemp.Format, ModeTemp.Format, False) >= 0 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 cmbRes.AddItem ModeTemp.Width & "x" & ModeTemp.Height & " 16 bit" & " [FMT: " & ModeTemp.Format & "]"
style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>End Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>End Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> style='text-align:justify'>Nextstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Istyle='text-align:justify'> style='text-align:justify'>cmbRes.ListIndex = cmbRes.ListCount - 1

style='color:blue'>End Sub


style='font-size:22.0pt;mso-bidi-font-size:12.0pt;color:navy'> 


Enumeration
of the Hardware


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>The final
useful part is to determine what features the hardware supports. This is particularly
useful if you want to use a special feature that is hardware dependent and not
very common yet (i.e., at the cutting edge of technology). Capability
enumeration is very simple, but there are 100's (maybe 1000's) or things you
can check for.


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>The first stage
is to actually retrieve the information, this is incredibly simple:style='font-family:"Times New Roman"'>

Now
that we have a structure holding all the information ("Caps") we can
query it for what we would like to know.


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>Following are
some example of how to get information on the hardware capabilities (Add a list
box).


Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Caps.MaxActiveLights = -1 style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 List1.AddItem "Maximum Active Lights: Unlimited"
style='text-align:justify'>Elsestyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>style='text-align:justify'> List1.AddItem "Maximum Active Lights: " & Caps.MaxActiveLightsstyle='text-align:justify'>Endstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If
List1.AddItem "Maximum Point Vertex size: " & Caps.MaxPointSize
style='text-align:justify'>List1.AddItem "Maximum Texture Size: " & Caps.MaxTextureWidth & "x" & Caps.MaxTextureHeightstyle='text-align:justify'>List1.AddItem "Maximum Primatives in one call: " & Caps.MaxPrimitiveCountstyle='text-align:justify'> style='text-align:justify'>Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Caps.TextureCaps And D3DPTEXTURECAPS_SQUAREONLY style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> List1.AddItem "Textures must always be square"
style='text-align:justify'>Endstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If
 
style='text-align:justify'>Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Caps.TextureCaps And D3DPTEXTURECAPS_CUBEMAP style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 List1.AddItem "Device Supports Cube Mapping"
style='text-align:justify'>Endstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>If
 
style='text-align:justify'>Ifstyle='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> Caps.TextureCaps And D3DPTEXTURECAPS_VOLUMEMAP style='font-size:12.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman";
color:blue'>Then
 List1.AddItem "Device Supports Volume Mapping"

style='color:blue'>End If


style='color:blue'> 


 


Again
please leave feedback 9and a vote or two might be quite nice too ;) ) Look out
for future Tutorials and if you want to E mail me then do so href="mailto:thomas.waddell@cableinet.co.uk?subject=DirectX%208.0%20Tut%202.0">here.
CYA ;)





About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 116 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.