home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / d3drm / src / rmcontrol / rmcanvas.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-08-10  |  42.9 KB  |  1,133 lines

  1. VERSION 5.00
  2. Begin VB.UserControl RMCanvas
  3.    ClientHeight    =   4065
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   5265
  7.    ScaleHeight     =   271
  8.    ScaleMode       =   3  'Pixel
  9.    ScaleWidth      =   351
  10.    Begin VB.PictureBox Picture1 
  11.       Height          =   2655
  12.       Left            =   0
  13.       ScaleHeight     =   173
  14.       ScaleMode       =   3  'Pixel
  15.       ScaleWidth      =   261
  16.       TabIndex        =   0
  17.       Top             =   0
  18.       Width           =   3975
  19.    End
  20. Attribute VB_Name = "RMCanvas"
  21. Attribute VB_GlobalNameSpace = False
  22. Attribute VB_Creatable = True
  23. Attribute VB_PredeclaredId = False
  24. Attribute VB_Exposed = True
  25. Option Explicit
  26. 'Functions
  27. '-  StartWindowed                   start in windowed mode  -
  28. '-  InitFullScreen                  allows you to start full screen given a w h and bpp
  29. '                                   allows you to select the ddraw and d3d guids
  30. '-  InitWindowed                    allows you to start in windowed mode
  31. '                                   allows you to select the ddraw and d3d guids
  32. '-  Tick                            increment simulation/animations
  33. '-  Render                          render the scene
  34. '-  Update                          Combines tick and render
  35. '-  GetBltRect                      get the update rectangle for our window
  36. '-  CreateBoxMesh                   create a 3d cube
  37. '-  CreateSheetMesh                 create a 1 or 2 sided rectange polygon
  38. '-  GetBoundingBox                  get the bounding box for a frame
  39. '-  PickTopMesh                     get a mesh from x,y screen coordinatess
  40. '-  PickTopFrame                    get a frame from x,y screen coordinatess
  41. '-  RotateFromXY                    xy screen coordinate rotation - for UI
  42. '-  CreateUpdateableTexture         creates textures that allow you to modify them
  43. '                                   unlikes LoadTexture. note: sFile can be ""
  44. ' Properties
  45. '-  hWnd                    r       return control hwnd
  46. '-  FPS                     r       returns Frame Persecond (from calls to update)
  47. '-  SceneSpeed              rw
  48. '-  IsFullScreen            r       returns true if fullscreen
  49. '-  DisplayModes            r       returns list of available display modes
  50. '                                   use before ChangeFullScreenDisplayMode
  51. '-  VideoCards              r       return list of available cards
  52. '-  Devices                 r       return list of available rederers
  53. '-  Device                  r       return current RMdevice
  54. '-  Viewport                r       return current RMviewport (hooked to CameraFrame)
  55. '-  SceneFrame              r       root frame of our scene graph
  56. '-  DirLightFrame           r       returns Frame (node) that contains DirLight
  57. '                                   use it to position the light
  58. '-  DirLight                r       returns default directional light
  59. '                                   use it to change light color
  60. '-  CameraFrame             r       returns Frame that represents camera
  61. '-  AmbientLight            r       returns AmbientLight object
  62. '-  DX                      r       returns DirectX object
  63. '-  DDRaw                   r       returns DDraw Object
  64. '-  BackBuffer              r       returns DDrawSurface used to write to
  65. '-  D3DRM                   r       returns D3DRM object
  66. '-  DirectDrawGuid          r       current directdraw object in use
  67. '-  Direct3DGuid            r       current d3d rasterizer in use
  68. '-  RotateFrame             rw      set enable use of auto UI rotation
  69. '-  RotateMode              rw      set to change how things are rotated in the UI
  70. '-  RotateRadius            rw      set to change how fast UI rotation is
  71. '-  Use3DHardware           rw      if false forces Software rasterization
  72. '-  UseBackbuffer           rw      if false allows for faster redering but
  73. '                                   at the expense of flexiblity-
  74. '                                   call before InitWindowed
  75. Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef r As RECT) As Long
  76. '-============================================================
  77. ' Full screen Events
  78. '-============================================================
  79. Dim WithEvents frmFSWindow As FSWindow
  80. Attribute frmFSWindow.VB_VarHelpID = -1
  81. Public Event KeyDown(keyCode As Integer, Shift As Integer)
  82. Public Event KeyPress(KeyAscii As Integer)
  83. Public Event KeyUp(keyCode As Integer, Shift As Integer)
  84. Public Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  85. Public Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  86. Public Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  87. Public Event Click()
  88. Public Event DblClick()
  89. Public Event RestoreSurfaces()
  90. Public Event Paint()
  91. Public Event DirecXNotInstalled()
  92. Public Event SceneMove(delta As Single)
  93. Public Event ViewportClear()
  94. Public Event PostRender()
  95. 'Enum
  96. Enum RotateMethodEnum
  97.     ROTATE_TRACKBALL = 0
  98.     ROTATE_GLOBE = 1
  99. End Enum
  100. 'UDTs
  101. Private Type DeviceCharacteristics
  102.     bDither As Long
  103.     Name As String
  104.     Quality As Long
  105.     RenderMode As Long
  106.     Shades As Long
  107.     TexQ As Long
  108.     WFoptions As Long
  109. End Type
  110. Private Type ViewportCharacteristics
  111.     Back As Single
  112.     Field As Single
  113.     Front As Single
  114.     Name As String
  115.     Projection As Long
  116.     left As Single
  117.     right As Single
  118.     bottom As Single
  119.     top As Single
  120.     scaling As Long
  121. End Type
  122. '- direct x object
  123. Dim m_dx As New DirectX7
  124. '- direct draw objects
  125. Dim m_dd As DirectDraw4
  126. Dim m_ddClip As DirectDrawClipper       'Direct Draw clipper object
  127. Dim m_frontBuffer As DirectDrawSurface4
  128. Dim m_backBuffer As DirectDrawSurface4
  129. '- direct 3drm objects
  130. Dim m_rm As Direct3DRM3
  131. Dim m_rmDevice As Direct3DRMDevice3
  132. Dim m_rmViewport As Direct3DRMViewport2
  133. Dim m_rmFrameScene As Direct3DRMFrame3
  134. Dim m_rmFrameCamera As Direct3DRMFrame3
  135. Dim m_rmFrameDirLight As Direct3DRMFrame3
  136. Dim m_rmFrameAmbientLight As Direct3DRMFrame3
  137. Dim m_rmDirLight As Direct3DRMLight
  138. Dim m_rmAmbientLight As Direct3DRMLight
  139. '- state
  140. Dim m_strDDGuid As String               'DirectDraw device guid
  141. Dim m_strD3DGuid As String              'Direct3DRM device guid
  142. Dim m_scenespeed As Single              'how fast animation run
  143. Dim m_hwnd As Long                      'hwnd (either FSWindow or our ocx)
  144. Dim m_binit As Boolean                  'are we initailized?
  145. Dim m_bResizing As Boolean              'Are we in the midle of a resize operation
  146. Dim m_lastRMMove As Long                'time stamp of last update
  147. Dim m_lastFPS As Long                   'time stamp of last FPS update
  148. Dim m_fps As Single                     'frame per second
  149. Dim m_bCreateFromClipper As Boolean  'Use a clipper to start the RM
  150. Dim m_DevInfo As DeviceCharacteristics
  151. Dim m_ViewInfo As ViewportCharacteristics
  152. Dim m_bfullscreen As Boolean
  153. Dim m_bUseSoftwareOnly As Boolean
  154. Dim m_errorReason As String
  155. Dim m_emptyrect As RECT
  156. Dim m_lastX As Long
  157. Dim m_lastY As Long
  158. Dim m_createid As Long
  159. Dim m_bMouseDown
  160. Public RotateMethod As RotateMethodEnum
  161. Public RotateFrame As Direct3DRMFrame3
  162. Public RotateRadius As Single
  163. Public UpdateDC
  164. Public Background   As DirectDrawSurface4
  165. '-============================================================
  166. ' StartWindowed
  167. '-============================================================
  168. Public Function StartWindowed() As Boolean
  169.     Dim b As Boolean
  170.                 
  171.     b = InitWindowed("", "IID_IDirect3DHALDevice")
  172.     If b = True Then
  173.         StartWindowed = True
  174.         Exit Function
  175.     End If
  176.     b = InitWindowed("", "IID_IDirect3DRGBDevice")
  177.     StartWindowed = b
  178.         
  179. End Function
  180. '-============================================================
  181. ' InitWindowed
  182. '-============================================================
  183. Public Function InitWindowed(ddrawguid As String, d3dguid As String) As Boolean
  184.     Dim b As Boolean
  185.     Dim ddsd As DDSURFACEDESC2
  186.     On Local Error GoTo errOut
  187.         
  188.     m_errorReason = ""
  189.     m_binit = False
  190.     Picture1.Visible = False
  191.     'make sure we have com out of fullscreen mode
  192.     If Not (m_dd Is Nothing) Then m_dd.RestoreDisplayMode
  193.     If Not (m_dd Is Nothing) Then m_dd.SetCooperativeLevel 0, DDSCL_NORMAL
  194.     Cleanup
  195.     CloseFSWindow
  196.     'get rid of our current rm device..
  197.     Set m_rmDevice = Nothing
  198.     Set m_rmViewport = Nothing
  199.     m_hwnd = UserControl.hwnd
  200.     m_strDDGuid = ddrawguid
  201.     m_strD3DGuid = d3dguid
  202.     If d3dguid = "" Then m_strD3DGuid = "IID_IDirect3DRGBDevice"
  203.     If m_bUseSoftwareOnly = True Then m_strD3DGuid = "IID_IDirect3DRGBDevice"
  204.     'DirectDrawCreate
  205.     m_errorReason = "RMCanvas: Could not create requested DirectDraw object from ddrawguid"
  206.     Set m_dd = m_dx.DirectDraw4Create(m_strDDGuid)
  207.     'Set The CooperativeLevel
  208.     m_errorReason = "RMCanvas: Could set the cooperative level to normal"
  209.     m_dd.SetCooperativeLevel m_hwnd, DDSCL_NORMAL
  210.     'CreatePrimary
  211.     m_errorReason = "RMCanvas: unable get screen surface from DirectDraw"
  212.     ddsd.lFlags = DDSD_CAPS
  213.     ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  214.     Set m_frontBuffer = m_dd.CreateSurface(ddsd)
  215.     'Setup a clipper
  216.     m_errorReason = "RMCanvas: failed to setup the clipper"
  217.     Set m_ddClip = m_dd.CreateClipper(0)
  218.     m_ddClip.SetHWnd m_hwnd
  219.     m_frontBuffer.SetClipper m_ddClip
  220.             
  221.     b = ResizeWindowedDevice(m_strD3DGuid)
  222.     If b = False Then GoTo errOut
  223.     SetDeviceDefaults
  224.     m_binit = True
  225.     m_bfullscreen = False
  226.     InitWindowed = True
  227.     Exit Function
  228. errOut:
  229.     Cleanup
  230. End Function
  231. '-============================================================
  232. ' ResizeWindowedDevice
  233. '-============================================================
  234. Private Function ResizeWindowedDevice(d3dg As String) As Boolean
  235.     If m_dd Is Nothing Then Exit Function
  236.     If m_bfullscreen Then Exit Function
  237.     On Local Error GoTo errOut
  238.     Dim memflags As Long
  239.     Dim r As RECT
  240.     Dim ddsd As DDSURFACEDESC2
  241.         
  242.     'Get window extent
  243.     Call GetWindowRect(m_hwnd, r)
  244.     ddsd.lWidth = r.right - r.left
  245.     ddsd.lHeight = r.bottom - r.top
  246.     Set m_rmViewport = Nothing
  247.     Set m_rmDevice = Nothing
  248.     Set m_backBuffer = Nothing
  249.     'Take care of createFromWindowed shortcut
  250.     If m_bCreateFromClipper Then
  251.       
  252.         
  253.         m_errorReason = "RMCanvas: unable to create RM Device or Viewport for current window size"
  254.         Set m_rmDevice = m_rm.CreateDeviceFromClipper(m_ddClip, d3dg, ddsd.lWidth, ddsd.lHeight)
  255.         Set m_rmViewport = m_rm.CreateViewport(m_rmDevice, m_rmFrameCamera, 0, 0, ddsd.lWidth, ddsd.lHeight)
  256.         
  257.         ResizeWindowedDevice = True
  258.         Exit Function
  259.     End If
  260.     If UCase(d3dg) = "IID_IDIRECT3DHALDEVICE" Then
  261.         memflags = DDSCAPS_VIDEOMEMORY
  262.     Else
  263.         memflags = DDSCAPS_SYSTEMMEMORY
  264.     End If
  265.     'CreateBacksurface
  266.     ddsd.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  267.     ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE Or memflags
  268.            
  269.     m_errorReason = "RMCanvas: unable to create backbuffer for current window size - try setting Use3DHardware=FALSE"
  270.     Set m_backBuffer = m_dd.CreateSurface(ddsd)
  271.     m_errorReason = "RMCanvas: unable to create RM Device or Viewport for current window size"
  272.     Set m_rmViewport = Nothing
  273.     Set m_rmDevice = Nothing
  274.     Set m_rmDevice = m_rm.CreateDeviceFromSurface(d3dg, m_dd, m_backBuffer, 0)
  275.     Set m_rmViewport = m_rm.CreateViewport(m_rmDevice, m_rmFrameCamera, 0, 0, ddsd.lWidth, ddsd.lHeight)
  276.         
  277.     ResizeWindowedDevice = True
  278.     RaiseEvent RestoreSurfaces
  279.     Exit Function
  280. errOut:
  281.     Err.Clear
  282.     Set m_rmDevice = Nothing
  283.     Set m_rmViewport = Nothing
  284.     Set m_backBuffer = Nothing
  285.     ResizeWindowedDevice = False
  286.     m_binit = False
  287. End Function
  288. '-============================================================
  289. ' InitFullScreen
  290. '-============================================================
  291. Public Function InitFullScreen(ddrawguid As String, d3dguid As String, w As Long, h As Long, bpp As Long) As Boolean
  292.     On Local Error GoTo errOut
  293.     m_binit = False
  294.     m_errorReason = ""
  295.     CloseFSWindow
  296.     Set frmFSWindow = New FSWindow
  297.     frmFSWindow.Show
  298.     m_hwnd = frmFSWindow.hwnd
  299.     'get rid of our current rm device..
  300.     Cleanup
  301.     'make sure fs window is up
  302.     DoEvents
  303.     m_strDDGuid = ddrawguid
  304.     If d3dguid = "" Then m_strD3DGuid = "IID_IDirect3DRGBDevice"
  305.     'DirectDrawCreate
  306.     m_errorReason = "RMCanvas: failed  on  DirectDraw Object Create for given ddrawguid"
  307.     Set m_dd = dx.DirectDraw4Create(m_strDDGuid)
  308.     'Set cooperative level
  309.     m_errorReason = "RMCanvas: failed  on SetCooperativeLevel for fullscreen operation"
  310.     m_dd.SetCooperativeLevel m_hwnd, DDSCL_ALLOWMODEX Or DDSCL_FULLSCREEN Or DDSCL_NOWINDOWCHANGES Or DDSCL_EXCLUSIVE
  311.     'set the display mode
  312.     If w <> 0 And h <> 0 And bpp <> 0 Then
  313.         m_errorReason = "RMCanvas: Unable to set full screen display mode at requested w h and bpp"
  314.         m_dd.SetDisplayMode w, h, bpp, 0, DDSDM_DEFAULT
  315.     End If
  316.     'create Flipping Surfaces - one front and 1 back buffer
  317.     Dim ddsd As DDSURFACEDESC2
  318.     ddsd.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  319.     ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX Or DDSCAPS_VIDEOMEMORY Or DDSCAPS_3DDEVICE
  320.     ddsd.lBackBufferCount = 1
  321.     m_errorReason = "RMCanvas: unable to create filipable surface for fullscreen operation"
  322.     Set m_frontBuffer = m_dd.CreateSurface(ddsd)
  323.     'Setup a clipper
  324.     m_errorReason = "RMCanvas: failed to setup the clipper"
  325.     Set m_ddClip = m_dd.CreateClipper(0)
  326.     m_ddClip.SetHWnd m_hwnd
  327.     m_frontBuffer.SetClipper m_ddClip
  328.             
  329.     'Get backbuffer
  330.     Dim caps As DDSCAPS2
  331.     caps.lCaps = DDSCAPS_BACKBUFFER
  332.     m_errorReason = "RMCanvas: unable to get the fullscreen backbuffer"
  333.     Set m_backBuffer = m_frontBuffer.GetAttachedSurface(caps)
  334.         
  335.     'get backbuffer description
  336.     Dim ddsd2 As DDSURFACEDESC2
  337.     m_errorReason = "RMCanvas: unable to get the fullscreen backbuffer description"
  338.     m_backBuffer.GetSurfaceDesc ddsd2
  339.     'see if they turned hw off
  340.     If m_bUseSoftwareOnly Then
  341.         m_strD3DGuid = "IID_IDirect3DRGBDevice"
  342.     Else
  343.         m_strD3DGuid = d3dguid
  344.     End If
  345.     'create the rm device from surface
  346.     m_errorReason = "RMCanvas: unable to create the Retained Mode device - try a smaller resolution or try setting  Use3DHardware=false"
  347.     Set m_rmDevice = m_rm.CreateDeviceFromSurface(m_strD3DGuid, m_dd, m_backBuffer, D3DRMDEVICE_DEFAULT)
  348.     Set m_rmViewport = m_rm.CreateViewport(m_rmDevice, m_rmFrameCamera, 0, 0, ddsd2.lWidth, ddsd2.lHeight)
  349.     SetDeviceDefaults
  350.     m_binit = True
  351.     m_bfullscreen = True
  352.     InitFullScreen = True
  353.     Exit Function
  354. errOut:
  355.     m_binit = False
  356.     Cleanup
  357.     Exit Function
  358. End Function
  359. 'Run Time R/O access properties
  360. '-============================================================
  361. '-============================================================
  362. Public Function dx() As DirectX7
  363.     Set dx = m_dx
  364. End Function
  365. '-============================================================
  366. ' DDraw
  367. '-============================================================
  368. Public Function DDraw() As DirectDraw4
  369.     Set DDraw = m_dd
  370. End Function
  371. '-============================================================
  372. ' BackBuffer
  373. '-============================================================
  374. Public Function BackBuffer() As DirectDrawSurface4
  375.     Set BackBuffer = m_backBuffer
  376. End Function
  377. '-============================================================
  378. ' D3drm
  379. '-============================================================
  380. Public Function D3DRM() As Direct3DRM3
  381.     Set D3DRM = m_rm
  382. End Function
  383. '-============================================================
  384. ' Device
  385. '-============================================================
  386. Public Function Device() As Direct3DRMDevice3
  387.     Set Device = m_rmDevice
  388. End Function
  389. '-============================================================
  390. ' Viewport
  391. '-============================================================
  392. Public Function Viewport() As Direct3DRMViewport2
  393.     Set Viewport = m_rmViewport
  394. End Function
  395. '-============================================================
  396. ' DirLightFrame
  397. '-============================================================
  398. Public Function DirLightFrame() As Direct3DRMFrame3
  399.     Set DirLightFrame = m_rmFrameDirLight
  400. End Function
  401. '-============================================================
  402. ' SceneFrame
  403. '-============================================================
  404. Public Function SceneFrame() As Direct3DRMFrame3
  405.     Set SceneFrame = m_rmFrameScene
  406. End Function
  407. '-============================================================
  408. ' CameraFrame
  409. '-============================================================
  410. Public Function CameraFrame() As Direct3DRMFrame3
  411.     Set CameraFrame = m_rmFrameCamera
  412. End Function
  413. '-============================================================
  414. ' DirLight
  415. '-============================================================
  416. Public Function DirLight() As Direct3DRMLight
  417.     Set DirLight = m_rmDirLight
  418. End Function
  419. '-============================================================
  420. ' AmbientLight
  421. '-============================================================
  422. Public Function AmbientLight() As Direct3DRMLight
  423.     Set AmbientLight = m_rmAmbientLight
  424. End Function
  425. '-============================================================
  426. ' Use3DHardware
  427. '-============================================================
  428. Property Let Use3DHardware(b As Boolean)
  429.     m_bUseSoftwareOnly = Not b
  430. End Property
  431. Property Get Use3DHardware() As Boolean
  432.     Use3DHardware = Not m_bUseSoftwareOnly
  433. End Property
  434. '-============================================================
  435. ' UseBackbuffer
  436. '-============================================================
  437. Property Let UseBackbuffer(b As Boolean)
  438.     m_bCreateFromClipper = Not b
  439. End Property
  440. Property Get UseBackbuffer() As Boolean
  441.     UseBackbuffer = Not m_bCreateFromClipper
  442. End Property
  443. '-============================================================
  444. ' LastError
  445. '-============================================================
  446. Property Get LastError() As String
  447.      LastError = m_errorReason
  448. End Property
  449. '-============================================================
  450. ' DirectDrawGuid
  451. '-============================================================
  452. Property Get DirectDrawGuid() As String
  453.      DirectDrawGuid = m_strDDGuid
  454. End Property
  455. '-============================================================
  456. ' Direct3DGuid
  457. '-============================================================
  458. Property Get Direct3DGuid() As String
  459.      Direct3DGuid = m_strD3DGuid
  460. End Property
  461. '- Runtime only List Functions
  462. '-============================================================
  463. ' Devices
  464. '-============================================================
  465. Public Function Devices(Optional ddrawguid = "") As Direct3DEnumDevices
  466.     On Local Error GoTo exitOut:
  467.     Dim dd As DirectDraw7
  468.     Dim d3d As Direct3D7
  469.     Set dd = dx.DirectDrawCreate(CStr(ddrawguid))
  470.         
  471.     Set d3d = dd.GetDirect3D()
  472.     Set Devices = d3d.GetDevicesEnum()
  473.     Set dd = Nothing
  474.     Set d3d = Nothing
  475.     Exit Function
  476. exitOut:
  477. End Function
  478. '-============================================================
  479. ' VideoCards
  480. '-============================================================
  481. Public Function VideoCards() As DirectDrawEnum
  482.     Set VideoCards = m_dx.GetDDEnum()
  483. End Function
  484. '-============================================================
  485. ' DisplayModes
  486. '-============================================================
  487. Public Function DisplayModes(Optional ddrawguid = "") As DirectDrawEnumModes
  488.     On Local Error GoTo exitOut
  489.     Dim dd As DirectDraw4
  490.     Set dd = dx.DirectDraw4Create(CStr(ddrawguid))
  491.     Dim ddsd As DDSURFACEDESC2
  492.     Set DisplayModes = dd.GetDisplayModesEnum(0, ddsd)
  493.     Set dd = Nothing
  494. exitOut:
  495. End Function
  496. '-============================================================
  497. ' GetBltRect
  498. '-============================================================
  499. Public Function GetBltRect(top As Long, left As Long, bottom As Long, right As Long)
  500.         Dim rc As RECT
  501.         GetWindowRect m_hwnd, rc
  502.         left = 0
  503.         right = rc.right - rc.left
  504.         top = 0
  505.         bottom = rc.bottom - rc.top
  506.         
  507. End Function
  508. '-============================================================
  509. ' IsFullScreen
  510. '-============================================================
  511. Property Get IsFullScreen() As Boolean
  512.     IsFullScreen = m_bfullscreen
  513. End Property
  514. '-============================================================
  515. ' SceneSpeed
  516. '-============================================================
  517. Public Property Get SceneSpeed() As Single
  518.     SceneSpeed = m_scenespeed
  519. End Property
  520. Public Property Let SceneSpeed(s As Single)
  521.     m_scenespeed = s
  522. End Property
  523. '-============================================================
  524. ' FPS
  525. '-============================================================
  526. Public Property Get FPS() As Single
  527.     FPS = m_fps
  528. End Property
  529. '-============================================================
  530. ' Update
  531. '-============================================================
  532. Public Sub Update()
  533.     Tick
  534.     Render
  535. End Sub
  536. '-============================================================
  537. ' Render
  538. '-============================================================
  539. Public Sub Render()
  540.     On Local Error GoTo errOut
  541.     If m_binit = False Then Exit Sub
  542.     Dim t As Long
  543.     Dim delta As Single
  544.     Dim r As RECT
  545.     Static fcount As Long
  546.     t = dx.TickCount()
  547.     m_rmViewport.Clear D3DRMCLEAR_ZBUFFER Or D3DRMCLEAR_TARGET
  548.     If Not Background Is Nothing Then
  549.         m_backBuffer.Blt m_emptyrect, Background, m_emptyrect, DDBLT_WAIT
  550.     End If
  551.     RaiseEvent ViewportClear
  552.     m_rmViewport.Render m_rmFrameScene
  553.     RaiseEvent PostRender
  554.     m_rmDevice.Update
  555.     If m_bfullscreen Then
  556.             m_frontBuffer.Flip Nothing, DDFLIP_WAIT
  557.     Else
  558.         If m_bCreateFromClipper = False Then
  559.             Call GetWindowRect(m_hwnd, r)
  560.             m_frontBuffer.Blt r, m_backBuffer, m_emptyrect, DDBLT_WAIT
  561.         End If
  562.     End If
  563.     fcount = fcount + 1
  564.     If fcount = 30 Then
  565.         t = dx.TickCount()
  566.         m_fps = 30000 / (t - m_lastFPS)
  567.         fcount = 0
  568.         m_lastFPS = t
  569.     End If
  570.      
  571. errOut:
  572. End Sub
  573. '-============================================================
  574. ' hwnd
  575. '-============================================================
  576. Public Function hwnd() As Long
  577.     hwnd = m_hwnd
  578. End Function
  579. '-============================================================
  580. ' Tick
  581. '-============================================================
  582. Public Sub Tick()
  583.     On Local Error GoTo errOut
  584.     If m_binit = False Then Exit Sub
  585.     Dim t As Long
  586.     Dim delta As Single
  587.     t = dx.TickCount()
  588.     If m_lastRMMove <> 0 Then
  589.         delta = (t - m_lastRMMove) * m_scenespeed / 1000
  590.         m_rmFrameScene.Move delta
  591.         RaiseEvent SceneMove(delta)
  592.     End If
  593.     m_lastRMMove = t
  594. errOut:
  595. End Sub
  596. '-============================================================
  597. ' UserControl_InitProperties
  598. '-============================================================
  599. Private Sub UserControl_InitProperties()
  600.     UserControl_Resize
  601. End Sub
  602. '-============================================================
  603. ' UserControl_Show
  604. '-============================================================
  605. Private Sub UserControl_Show()
  606.     If m_binit = False Then Exit Sub
  607.     m_rmDevice.HandleActivate 0
  608.     m_rmDevice.HandlePaint UserControl.hDC
  609. End Sub
  610. '-============================================================
  611. ' UserControl_Resize
  612. '-============================================================
  613. Private Sub UserControl_Resize()
  614.     on local error resume next    
  615.     Dim b As Boolean
  616.     If m_binit = False Then
  617.         Picture1.width = UserControl.ScaleWidth
  618.         Picture1.height = UserControl.ScaleHeight
  619.         Exit Sub
  620.     End If
  621.     'full screen apps shouldnt resize
  622.     If m_bfullscreen Then Exit Sub
  623.     'tell others functions not to try and render during a resize
  624.     m_bResizing = True
  625.     SaveDeviceViewportCharacteristics
  626.    If Not m_bUseSoftwareOnly Then
  627.         b = InitWindowed(m_strDDGuid, "IID_IDirect3DHALDevice")
  628.     End If
  629.     If Not b Then
  630.        b = InitWindowed(m_strDDGuid, "IID_IDirect3DRGBDevice")
  631.     End If
  632.     RestoreDeviceViewportCharacteristics
  633.     'let others functions render
  634.     m_bResizing = False
  635.     'update the display
  636.     UserControl_Paint
  637. End Sub
  638. '-============================================================
  639. ' UserControl_Initialize
  640. '-============================================================
  641. Private Sub UserControl_Initialize()
  642.     Dim b As Boolean
  643.     m_bCreateFromClipper = TRUE
  644.     m_scenespeed = 30
  645.     RotateRadius = 100
  646.     b = InitSceneGraph()
  647.     If Not b Then
  648.         RaiseEvent DirecXNotInstalled
  649.         Exit Sub
  650.     End If
  651.     UserControl_Resize
  652. End Sub
  653. '-============================================================
  654. ' UserControl_Terminate
  655. '-============================================================
  656. Private Sub UserControl_Terminate()
  657.     CloseFSWindow
  658.     Cleanup
  659.     CleanupRMObjects
  660.     m_binit = False
  661. End Sub
  662. '-============================================================
  663. ' CloseFSWindow
  664. '-============================================================
  665. Private Sub CloseFSWindow()
  666.     On Local Error Resume Next
  667.     Unload frmFSWindow
  668.     Set frmFSWindow = Nothing
  669. End Sub
  670. '-============================================================
  671. ' Marshall full screen events
  672. '-============================================================
  673. Private Sub frmFSWindow_KeyDown(keyCode As Integer, Shift As Integer)
  674.     RaiseEvent KeyDown(keyCode, Shift)
  675. End Sub
  676. Private Sub frmFSWindow_KeyPress(KeyAscii As Integer)
  677.     RaiseEvent KeyPress(KeyAscii)
  678. End Sub
  679. Private Sub frmFSWindow_KeyUp(keyCode As Integer, Shift As Integer)
  680.     RaiseEvent KeyUp(keyCode, Shift)
  681. End Sub
  682. Private Sub frmFSWindow_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  683.     m_bMouseDown = True
  684.     RotateFromXY CInt(X), CInt(Y), True
  685.     RaiseEvent MouseDown(Button, Shift, X, Y)
  686. End Sub
  687. Private Sub frmFSWindow_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  688.     If m_bMouseDown Then RotateFromXY CInt(X), CInt(Y), False
  689.     RaiseEvent MouseMove(Button, Shift, X, Y)
  690. End Sub
  691. Private Sub frmFSWindow_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  692.     m_bMouseDown = False
  693.     RaiseEvent MouseUp(Button, Shift, X, Y)
  694. End Sub
  695. Private Sub frmFSWindow_Click()
  696.     RaiseEvent Click
  697. End Sub
  698. Private Sub frmFSWindow_DblClick()
  699.     RaiseEvent DblClick
  700. End Sub
  701. Private Sub frmFSWindow_Paint()
  702.     UserControl_Paint
  703. End Sub
  704. '-============================================================
  705. ' Marshall windowed events
  706. '-============================================================
  707. Private Sub UserControl_KeyDown(keyCode As Integer, Shift As Integer)
  708.     RaiseEvent KeyDown(keyCode, Shift)
  709. End Sub
  710. Private Sub UserControl_KeyPress(KeyAscii As Integer)
  711.     RaiseEvent KeyPress(KeyAscii)
  712. End Sub
  713. Private Sub UserControl_KeyUp(keyCode As Integer, Shift As Integer)
  714.     RaiseEvent KeyUp(keyCode, Shift)
  715. End Sub
  716. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  717.     m_bMouseDown = True
  718.     RotateFromXY CInt(X), CInt(Y), True
  719.     RaiseEvent MouseDown(Button, Shift, X, Y)
  720. End Sub
  721. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  722.     If m_bMouseDown Then RotateFromXY CInt(X), CInt(Y), False
  723.     RaiseEvent MouseMove(Button, Shift, X, Y)
  724. End Sub
  725. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  726.     m_bMouseDown = False
  727.     RaiseEvent MouseUp(Button, Shift, X, Y)
  728. End Sub
  729. Private Sub UserControl_Click()
  730.     RaiseEvent Click
  731. End Sub
  732. Private Sub UserControl_DblClick()
  733.     RaiseEvent DblClick
  734. End Sub
  735. Private Sub UserControl_Paint()
  736.     If m_binit = False Then Exit Sub
  737.     RaiseEvent Paint
  738.     m_rmDevice.HandleActivate 0
  739.     m_rmDevice.HandlePaint m_hwnd
  740.     Render
  741. End Sub
  742. '-============================================================
  743. ' Cleanup objects that can hold onto vmem
  744. '-============================================================
  745. Private Sub Cleanup()
  746.     Err.Clear
  747.     On Local Error Resume Next
  748.     m_dd.RestoreDisplayMode
  749.     m_dd.SetCooperativeLevel m_hwnd, DDSCL_NORMAL
  750.     Set m_backBuffer = Nothing
  751.     Set m_frontBuffer = Nothing
  752.     Set m_dd = Nothing
  753.     Set m_ddClip = Nothing
  754.     Set m_rmViewport = Nothing
  755.     Set m_rmDevice = Nothing
  756.     m_bfullscreen = False
  757.     m_binit = False
  758. End Sub
  759. '-============================================================
  760. ' Cleanup rest of RM objects
  761. '-============================================================
  762. Private Sub CleanupRMObjects()
  763.     Set m_rmFrameCamera = Nothing
  764.     Set m_rmFrameScene = Nothing
  765.     Set m_rmFrameDirLight = Nothing
  766.     Set m_rmFrameAmbientLight = Nothing
  767.     Set m_rmDirLight = Nothing
  768.     Set m_rmAmbientLight = Nothing
  769. End Sub
  770. '-====================================================
  771. ' RestoreDeviceViewportCharacteristics
  772. ' when the viewport is destroyed for whatever reason (resize)
  773. ' this function allows us to retain the characteristics
  774. ' of the viewport we just destroyed
  775. '-====================================================
  776. Private Sub RestoreDeviceViewportCharacteristics()
  777.     With m_DevInfo
  778.         m_rmDevice.SetDither .bDither
  779.         m_rmDevice.SetName .Name
  780.         m_rmDevice.SetQuality .Quality
  781.         m_rmDevice.SetRenderMode .RenderMode
  782.         m_rmDevice.SetShades .Shades
  783.         m_rmDevice.SetTextureQuality .TexQ
  784.     End With
  785.     With m_ViewInfo
  786.         m_rmViewport.SetBack .Back
  787.         m_rmViewport.SetField .Field
  788.         m_rmViewport.SetFront .Front
  789.         m_rmViewport.SetName .Name
  790.         m_rmViewport.SetProjection .Projection
  791.         m_rmViewport.SetPlane .left, .right, .bottom, .top
  792.         m_rmViewport.SetUniformScaling .scaling
  793.     End With
  794. End Sub
  795. '-====================================================
  796. ' SaveDeviceViewportCharacteristics
  797. ' we need to retain certain characteristics about the
  798. ' viewport and device so that they look the same
  799. ' when recreated after a resize
  800. '-====================================================
  801. Private Sub SaveDeviceViewportCharacteristics()
  802.     With m_DevInfo
  803.         .bDither = m_rmDevice.GetDither
  804.         .Name = m_rmDevice.GetName
  805.         .Quality = m_rmDevice.GetQuality
  806.         .RenderMode = m_rmDevice.GetRenderMode
  807.         .Shades = m_rmDevice.GetShades
  808.         .TexQ = m_rmDevice.GetTextureQuality
  809.         .WFoptions = m_rmDevice.GetWireframeOptions
  810.     End With
  811.     With m_ViewInfo
  812.         .Back = m_rmViewport.GetBack
  813.         .Field = m_rmViewport.GetField
  814.         .Front = m_rmViewport.GetFront
  815.         .Name = m_rmViewport.GetName
  816.         .Projection = m_rmViewport.GetProjection
  817.         .scaling = m_rmViewport.GetUniformScaling
  818.         m_rmViewport.GetPlane .left, .right, .bottom, .top
  819.         
  820.     End With
  821. End Sub
  822. '-====================================================
  823. ' SetDeviceDefaults
  824. '-====================================================
  825. Private Sub SetDeviceDefaults()
  826.     m_rmDevice.SetQuality D3DRMRENDER_GOURAUD
  827. End Sub
  828. '-====================================================
  829. ' InitSceneGraph
  830. ' create default lighting and cameras
  831. '-====================================================
  832. Private Function InitSceneGraph() As Boolean
  833.     On Local Error GoTo errOut
  834.     'create a skeletal scene graph
  835.     Set m_rm = m_dx.Direct3DRMCreate()
  836.     Set m_rmFrameScene = m_rm.CreateFrame(Nothing)
  837.     Set m_rmFrameCamera = m_rm.CreateFrame(m_rmFrameScene)
  838.     m_rmFrameCamera.SetPosition Nothing, 0, 0, -10
  839.     'create a bright directional light
  840.     Set m_rmFrameDirLight = m_rm.CreateFrame(m_rmFrameScene)
  841.     Set m_rmDirLight = m_rm.CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, 1, 1, 1)
  842.     'create a dull ambient light
  843.     Set m_rmAmbientLight = m_rm.CreateLightRGB(D3DRMLIGHT_AMBIENT, 0.2, 0.2, 0.2)
  844.     'add the lights to the scene graph
  845.     m_rmFrameDirLight.AddLight m_rmDirLight
  846.     m_rmFrameScene.AddLight m_rmAmbientLight
  847.     m_rmFrameDirLight.SetPosition Nothing, 5, 5, -5
  848.     m_rmFrameDirLight.LookAt m_rmFrameScene, Nothing, 0
  849.        
  850.     InitSceneGraph = True
  851.     Exit Function
  852. errOut:
  853.     InitSceneGraph = False
  854. End Function
  855. '-============================================================
  856. ' RotateFromXY
  857. '-============================================================
  858. Public Sub RotateFromXY(X As Integer, Y As Integer, bStartPos As Boolean)
  859.     If RotateFrame Is Nothing Then Exit Sub
  860.     If RotateRadius = 0 Then Exit Sub
  861.     If bStartPos Then
  862.         m_lastX = X
  863.         m_lastY = Y
  864.         Exit Sub
  865.     End If
  866.     If RotateMethod = ROTATE_GLOBE Then
  867.         RotateGlobe X, Y
  868.     Else
  869.         RotateTrackBall X, Y
  870.     End If
  871.     Update
  872. End Sub
  873. '-============================================================
  874. ' RotateTrackBall
  875. '-============================================================
  876. Private Sub RotateTrackBall(X As Integer, Y As Integer)
  877.     On Local Error GoTo errOut:
  878.     Dim delta_x As Single, delta_y As Single
  879.     Dim delta_r As Single, radius As Single, denom As Single, angle As Single
  880.     ' rotation axis in camcoords, worldcoords, sframecoords
  881.     Dim axisC As D3DVECTOR
  882.     Dim wc As D3DVECTOR
  883.     Dim axisS As D3DVECTOR
  884.     Dim base As D3DVECTOR
  885.     Dim origin As D3DVECTOR
  886.     delta_x = X - m_lastX
  887.     delta_y = Y - m_lastY
  888.     m_lastX = X
  889.     m_lastY = Y
  890.     delta_r = Sqr(delta_x * delta_x + delta_y * delta_y)
  891.     radius = RotateRadius
  892.     denom = Sqr(radius * radius + delta_r * delta_r)
  893.     If (delta_r = 0 Or denom = 0) Then Exit Sub
  894.     angle = (delta_r / denom)
  895.     axisC.X = (-delta_y / delta_r)
  896.     axisC.Y = (-delta_x / delta_r)
  897.     axisC.z = 0
  898.     m_rmFrameCamera.Transform wc, axisC
  899.     RotateFrame.InverseTransform axisS, wc
  900.     m_rmFrameCamera.Transform wc, origin
  901.     RotateFrame.InverseTransform base, wc
  902.     axisS.X = axisS.X - base.X
  903.     axisS.Y = axisS.Y - base.Y
  904.     axisS.z = axisS.z - base.z
  905.     RotateFrame.AddRotation D3DRMCOMBINE_BEFORE, axisS.X, axisS.Y, axisS.z, angle
  906. errOut:
  907. End Sub
  908. '-============================================================
  909. ' RotateGlobe
  910. '-============================================================
  911. Private Sub RotateGlobe(newx As Integer, newy As Integer)
  912.    On Local Error GoTo errOut:
  913.    Dim X As Integer
  914.    Dim Y As Integer
  915.    Dim dx As Integer
  916.    Dim dy As Integer
  917.     dx = m_lastX - newx
  918.     dy = m_lastY - newy
  919.     X = m_lastX
  920.     Y = m_lastY
  921.     m_lastX = 0
  922.     RotateFrame.AddRotation D3DRMCOMBINE_BEFORE, 0, 1, 0, (3.14 / (10 * RotateRadius)) * dx
  923.     RotateTrackBall 0, newy
  924.     m_lastX = newx
  925.                     
  926. errOut:
  927. End Sub
  928. '-============================================================
  929. ' GetBoundingBox
  930. '-============================================================
  931. Public Sub GetBoundingBox(frame As Direct3DRMFrame3, ByRef xmin As Single, ByRef ymin As Single, ByRef zmin As Single, ByRef xmax As Single, ByRef ymax As Single, ByRef zmax As Single)
  932.     Dim box1 As D3DRMBOX
  933.     Dim mb As Direct3DRMMeshBuilder3
  934.     Set mb = m_rm.CreateMeshBuilder()
  935.     mb.AddFrame frame
  936.     mb.GetBox box1
  937.     xmin = box1.Min.X
  938.     ymin = box1.Min.Y
  939.     zmin = box1.Min.z
  940.     xmax = box1.Max.X
  941.     ymax = box1.Max.Y
  942.     zmax = box1.Max.z
  943.         
  944. End Sub
  945. '-============================================================
  946. ' CreateSheetMesh
  947. '-============================================================
  948. Public Function CreateSheetMesh(nSides As Integer, height As Single, width As Single) As Direct3DRMMeshBuilder3
  949.     Dim m As Direct3DRMMeshBuilder3
  950.     Dim f As Direct3DRMFace2
  951.     Set m = m_rm.CreateMeshBuilder()
  952.         
  953.     Dim dx  As Single
  954.     Dim dy  As Single
  955.     dy = height / 2
  956.     dx = width / 2
  957.     'Front Face
  958.     Set f = m_rm.CreateFace()
  959.     f.AddVertex dx, dy, 0
  960.     f.AddVertex dx, -dy, 0
  961.     f.AddVertex -dx, -dy, 0
  962.     f.AddVertex -dx, dy, 0
  963.     m.AddFace f
  964.     m.SetTextureCoordinates 3, 0, 0
  965.     m.SetTextureCoordinates 2, 0, 1
  966.     m.SetTextureCoordinates 1, 1, 1
  967.     m.SetTextureCoordinates 0, 1, 0
  968.     If nSides > 1 Then
  969.         'Back Face
  970.         Set f = m_rm.CreateFace()
  971.         f.AddVertex -dx, dy, 0
  972.         f.AddVertex -dx, -dy, 0
  973.         f.AddVertex dx, -dy, 0
  974.         f.AddVertex dx, dy, 0
  975.         m.AddFace f
  976.         m.SetTextureCoordinates 7, 0, 0
  977.         m.SetTextureCoordinates 6, 0, 1
  978.         m.SetTextureCoordinates 5, 1, 1
  979.         m.SetTextureCoordinates 4, 1, 0
  980.     End If
  981.         
  982.     m.SetName "Sheet" + CStr(m_createid)
  983.     m_createid = m_createid + 1
  984.     Set CreateSheetMesh = m
  985. End Function
  986. '-============================================================
  987. ' CreateBoxMesh
  988. '-============================================================
  989. Public Function CreateBoxMesh(width As Single, height As Single, depth As Single) As Direct3DRMMeshBuilder3
  990.     Dim m As Direct3DRMMeshBuilder3
  991.     Dim f As Direct3DRMFace2
  992.     Set m = m_rm.CreateMeshBuilder()
  993.     Dim dx As Single
  994.     Dim dy As Single
  995.     Dim dz As Single
  996.     dx = width / 2
  997.     dy = height / 2
  998.     dz = depth / 2
  999.     'Front Face
  1000.     Set f = m_rm.CreateFace()
  1001.     f.AddVertex dx, dy, -dz
  1002.     f.AddVertex dx, -dy, -dz
  1003.     f.AddVertex -dx, -dy, -dz
  1004.     f.AddVertex -dx, dy, -dz
  1005.     m.AddFace f
  1006.     'Back Face
  1007.     Set f = m_rm.CreateFace()
  1008.     f.AddVertex -dx, dy, dz
  1009.     f.AddVertex -dx, -dy, dz
  1010.     f.AddVertex dx, -dy, dz
  1011.     f.AddVertex dx, dy, dz
  1012.     m.AddFace f
  1013.     'Right face
  1014.     Set f = m_rm.CreateFace()
  1015.     f.AddVertex dx, dy, dz
  1016.     f.AddVertex dx, -dy, dz
  1017.     f.AddVertex dx, -dy, -dz
  1018.     f.AddVertex dx, dy, -dz
  1019.     m.AddFace f
  1020.     'Left face
  1021.     Set f = m_rm.CreateFace()
  1022.     f.AddVertex -dx, -dy, dz
  1023.     f.AddVertex -dx, dy, dz
  1024.     f.AddVertex -dx, dy, -dz
  1025.     f.AddVertex -dx, -dy, -dz
  1026.     m.AddFace f
  1027.     'Top face
  1028.     Set f = m_rm.CreateFace()
  1029.     f.AddVertex dx, dy, -dz
  1030.     f.AddVertex -dx, dy, -dz
  1031.     f.AddVertex -dx, dy, dz
  1032.     f.AddVertex dx, dy, dz
  1033.     m.AddFace f
  1034.     'Bottom face
  1035.     Set f = m_rm.CreateFace()
  1036.     f.AddVertex dx, -dy, dz
  1037.     f.AddVertex -dx, -dy, dz
  1038.     f.AddVertex -dx, -dy, -dz
  1039.     f.AddVertex dx, -dy, -dz
  1040.     m.AddFace f
  1041.     m.SetTextureCoordinates 3, 0, 0
  1042.     m.SetTextureCoordinates 2, 0, 1
  1043.     m.SetTextureCoordinates 1, 1, 1
  1044.     m.SetTextureCoordinates 0, 1, 0
  1045.     m.SetTextureCoordinates 7, 0, 0
  1046.     m.SetTextureCoordinates 6, 0, 1
  1047.     m.SetTextureCoordinates 5, 1, 1
  1048.     m.SetTextureCoordinates 4, 1, 0
  1049.     m.SetTextureCoordinates 11, 0, 0
  1050.     m.SetTextureCoordinates 10, 0, 1
  1051.     m.SetTextureCoordinates 9, 1, 1
  1052.     m.SetTextureCoordinates 8, 1, 0
  1053.     m.SetTextureCoordinates 12, 0, 1
  1054.     m.SetTextureCoordinates 13, 1, 1
  1055.     m.SetTextureCoordinates 14, 1, 0
  1056.     m.SetTextureCoordinates 15, 0, 0
  1057.     m.SetTextureCoordinates 19, 0, 0
  1058.     m.SetTextureCoordinates 18, 0, 1
  1059.     m.SetTextureCoordinates 17, 1, 1
  1060.     m.SetTextureCoordinates 16, 1, 0
  1061.                                 
  1062.     m.SetName "Box" + CStr(m_createid)
  1063.     m_createid = m_createid + 1
  1064.     Set CreateBoxMesh = m
  1065. End Function
  1066. '-============================================================
  1067. ' PickTopMesh
  1068. '-============================================================
  1069. Public Function PickTopMesh(X As Long, Y As Long) As Direct3DRMMeshBuilder3
  1070.     On Local Error GoTo errOut
  1071.     Dim pickarray As Direct3DRMPickArray
  1072.     Dim mb As Direct3DRMMeshBuilder3
  1073.     Dim desc As D3DRMPICKDESC
  1074.     Set pickarray = m_rmViewport.Pick(X, Y)
  1075.     If pickarray.GetSize() = 0 Then Exit Function
  1076.     Set mb = pickarray.GetPickVisual(0, desc)
  1077.     Set PickTopMesh = mb
  1078. errOut:
  1079. End Function
  1080. '-============================================================
  1081. ' PickTopFrame
  1082. '-============================================================
  1083. Public Function PickTopFrame(X As Long, Y As Long) As Direct3DRMFrame3
  1084.     On Local Error GoTo errOut
  1085.     Dim pickarray As Direct3DRMPickArray
  1086.     Dim f As Direct3DRMFrame3
  1087.     Dim fa As Direct3DRMFrameArray
  1088.     Dim desc As D3DRMPICKDESC
  1089.     Set pickarray = m_rmViewport.Pick(X, Y)
  1090.     If pickarray.GetSize() = 0 Then Exit Function
  1091.     Set fa = pickarray.GetPickFrame(0, desc)
  1092.     Set f = fa.GetElement(fa.GetSize() - 1)
  1093.     Set PickTopFrame = f
  1094. errOut:
  1095. End Function
  1096. '-============================================================
  1097. ' CreateUpdateableTexture
  1098. '-============================================================
  1099. Public Function CreateUpdateableTexture(w As Long, h As Long, sfile As String) As Direct3DRMTexture3
  1100.     On Local Error GoTo errOut
  1101.     Dim sLoadFile As String
  1102.     Dim ddsd As DDSURFACEDESC2
  1103.     Dim SurfaceObject As DirectDrawSurface4
  1104.     Dim out As Direct3DRMTexture3
  1105.     Dim Init As Boolean
  1106.     ddsd.lFlags = DDSD_CAPS
  1107.     ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
  1108.     If (w <> 0) Then
  1109.         ddsd.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
  1110.         ddsd.lWidth = w
  1111.         ddsd.lHeight = h
  1112.     End If
  1113.     If sfile = "" Then
  1114.         Set SurfaceObject = m_dd.CreateSurface(ddsd)
  1115.     Else
  1116.         Set SurfaceObject = m_dd.CreateSurfaceFromFile(sfile, ddsd)
  1117.     End If
  1118.     Set out = m_rm.CreateTextureFromSurface(SurfaceObject)
  1119.     Set CreateUpdateableTexture = out
  1120. errOut:
  1121.     Set SurfaceObject = Nothing
  1122. End Function
  1123. '-============================================================
  1124. ' LoadBackground
  1125. '-============================================================
  1126. Function LoadBackground(sfile As String) As Boolean
  1127.     On Local Error GoTo errOut
  1128.     Dim ddsd As DDSURFACEDESC2
  1129.     Set Background = m_dd.CreateSurfaceFromFile(sfile, ddsd)
  1130.     LoadBackground = True
  1131. errOut:
  1132. End Function
  1133.