home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / common / d3dinit.bas < prev    next >
Encoding:
BASIC Source File  |  2000-10-02  |  39.7 KB  |  1,101 lines

  1. Attribute VB_Name = "D3DInit"
  2.  
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. '
  5. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  6. '
  7. '  File:       D3DInit.bas
  8. '  Content:    VB D3DFramework global initialization module
  9. '
  10. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11.  
  12. ' DOC:  Use with
  13. ' DOC:        D3DAnimation.cls
  14. ' DOC:        D3DFrame.cls
  15. ' DOC:        D3DMesh.cls
  16. ' DOC:        D3DSelectDevice.frm (optional)
  17. ' DOC:
  18. ' DOC:  Short list of usefull functions
  19. ' DOC:        D3DUtil_Init                  first call to framework
  20. ' DOC:        D3DUtil_LoadFromFile          loads an x-file
  21. ' DOC:        D3DUtil_SetupDefaultScene     setup a camera lights and materials
  22. ' DOC:        D3DUtil_SetupCamera           point camera
  23. ' DOC:        D3DUtil_SetupMediaPath        set directory to load textures from
  24. ' DOC:        D3DUtil_PresentAll            show graphic on the screen
  25. ' DOC:        D3DUtil_ResizeWindowed        resize for windowed modes
  26. ' DOC:        D3DUtil_ResizeFullscreen      resize to fullscreen mode
  27. ' DOC:        D3DUtil_CreateTextureInPool   create a texture
  28.  
  29. Option Explicit
  30.  
  31. Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef RECT As RECT) As Long
  32. Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef RECT As RECT) As Long
  33. Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Long, ByVal hwndafter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal options As Long) As Long
  34. Private Declare Function SetWindowLongA Lib "user32.dll" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal newVal As Long) As Long
  35. Private Declare Function GetWindowLongA Lib "user32.dll" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  36.  
  37.  
  38. '------------------------------------------------------------------
  39. '  User Defined types
  40. '------------------------------------------------------------------
  41.  
  42. ' DOC: Type that hold info about a display mode
  43. Public Type D3DUTIL_MODEINFO
  44.     lWidth As Long                                  'Screen width in this mode
  45.     lHeight As Long                                 'Screen height in this mode
  46.     format As CONST_D3DFORMAT                       'Pixel format in this mode
  47.     VertexBehavior As CONST_D3DCREATEFLAGS          'Whether this mode does SW or HW vertex processing
  48. End Type
  49.  
  50. ' DOC: Type that hold info about a particular back buffer format
  51. Public Type D3DUTIL_FORMATINFO
  52.     format As CONST_D3DFORMAT
  53.     usage As Long
  54.     bCanDoWindowed As Boolean
  55.     bCanDoFullscreen As Boolean
  56. End Type
  57.  
  58. Public Type D3DUTIL_DEVTYPEINFO
  59.     'List of compatible Modes for this device
  60.     lNumModes As Long
  61.     Modes() As D3DUTIL_MODEINFO
  62.         
  63.     'List of compatible formats for this device
  64.     lNumFormats As Long
  65.     FormatInfo() As D3DUTIL_FORMATINFO
  66.  
  67.     lCurrentMode As Long        'index into modes array of the current mode
  68.     
  69. End Type
  70.  
  71. ' DOC: Type that holds info about adapters installed on the system
  72. Public Type D3DUTIL_ADAPTERINFO
  73.     'Device data
  74.     DeviceType As CONST_D3DDEVTYPE                  'Reference, HAL
  75.     d3dcaps As D3DCAPS8                             'Caps of this device
  76.     sDesc As String                                 'Name of this device
  77.     lCanDoWindowed As Long                          'Whether or not this device can work windowed
  78.                 
  79.     DevTypeInfo(2) As D3DUTIL_DEVTYPEINFO           'format and display mode list for hal=1 for ref=2
  80.     
  81.     d3dai As D3DADAPTER_IDENTIFIER8
  82.             
  83.     DesktopMode As D3DDISPLAYMODE
  84.     
  85.     'CurrentState
  86.     bWindowed As Boolean        'currently in windowed mode
  87.     bReference As Boolean       'currently using reference rasterizer
  88.  
  89.     
  90.     
  91. End Type
  92.  
  93. '------------------------------------------------------------------
  94. ' DOC: Usefull globals
  95. '------------------------------------------------------------------
  96. '
  97. Public g_Adapters() As D3DUTIL_ADAPTERINFO      ' Array of Adapter infos
  98. Public g_lCurrentAdapter As Long                ' current adapter (index into infos)
  99. Public g_lNumAdapters As Long                   ' size of the g_Adapters array
  100. Public g_EnumCallback As Object                 ' object that defines VerifyDevice function
  101.  
  102. Public g_dx As DirectX8                         ' Root objects for DX8
  103. Public g_d3dx As D3DX8                          ' Root objects for d3dx
  104. Public g_d3d As Direct3D8                       ' Root object for d3d
  105. Public g_dev As Direct3DDevice8                 ' D3D device
  106.     
  107.                                                 ' Current state (use as read only)
  108. Public g_d3dCaps As D3DCAPS8                    ' Current caps of g_dev
  109. Public g_devType As CONST_D3DDEVTYPE            ' Current device type (hardware or software)
  110. Public g_behaviorflags As Long                  ' Current VertexProcessing (hardware or software)
  111. Public g_focushwnd As Long                      ' Current focus window handle
  112. Public g_d3dpp As D3DPRESENT_PARAMETERS         ' Current presentation parameters
  113.  
  114.   
  115. Public g_lWindowWidth As Long                   ' backbuffer width of windowed state
  116. Public g_lWindowHeight As Long                  ' backbuffer  height of windowed state
  117. Public g_WindowRect As RECT                     ' size of window (including title bar)
  118.  
  119.  
  120.  
  121. '------------------------------------------------------------------
  122. ' Public Functions
  123. '------------------------------------------------------------------
  124.  
  125. Sub D3DUtil_Destory()
  126.     Dim emptycaps As D3DCAPS8
  127.     Dim emptyrect As RECT
  128.     Dim emptypresent As D3DPRESENT_PARAMETERS
  129.     
  130.     D3DUtil_ReleaseAllTexturesFromPool
  131.     
  132.     Set g_dx = Nothing
  133.     Set g_d3dx = Nothing
  134.     Set g_dev = Nothing
  135.     Set g_EnumCallback = Nothing
  136.     
  137.     g_focushwnd = 0
  138.     g_behaviorflags = 0
  139.     g_lWindowWidth = 0
  140.     g_lWindowHeight = 0
  141.     
  142.     g_d3dCaps = emptycaps
  143.     g_d3dpp = emptypresent
  144.     g_WindowRect = emptyrect
  145.     
  146.     ReDim g_Adapters(0)
  147.     g_lNumAdapters = 0
  148.         
  149. End Sub
  150.  
  151. '-----------------------------------------------------------------------------
  152. 'DOC: D3DUtil_Init
  153. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  154. 'DOC:   Direc3DDevice8.
  155. 'DOC:
  156. 'DOC: Params:
  157. 'DOC:   bWindowed       Start in full screen or windowed mode
  158. 'DOC:
  159. 'DOC:   hwnd            starting hwnd to display graphics in
  160. 'DOC:                   This can be changed with a call to Reset if drawing
  161. 'DOC:                   to multiple windows
  162. 'DOC:                   For full screen operation the hwnd will have to belong
  163. 'DOC:                   to a top level window
  164. 'DOC:
  165. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  166. 'DOC:
  167. 'DOC:   ModeIndex       Ignored if bWindowed is TRUE
  168. 'DOC:                   Otherwise and index into the g_adapters(AdapterIndex).Modes
  169. 'DOC:                   array for a given width height and backbuffer format
  170. 'DOC:                   Use D3DUtil_FindDisplayMode to obtain an mode index given
  171. 'DOC:                   a desired height width and backbuffer format
  172. 'DOC:
  173. 'DOC:   CallbackObject  Can be Nothing or an object that has implemented
  174. 'DOC:                   VerifyDevice(usageflags as long ,format as CONST_D3DFORMAT)
  175. 'DOC:
  176. 'DOC:
  177. 'DOC: Remarks:
  178. 'DOC:   caps for the device are passed to VerifyDevice in the g_d3dcaps global
  179. 'DOC:
  180. '-----------------------------------------------------------------------------
  181.  
  182. Public Function D3DUtil_Init(hwnd As Long, bWindowed As Boolean, AdapterIndex As Long, modeIndex As Long, devtype As CONST_D3DDEVTYPE, CallbackObject As Object) As Boolean
  183.     
  184.     On Local Error GoTo errOut
  185.  
  186.     ' Initialize the DirectX8 and d3dx8 objects
  187.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  188.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  189.     
  190.     ' Create the Direct3D object
  191.     Set g_d3d = g_dx.Direct3DCreate
  192.  
  193.     ' Call the sub that builds a list of available adapters,
  194.     ' adapter device types, and display modes
  195.     Call D3DEnum_BuildAdapterList(CallbackObject)
  196.     
  197.     If bWindowed Then
  198.         D3DUtil_Init = D3DUtil_InitWindowed(hwnd, AdapterIndex, devtype, True)
  199.     Else
  200.         D3DUtil_Init = D3DUtil_InitFullscreen(hwnd, AdapterIndex, modeIndex, devtype, True)
  201.     End If
  202.     
  203.     Exit Function
  204.     
  205. errOut:
  206.     Debug.Print "Failed D3DUtil_Init"
  207. End Function
  208.  
  209.  
  210. '-----------------------------------------------------------------------------
  211. 'DOC: D3DUtil_InitWindowed
  212. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  213. 'DOC:   Direc3DDevice8.
  214. 'DOC:
  215. 'DOC: Params:
  216. 'DOC:
  217. 'DOC:   hwnd            starting hwnd to display graphics in
  218. 'DOC:                   This can be changed with a call to Reset if drawing
  219. 'DOC:                   to multiple windows
  220. 'DOC:                   For full screen operation the hwnd will have to belong
  221. 'DOC:                   to a top level window
  222. 'DOC:
  223. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  224. 'DOC:
  225. 'DOC:   DevType         Indicates if user wants HAL (hardware) or REF (software) rendering
  226. 'DOC:
  227. 'DOC:   bTryFallbacks   True if wants function to attempt to fallback to the reference device
  228. 'DOC:                   on faulure and display dialogs to that effect
  229. 'DOC:
  230. '-----------------------------------------------------------------------------
  231.  
  232. Function D3DUtil_InitWindowed(hwnd As Long, AdapterIndex As Long, devtype As CONST_D3DDEVTYPE, bTryFallbacks As Boolean) As Boolean
  233.    
  234.     On Error GoTo errOut
  235.     
  236.         
  237.     
  238.     'save the current adapter
  239.     g_lCurrentAdapter = AdapterIndex
  240.     
  241.     Dim d3ddm As D3DDISPLAYMODE
  242.  
  243.     ' Initialize the present parameters structure
  244.     ' to use 1 back buffer and a 16 bit depth buffer
  245.     ' change the autoDepthStencilFormat if you need stencil bits
  246.     With g_d3dpp
  247.         .BackBufferCount = 1
  248.         .EnableAutoDepthStencil = 1
  249.         .AutoDepthStencilFormat = D3DFMT_D16
  250.         .hDeviceWindow = hwnd
  251.     End With
  252.         
  253.     g_focushwnd = hwnd
  254.     
  255.     Dim rc As RECT
  256.     Call GetClientRect(g_focushwnd, rc)
  257.     g_lWindowWidth = rc.Right - rc.Left
  258.     g_lWindowHeight = rc.bottom - rc.Top
  259.     Call GetWindowRect(g_focushwnd, g_WindowRect)
  260.     
  261.     
  262.     With g_Adapters(g_lCurrentAdapter)
  263.     
  264.         ' If running windowed, set the current desktop format
  265.         ' as the format the device will use.
  266.         ' note the the current mode and backbuffer width and height
  267.         ' information is ignored by d3d
  268.         Call g_d3d.GetAdapterDisplayMode(g_lCurrentAdapter, d3ddm)
  269.         
  270.         ' figure out if this format supports hardware acceleration
  271.         ' by looking it up in our format list
  272.         g_behaviorflags = D3DEnum_FindInFormatList(g_lCurrentAdapter, devtype, d3ddm.format)
  273.         If g_behaviorflags <= 0 Then g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  274.         
  275.         
  276.         g_d3dpp.BackBufferFormat = d3ddm.format
  277.         g_d3dpp.BackBufferWidth = 0
  278.         g_d3dpp.BackBufferHeight = 0
  279.         
  280.         g_d3dpp.Windowed = 1
  281.         g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD
  282.         
  283.                 
  284.         .bWindowed = True
  285.         .DeviceType = devtype
  286.         
  287.         g_devType = devtype
  288.        
  289.                 
  290.         
  291.         
  292.     End With
  293.             
  294.     'Try to create the device now that we have everything set.
  295.     On Local Error Resume Next
  296.     Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, devtype, g_focushwnd, g_behaviorflags, g_d3dpp)
  297.  
  298.     If Err.Number Then
  299.  
  300.         If bTryFallbacks = False Then Exit Function
  301.         
  302.         'If a HAL device was being attempted, try again with a REF device instead.
  303.         If g_devType = D3DDEVTYPE_HAL Then
  304.             Err.Clear
  305.  
  306.             'Make sure the user knows that this is less than an optimal 3D environment.
  307.             MsgBox "No hardware support found. Switching to reference rasterizer.", vbInformation
  308.             
  309.             'reset our variable to use ref
  310.             g_Adapters(g_lCurrentAdapter).DeviceType = D3DDEVTYPE_REF
  311.             g_Adapters(g_lCurrentAdapter).bReference = True
  312.             g_devType = D3DDEVTYPE_REF
  313.             Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, D3DDEVTYPE_REF, g_focushwnd, g_behaviorflags, g_d3dpp)
  314.             
  315.         End If
  316.             
  317.     End If
  318.  
  319.     If Err.Number Then
  320.         
  321.         'The app still hit an error. Both HAL and REF devices weren't created. The app will have to exit at this point.
  322.         MsgBox "No suitable device was found to initialize D3D. Application will now exit.", vbCritical
  323.         D3DUtil_InitWindowed = False
  324.         End
  325.         Exit Function
  326.  
  327.     End If
  328.  
  329.     'update our device caps data
  330.     g_dev.GetDeviceCaps g_d3dCaps
  331.     
  332.     'set any state we need to initialize
  333.     D3DXMatrixIdentity g_identityMatrix
  334.     
  335.     'set the reference flag if we choose a ref device
  336.     With g_Adapters(g_lCurrentAdapter)
  337.         If .DeviceType = D3DDEVTYPE_REF Then
  338.             .bReference = True
  339.         Else
  340.             .bReference = False
  341.         End If
  342.     End With
  343.     
  344.     D3DUtil_InitWindowed = True
  345.     Exit Function
  346.     
  347. errOut:
  348.     Debug.Print "Failed D3DUtil_Init"
  349.  
  350. End Function
  351.  
  352.  
  353. '-----------------------------------------------------------------------------
  354. 'DOC: D3DUtil_InitFullscreen
  355. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  356. 'DOC:   Direc3DDevice8.
  357. 'DOC:
  358. 'DOC: Params:
  359. 'DOC:
  360. 'DOC:   hwnd            starting hwnd to display graphics in
  361. 'DOC:                   This can be changed with a call to Reset if drawing
  362. 'DOC:                   to multiple windows
  363. 'DOC:                   For full screen operation the hwnd will have to belong
  364. 'DOC:                   to a top level window
  365. 'DOC:
  366. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  367. 'DOC:
  368. 'DOC:   ModeIndex       index into the g_adapters(AdapterIndex).Modes for width
  369. 'DOC:                   height and format
  370. 'DOC:
  371. 'DOC:   DevType         Indicates if user wants HAL (hardware) or REF (software) rendering
  372. 'DOC:
  373. 'DOC:   bTryFallbacks   True if wants function to attempt to fallback to the reference device
  374. 'DOC:                   on faulure and display dialogs to that effect
  375. 'DOC:
  376. '-----------------------------------------------------------------------------
  377. Function D3DUtil_InitFullscreen(hwnd As Long, AdapterIndex As Long, modeIndex As Long, devtype As CONST_D3DDEVTYPE, bTryFallbacks As Boolean) As Boolean
  378.     
  379.     On Error GoTo errOut
  380.     
  381.     Dim ModeInfo As D3DUTIL_MODEINFO
  382.  
  383.     Dim rc As RECT
  384.     
  385.     'save the current adapter
  386.     g_lCurrentAdapter = AdapterIndex
  387.         
  388.     
  389.     g_focushwnd = hwnd
  390.     
  391.     
  392.     'switching from windowed to fullscreen so save height and width
  393.     If g_d3dpp.Windowed = 1 Then
  394.         Call GetClientRect(g_focushwnd, rc)
  395.         g_lWindowWidth = rc.Right - rc.Left
  396.         g_lWindowHeight = rc.bottom - rc.Top
  397.         Call GetWindowRect(g_focushwnd, g_WindowRect)
  398.     End If
  399.  
  400.     
  401.     ' Initialize the present parameters structure
  402.     ' to use 1 back buffer and a 16 bit depth buffer
  403.     ' change the autoDepthStencilFormat if you need stencil bits
  404.     With g_d3dpp
  405.         .BackBufferCount = 1
  406.         .EnableAutoDepthStencil = 1
  407.         .AutoDepthStencilFormat = D3DFMT_D16
  408.         .hDeviceWindow = g_focushwnd
  409.     End With
  410.     
  411.     
  412.     
  413.     
  414.     With g_Adapters(g_lCurrentAdapter)
  415.             
  416.         With .DevTypeInfo(devtype)
  417.             ModeInfo = .Modes(modeIndex)
  418.             g_behaviorflags = .Modes(modeIndex).VertexBehavior
  419.             
  420.             g_d3dpp.BackBufferWidth = ModeInfo.lWidth
  421.             g_d3dpp.BackBufferHeight = ModeInfo.lHeight
  422.             g_d3dpp.BackBufferFormat = ModeInfo.format
  423.             g_d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP
  424.             g_d3dpp.Windowed = 0
  425.             .lCurrentMode = modeIndex
  426.         End With
  427.              
  428.  
  429.         .bWindowed = False
  430.         .DeviceType = devtype
  431.         
  432.         
  433.         If g_behaviorflags = 0 Then g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  434.         g_devType = devtype
  435.         
  436.     End With
  437.             
  438.     'Try to create the device now that we have everything set.
  439.     On Local Error Resume Next
  440.     Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, devtype, g_focushwnd, g_behaviorflags, g_d3dpp)
  441.  
  442.     If Err.Number Then
  443.         If bTryFallbacks = False Then Exit Function
  444.  
  445.         'If a HAL device was being attempted, try again with a REF device instead.
  446.         If g_devType = D3DDEVTYPE_HAL Then
  447.             Err.Clear
  448.  
  449.             'Make sure the user knows that this is less than an optimal 3D environment.
  450.             MsgBox "No hardware support found. Switching to reference rasterizer.", vbInformation
  451.             
  452.             'reset our variable to use ref
  453.             g_Adapters(g_lCurrentAdapter).DeviceType = D3DDEVTYPE_REF
  454.             g_devType = D3DDEVTYPE_REF
  455.             Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, D3DDEVTYPE_REF, g_focushwnd, g_behaviorflags, g_d3dpp)
  456.             
  457.         End If
  458.             
  459.     End If
  460.  
  461.  
  462.     If Err.Number Then
  463.         
  464.         'The app still hit an error. Both HAL and REF devices weren't created. The app will have to exit at this point.
  465.         MsgBox "No suitable device was found to initialize D3D. Application will now exit.", vbCritical
  466.         D3DUtil_InitFullscreen = False
  467.         Exit Function
  468.  
  469.     End If
  470.  
  471.     'update our device caps data
  472.     g_dev.GetDeviceCaps g_d3dCaps
  473.     
  474.     'set any state we need to initialize
  475.     D3DXMatrixIdentity g_identityMatrix
  476.     
  477.     'set the reference flag if we choose a ref device
  478.     With g_Adapters(g_lCurrentAdapter)
  479.         If .DeviceType = D3DDEVTYPE_REF Then
  480.            .bReference = True
  481.         Else
  482.            .bReference = False
  483.         End If
  484.     End With
  485.     D3DUtil_InitFullscreen = True
  486.     Exit Function
  487.     
  488. errOut:
  489.     Debug.Print "Failed D3DUtil_InitFullscreen"
  490. End Function
  491.  
  492.  
  493. '-----------------------------------------------------------------------------
  494. 'DOC: D3DUtil_ResetWindowed
  495. 'DOC:
  496. 'DOC: Remarks
  497. 'DOC:   Used to move out of Fullscreen mode to windowed mode with out changing
  498. 'DOC:   the current device
  499. '-----------------------------------------------------------------------------
  500. Function D3DUtil_ResetWindowed() As Long
  501.         Dim ws As Long
  502.         Dim d3dppnew As D3DPRESENT_PARAMETERS
  503.         
  504.         On Local Error GoTo errOut
  505.         
  506.         d3dppnew.Windowed = 1
  507.         d3dppnew.BackBufferFormat = g_Adapters(g_lCurrentAdapter).DesktopMode.format
  508.         d3dppnew.EnableAutoDepthStencil = g_d3dpp.EnableAutoDepthStencil
  509.         d3dppnew.AutoDepthStencilFormat = g_d3dpp.AutoDepthStencilFormat
  510.         d3dppnew.SwapEffect = D3DSWAPEFFECT_DISCARD
  511.         d3dppnew.hDeviceWindow = g_d3dpp.hDeviceWindow
  512.         
  513.         g_dev.Reset d3dppnew
  514.         
  515.         g_d3dpp = d3dppnew
  516.         
  517.         Const GWL_EXSTYLE = -20
  518.         Const GWL_STYLE = -16
  519.         Const WS_EX_TOPMOST = 8
  520.         Const HWND_NOTOPMOST = -2
  521.             
  522.                 
  523.         With g_WindowRect
  524.             Call SetWindowPos(g_focushwnd, HWND_NOTOPMOST, .Left, .Top, .Right - .Left, .bottom - .Top, 0)
  525.             ws = GetWindowLongA(g_focushwnd, GWL_STYLE)
  526.             If (ws And WS_EX_TOPMOST) = WS_EX_TOPMOST Then
  527.                 ws = ws - WS_EX_TOPMOST
  528.                 Call SetWindowLongA(g_focushwnd, GWL_STYLE, ws)
  529.             End If
  530.         End With
  531.         
  532.         
  533.         DoEvents
  534.         D3DUtil_ResetWindowed = 0
  535.         Exit Function
  536. errOut:
  537.     D3DUtil_ResetWindowed = Err.Number
  538.     Debug.Print "err in ResetWindow"
  539. End Function
  540.  
  541.  
  542. '-----------------------------------------------------------------------------
  543. 'DOC: D3DUtil_ResetFullscreen
  544. 'DOC:
  545. 'DOC: Remarks
  546. 'DOC:   Used to to toggle from windowed mode to the current fullscreen mode
  547. 'DOC:   Without changing the current device
  548. '-----------------------------------------------------------------------------
  549. Function D3DUtil_ResetFullscreen() As Long
  550.         Dim hr As Long
  551.         Dim lMode As Long
  552.         Dim rc As RECT
  553.         Dim devtype As CONST_D3DDEVTYPE
  554.         On Local Error GoTo errOut
  555.         If g_d3dpp.Windowed = 1 Then
  556.             Call GetClientRect(g_focushwnd, rc)
  557.             g_lWindowWidth = rc.Right - rc.Left
  558.             g_lWindowHeight = rc.bottom - rc.Top
  559.             Call GetWindowRect(g_focushwnd, g_WindowRect)
  560.         End If
  561.         
  562.         devtype = g_Adapters(g_lCurrentAdapter).DeviceType
  563.         With g_Adapters(g_lCurrentAdapter).DevTypeInfo(devtype)
  564.             g_d3dpp.Windowed = 0
  565.             g_d3dpp.BackBufferWidth = .Modes(.lCurrentMode).lWidth
  566.             g_d3dpp.BackBufferHeight = .Modes(.lCurrentMode).lHeight
  567.             g_d3dpp.BackBufferFormat = .Modes(.lCurrentMode).format
  568.         End With
  569.         
  570.         g_dev.Reset g_d3dpp
  571. errOut:
  572.         D3DUtil_ResetFullscreen = Err.Number
  573. End Function
  574.  
  575. '-----------------------------------------------------------------------------
  576. 'DOC: D3DEnum_BuildAdapterList
  577. 'DOC:   Used to intialzed a list of valid adapters and display modes
  578. 'DOC:
  579. 'DOC: Params:
  580. 'DOC:   EnumCallback    - can be Nothing or an object that has implemented
  581. 'DOC:                     VerifyDevice(usageflags as long ,format as CONST_D3DFORMAT)
  582. 'DOC:                     ussgeflags can be
  583. 'DOC:                           D3DCREATE_SOFTWARE_VERTEXPROCESSING
  584. 'DOC:                           D3DCREATE_HARDWARE_VERTEXPROCESSING
  585. 'DOC: Remarks:
  586. 'DOC:   caps for the device are passed to VerifyDevice in the g_d3dcaps global
  587. 'DOC:
  588. '-----------------------------------------------------------------------------
  589. Public Function D3DEnum_BuildAdapterList(EnumCallback As Object) As Boolean
  590.     
  591.     On Local Error GoTo errOut
  592.     
  593.     Dim lAdapter As Long
  594.         
  595.     ' empty the list
  596.     Call D3DEnum_Cleanup
  597.             
  598.     ' create d3d and dx objects if not already created
  599.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  600.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  601.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  602.     
  603.     ' save callback
  604.     Set g_EnumCallback = EnumCallback
  605.     
  606.     ' Make space for new adapter
  607.     g_lNumAdapters = g_d3d.GetAdapterCount
  608.     ReDim g_Adapters(g_lNumAdapters)
  609.     
  610.     ' Loop through all the adapters on the system
  611.     For lAdapter = 0 To g_lNumAdapters - 1
  612.     
  613.         ' build a list of valid backbuffer formats
  614.         D3DEnum_BuildValidFormatList lAdapter, D3DDEVTYPE_HAL
  615.         D3DEnum_BuildValidFormatList lAdapter, D3DDEVTYPE_REF
  616.         
  617.                 
  618.         ' build a list of valid display modes for those formats
  619.         D3DEnum_BuildDisplayModeList lAdapter, D3DDEVTYPE_HAL
  620.         D3DEnum_BuildDisplayModeList lAdapter, D3DDEVTYPE_REF
  621.         
  622.         ' get the adapter identifier
  623.         g_d3d.GetAdapterIdentifier lAdapter, 0, g_Adapters(lAdapter).d3dai
  624.         
  625.     Next
  626.     
  627.     D3DEnum_BuildAdapterList = True
  628.     Exit Function
  629.     
  630. errOut:
  631.     Debug.Print "Failed D3DEnum_BuildAdapterList"
  632. End Function
  633.  
  634.  
  635.  
  636. '-----------------------------------------------------------------------------
  637. 'DOC:  D3DUtil_ResizeWindowed
  638. 'DOC:
  639. 'DOC:  Paramters
  640. 'DOC:       hWnd device window
  641. 'DOCL
  642. 'DOC:  Remarks
  643. 'DOC:       use when already in windowed mode to resize the backbuffer
  644. 'DOC:       do not use to switch from fullscreen to windowed mode
  645. '-----------------------------------------------------------------------------
  646. Function D3DUtil_ResizeWindowed(hwnd As Long) As Boolean
  647.     On Local Error GoTo errOut
  648.     
  649.     Dim d3dpp As D3DPRESENT_PARAMETERS
  650.     Dim rc As RECT
  651.     
  652.     d3dpp = g_d3dpp
  653.     
  654.     If d3dpp.Windowed = 0 Then Exit Function
  655.     
  656.     g_focushwnd = hwnd
  657.     Call GetClientRect(g_focushwnd, rc)
  658.     g_lWindowWidth = rc.Right - rc.Left
  659.     g_lWindowHeight = rc.bottom - rc.Top
  660.     Call GetWindowRect(g_focushwnd, g_WindowRect)
  661.     
  662.         
  663.     
  664.     d3dpp.BackBufferWidth = 0 'g_lWindowWidth
  665.     d3dpp.BackBufferHeight = 0 'g_lWindowHeight
  666.     d3dpp.hDeviceWindow = hwnd
  667.     d3dpp.Windowed = 1
  668.                
  669.     g_dev.Reset d3dpp
  670.     
  671.     g_d3dpp = d3dpp
  672.     
  673.     
  674.     g_Adapters(g_lCurrentAdapter).bWindowed = True
  675.  
  676.     D3DUtil_ResizeWindowed = True
  677.     Exit Function
  678.     
  679. errOut:
  680.     Debug.Print "D3DUtil_ResizeWindowed failed - make sure width and height are in pixels"
  681. End Function
  682.  
  683.  
  684. '-----------------------------------------------------------------------------
  685. 'DOC:  D3DUtil_ResizeFullscreen
  686. 'DOC:
  687. 'DOC:  Paramters
  688. 'DOC:       hWnd device window
  689. 'DOC:       modeIndex index into Modes list
  690. 'DOC:
  691. 'DOC:  Remarks
  692. 'DOC:       D3DUtil_Init or D3DEnum_BuildAdapterList must have been called
  693. 'DOC:       prior to call D3DUtil_ResizeFullscreen
  694. 'DOC:       Use this method when moving from windowed mode to fullscreen
  695. 'DOC:       on the current device
  696. 'DOC:       Note that all device state is lost and that the caller
  697. 'DOC:       will need to call ther RestoreDeviceObjects function
  698. 'DOC:
  699. '-----------------------------------------------------------------------------
  700. Function D3DUtil_ResizeFullscreen(hwnd As Long, modeIndex As Long) As Boolean
  701.     On Local Error GoTo errOut
  702.     
  703.     Dim d3dpp As D3DPRESENT_PARAMETERS
  704.     Dim devtype As CONST_D3DDEVTYPE
  705.     Dim prevmode As Long
  706.     
  707.     'let ResizeWindowed know we are trying to go fullscreen
  708.     prevmode = g_d3dpp.Windowed
  709.     g_d3dpp.Windowed = 0
  710.         
  711.     devtype = g_Adapters(g_lCurrentAdapter).DeviceType
  712.     With g_Adapters(g_lCurrentAdapter).DevTypeInfo(devtype).Modes(modeIndex)
  713.         d3dpp.BackBufferWidth = .lWidth
  714.         d3dpp.BackBufferHeight = .lHeight
  715.         d3dpp.BackBufferFormat = .format
  716.         d3dpp.hDeviceWindow = hwnd
  717.         d3dpp.AutoDepthStencilFormat = g_d3dpp.AutoDepthStencilFormat
  718.         d3dpp.EnableAutoDepthStencil = g_d3dpp.EnableAutoDepthStencil
  719.         d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP
  720.         d3dpp.Windowed = 0
  721.     End With
  722.     
  723.     g_dev.Reset d3dpp
  724.     
  725.     g_d3dpp = d3dpp
  726.     
  727.     'reset succeeded so set new behavior flags
  728.     With g_Adapters(g_lCurrentAdapter)
  729.         g_behaviorflags = .DevTypeInfo(devtype).Modes(modeIndex).VertexBehavior
  730.         .bWindowed = False
  731.     End With
  732.     
  733.     D3DUtil_ResizeFullscreen = True
  734.     Exit Function
  735.     
  736. errOut:
  737.     'we where unsuccessfull in going fullscreen
  738.     'indicate we are still in previous mode
  739.     g_d3dpp.Windowed = prevmode
  740.     Debug.Print "D3DUtil_ResizeWindowed failed - make sure width and height are in pixels"
  741. End Function
  742.  
  743.  
  744. '-----------------------------------------------------------------------------
  745. 'DOC: D3DUtil_DefaultInitWindowed
  746. 'DOC:   Used to intialzed D3DUtil device in a windowed mode
  747. 'DOC  Params:
  748. 'DOC:   iAdapter    DisplayAdapter ordinal
  749. 'DOC:   hwnd        Display hwnd
  750. 'DOC: Remarks:
  751. 'DOC:
  752. 'DOC:   Users can initialze the g_d3d and g_dev objects themselves
  753. 'DOC:   and not use this function be sure to initialize
  754. 'DOC:       g_iAdapter, g_devType,g_behaviorFlags,g_focushwnd,g_presentParams
  755. 'DOC:
  756. 'DOC:   This function defaults to using SOFTWARE_VERTEXPROCESSING
  757. 'DOC:   and requires HAL 3d support
  758. '-----------------------------------------------------------------------------
  759.  
  760. Public Function D3DUtil_DefaultInitWindowed(iAdapter As Long, hwnd As Long) As Boolean
  761.     On Local Error GoTo errOut
  762.     
  763.     Dim emptyparams As D3DPRESENT_PARAMETERS
  764.     
  765.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  766.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  767.     
  768.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  769.     
  770.     
  771.     g_lCurrentAdapter = iAdapter
  772.     g_devType = D3DDEVTYPE_HAL
  773.     g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  774.     g_focushwnd = hwnd
  775.     g_d3dpp = emptyparams
  776.     
  777.     Dim dm As D3DDISPLAYMODE
  778.     
  779.     g_d3d.GetAdapterDisplayMode iAdapter, dm
  780.     
  781.     
  782.     With g_d3dpp
  783.         .BackBufferFormat = dm.format
  784.         .EnableAutoDepthStencil = 1 'TRUE
  785.         .AutoDepthStencilFormat = D3DFMT_D16
  786.         .Windowed = 1   'TRUE
  787.         .SwapEffect = D3DSWAPEFFECT_DISCARD
  788.     End With
  789.             
  790.             
  791.     Set g_dev = g_d3d.CreateDevice(iAdapter, g_devType, g_focushwnd, g_behaviorflags, g_d3dpp)
  792.     
  793.     g_dev.GetDeviceCaps g_d3dCaps
  794.     
  795.     D3DUtil_DefaultInitWindowed = True
  796.     Exit Function
  797.     
  798. errOut:
  799. End Function
  800.  
  801.  
  802. '-----------------------------------------------------------------------------
  803. 'DOC: D3DUtil_DefaultInitFullscreen
  804. 'DOC:   Used to intialzed D3DUtil device in a windowed mode
  805. 'DOC  Params:
  806. 'DOC:   iAdapter    DisplayAdapter ordinal
  807. 'DOC:   hwnd        Display hwnd
  808. 'DOC:   w           width
  809. 'DOC    h           height
  810. 'DOC    fmt         desired format
  811. 'DOC: Remarks:
  812. 'DOC:
  813. 'DOC:   Users can initialze the g_d3d and g_dev objects themselves
  814. 'DOC:   and not use this function be sure to initialize
  815. 'DOC:       g_iAdapter, g_devType,g_behaviorFlags,g_focushwnd,g_presentParams
  816. 'DOC:
  817. 'DOC:   This function defaults to using SOFTWARE_VERTEXPROCESSING
  818. 'DOC:   and requires HAL 3d support
  819. '-----------------------------------------------------------------------------
  820.  
  821. Public Function D3DUtil_DefaultInitFullscreen(iAdapter As Long, hwnd As Long, w As Long, h As Long, fmt As CONST_D3DFORMAT) As Boolean
  822.     On Local Error GoTo errOut
  823.     
  824.     Dim emptyparams As D3DPRESENT_PARAMETERS
  825.     
  826.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  827.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  828.     
  829.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  830.     
  831.     
  832.     g_lCurrentAdapter = iAdapter
  833.     g_devType = D3DDEVTYPE_HAL
  834.     g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  835.     g_focushwnd = hwnd
  836.     g_d3dpp = emptyparams
  837.     
  838.     Dim dm As D3DDISPLAYMODE
  839.     
  840.     g_d3d.GetAdapterDisplayMode iAdapter, dm
  841.     
  842.     
  843.     With g_d3dpp
  844.         .BackBufferFormat = fmt
  845.         .EnableAutoDepthStencil = 1 'TRUE
  846.         .AutoDepthStencilFormat = D3DFMT_D16
  847.         .BackBufferWidth = w
  848.         .BackBufferHeight = h
  849.         .Windowed = 0   'FALSE
  850.         .SwapEffect = D3DSWAPEFFECT_FLIP
  851.     End With
  852.             
  853.             
  854.     Set g_dev = g_d3d.CreateDevice(iAdapter, g_devType, g_focushwnd, g_behaviorflags, g_d3dpp)
  855.     
  856.     g_dev.GetDeviceCaps g_d3dCaps
  857.     
  858.     D3DUtil_DefaultInitFullscreen = True
  859.     Exit Function
  860.     
  861. errOut:
  862. End Function
  863.  
  864.  
  865.  
  866.  
  867.  
  868. '-----------------------------------------------------------------------------
  869. 'DOC: D3DEnum_Cleanup
  870. 'DOC:   Used to release any reference to the callback object passed in
  871. 'DOC:   and deallocate memory
  872. 'DOC: Params:
  873. 'DOC:   none
  874. 'DOC: Remarks:
  875. 'DOC:   none
  876. '-----------------------------------------------------------------------------
  877. Public Sub D3DEnum_Cleanup()
  878.     Set g_EnumCallback = Nothing
  879.     ReDim g_Adapters(0)
  880. End Sub
  881.  
  882.  
  883. '------------------------------------------------------------------
  884. ' Private Functions
  885. '------------------------------------------------------------------
  886.  
  887. '-----------------------------------------------------------------------------
  888. ' D3DEnum_BuildValidFormatList
  889. '-----------------------------------------------------------------------------
  890. Private Sub D3DEnum_BuildValidFormatList(lAdapter As Long, devtype As CONST_D3DDEVTYPE)
  891.                         
  892.         Dim lMode As Long
  893.         Dim lUsage As Long
  894.         Dim NumModes As Long
  895.         Dim DisplayMode As D3DDISPLAYMODE
  896.         Dim bCanDoWindowed As Boolean
  897.         Dim bCanDoFullscreen As Boolean
  898.                 
  899.         
  900.         With g_Adapters(lAdapter).DevTypeInfo(devtype)
  901.         
  902.             ' Reset the number of available formats to
  903.             .lNumFormats = 0
  904.         
  905.             ' Get the number of display modes
  906.             ' a display mode is a size and format (ie 640x480 X8R8G8B8 60hz)
  907.             NumModes = g_d3d.GetAdapterModeCount(lAdapter)
  908.             ReDim .FormatInfo(NumModes)
  909.                                 
  910.             ' Loop through all the display modes
  911.             For lMode = 0 To NumModes - 1
  912.                     
  913.                 ' Get information about this adapter in all the modes it supports
  914.                 Call g_d3d.EnumAdapterModes(lAdapter, lMode, DisplayMode)
  915.                                 
  916.                 ' See if the format is already in our format list
  917.                 If -1 <> D3DEnum_FindInFormatList(lAdapter, devtype, DisplayMode.format) Then GoTo Continue
  918.                                     
  919.                 ' Check the compatiblity of the format
  920.                 
  921.                 lUsage = D3DEnum_CheckFormatCompatibility(lAdapter, devtype, DisplayMode.format, bCanDoWindowed, bCanDoFullscreen)
  922.                                                                             
  923.                 ' Usage will come back -1 if VerifyDevice reject format
  924.                 If -1 = lUsage Then GoTo Continue
  925.                 
  926.                 ' Add the valid format and ussage
  927.                 .FormatInfo(.lNumFormats).format = DisplayMode.format
  928.                 .FormatInfo(.lNumFormats).usage = lUsage
  929.                 .FormatInfo(.lNumFormats).bCanDoWindowed = bCanDoWindowed
  930.                 .FormatInfo(.lNumFormats).bCanDoFullscreen = bCanDoFullscreen
  931.                 .lNumFormats = .lNumFormats + 1
  932.  
  933.                                 
  934. Continue:
  935.             Next
  936.             
  937.         End With
  938.  
  939. End Sub
  940.  
  941.  
  942. '-----------------------------------------------------------------------------
  943. ' D3DEnum_BuildDisplayModeList
  944. '-----------------------------------------------------------------------------
  945. Private Sub D3DEnum_BuildDisplayModeList(lAdapter As Long, devtype As CONST_D3DDEVTYPE)
  946.                         
  947.         Dim lMode As Long
  948.         Dim NumModes As Long
  949.         Dim DisplayMode As D3DDISPLAYMODE
  950.  
  951.         With g_Adapters(lAdapter).DevTypeInfo(devtype)
  952.         
  953.             ' Reset the number of validated display modes to 0
  954.             .lNumModes = 0
  955.             
  956.             ' Get the number of display modes
  957.             ' Note this list includes refresh rates
  958.             ' a display mode is a size and format (ie 640x480 X8R8G8B8 60hz)
  959.             NumModes = g_d3d.GetAdapterModeCount(lAdapter)
  960.  
  961.             ' Allocate space for our mode list
  962.             ReDim .Modes(NumModes)
  963.  
  964.             ' Save the format of the desktop for windowed operation
  965.             Call g_d3d.GetAdapterDisplayMode(lAdapter, g_Adapters(lAdapter).DesktopMode)
  966.                                 
  967.             ' Loop through all the display modes
  968.             For lMode = 0 To NumModes - 1
  969.                     
  970.                 ' Get information about this adapter in all the modes it supports
  971.                 Call g_d3d.EnumAdapterModes(lAdapter, lMode, DisplayMode)
  972.                 
  973.                 ' filter out low resolution modes
  974.                 If DisplayMode.width < 640 Or DisplayMode.height < 400 Then GoTo Continue
  975.                 
  976.                 ' filter out modes allready in the list
  977.                 ' that might differ only in refresh rate
  978.                 If -1 <> D3DEnum_FindInDisplayModeList(lAdapter, devtype, DisplayMode) Then GoTo Continue
  979.                 
  980.                 
  981.                 ' filter out modes with formats that arent confirmed to work
  982.                 ' see BuildFormatList and ConfirmFormatList
  983.                 If -1 = D3DEnum_FindInFormatList(lAdapter, devtype, DisplayMode.format) Then GoTo Continue
  984.                                                 
  985.                 ' At this point, the modes format has been validated,
  986.                 ' is not a duplicate refresh rate, and not a low res mode
  987.                 ' Add the mode to the list of working modes for the adapter
  988.                 .Modes(.lNumModes).lHeight = DisplayMode.height
  989.                 .Modes(.lNumModes).lWidth = DisplayMode.width
  990.                 .Modes(.lNumModes).format = DisplayMode.format
  991.                 .lNumModes = .lNumModes + 1
  992.                                             
  993. Continue:
  994.             Next
  995.             
  996.         End With
  997.  
  998. End Sub
  999.  
  1000. '-----------------------------------------------------------------------------
  1001. ' D3DEnum_FindInDisplayModeList
  1002. '-----------------------------------------------------------------------------
  1003. Private Function D3DEnum_FindInDisplayModeList(lAdapter As Long, devtype As CONST_D3DDEVTYPE, DisplayMode As D3DDISPLAYMODE) As Long
  1004.     
  1005.     Dim lMode As Long
  1006.     Dim NumModes As Long
  1007.     
  1008.     NumModes = g_Adapters(lAdapter).DevTypeInfo(devtype).lNumModes
  1009.     D3DEnum_FindInDisplayModeList = -1
  1010.     
  1011.     For lMode = 0 To NumModes - 1
  1012.       With g_Adapters(lAdapter).DevTypeInfo(devtype).Modes(lMode)
  1013.           If .lWidth = DisplayMode.width And _
  1014.               .lHeight = DisplayMode.height And _
  1015.               .format = DisplayMode.format Then
  1016.               D3DEnum_FindInDisplayModeList = lMode
  1017.               Exit Function
  1018.           End If
  1019.       End With
  1020.     Next
  1021.     
  1022. End Function
  1023.  
  1024. '-----------------------------------------------------------------------------
  1025. ' D3DEnum_FindInFormatList
  1026. '-----------------------------------------------------------------------------
  1027. Private Function D3DEnum_FindInFormatList(lAdapter As Long, devtype As CONST_D3DDEVTYPE, format As CONST_D3DFORMAT) As Long
  1028.     
  1029.     Dim lFormat As Long
  1030.     Dim NumFormats As Long
  1031.     
  1032.     NumFormats = g_Adapters(lAdapter).DevTypeInfo(devtype).lNumFormats
  1033.     D3DEnum_FindInFormatList = -1
  1034.     
  1035.     For lFormat = 0 To NumFormats - 1
  1036.       With g_Adapters(lAdapter).DevTypeInfo(devtype).FormatInfo(lFormat)
  1037.           If .format = format Then
  1038.              D3DEnum_FindInFormatList = .usage
  1039.              Exit Function
  1040.           End If
  1041.       End With
  1042.     Next
  1043.     
  1044.     D3DEnum_FindInFormatList = -1
  1045.     
  1046. End Function
  1047.  
  1048.  
  1049. '-----------------------------------------------------------------------------
  1050. ' D3DEnum_CheckFormatCompatibility
  1051. '-----------------------------------------------------------------------------
  1052. Private Function D3DEnum_CheckFormatCompatibility(lAdapter As Long, DeviceType As CONST_D3DDEVTYPE, format As CONST_D3DFORMAT, ByRef OutCanDoWindowed As Boolean, ByRef OutCanDoFullscreen As Boolean) As Long
  1053.         On Local Error GoTo errOut
  1054.  
  1055.         D3DEnum_CheckFormatCompatibility = -1
  1056.         
  1057.         Dim d3dcaps As D3DCAPS8
  1058.         Dim flags As Long
  1059.  
  1060.         ' Filter out incompatible backbuffers
  1061.         ' Note: framework always has the backbuffer and the frontbuffer (screen) format matching
  1062.         OutCanDoWindowed = True: OutCanDoFullscreen = True
  1063.         If 0 <> g_d3d.CheckDeviceType(lAdapter, DeviceType, format, format, 0) Then OutCanDoWindowed = False
  1064.         If 0 <> g_d3d.CheckDeviceType(lAdapter, DeviceType, format, format, 1) Then OutCanDoFullscreen = False
  1065.         If (OutCanDoWindowed = False) And (OutCanDoFullscreen = False) Then Exit Function
  1066.  
  1067.         ' If no form was passed in to use as a callback
  1068.         ' then default to sofware vertex processing
  1069.  
  1070.         ' Get the device capablities
  1071.         g_d3d.GetDeviceCaps lAdapter, DeviceType, g_d3dCaps
  1072.         g_Adapters(lAdapter).d3dcaps = g_d3dCaps
  1073.         
  1074.         ' If user doesnt want to verify the device (didnt provide a callback)
  1075.         ' fall back to software
  1076.         D3DEnum_CheckFormatCompatibility = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  1077.         If g_EnumCallback Is Nothing Then Exit Function
  1078.         
  1079.         ' Confirm the device for HW vertex processing
  1080.         flags = D3DCREATE_HARDWARE_VERTEXPROCESSING
  1081.         D3DEnum_CheckFormatCompatibility = flags
  1082.         If g_d3dCaps.DevCaps And D3DDEVCAPS_HWTRANSFORMANDLIGHT Then
  1083.            If g_EnumCallback.VerifyDevice(flags, format) Then Exit Function
  1084.         End If
  1085.         
  1086.         ' Try Software VertexProcesing
  1087.         flags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  1088.         D3DEnum_CheckFormatCompatibility = flags
  1089.         If g_EnumCallback.VerifyDevice(flags, format) Then Exit Function
  1090.                                 
  1091.         ' Fail
  1092.         D3DEnum_CheckFormatCompatibility = -1
  1093.         
  1094.         Exit Function
  1095. errOut:
  1096.  
  1097. End Function
  1098.  
  1099.  
  1100.  
  1101.