home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / d3dim / src / imcontrol / imcanvas.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-29  |  26.2 KB  |  736 lines

  1. VERSION 5.00
  2. Begin VB.UserControl IMCanvas 
  3.    ClientHeight    =   3600
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   4800
  7.    ScaleHeight     =   240
  8.    ScaleMode       =   3  'Pixel
  9.    ScaleWidth      =   320
  10.    Begin VB.PictureBox Picture1 
  11.       Height          =   3135
  12.       Left            =   0
  13.       ScaleHeight     =   3075
  14.       ScaleWidth      =   3795
  15.       TabIndex        =   0
  16.       Top             =   0
  17.       Width           =   3855
  18.    End
  19. Attribute VB_Name = "IMCanvas"
  20. Attribute VB_GlobalNameSpace = False
  21. Attribute VB_Creatable = True
  22. Attribute VB_PredeclaredId = False
  23. Attribute VB_Exposed = True
  24. Option Explicit
  25. Dim m_dx As New DirectX7
  26. Dim m_dd As DirectDraw7
  27. Dim m_d3d As Direct3D7
  28. Dim m_dev As Direct3DDevice7
  29. Dim m_ddClipper As DirectDrawClipper
  30. Dim m_frontSurface As DirectDrawSurface7
  31. Dim m_backSurface As DirectDrawSurface7
  32. Dim m_ZBuffer As DirectDrawSurface7
  33. Dim m_DevEnum As Direct3DEnumDevices
  34. Dim m_DevDesc As D3DDEVICEDESC7
  35. Dim m_ViewPortDesc As D3DVIEWPORT7
  36. Dim m_backSurfaceDesc As DDSURFACEDESC2
  37. Dim m_DDSDescPrim As DDSURFACEDESC2
  38. Dim m_srcRect As RECT
  39. Dim m_destRect As RECT
  40. Dim m_backRect As RECT
  41. Dim m_memFlags As Long
  42. Dim m_binit As Boolean
  43. Dim m_strDDGuid As String
  44. Dim m_str3dDevGuid As String
  45. Dim m_bIsfullscreen As Boolean
  46. Dim m_fsH As Long
  47. Dim m_fsW As Long
  48. Dim m_fsbpp As Long
  49. Dim m_bShowFps As Boolean
  50. Dim m_bClearZ as Boolean
  51. Public BackBufferClearValue As Long
  52. Public EnableF5ResChange As Boolean
  53. Public Enum TEXTUREFLAGS
  54.  D3DTEXTR_DEFAULT = 0
  55.  D3DTEXTR_TRANSPARENTBLACK = 1
  56.  D3DTEXTR_TRANSPARENTWHITE = 2
  57. End Enum
  58. Dim WithEvents frmFSWindow As frmFullScreen
  59. Attribute frmFSWindow.VB_VarHelpID = -1
  60. Public Event KeyDown(KeyCode As Integer, Shift As Integer)
  61. Public Event KeyPress(KeyAscii As Integer)
  62. Public Event KeyUp(KeyCode As Integer, Shift As Integer)
  63. Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  64. Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  65. Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  66. Public Event Click()
  67. Public Event DblClick()
  68. Public Event Paint()
  69. Public Event NewDDraw()
  70. '''''''''''''''''''''''''''''''''''''''
  71. '- PUBLIC FUNCTIONS                   '
  72. '''''''''''''''''''''''''''''''''''''''
  73. Public Function StartWindowed() As Boolean
  74.     m_str3dDevGuid = "IID_IDIRECT3DHALDEVICE"
  75.     If InitWindowed("", m_str3dDevGuid) = False Then
  76.         m_str3dDevGuid = "IID_IDIRECT3DRGBDEVICE"
  77.         If InitWindowed("", m_str3dDevGuid) = False Then
  78.             StartWindowed = False
  79.             Exit Function
  80.         End If
  81.     End If
  82.     StartWindowed = True
  83. End Function
  84. Public Function InitWindowed(sDDGuid As String, sD3DGuid As String) As Boolean
  85.     Dim attempt As String
  86.     On Local Error GoTo errOut
  87.     m_binit = False
  88.     Picture1.Visible = False
  89.     If Not m_dd Is Nothing Then m_dd.RestoreDisplayMode
  90.     If Not m_dd Is Nothing Then m_dd.SetCooperativeLevel 0, DDSCL_NORMAL
  91.     If Not frmFSWindow Is Nothing Then
  92.         Unload frmFSWindow
  93.         Set frmFSWindow = Nothing
  94.         DoEvents
  95.     End If
  96.     DoEvents
  97.     Cleanup
  98.     If sD3DGuid = "" Then sD3DGuid = "IID_IDirect3DRGBDevice"
  99.     m_str3dDevGuid = sD3DGuid
  100.     If UCase(sD3DGuid) = "IID_IDIRECT3DRGBDEVICE" Then
  101.         m_memFlags = DDSCAPS_SYSTEMMEMORY
  102.     ElseIf UCase(sD3DGuid) = "IID_IDIRECT3DHALDEVICE" Then
  103.         m_memFlags = DDSCAPS_VIDEOMEMORY
  104.     End If
  105.     'DDRAWCREATE
  106.     attempt = "create the directdraw object"
  107.     Set m_dd = m_dx.DirectDrawCreate(sDDGuid)
  108.     'SET COOPERATIVE LEVEL
  109.     attempt = "set the cooperative level"
  110.     m_dd.SetCooperativeLevel 0, DDSCL_NORMAL
  111.     'GET FRONTBUFFER
  112.     attempt = "get the screen surface"
  113.     m_DDSDescPrim.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  114.     Set m_frontSurface = m_dd.CreateSurface(m_DDSDescPrim)
  115.     'APPLY A CLIPPER TO OUR FRONT BUFFER
  116.     attempt = "create and set Clipper"
  117.     Set m_ddClipper = m_dd.CreateClipper(0&)
  118.     m_ddClipper.SetHWnd UserControl.hWnd
  119.     m_frontSurface.SetClipper m_ddClipper
  120.         
  121.     'CREATE BACKBUFFER
  122.     attempt = "Create BackBuffer"
  123.     CreateOffscreenBackBuffer
  124.         
  125.     'CREATE THE D3D OBJECT
  126.     attempt = "GetDirect3D"
  127.     Set m_d3d = m_dd.GetDirect3D
  128.     'ATTATCH ZBUFER
  129.     attempt = "Attatch Z Buffer"
  130.     AttatchZBuffer
  131.     'CREATE THE D3D DEVICE
  132.     attempt = "Create the D3D Device"
  133.     Set m_dev = m_d3d.CreateDevice(m_str3dDevGuid, m_backSurface)
  134.     'SET THE VIEWPORT
  135.     attempt = "Setup the viewport"
  136.     With m_ViewPortDesc
  137.         .lHeight = m_backSurfaceDesc.lHeight
  138.         .lWidth = m_backSurfaceDesc.lWidth
  139.         .minz = 0#
  140.         .maxz = 1#
  141.     End With
  142.     m_dev.SetViewport m_ViewPortDesc
  143.         
  144.     'SETUP OUR SRC AND DEST RECTS FOR BLTS
  145.     m_srcRect.Bottom = m_backSurfaceDesc.lHeight
  146.     m_srcRect.Right = m_backSurfaceDesc.lWidth
  147.     m_dx.GetWindowRect UserControl.hWnd, m_destRect
  148.     InitWindowed = True
  149.     m_binit = True
  150.     Exit Function
  151. errOut:
  152.     Debug.Print "ERROR: " + attempt
  153.     InitWindowed = False
  154. End Function
  155. Public Function InitFullScreen(sDDGuid As String, sD3DGuid As String, w As Long, h As Long, bpp As Long) As Boolean
  156.     On Local Error GoTo errOut
  157.     Dim attempt As String
  158.     Dim e As Long
  159.     m_binit = False
  160.         
  161.     Picture1.Visible = False
  162.     If frmFSWindow Is Nothing Then Set frmFSWindow = New frmFullScreen
  163.     frmFSWindow.Show
  164.     DoEvents
  165.     Cleanup
  166.     If sD3DGuid = "" Then sD3DGuid = "IID_IDirect3DRGBDevice"
  167.     m_str3dDevGuid = sD3DGuid
  168.     m_memFlags = DDSCAPS_VIDEOMEMORY
  169.     'DDRAWCREATE
  170.     attempt = "create the directdraw object"
  171.     Set m_dd = m_dx.DirectDrawCreate(sDDGuid)
  172.         
  173.     '- Setting the CooperativeLevel
  174.     '  Modex allows us to change display modes
  175.     '  Exclusive allows us to perform flip operations
  176.     '  and indicates we dont want windows to get in the way
  177.     attempt = "SetCooperativeLevel"
  178.     'm_dd.SetCooperativeLevel frmfswindow.hWnd, DDSCL_NOWINDOWCHANGES Or DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE
  179.     m_dd.SetCooperativeLevel frmFSWindow.hWnd, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE
  180.             
  181.     '- SetDisplayMode
  182.     attempt = "SetDisplayMode"
  183.     If w <> 0 And h <> 0 And bpp <> 0 Then
  184.         m_dd.SetDisplayMode w, h, bpp, 0, DDSDM_DEFAULT
  185.     End If
  186.         
  187.     '- Get the SCREEN SURFACE and create a back buffer too
  188.     '  the DDSCAPS_FLIP us to call flip and swap the
  189.     '  front and back buffers for fast rendering
  190.     attempt = "CreateScreenSurface"
  191.     m_DDSDescPrim.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  192.     m_DDSDescPrim.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX Or DDSCAPS_VIDEOMEMORY Or DDSCAPS_3DDEVICE
  193.     m_DDSDescPrim.lBackBufferCount = 1
  194.     Set m_frontSurface = m_dd.CreateSurface(m_DDSDescPrim)
  195.     '- Get the BACK SURFACE we render to
  196.     '  since the back buffer is already attached
  197.     '  all we need to do is get a reference to it
  198.     Dim caps As DDSCAPS2
  199.     caps.lCaps = DDSCAPS_BACKBUFFER
  200.     attempt = "GetBackBuffer"
  201.     Set m_backSurface = screenSurface.GetAttachedSurface(caps)
  202.     '- Get some default info about the back surface
  203.     m_backSurface.GetSurfaceDesc m_backSurfaceDesc
  204.         
  205.     'APPLY A CLIPPER TO OUR FRONT BUFFER
  206.     'attempt = "create and set Clipper"
  207.     'Set m_ddClipper = m_dd.CreateClipper(0&)
  208.     'm_ddClipper.SetHWnd frmFullScreen.hWnd
  209.     'm_frontSurface.SetClipper m_ddClipper
  210.         
  211.     'CREATE THE D3D OBJECT
  212.     attempt = "GetDirect3D"
  213.     Set m_d3d = m_dd.GetDirect3D
  214.     AttatchZBuffer
  215.         
  216.     'CREATE THE D3D DEVICE
  217.     attempt = "Create the D3D Device"
  218.     Set m_dev = m_d3d.CreateDevice(m_str3dDevGuid, m_backSurface)
  219.     'SET THE VIEWPORT
  220.     attempt = "Setup the viewport"
  221.     With m_ViewPortDesc
  222.         .lHeight = m_backSurfaceDesc.lHeight
  223.         .lWidth = m_backSurfaceDesc.lWidth
  224.         .minz = 0#
  225.         .maxz = 1#
  226.     End With
  227.     m_dev.SetViewport m_ViewPortDesc
  228.         
  229.     'SETUP OUR SRC AND DEST RECTS FOR BLTS
  230.     m_srcRect.Bottom = m_backSurfaceDesc.lHeight
  231.     m_srcRect.Right = m_backSurfaceDesc.lWidth
  232.     m_dx.GetWindowRect frmFSWindow.hWnd, m_destRect
  233.     '- Indicate our surfaces are setup for full screen operation
  234.     m_bIsfullscreen = True
  235.     m_binit = True
  236.     InitFullScreen = True
  237.     Exit Function
  238. errOut:
  239.     e = Err.Number
  240.     Call m_dd.SetCooperativeLevel(0, DDSCL_NORMAL)
  241.     m_dd.RestoreDisplayMode
  242.     Debug.Print "Error " + attempt
  243.     InitFullScreen = False
  244. End Function
  245. Public Sub SetDefaultTransformsLightsAndMaterials()
  246.     Dim matWorld As D3DMATRIX, matView As D3DMATRIX, matProj As D3DMATRIX
  247.     Dim Mat As D3DMATERIAL7
  248.     Dim light As D3DLIGHT7
  249.     Dim c As D3DCOLORVALUE
  250.     m_dx.IdentityMatrix matWorld
  251.     m_dx.IdentityMatrix matView
  252.     m_dx.IdentityMatrix matProj
  253.     Call m_dx.ViewMatrix(matView, MakeVector(0, 0, -5), MakeVector(0, 0, 0), MakeVector(0, 1, 0), 0)
  254.     Call m_dx.ProjectionMatrix(matProj, 10, 1000, 3.141 / 4)
  255.     m_dev.SetTransform D3DTRANSFORMSTATE_WORLD, matWorld
  256.     m_dev.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  257.     m_dev.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  258.      
  259.     With c
  260.         .a = 1
  261.         .r = 1
  262.         .g = 0.5
  263.         .b = 0.5
  264.     End With
  265.     With light
  266.         .dltType = D3DLIGHT_POINT
  267.         .Ambient = c
  268.         .diffuse = c
  269.         .specular = c
  270.         .position.Y = 100
  271.     End With
  272.         
  273.     m_dev.SetLight 0, light
  274.     m_dev.LightEnable 0, True
  275.     With c
  276.         .a = 0#
  277.         .r = 4
  278.         .g = 4
  279.         .b = 4
  280.     End With
  281.     Mat.Ambient = c
  282.     With c
  283.         .a = 1
  284.         .r = 0.5
  285.         .g = 0.5
  286.         .b = 0.5
  287.     End With
  288.     Mat.diffuse = c
  289.     With c
  290.         .a = 0#
  291.         .r = 0.5
  292.         .g = 0.5
  293.         .b = 0.5
  294.     End With
  295.     Mat.specular = c
  296.     Mat.emissive = c
  297.     ' Set the material as the current material
  298.     m_dev.SetMaterial Mat
  299. End Sub
  300. Public Sub Update()
  301.     On Local Error Resume Next
  302.     If m_binit = False Then Exit Sub
  303.     Dim srcR As RECT
  304.     Dim dstR As RECT
  305.     If m_bShowFps Then UpdateStats
  306.     If m_bIsfullscreen Then
  307.         m_frontSurface.Flip Nothing, DDFLIP_WAIT
  308.     Else
  309.         srcR.Right = m_backSurfaceDesc.lWidth
  310.         srcR.Bottom = m_backSurfaceDesc.lHeight
  311.         m_dx.GetWindowRect UserControl.hWnd, dstR
  312.         m_frontSurface.Blt dstR, m_backSurface, srcR, DDBLT_WAIT
  313.     End If
  314. End Sub
  315. Public Sub ClearBackSurface()
  316.     on local error resume next
  317.     If m_binit = False Then Exit Sub
  318.     Dim rSrc As RECT
  319.     Dim i As Integer
  320.     Dim recs(1) As D3DRECT
  321.     recs(0).X2 = m_backSurfaceDesc.lWidth
  322.     recs(0).Y2 = m_backSurfaceDesc.lHeight
  323.     If  m_bClearZ then
  324.         m_dev.Clear 1, recs(), D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, BackBufferClearValue, 1, 0
  325.     Else 
  326.         m_dev.Clear 1, recs(), D3DCLEAR_TARGET,BackBufferClearValue, 1, 0
  327.     End If
  328. End Sub
  329. Public Sub SetViewPosition(fromX As Single, fromY As Single, fromZ As Single, atX As Single, atY As Single, atZ As Single, upX As Single, upY As Single, upZ As Single)
  330.                 
  331.     Dim matView As D3DMATRIX
  332.     Call m_dx.ViewMatrix(matView, MakeVector(fromX, fromY, fromZ), MakeVector(atX, atY, atZ), MakeVector(upX, upY, upZ), 0)
  333.     If m_dev Is Nothing Then Exit Sub
  334.     m_dev.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  335. End Sub
  336. Public Sub SetViewFrustrum(nearZ As Single, farZ As Single, radian As Single)
  337.             
  338.     Dim matProj As D3DMATRIX
  339.     Call m_dx.ProjectionMatrix(matProj, nearZ, farZ, radian)
  340.     If m_dev Is Nothing Then Exit Sub
  341.     m_dev.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  342. End Sub
  343. Public Function CreateTextureSurface(sFile As String, extraFlags As TEXTUREFLAGS, w As Long, h As Long)
  344.     Dim bOK As Boolean
  345.     Dim enumTex As Direct3DEnumPixelFormats
  346.     Dim sLoadFile As String
  347.     Dim i As Long
  348.     Dim ddsd As DDSURFACEDESC2
  349.     Dim SurfaceObject As DirectDrawSurface7
  350.     Dim Init As Boolean
  351.     ddsd.lFlags = DDSD_CAPS Or DDSD_TEXTURESTAGE Or DDSD_PIXELFORMAT
  352.     If ((h <> 0) And (w <> 0)) Then
  353.         ddsd.lFlags = ddsd.lFlags Or DDSD_HEIGHT Or DDSD_WIDTH
  354.         ddsd.lHeight = h
  355.         ddsd.lWidth = w
  356.     End If
  357.      
  358.     Set enumTex = m_dev.GetTextureFormatsEnum()
  359.     For i = 1 To enumTex.GetCount()
  360.         bOK = True
  361.         Call enumTex.GetItem(i, ddsd.ddpfPixelFormat)
  362.             
  363.         With ddsd.ddpfPixelFormat
  364.             
  365.             If .lRGBBitCount <> 16 Then bOK = False
  366.             If .lFourCC <> 0 Then bOK = False
  367.                         
  368.             
  369.             If ((D3DTEXTR_TRANSPARENTBLACK And extraFlags) _
  370.                   Or (D3DTEXTR_TRANSPARENTWHITE And extraFlags)) Then
  371.                   If (.lRGBAlphaBitMask = 0) Then bOK = False
  372.                   'DDPF_ALPHAPIXELS
  373.             Else
  374.                   If (.lRGBAlphaBitMask <> 0) Then bOK = False
  375.             End If
  376.                   
  377.             
  378.         End With
  379.         If bOK = True Then Exit For
  380.     Next
  381.     If bOK = False Then
  382.         Debug.Print "Unable to find 16bit surface support on your hardware - exiting"
  383.         Init = False
  384.     End If
  385.     If (((D3DTEXTR_TRANSPARENTBLACK And extraFlags) _
  386.           Or (D3DTEXTR_TRANSPARENTWHITE And extraFlags))) Then
  387.         ddsd.ddpfPixelFormat.lFlags = DDPF_ALPHAPIXELS Or DDPF_RGB
  388.         ddsd.lFlags = ddsd.lFlags Or DDSD_PIXELFORMAT
  389.         
  390.     Else
  391.         ddsd.ddpfPixelFormat.lFlags = DDPF_RGB
  392.     End If
  393.     If m_dev.GetDeviceGuid()="IID_IDirect3DHALDevice" then
  394.         ddsd.ddsCaps.lCaps = DDSCAPS_TEXTURE
  395.         ddsd.ddsCaps.lCaps2 = DDSCAPS2_TEXTUREMANAGE
  396.         ddsd.lTextureStage = 0
  397.     Else
  398.         ddsd.ddsCaps.lCaps = DDSCAPS_TEXTURE or DDSCAPS_SYSTEMMEMORY
  399.         ddsd.ddsCaps.lCaps2 = 0
  400.         ddsd.lTextureStage = 0
  401.     End If    
  402.     If sFile = "" Then
  403.         Set SurfaceObject = m_dd.CreateSurface(ddsd)
  404.     Else
  405.         Set SurfaceObject = m_dd.CreateSurfaceFromFile(sFile, ddsd)
  406.     End If
  407.     Set CreateTextureSurface = SurfaceObject
  408.     If Not (((D3DTEXTR_TRANSPARENTBLACK = extraFlags) _
  409.         Or (D3DTEXTR_TRANSPARENTWHITE = extraFlags)) _
  410.         ) Then Exit Function
  411.         
  412.         
  413.     Dim ddsd3 As DDSURFACEDESC2
  414.     Dim mem() As Integer
  415.     Dim lAlphaMask As Long
  416.     Dim lRGBMask As Long
  417.     Dim lColorKey As Long
  418.     Dim c As Long
  419.     Dim X As Long
  420.     Dim Y As Long
  421.     Dim r1 As RECT
  422.     ReDim mem(ddsd.lHeight * ddsd.lPitch)
  423.                         
  424.     With ddsd.ddpfPixelFormat
  425.         lAlphaMask = .lRGBAlphaBitMask
  426.         lRGBMask = .lRBitMask Or .lGBitMask Or .lBBitMask
  427.     End With
  428.     If (extraFlags And D3DTEXTR_TRANSPARENTWHITE) Then
  429.         lColorKey = lRGBMask     'color key on white
  430.     End If
  431.     If (extraFlags And D3DTEXTR_TRANSPARENTBLACK) Then
  432.         lColorKey = 0                  'color key on black
  433.     End If
  434.     'pixel format should be 16 bit because thats what we selected
  435.     Dim rl As RECT
  436.     r1.Bottom = ddsd.lHeight
  437.     r1.Right = ddsd.lWidth
  438.     SurfaceObject.Lock r1, ddsd3, DDLOCK_WAIT, 0
  439.     ' Add an opaque alpha value to each non-colorkeyed pixel
  440.     For Y = 0 To ddsd3.lHeight - 1
  441.         For X = 0 To ddsd3.lWidth - 1
  442.             c = SurfaceObject.GetLockedPixel(X, Y)
  443.             If c And lRGBMask <> lColorKey Then
  444.                 SurfaceObject.SetLockedPixel X, Y, c Or lAlphaMask
  445.             End If
  446.         Next
  447.     Next
  448.     SurfaceObject.Unlock r1
  449.         
  450. End Function
  451. '''''''''''''''''''''''''''''''''''''''
  452. '- PUBLIC Properties
  453. '''''''''''''''''''''''''''''''''''''''
  454. Property Get dx() As DirectX7
  455.     Set dx = m_dx
  456. End Property
  457. Property Get Direct3d() As Direct3D7
  458.     Set Direct3d = m_d3d
  459. End Property
  460. Property Get Direct3DDevice() As Direct3DDevice7
  461.     Set Direct3DDevice = m_dev
  462. End Property
  463. Property Get DirectDraw() As DirectDraw7
  464.     Set DirectDraw = m_dd
  465. End Property
  466. Property Get screenSurface() As DirectDrawSurface7
  467.     Set screenSurface = m_frontSurface
  468. End Property
  469. Property Get backSurface() As DirectDrawSurface7
  470.     Set backSurface = m_backSurface
  471. End Property
  472. '''''''''''''''''''''''''''''''''''''''
  473. '- Private functions
  474. '''''''''''''''''''''''''''''''''''''''
  475. Private Sub Cleanup()
  476.     Set m_dx = Nothing
  477.     Set m_d3d = Nothing
  478.     Set m_dev = Nothing
  479.     Set m_DevEnum = Nothing
  480.     Set m_ZBuffer = Nothing
  481.     Set m_backSurface = Nothing
  482.     Set m_frontSurface = Nothing
  483.     Set m_ddClipper = Nothing
  484.     Set m_dd = Nothing
  485.     Dim emptydesc As DDSURFACEDESC2
  486.     Dim devDesc As D3DDEVICEDESC7
  487.     Dim viewDesc As D3DVIEWPORT7
  488.     Dim rc As RECT
  489.     m_DDSDescPrim = emptydesc
  490.     m_backSurfaceDesc = emptydesc
  491.     m_DevDesc = devDesc
  492.     m_ViewPortDesc = viewDesc
  493.     m_srcRect = rc
  494.     m_destRect = rc
  495.     m_backRect = rc
  496.     m_bIsfullscreen = False
  497.     m_binit = False
  498.     m_memFlags = 0
  499. End Sub
  500. Private Function MakeVector(a As Single, b As Single, c As Single) As D3DVECTOR
  501.     Dim vecOut As D3DVECTOR
  502.     With vecOut
  503.         .X = a
  504.         .Y = b
  505.         .z = c
  506.     End With
  507.     MakeVector = vecOut
  508. End Function
  509. Function CreateOffscreenBackBuffer() As Boolean
  510.     'CREATE A BACK BUFFER THE SAME SIZE AS OUR WINDOW
  511.     m_dx.GetWindowRect UserControl.hWnd, m_backRect
  512.     m_backSurfaceDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  513.     m_backSurfaceDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE Or m_memFlags
  514.     m_backSurfaceDesc.lWidth = m_backRect.Right - m_backRect.Left
  515.     m_backSurfaceDesc.lHeight = m_backRect.Bottom - m_backRect.Top
  516.     Set m_backSurface = m_dd.CreateSurface(m_backSurfaceDesc)
  517.     m_backSurface.GetSurfaceDesc m_backSurfaceDesc
  518. End Function
  519. '-=========================================
  520. '- AttatchZBuffer
  521. '  (A ZBuffer holds state for every pixel rendered
  522. '  indicating what depth the rendered pixel is supposed to
  523. '  represent so that when pixels are rendered on top of it
  524. '  the rendered can decide if the new pixel is behind the
  525. '  all ready rendered one)
  526. '-=========================================
  527. Function AttatchZBuffer() As Boolean
  528.     On Local Error GoTo errOut
  529.             
  530.     Dim hr As Integer
  531.     Dim ddsZBuff  As DirectDrawSurface7
  532.     Dim ddsd As DDSURFACEDESC2
  533.     Dim ddsd2 As DDSURFACEDESC2
  534.     Dim l As Long
  535.     Dim i As Long
  536.     m_bClearZ =FALSE
  537.     '- look for a 16 bit z buffer formats
  538.     '  each card suports only certain formats
  539.     '  but as a rule they all have at least
  540.     '  a 16 bit z buffer - or none at all
  541.     Dim fenum As Direct3DEnumPixelFormats
  542.     Set fenum = m_d3d.GetEnumZBufferFormats(m_str3dDevGuid)
  543.     l = fenum.GetCount()
  544.     '-  some cards dont support zbuffering so
  545.     '   we return success here because some cards can do
  546.     '   3d with out none
  547.     If l = 0 Then
  548.         Exit Function
  549.         AttatchZBuffer = True
  550.     End If
  551.     '- loop through zbuffer formats and get the first 16 bit one
  552.     '  we find
  553.     For i = 1 To l
  554.         Call fenum.GetItem(i, ddsd2.ddpfPixelFormat)
  555.         If ddsd2.ddpfPixelFormat.lZBufferBitDepth = 16 Then Exit For
  556.     Next
  557.     '- Get Z-buffer surface info
  558.     '  from back buffer
  559.     '  (w, h, bpp, video vs. system memory)
  560.     Call m_backSurface.GetSurfaceDesc(ddsd)
  561.                            
  562.     '- to describe a zbuffer surface we need the pixel format that
  563.     '  we already copied into ddsd2 above and the DDSCAPS_ZBUFFER
  564.     '  flag. m_memtype must be the same for the back buffer and the
  565.     '  the zbuffer (SYSTEM or VIDEO)
  566.     ddsd2.lFlags = DDSD_CAPS Or _
  567.                 DDSD_WIDTH Or _
  568.                 DDSD_HEIGHT Or _
  569.                 DDSD_PIXELFORMAT
  570.     ddsd2.ddsCaps.lCaps = ddsd2.ddsCaps.lCaps Or DDSCAPS_ZBUFFER Or m_memFlags
  571.     ddsd2.lWidth = ddsd.lWidth
  572.     ddsd2.lHeight = ddsd.lHeight
  573.     ddsd2.ddpfPixelFormat.lFlags = DDPF_ZBUFFER Or ddsd2.ddpfPixelFormat.lFlags
  574.     Set m_ZBuffer = m_dd.CreateSurface(ddsd2)
  575.     '- Attach Z-buffer to rendering surface
  576.     Call m_backSurface.AddAttachedSurface(m_ZBuffer)
  577.     m_bClearZ =TRUE
  578.     AttatchZBuffer = True
  579.     Exit Function
  580. errOut:
  581.     AttatchZBuffer = False
  582. End Function
  583. Private Sub UpdateStats()
  584.     Static FPS   As Single
  585.     Static LastTime  As Single
  586.     Static nFrames   As Single
  587.     ' Keep track of the time lapse and frame count
  588.     Dim fTime As Single
  589.     fTime = m_dx.TickCount() * 0.001 ' // Get current time in seconds
  590.     nFrames = nFrames + 1
  591.     '// Update the frame rate once per second
  592.     If (fTime - LastTime > 1#) Then
  593.         FPS = nFrames / (fTime - LastTime)
  594.         LastTime = fTime
  595.         nFrames = 0
  596.     End If
  597.     If m_backSurface Is Nothing Then Exit Sub
  598.     Dim desc As DDSURFACEDESC2
  599.     m_backSurface.GetSurfaceDesc desc
  600.     m_backSurface.SetForeColor vbYellow
  601.     m_backSurface.DrawText 10, 10, "FPS:" + Str(FPS) + Str(desc.lWidth) + Str(desc.lHeight) + Str(desc.ddpfPixelFormat.lRGBBitCount) + " " + m_str3dDevGuid, False
  602. End Sub
  603. Private Function F5ResChange(KeyCode As Integer)
  604.     Dim b As Boolean
  605.     if not m_binit then exit function
  606.     If m_bIsfullscreen Then
  607.         'Go to windowed mode from Fullscreen on Esc
  608.         If KeyCode = vbKeyEscape Then
  609.             m_bIsfullscreen = False
  610.             b = InitWindowed(m_strDDGuid, m_str3dDevGuid)
  611.             If b = False Then
  612.                 m_str3dDevGuid = "IID_IDirect3DRGBDevice"
  613.                 b = InitWindowed(m_strDDGuid, m_str3dDevGuid)
  614.             End If
  615.             
  616.             RaiseEvent NewDDraw
  617.         End If
  618.     Else
  619.         'In windowed mode F5 brings up selection dialog
  620.         If KeyCode = vbKeyF5 Then
  621.             b = frmSelectRes.ChangeConfig(m_strDDGuid, m_str3dDevGuid, m_fsW, m_fsH, m_fsbpp, m_bIsfullscreen, m_bShowFps)
  622.             If b Then
  623.                 b = False
  624.                 'if they ask to go Full Screen - try
  625.                 If m_bIsfullscreen Then
  626.                     b = InitFullScreen(m_strDDGuid, m_str3dDevGuid, m_fsW, m_fsH, m_fsbpp)
  627.                 End If
  628.             
  629.                 'if failed to go full screen or haven tried - try windowed
  630.                 If b = False Then
  631.                     b = InitWindowed(m_strDDGuid, m_str3dDevGuid)
  632.                 End If
  633.                 
  634.                 'if still failed try windowed with RGB rasterizer
  635.                 If b = False Then
  636.                     m_str3dDevGuid = "IID_IDirect3DRGBDevice"
  637.                     b = InitWindowed(m_strDDGuid, m_str3dDevGuid)
  638.                 End If
  639.             
  640.                 RaiseEvent NewDDraw
  641.             End If
  642.        End If
  643.     End If
  644. End Function
  645. 'Setup defaults
  646. Private Sub UserControl_Initialize()
  647.     EnableF5ResChange = True
  648. End Sub
  649. Private Sub UserControl_Resize()
  650.     If m_binit Then
  651.       '  Set m_backSurface = Nothing
  652.       '  Set m_ZBuffer = Nothing
  653.         
  654.       '  CreateOffScreenBackBuffer
  655.       '  AttatchZBuffer
  656.       '
  657.       '  m_dev.SetRenderTarget m_backSurface
  658.     Else
  659.         Picture1.Width = UserControl.ScaleWidth
  660.         Picture1.Height = UserControl.ScaleHeight
  661.     End If
  662. End Sub
  663. Private Sub UserControl_Show()
  664.     If UserControl.Ambient.UserMode = True Then
  665.         Picture1.Visible = False
  666.     Else
  667.         Picture1.Visible = True
  668.     End If
  669. End Sub
  670. 'Events to marhall back to the user from windowed operation
  671. Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
  672.     If EnableF5ResChange Then
  673.         F5ResChange KeyCode
  674.     End If
  675.     RaiseEvent KeyDown(KeyCode, Shift)
  676. End Sub
  677. Private Sub UserControl_KeyPress(KeyAscii As Integer)
  678.     RaiseEvent KeyPress(KeyAscii)
  679. End Sub
  680. Private Sub UserControl_KeyUp(KeyCode As Integer, Shift As Integer)
  681.     RaiseEvent KeyUp(KeyCode, Shift)
  682. End Sub
  683. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  684.     RaiseEvent MouseDown(Button, Shift, X, Y)
  685. End Sub
  686. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  687.     RaiseEvent MouseUp(Button, Shift, X, Y)
  688. End Sub
  689. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  690.     RaiseEvent MouseMove(Button, Shift, X, Y)
  691. End Sub
  692. Private Sub UserControl_Paint()
  693.     RaiseEvent Paint
  694. End Sub
  695. Private Sub UserControl_Click()
  696.     RaiseEvent Click
  697. End Sub
  698. Private Sub UserControl_DblClick()
  699.     RaiseEvent DblClick
  700. End Sub
  701. 'Marshal events from full screen window
  702. 'NOTE: we must use a form full fullscreen operation because
  703. ' SetCooperativeLevel expects an hwnd that has no parent
  704. ' That is why we need to marshal events for both the UserControl
  705. ' and the FSWindow
  706. Private Sub frmFSWindow_Click()
  707.     RaiseEvent Click
  708. End Sub
  709. Private Sub frmFSWindow_DblClick()
  710.     RaiseEvent DblClick
  711. End Sub
  712. Private Sub frmFSWindow_KeyDown(KeyCode As Integer, Shift As Integer)
  713.     If EnableF5ResChange Then
  714.         F5ResChange KeyCode
  715.     End If
  716.     RaiseEvent KeyDown(KeyCode, Shift)
  717. End Sub
  718. Private Sub frmFSWindow_KeyPress(KeyAscii As Integer)
  719.     RaiseEvent KeyPress(KeyAscii)
  720. End Sub
  721. Private Sub frmFSWindow_KeyUp(KeyCode As Integer, Shift As Integer)
  722.     RaiseEvent KeyUp(KeyCode, Shift)
  723. End Sub
  724. Private Sub frmFSWindow_MouseDown(ButtonN As Integer, Shift As Integer, X As Single, Y As Single)
  725.     RaiseEvent MouseDown(ButtonN, Shift, X, Y)
  726. End Sub
  727. Private Sub frmFSWindow_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  728.     RaiseEvent MouseDown(Button, Shift, X, Y)
  729. End Sub
  730. Private Sub frmFSWindow_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  731.     RaiseEvent MouseUp(Button, Shift, X, Y)
  732. End Sub
  733. Private Sub frmFSWindow_Paint()
  734.     RaiseEvent Paint
  735. End Sub
  736.