home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Direct3D"
- '---------------------------------------------------------------
- ' Visual Basic Game Programming for Teens
- ' Direct3D Support File
- '---------------------------------------------------------------
-
- 'the DirectX objects
- Public dx As DirectX8
- Public d3d As Direct3D8
- Public d3dx As New D3DX8
- Public dispmode As D3DDISPLAYMODE
- Public d3dpp As D3DPRESENT_PARAMETERS
- Public d3ddev As Direct3DDevice8
- Public backbuffer As Direct3DSurface8
-
- Public Sub InitDirect3D(ByVal windowhandle As Long)
- 'catch any errors here
- On Local Error GoTo fatal_error
-
- 'create the DirectX object
- Set dx = New DirectX8
-
- 'create the Direct3D object
- Set d3d = dx.Direct3DCreate()
- If d3d Is Nothing Then
- MsgBox "Error initializing Direct3D!"
- Shutdown
- End If
-
- 'set the display settings used to create the device
- Dim d3dpp As D3DPRESENT_PARAMETERS
- d3dpp.hDeviceWindow = windowhandle
- d3dpp.BackBufferCount = 1
- d3dpp.BackBufferWidth = SCREENWIDTH
- d3dpp.BackBufferHeight = SCREENHEIGHT
- d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
-
- 'tell D3D to use the current color depth
- d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, dispmode
- d3dpp.BackBufferFormat = dispmode.Format
-
- 'set windowed or fullscreen mode
- If FULLSCREEN Then
- d3dpp.Windowed = 0
- Else
- d3dpp.Windowed = 1
- End If
-
- 'create the D3D primary device
- Set d3ddev = d3d.CreateDevice( _
- D3DADAPTER_DEFAULT, _
- D3DDEVTYPE_HAL, _
- hwnd, _
- D3DCREATE_SOFTWARE_VERTEXPROCESSING, _
- d3dpp)
-
- If d3ddev Is Nothing Then
- MsgBox "Error creating the Direct3D device!"
- Shutdown
- End If
-
- Exit Sub
- fatal_error:
- MsgBox "Critical error in Start_Direct3D!"
- Shutdown
- End Sub
-
- Public Function LoadSurface( _
- ByVal filename As String, _
- ByVal width As Long, _
- ByVal height As Long) _
- As Direct3DSurface8
-
- On Local Error GoTo fatal_error
- Dim surf As Direct3DSurface8
-
- 'return error by default
- Set LoadSurface = Nothing
-
- 'create the new surface
- Set surf = d3ddev.CreateImageSurface(width, height, dispmode.Format)
- If surf Is Nothing Then
- MsgBox "Error creating surface!"
- Exit Function
- End If
-
- 'load surface from file
- d3dx.LoadSurfaceFromFile surf, ByVal 0, ByVal 0, filename, _
- ByVal 0, D3DX_DEFAULT, 0, ByVal 0
-
- If surf Is Nothing Then
- MsgBox "Error loading " & filename & "!"
- Exit Function
- End If
-
- 'return the new surface
- Set LoadSurface = surf
-
- fatal_error:
- Exit Function
- End Function
-
- Public Sub DrawSurface(ByRef source As Direct3DSurface8, _
- ByVal lLeft As Long, _
- ByVal lTop As Long, _
- ByVal lRight As Long, _
- ByVal lBottom As Long, _
- ByVal dest As Direct3DSurface8, _
- ByVal destx As Long, _
- ByVal desty As Long)
-
- Dim r As DxVBLibA.RECT
- Dim point As DxVBLibA.point
- Dim desc As D3DSURFACE_DESC
-
- 'set dimensions of the source image
- r.Left = lLeft
- r.Top = lTop
- r.Right = lRight
- r.bottom = lBottom
-
- 'set the destination point
- point.x = destx
- point.y = desty
-
- 'draw the scroll window
- d3ddev.CopyRects source, r, 1, dest, point
-
- End Sub
-
-
- Public Sub Shutdown()
- Set scrollbuffer = Nothing
- Set tiles = Nothing
- Set d3ddev = Nothing
- Set d3d = Nothing
- Set d3dx = Nothing
- Set dx = Nothing
- End
- End Sub
-
-