Public Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Public Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Public Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
Public DirectX8 As DirectX8 'The master DirectX object.
Public Direct3D As Direct3D8 'Controls all things 3D.
Public Direct3DX As D3DX8
Public Direct3D_Device As Direct3DDevice8 'Represents the hardware rendering.
Public Sub StartDirectX(MainForm As Form, ScreenWidth As Single, ScreenHeight As Single, ColourDepth As Single, FullScreen As Boolean)
On Error GoTo errmsg
Dim Display_Mode As D3DDISPLAYMODE 'Display mode desciption.
Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer and viewport description.
Set DirectX8 = New DirectX8 'Creates the DirectX object.
Set Direct3D = DirectX8.Direct3DCreate() 'Creates the Direct3D object using the DirectX object.
'Now that we are working with fullscreen mode, we must set up the
'screen resolution to switch to, rather than use the default screen
'resolution.
Select Case ColourDepth
Case 16
Display_Mode.Format = COLOR_DEPTH_16_BIT
Case 24
Display_Mode.Format = COLOR_DEPTH_24_BIT
Case 32
Display_Mode.Format = COLOR_DEPTH_32_BIT
Case Else
MsgBox "Invalid Colour Depth. Using Defualt"
Display_Mode.Format = COLOR_DEPTH_16_BIT
End Select
Display_Mode.Width = ScreenWidth
Display_Mode.Height = ScreenHeight
Direct3D_Window.BackBufferCount = 1 '1 backbuffer only
Direct3D_Window.BackBufferFormat = Display_Mode.Format 'Sets the format that was retrieved into the backbuffer.
Direct3D_Window.BackBufferWidth = Display_Mode.Width 'Match the backbuffer width with the display width
Direct3D_Window.BackBufferHeight = Display_Mode.Height 'Match the backbuffer height with the display height
Direct3D_Window.hDeviceWindow = MainForm.hWnd 'Use frmMain as the device window.
Direct3D_Window.AutoDepthStencilFormat = D3DFMT_D16
If FullScreen = True Then
Direct3D_Window.Windowed = False 'The app will be in fullscreen mode.
Else
'Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode 'Use the current display mode that you
'are already on. Incase you are confused, I'm
'talking about your current screen resolution. ;)
Direct3D_Window.Windowed = True 'The app will be in windowed mode.
End If
Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC 'Refresh when the monitor does.
'Creates the rendering device with some useful info, along with the info
'we've already setup for Direct3D_Window.
Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, MainForm.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
Direct3D_Device.SetVertexShader FVF_TLVERTEX 'Set the type of vertex shading. (Required)
'Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True
Direct3D_Device.SetRenderState D3DRS_LIGHTING, 0 '//Disable lighting.
Direct3D_Device.SetRenderState D3DRS_ZENABLE, 1
Direct3D_Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_POINT
Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_POINT
Running = True 'Initializations all set. It's now ok to activate the game loop.
' '//configure the world matrices
'
' '//1. The World Matrix
' D3DXMatrixIdentity matWorld
' Direct3D_Device.SetTransform D3DTS_WORLD, matWorld 'commit this matrix to the device
'
' '//2. The View Matrix
' D3DXMatrixLookAtLH matView, MakeVector(0, 5, 2), MakeVector(0, 0, 0), MakeVector(0, 1, 0)
' Direct3D_Device.SetTransform D3DTS_VIEW, matView
'
' '//3. The projection Matrix
' D3DXMatrixPerspectiveFovLH matProj, pi / 3, 1, 0.1, 75
' Direct3D_Device.SetTransform D3DTS_PROJECTION, matProj
MainForm.GameLoop
Exit Sub
errmsg:
MsgBox "Error number " & Err.Number & vbNewLine & Err.Description
Running = False
CloseDirectX
End Sub