home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / room3d / mmain.bas < prev    next >
Encoding:
BASIC Source File  |  1999-05-18  |  48.6 KB  |  1,142 lines

  1. Attribute VB_Name = "mMain"
  2. Option Explicit
  3. 'Almost all this module (except for buildworld and checkKeys)
  4. 'Was taken from Wolfgang Kienreich's Xdemo3D example
  5. 'Hardware support code was also fixed for me by Wolfgang.... many, many,
  6. 'thanx for that
  7.  
  8. 'Variables for 3d objects in the scene
  9. Global ObjectFrame(1 To 2) As IDirect3DRMFrame2       ' Frame to hold object
  10. Public RMLight(1 To 2) As IDirect3DRMFrame
  11.  
  12. 'Variables holding DDraw and D3DRM instances ...
  13. Public G_oDDInstance As IDirectDraw                 ' Instance of DirectDraw interface
  14. Public G_oD3DInstance As IDirect3DRM                ' Instance of Direct3DRM interface
  15. ' Variables for primary D3DRM display system ...
  16. Public RMDevice As IDirect3DRMDevice2           ' Device to use for Direct3DRM operations
  17. Public RMViewport As IDirect3DRMViewPort        ' Viewport for Direct3DRM to display results in
  18. Public RMScene As IDirect3DRMFrame2       ' Top level frame that contains all other frames
  19. Public RMCamera As IDirect3DRMFrame2                ' Frame to contain the camera; The viewport is created from this frame
  20. Public RMDriver As tD3DDriver                   ' Driver for use with Direct3DRM
  21. Public RMDriverExist As Boolean               ' Flag holding presence of driver (equals driver enumeration success)
  22. Public G_dCamPosLookup(359) As D3DVECTOR            ' Lookup table of position values for camera
  23. Public G_nCamPosCurrent As Integer                  ' Current position of camera according to lookup table
  24. Global BufferDC As Long
  25. ' Variables for DirectDraw blit system ...
  26. Public G_oDDPrimary As IDirectDrawSurface3          ' Primary DirectDraw surface that is displayed on the form
  27. Public G_oDDBackbuffer As IDirectDrawSurface3       ' Backbuffer DirectDraw surface that is flipped onto the primary
  28. Public G_dDDWindow(0) As tDDWindow                  ' Buffers holding windows for effects
  29.  
  30. ' Various variables ...
  31. Public G_nFrameCount As Long                       ' Global framecounter
  32. Public G_nFrameAvg As Double                       ' Global average frames per second
  33.  
  34. ' Various constants
  35.     Public Const PIFACTOR = 0.0174532
  36.  
  37. ' Types for use with XDemo3D ...
  38.     ' Driver type for enumeration of D3D driver
  39.     Public Type tD3DDriver
  40.       DESC    As String                         ' Driver description
  41.       NAME    As String                         ' Driver name
  42.       GUID    As Byte                           ' Unique interface ID for accessing driver
  43.       GUID1   As Byte                           ' ...
  44.       GUID2   As Byte                           ' ...
  45.       GUID3   As Byte                           ' ...
  46.       GUID4   As Byte                           ' ...
  47.       GUID5   As Byte                           ' ...
  48.       GUID6   As Byte                           ' ...
  49.       GUID7   As Byte                           ' ...
  50.       GUID8   As Byte                           ' ...
  51.       GUID9   As Byte                           ' ...
  52.       GUID10  As Byte                           ' ...
  53.       GUID11  As Byte                           ' ...
  54.       GUID12  As Byte                           ' ...
  55.       GUID13  As Byte                           ' ...
  56.       GUID14  As Byte                           ' ...
  57.       GUID15  As Byte                           ' ...
  58.       DEVDESC As D3DDEVICEDESC                  ' Device description for use by D3DRM
  59.       HDW     As Boolean                        ' Device is hardware
  60.       EMU     As Boolean                        ' Device is software-emulated
  61.       RGB     As Boolean                        ' Device has rgb caps
  62.       MONO    As Boolean                        ' Device has mono ramp caps
  63.     End Type
  64.     ' Viewport window for scrolling effects windows
  65.     Public Type tDDWindow
  66.         nX As Integer
  67.         nY As Integer
  68.         nDX As Integer
  69.         nDY As Integer
  70.         oDDSurface As IDirectDrawSurface3
  71.         dRenderArea As RECT
  72.     End Type
  73.     Global PickInfo As String
  74. ' APPERROR: Reports application errors and terminates application properly
  75. Public Sub AppError(nNumber As Long, sText As String, sSource As String)
  76.  
  77.     ' Enable error handling
  78.     On Error Resume Next
  79.     
  80.     ' Cleanup
  81.     Call AppTerminate
  82.     
  83.     ' Display error
  84.     MsgBox "ERROR: " & IIf(InStr(1, UCase(sText), "AUTOM") > 0, "DirectX reports '" & GetDXError(nNumber) & "'", " Application reports '" & sText & "'") & vbCrLf & "SOURCE: " & sSource, vbCritical + vbOKOnly, "XDEMO3D"
  85.     ' Terminate program
  86.     End
  87. End Sub
  88. Public Sub AppInitialize() 'Startup of program
  89.     ' Enable error handling
  90.         On Error GoTo E_AppInitialize
  91.  
  92.     ' Setup local variables...
  93.     
  94.         Dim L_dDDSD As DDSURFACEDESC           ' Utility surface description
  95.         Dim L_dDDSC As DDSCAPS                 ' Utility display capabilities description
  96.         Dim L_oD3DIM As IDirect3D2             ' Utility Direct3DIM interface for retrieving drivers
  97.         Dim L_dDDCK As DDCOLORKEY              ' Color key for applying to various surfaces
  98.         
  99.     ' Initialize DirectDraw interface instance ...
  100.         ' Create DirectDraw instance
  101.         DirectDrawCreate ByVal 0&, G_oDDInstance, Nothing
  102.  
  103.         ' Check instance existance, terminate if missing
  104.         If G_oDDInstance Is Nothing Then
  105.            AppError 0, "Could not create DirectDraw instance", "AppInitialize"
  106.            Exit Sub
  107.         End If
  108.          
  109.         ' Set cooperation mode of DirectX
  110.         G_oDDInstance.SetCooperativeLevel fMain.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN
  111.         
  112.         ' Set display mode
  113.         G_oDDInstance.SetDisplayMode 640, 480, 16
  114.         ' Initialize primary surface description
  115.         With L_dDDSD
  116.             ' Get Structure size
  117.             .dwSize = Len(L_dDDSD)
  118.             ' Structure uses Surface Caps and count of BackBuffers
  119.             .dwFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  120.             ' Structure describes a flippable (buffered) surface
  121.             .DDSCAPS.dwCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX Or DDSCAPS_SYSTEMMEMORY
  122.             ' Structure uses one BackBuffer
  123.             .dwBackBufferCount = 1
  124.          End With
  125.     
  126.         ' Create primary surface from structure
  127.         G_oDDInstance.CreateSurface L_dDDSD, G_oDDPrimary, Nothing
  128.  
  129.         ' Check primary existance, terminate if missing
  130.         If G_oDDPrimary Is Nothing Then
  131.            AppError 0, "Could not create primary surface", "AppInitialize"
  132.            Exit Sub
  133.         End If
  134.     
  135.     ' Initialize backbuffer from primary ...
  136.     
  137.         ' Set surface description to backbuffer creation
  138.         L_dDDSD.dwFlags = DDSD_CAPS
  139.         L_dDDSD.DDSCAPS.dwCaps = DDSCAPS_BACKBUFFER
  140.         
  141.         ' Create backbuffer from frontbuffer
  142.         G_oDDPrimary.GetAttachedSurface L_dDDSD.DDSCAPS, G_oDDBackbuffer
  143.         ' Check backbuffer existance, terminate if missing
  144.         If G_oDDBackbuffer Is Nothing Then
  145.            AppError 0, "Could not create backbuffer", "AppInitialize"
  146.            Exit Sub
  147.         End If
  148.  
  149.         
  150.     ' Initialize windows for displaying various effects
  151.         Call CreateWindows
  152.             
  153.     ' Initialize Direct3DRM interface instance ...
  154.     
  155.         ' Create Direct3DRM instance
  156.         Direct3DRMCreate G_oD3DInstance
  157.     
  158.         ' Check instance existance, terminate if missing
  159.         If G_oD3DInstance Is Nothing Then
  160.            AppError 0, "Could not create D3DRM instance", "AppInitialize"
  161.            Exit Sub
  162.         End If
  163.     
  164.     ' Initialize Direct3DRM driver ...
  165.     
  166.         ' Get a Direct3D immediate object from the existing DirectDraw object
  167.         Set L_oD3DIM = G_oDDInstance
  168.     
  169.         ' Set error handler to local for enumeration only
  170.         On Error Resume Next
  171.         
  172.         ' Start the callback that does the driver enumeration
  173.         L_oD3DIM.EnumDevices AddressOf EnumDeviceCallback, 0
  174.     
  175.         ' Catch any error resulting from the enumeration and terminate
  176.         If err.Number > 0 Then
  177.            AppError err.Number, err.Description, "AppInitialize"
  178.            Exit Sub
  179.         End If
  180.     
  181.         ' Reset error handler to default
  182.         On Error GoTo E_AppInitialize
  183.         
  184.         ' Reset Direct3D immediate object
  185.         Set L_oD3DIM = Nothing
  186.         
  187.     ' Initialize D3DRM display system
  188.     
  189.         ' Create a D3DRM device from the 3D buffer
  190.         G_oD3DInstance.CreateDeviceFromSurface G_dD3DSelectedDriver.GUID, G_oDDInstance, G_dDDWindow(0).oDDSurface, RMDevice
  191.         
  192.         ' Check device existance, terminate if missing
  193.         If RMDevice Is Nothing Then
  194.            AppError 0, "Could not create D3DRM device", "AppInitialize"
  195.            Exit Sub
  196.         End If
  197.     
  198.         ' Set D3DRM device quality
  199.         RMDevice.SetQuality D3DRMLIGHT_ON Or D3DRMFILL_SOLID Or D3DRMSHADE_PHONG
  200.         ' Create the master frame containing all other frames
  201.         G_oD3DInstance.CreateFrame Nothing, RMScene
  202.         ' Create the camera frame containing the primary camera
  203.         G_oD3DInstance.CreateFrame RMScene, RMCamera
  204.         ' Create a D3D viewport from the device, using the camera frame for output
  205.         G_oD3DInstance.CreateViewport RMDevice, RMCamera, 0, 0, 640, 480, RMViewport
  206.             
  207.         ' Check viewport existance, terminate if missing
  208.         If RMViewport Is Nothing Then
  209.            AppError 0, "Could not create D3DRM viewport", "AppInitialize"
  210.            Exit Sub
  211.         End If
  212.         
  213.         ' Initialize scene and display settings ...
  214.         Call CreateScene
  215.         ' Create scene from 3DSE mesh
  216.         Call BuildWorld
  217.         'Apply object to objectbuilder
  218.         Call AddObjects
  219.         Call AddLights
  220.         RMDevice.SetDither 0
  221.         Exit Sub
  222. E_AppInitialize:
  223.         AppError err.Number, err.Description, "AppInitialize"
  224. End Sub
  225. Public Sub AppLoop() 'Contains MASTER LOOP for doing all stuff
  226.  
  227.     ' Enable error handling
  228.         On Error GoTo E_AppLoop
  229.  
  230.     ' Setup local variables...
  231.         Dim L_nNextFrameTime As Long        ' Timer used to time frames to a minimum duration
  232.         Dim L_nFrameCount As Long           ' Frame counter used for calculating average framerate
  233.         Dim L_nNextSecond As Long           ' TimeGetTime value above which next second begins
  234.         Dim L_nCurrentTime As Long          ' Time at start of frame, to avoid multiple calls of TimeGetTime
  235.         Dim L_dRenderArea As RECT           ' Rectangle to describe render area for blitting
  236.         Dim L_dDDBLTFX As DDBLTFX           ' Holds F/X settings for blitting
  237.     
  238.         ' Prepare BLTFX structure for color fill blit to clear backbuffer
  239.         With L_dDDBLTFX
  240.             .dwSize = Len(L_dDDBLTFX)
  241.             .dwFillColor = 0
  242.         End With
  243.         
  244.     ' Master loop controlling application behavior...
  245.         Do
  246.             ' Do frame timing and statistics ...
  247.                 ' Increase global frame counter
  248.                 G_nFrameCount = G_nFrameCount + 1
  249.                 
  250.                 ' Get frame start time
  251.                 L_nCurrentTime = timeGetTime
  252.                 
  253.                 ' Increase frame count for avg frametime calculation
  254.                 L_nFrameCount = L_nFrameCount + 1
  255.                 
  256.                 ' Protocol frame time: Count frames and write out average frame count every second
  257.                 If L_nNextSecond < L_nCurrentTime Then
  258.                     G_nFrameAvg = (G_nFrameAvg + L_nFrameCount) / 2
  259.                     L_nNextSecond = L_nCurrentTime + 1000
  260.                     L_nFrameCount = 0
  261.                 End If
  262.             
  263.                 ' Prepare timing: Set next frame time to current time plus minimum frame duration (15fps , makes for ~60ms)
  264.                 L_nNextFrameTime = L_nCurrentTime + 50
  265.                 
  266.                 ' Query user input
  267.                         
  268.             ' Clear backbuffer ...
  269.                 
  270.                 ' FX-Blit filling background with black
  271.                 With L_dRenderArea
  272.                     .Top = 0
  273.                     .Left = 0
  274.                     .Bottom = 480
  275.                     .Right = 640
  276.                 End With
  277.                 G_oDDBackbuffer.Blt L_dRenderArea, ByVal Nothing, ByVal 0&, DDBLT_COLORFILL, L_dDDBLTFX
  278.                 
  279.             ' Check keyboard for movement
  280.             Call CheckKeyboard
  281.             ' Check Mouse for firing
  282.             Call CheckMouse
  283.             ' Text Output Procedures
  284.             Call TextOutput("Direct3DRM Room Demo " & PickInfo, 0, 10)
  285.             ' Do updating for D3DRM scene ...
  286.             Call UpdateScene
  287.             ' Update display system ...
  288.             Call UpdateWindows
  289.                                 
  290.             ' Flip DirectX buffers...
  291.             G_oDDPrimary.Flip Nothing, 0
  292.                 
  293.             'Uses a set speed (in case FPS is too high, yeah right)
  294.             Do
  295.             Loop Until timeGetTime > L_nNextFrameTime
  296.         
  297.         Loop
  298.         
  299.     ' Error handling ...
  300.     
  301.         Exit Sub
  302.  
  303. E_AppLoop:
  304.         ' Resume to ignore the weird math errors Direct3DRM reports from time to time
  305.         Resume Next
  306. End Sub
  307. Public Sub AppTerminate() 'exit program and shut down directx to avoid crashing
  308.     ' Enable error handling...
  309.         On Error GoTo E_AppTerminate
  310.  
  311.     ' Setup local variables ...
  312.         Dim L_nRun As Integer             ' Variable to run through various array data
  313.     
  314.     ' Return control from DirectX to windows ...
  315.  
  316.         ' Restore old resolution and depth
  317.         G_oDDInstance.RestoreDisplayMode
  318.     
  319.         ' Return control to windows
  320.         G_oDDInstance.SetCooperativeLevel fMain.hWnd, DDSCL_NORMAL
  321.         
  322.     ' Reset DirectX objects ...
  323.         
  324.         ' D3DRM display system ...
  325.         
  326.             Set RMCamera = Nothing
  327.             Set RMScene = Nothing
  328.             Set RMViewport = Nothing
  329.             Set RMDevice = Nothing
  330.             
  331.         ' Object Builders
  332.  
  333.             Set ObjectFrame(1) = Nothing
  334.             Set ObjectFrame(2) = Nothing
  335.         
  336.         ' DD Display system ...
  337.         
  338.             Set G_oDDBackbuffer = Nothing
  339.             Set G_oDDPrimary = Nothing
  340.                 
  341.         ' DD Windows ...
  342.         
  343.             For L_nRun = 0 To 4
  344.                 Set G_dDDWindow(L_nRun).oDDSurface = Nothing
  345.             Next
  346.                 
  347.         ' DirectX interfaces ...
  348.         
  349.             Set G_oDDInstance = Nothing
  350.             Set G_oD3DInstance = Nothing
  351.         
  352.         ' Dinput Keyboard
  353.             
  354.             Set Keyboarddevice2 = Nothing
  355.  
  356.     ' Error handling ...
  357.         
  358.         Exit Sub
  359.  
  360. E_AppTerminate:
  361.         ' Resume to ensure that all objects available are cleaned up
  362.         Resume Next
  363. End Sub
  364. Private Sub CreateScene() 'creates 3d scene
  365.  
  366.     ' Enable error handling...
  367.         On Error GoTo E_CreateScene
  368.  
  369.     ' Setup local variables ...
  370.         Dim L_oD3DLight As IDirect3DRMLight     ' Variable for light creating
  371.         Dim L_nRun As Single                    ' Variable to run through arrays
  372.         Dim L_dDDCK As DDCOLORKEY               ' Color key for making status display transparent
  373.         
  374.     ' Initialize scenario settings ...
  375.     
  376.         ' Create position lookup table for camera
  377.         For L_nRun = 0 To 179
  378.             With G_dCamPosLookup(L_nRun)
  379.                 .z = 5
  380.                 .X = 11 + Sin((L_nRun * 2) * PIFACTOR) * 7.8
  381.                 .Y = 10.5 + Cos((L_nRun * 2) * PIFACTOR) * 7.8
  382.             End With
  383.         Next
  384.         
  385.         ' Set the projection model and properties for the viewport
  386.         With RMViewport
  387.             .SetProjection D3DRMPROJECT_PERSPECTIVE
  388.             .SetBack 20
  389.             .SetFront 1
  390.             .SetUniformScaling 1
  391.         End With
  392.         
  393.         ' Set the scene properties (fog, backcolor)
  394.         With RMScene
  395.             .SetSceneBackgroundRGB 0, 0, 0
  396.         End With
  397.         
  398.         ' Create ambient light
  399.         G_oD3DInstance.CreateLightRGB D3DRMLIGHT_AMBIENT, 0.3, 0.3, 0.3, L_oD3DLight
  400.         RMScene.AddLight L_oD3DLight
  401.         Set L_oD3DLight = Nothing
  402.         
  403.     ' Error handling ...
  404.     
  405.         Exit Sub
  406.  
  407. E_CreateScene:
  408.         AppError err.Number, err.Description, "CreateScene"
  409.         Exit Sub
  410. End Sub
  411. Private Sub CreateWindows() 'creates windowed views
  412.  
  413.     ' Enable error handling...
  414.         On Error GoTo E_CreateWindows
  415.  
  416.     ' Setup local variables ...
  417.     Dim L_dDDCK As DDCOLORKEY
  418.     
  419.     L_dDDCK.dwColorSpaceHighValue = 0
  420.     L_dDDCK.dwColorSpaceLowValue = 0
  421.     
  422.     ' Initialize threed buffer surface ...
  423.     
  424.         With G_dDDWindow(0)
  425.         
  426.             ' Create surface capable of being a 3d device
  427.             Set .oDDSurface = MakeDXSurface(640, 480, True)
  428.    
  429.              ' Set color key to enable transparent blits
  430.             .oDDSurface.SetColorKey DDCKEY_SRCBLT, L_dDDCK
  431.             
  432.             ' Set render area
  433.             .dRenderArea.Top = 0
  434.             .dRenderArea.Left = 0
  435.             .dRenderArea.Bottom = 480
  436.             .dRenderArea.Right = 640
  437.             
  438.             ' Set initial position and motion
  439.             .nX = 0
  440.             .nY = 0
  441.             .nDX = 2
  442.             .nDY = -2
  443.             
  444.         End With
  445.         Exit Sub
  446. E_CreateWindows:
  447.     AppError err.Number, err.Description, "CreateWindows"
  448. End Sub
  449. Private Sub UpdateWindows() 'updates all windowed viewports (3d scene, logo)
  450.     'Exit Sub
  451.     ' Enable error handling ...
  452.         
  453.         On Error GoTo E_UpdateWindows
  454.  
  455.     ' Setup local variables ...
  456.         
  457.         Dim L_nRunWindows As Integer      ' Variable to run through all windows
  458.         
  459.     ' Update all windows that are activated ...
  460.             With G_dDDWindow(0)
  461.                 ' Redraw window contents
  462.                 If Not (G_dDDWindow(L_nRunWindows).oDDSurface Is Nothing) Then
  463.                     G_oDDBackbuffer.BltFast .nX, .nY, .oDDSurface, .dRenderArea, DDBLTFAST_SRCCOLORKEY
  464.                 End If
  465.             End With
  466.         
  467.     ' Error handler ...
  468.         Exit Sub
  469.     
  470. E_UpdateWindows:
  471.         AppError err.Number, err.Description, "UpdateWindows"
  472. End Sub
  473. Private Sub UpdateScene() 'updates 3d scene
  474. '    Exit Sub
  475.     ' Enable error handling ...
  476.         
  477. On Error GoTo E_UpdateScene
  478.     
  479.     ' Setup local variables ...
  480.  
  481.         ' Update D3DRM model
  482.         G_oD3DInstance.Tick 1
  483.         
  484.     ' Error handler ...
  485.         Exit Sub
  486.     
  487. E_UpdateScene:
  488.         AppError err.Number, err.Description, "UpdateScene"
  489. End Sub
  490. Public Sub Restore()
  491.     'If problems, breaking, selecting CTRL+G, and typing restore should fix then
  492.     Call AppTerminate
  493.     End
  494. End Sub
  495. Public Sub CheckKeyboard()
  496.     Dim MoveVelocity As Single
  497.     Dim ThetaFactor As Single
  498.     Dim SinTheta As Single
  499.     Dim CosTheta As Single
  500.     Dim Strafe As Byte 'Byte takes less memory than boolean
  501.     Dim OldPos As D3DVECTOR
  502.     Dim NewFrame As IDirect3DRMFrame
  503.     Dim LookFrame As IDirect3DRMFrame
  504.     
  505.     'Make position Check frames
  506.     G_oD3DInstance.CreateFrame RMScene, NewFrame
  507.     G_oD3DInstance.CreateFrame RMScene, LookFrame
  508.     
  509.     'Get Position of camera BEFORE move
  510.     RMCamera.GetPosition RMScene, OldPos
  511.     
  512.     'Set as position for LookFrame
  513.     LookFrame.SetPosition RMScene, OldPos.X, OldPos.Y, OldPos.z
  514.     
  515.     'Acquire Keyboard State
  516.     Keyboarddevice2.Acquire
  517.     Keyboarddevice2.GetDeviceState 256, KeyboardState(0)
  518.     
  519.     If (KeyboardState(DIK_LCONTROL) And &H80) <> 0 Or (KeyboardState(DIK_RCONTROL) And &H80) <> 0 Then
  520.         Strafe = 1
  521.     Else
  522.         Strafe = 0
  523.     End If
  524.     If (KeyboardState(DIK_LSHIFT) And &H80) <> 0 Or (KeyboardState(DIK_RSHIFT) And &H80) <> 0 Then
  525.         MoveVelocity = 0.3
  526.         ThetaFactor = 0.01745329252
  527.         SinTheta = 0.0871557!      ' Sin(5░)
  528.         CosTheta = 0.9961946!         ' Cos(5░)
  529.     Else
  530.         MoveVelocity = 0.1
  531.         ThetaFactor = 0.008725
  532.         SinTheta = 0.0348995!      ' Sin(2░)
  533.         CosTheta = 0.9993908!         ' Cos(2░)
  534.     End If
  535.     'Movement
  536.     If (KeyboardState(DIK_ESCAPE) And &H80) <> 0 Then
  537.         Call AppTerminate
  538.         End
  539.     ElseIf (KeyboardState(DIK_LEFT) And &H80) <> 0 Then
  540.         If Strafe = 0 Then
  541.             RMCamera.SetOrientation RMCamera, -SinTheta, 0, CosTheta, 0, 1, 0
  542.         Else
  543.             RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, -MoveVelocity, 0, 0
  544.         End If
  545.     ElseIf (KeyboardState(DIK_RIGHT) And &H80) <> 0 Then
  546.         If Strafe = 0 Then
  547.             RMCamera.SetOrientation RMCamera, SinTheta, 0, CosTheta, 0, 1, 0
  548.         Else
  549.             RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, MoveVelocity, 0, 0
  550.         End If
  551.     End If
  552.     If (KeyboardState(DIK_UP) And &H80) <> 0 Then
  553.         RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, 0, 0, MoveVelocity '1
  554.     ElseIf (KeyboardState(DIK_DOWN) And &H80) <> 0 Then
  555.         RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, 0, 0, -MoveVelocity '-1
  556.     End If
  557.     If (KeyboardState(DIK_S) And &H80) <> 0 Then
  558.         RMCamera.AddRotation D3DRMCOMBINE_BEFORE, 1, 0, 0, -(ThetaFactor)
  559.     ElseIf (KeyboardState(DIK_X) And &H80) <> 0 Then
  560.         RMCamera.AddRotation D3DRMCOMBINE_BEFORE, 1, 0, 0, (ThetaFactor)
  561.     End If
  562.     If (KeyboardState(DIK_A) And &H80) <> 0 Then
  563.         RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, 0, MoveVelocity, 0 '0,1,0
  564.     ElseIf (KeyboardState(DIK_Z) And &H80) <> 0 Then
  565.         RMCamera.AddTranslation D3DRMCOMBINE_BEFORE, 0, -MoveVelocity, 0 '0,-1,0
  566.     End If
  567.     If (KeyboardState(DIK_END) And &H80) <> 0 Then
  568.         RMCamera.SetOrientation RMScene, 0, 0, 1, 0, 1, 0
  569.     End If
  570.     Dim CamPos As D3DVECTOR
  571.     Dim CamDir As D3DVECTOR, CamUp As D3DVECTOR
  572.     Dim CameraRay As D3DRMRAY
  573.     Dim ReturnVis As IDirect3DRMPicked2Array
  574.     Dim PickSize As Long
  575.     RMCamera.GetPosition RMScene, CamPos
  576.     NewFrame.SetPosition RMScene, CamPos.X, CamPos.Y, CamPos.z
  577.     LookFrame.LookAt NewFrame, RMScene, D3DRMCONSTRAIN_X
  578.     LookFrame.LookAt NewFrame, RMScene, D3DRMCONSTRAIN_Y
  579.     'RMCamera.GetOrientation RMScene, CamDir, CamUp
  580.     'Use lookframe instead of camera to track backwards, etc moves
  581.     LookFrame.GetOrientation RMScene, CamDir, CamUp
  582.     CameraRay.dvPos = CamPos
  583.     CameraRay.dvDir = CamDir
  584.     RMScene.RayPick RMScene, CameraRay, 0, ReturnVis
  585.     
  586.     PickSize = ReturnVis.GetSize
  587.     If PickSize < 1 Then
  588.         RMCamera.SetPosition RMScene, OldPos.X, OldPos.Y, OldPos.z
  589.         'PickInfo = ""
  590.         Exit Sub
  591.     End If
  592.     'PickInfo = "- Picked " & PickSize & " points"
  593.     
  594.     Set NewFrame = Nothing
  595.     Set LookFrame = Nothing
  596.  
  597. End Sub
  598. Public Sub BuildWorld()
  599.     Dim n As Long, n1 As Long, n2 As Long
  600.     Dim aVertices(0 To 19) As D3DVECTOR
  601.     Dim aFaces(1 To 26) As Long
  602.     Dim aNormals(0) As D3DVECTOR
  603.     Dim d3dRMmeshbuilder As IDirect3DRMMeshBuilder2    ' Meshbuilder to hold created faces
  604.     Dim Face3d As IDirect3DRMFace
  605.     Dim FaceArray As IDirect3DRMFaceArray
  606.     Dim Texture As IDirect3DRMTexture
  607.     Dim ThisTex As String
  608.     Dim Vert As D3DVECTOR
  609.     'These are all the vertices that make up the faces
  610.     aVertices(0).X = -2:     aVertices(0).Y = 0:     aVertices(0).z = 5
  611.     aVertices(1).X = -2:     aVertices(1).Y = 3:     aVertices(1).z = 5
  612.     aVertices(2).X = 2:      aVertices(2).Y = 3:     aVertices(2).z = 5
  613.     aVertices(3).X = 2:     aVertices(3).Y = 0:     aVertices(3).z = 5
  614.     
  615.     aVertices(4).X = -2:    aVertices(4).Y = 0:    aVertices(4).z = -5
  616.     aVertices(5).X = -2:    aVertices(5).Y = 3:    aVertices(5).z = -5
  617.     aVertices(6).X = 2:    aVertices(6).Y = 3:    aVertices(6).z = -5
  618.     aVertices(7).X = 2:    aVertices(7).Y = 0:    aVertices(7).z = -5
  619.     
  620.     aVertices(8).X = 2:    aVertices(8).Y = 3:    aVertices(8).z = -5
  621.     aVertices(9).X = 2:    aVertices(9).Y = 3:    aVertices(9).z = 5
  622.     aVertices(10).X = 2:    aVertices(10).Y = 0:    aVertices(10).z = 5
  623.     aVertices(11).X = 2:    aVertices(11).Y = 0:    aVertices(11).z = -5
  624.     
  625.     aVertices(12).X = -2:    aVertices(12).Y = 3:    aVertices(12).z = -5
  626.     aVertices(13).X = -2:    aVertices(13).Y = 3:    aVertices(13).z = 5
  627.     aVertices(14).X = -2:    aVertices(14).Y = 0:    aVertices(14).z = 5
  628.     aVertices(15).X = -2:    aVertices(15).Y = 0:    aVertices(15).z = -5
  629.     
  630.     aVertices(16).X = -2:    aVertices(16).Y = 0:    aVertices(16).z = -5
  631.     aVertices(17).X = 2:    aVertices(17).Y = 0:    aVertices(17).z = -5
  632.     aVertices(18).X = 2:    aVertices(18).Y = 0:    aVertices(18).z = 5
  633.     aVertices(19).X = -2:    aVertices(19).Y = 0:    aVertices(19).z = 5
  634.     
  635.     'Front Wall, Face #0
  636.     aFaces(1) = 4 '4 Vertices in face
  637.     aFaces(2) = 0
  638.     aFaces(3) = 1
  639.     aFaces(4) = 2
  640.     aFaces(5) = 3
  641.     'Back wall, Face #1  (vertices in reverse order, order determines which side of face is visible)
  642.     aFaces(6) = 4 '4 Vertices in face
  643.     aFaces(7) = 7
  644.     aFaces(8) = 6
  645.     aFaces(9) = 5
  646.     aFaces(10) = 4
  647.     'Left Wall, Face #2
  648.     aFaces(11) = 4 '4 Vertices in face
  649.     aFaces(12) = 12
  650.     aFaces(13) = 13
  651.     aFaces(14) = 14
  652.     aFaces(15) = 15
  653.     'Right Wall, Face #3
  654.     aFaces(16) = 4 '4 Vertices in face
  655.     aFaces(17) = 11
  656.     aFaces(18) = 10
  657.     aFaces(19) = 9
  658.     aFaces(20) = 8
  659.     'Floor, Face #4
  660.     aFaces(21) = 4 '4 Vertices in face
  661.     aFaces(22) = 19
  662.     aFaces(23) = 18
  663.     aFaces(24) = 17
  664.     aFaces(25) = 16
  665.     'Ceiling, Face #5
  666.     aFaces(26) = 0
  667.     
  668.     ' Initialize meshbuilder
  669.     G_oD3DInstance.CreateMeshBuilder d3dRMmeshbuilder
  670.     
  671.     d3dRMmeshbuilder.AddFaces 20, aVertices(0), 0, aNormals(0), aFaces(1), Nothing
  672.     d3dRMmeshbuilder.SetPerspective 1
  673.     
  674.     d3dRMmeshbuilder.GetFace 0, Face3d
  675.         Face3d.SetColorRGB 1!, 0, 0
  676.     d3dRMmeshbuilder.GetFace 1, Face3d
  677.         Face3d.SetColorRGB 0, 0, 1!
  678.     'Load Texture
  679.         ThisTex = App.Path & "\textures\bricks.bmp"
  680.         G_oD3DInstance.LoadTexture ThisTex, Texture
  681.     d3dRMmeshbuilder.GetFace 2, Face3d
  682.         Face3d.SetTexture Texture
  683.         'Texture Coordinates (Vertex, Horizontal stretch to vertex, vertical stretch to vertex)
  684.         Face3d.SetTextureCoordinates 0, 0, 5
  685.         Face3d.SetTextureCoordinates 1, 25, 5
  686.         Face3d.SetTextureCoordinates 2, 25, 0
  687.         Face3d.SetTextureCoordinates 3, 0, 0
  688.     d3dRMmeshbuilder.GetFace 3, Face3d
  689.         Face3d.SetTexture Texture
  690.         'Texture Coordinates (Vertex, Horizontal stretch to vertex, vertical stretch to vertex)
  691.         Face3d.SetTextureCoordinates 0, 0, 5
  692.         Face3d.SetTextureCoordinates 1, 25, 5
  693.         Face3d.SetTextureCoordinates 2, 25, 0
  694.         Face3d.SetTextureCoordinates 3, 0, 0
  695.     'Load Texture
  696.         ThisTex = App.Path & "\textures\floor.bmp"
  697.         G_oD3DInstance.LoadTexture ThisTex, Texture
  698.     d3dRMmeshbuilder.GetFace 4, Face3d
  699.         Face3d.SetTexture Texture
  700.         'Texture Coordinates (Vertex, Horizontal stretch to vertex, vertical stretch to vertex)
  701.         Face3d.SetTextureCoordinates 0, 0, 25
  702.         Face3d.SetTextureCoordinates 1, 25, 25
  703.         Face3d.SetTextureCoordinates 2, 25, 0
  704.         Face3d.SetTextureCoordinates 3, 0, 0
  705. RenderView:
  706.     RMCamera.SetPosition RMScene, 0.5!, 0.5!, 0
  707.     RMScene.AddVisual d3dRMmeshbuilder
  708.     
  709.     'Create BackGround Surface
  710.     
  711.     'Assign an Image to the scene's Background
  712.     ThisTex = App.Path & "\textures\background.bmp"
  713.     G_oD3DInstance.LoadTexture ThisTex, Texture
  714.     RMScene.SetSceneBackgroundImage Texture
  715.     '----------unload unneeded stuff------------'
  716.     Set Face3d = Nothing
  717.     Set Texture = Nothing
  718.     Exit Sub
  719. GenScene_Err:
  720.     On Error GoTo 0
  721.     Debug.Print "GenScene Err... Source:" & err.source & "Description:" & err.Description
  722.     Exit Sub
  723. End Sub
  724.  
  725. Public Function LoadBitmapIntoDXS(ByVal sFileName As String) As IDirectDrawSurface3
  726.  
  727.     ' Enable error handling ...
  728.         On Error GoTo E_LoadBitmapIntoDXS
  729.     
  730.     ' Setup local variables ...
  731.         
  732.         Dim L_nBMBitmap As Long               ' Handle on bitmap
  733.         Dim L_nDCBitmap As Long               ' Handle on dc of bitmap
  734.         Dim L_dBitmap As BITMAP               ' Bitmap descriptor
  735.         Dim L_dDXD As DDSURFACEDESC           ' Surface descriptor
  736.         Dim L_nDCDXS As Long                  ' Handle on dc of surface
  737.         Dim L_oDDSTemp As IDirectDrawSurface3 ' Temporary DD surface
  738.     
  739.     ' Load bitmap into surface ...
  740.     
  741.         ' Load bitmap
  742.         L_nBMBitmap = LoadImage(ByVal 0&, sFileName, 0, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
  743.         
  744.         ' Check for validity of bitmap handle
  745.         If L_nBMBitmap < 1 Then
  746.             AppError 0, "Bitmap could not be loaded", "LoadBitmapIntoDXS"
  747.             Exit Function
  748.         End If
  749.         
  750.         ' Get bitmap descriptor
  751.         GetObject L_nBMBitmap, Len(L_dBitmap), L_dBitmap
  752.         
  753.         ' Fill DX surface description
  754.         With L_dDXD
  755.             .dwSize = Len(L_dDXD)
  756.             .dwFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  757.             .DDSCAPS.dwCaps = DDSCAPS_OFFSCREENPLAIN
  758.             .dwWidth = L_dBitmap.bmWidth
  759.             .dwHeight = L_dBitmap.bmHeight
  760.         End With
  761.         
  762.         ' Create DX surface
  763.         G_oDDInstance.CreateSurface L_dDXD, L_oDDSTemp, Nothing
  764.         
  765.         ' Check surface existance
  766.         If L_oDDSTemp Is Nothing Then
  767.             AppError 0, "Surface could not be created", "LoadBitmapIntoDXS"
  768.             Exit Function
  769.         End If
  770.         
  771.         ' Create API memory DC
  772.         L_nDCBitmap = CreateCompatibleDC(ByVal 0&)
  773.         
  774.         ' Select the bitmap into API memory DC
  775.         SelectObject L_nDCBitmap, L_nBMBitmap
  776.         
  777.         ' Restore DX surface
  778.         L_oDDSTemp.Restore
  779.         
  780.         ' Get DX surface API DC
  781.         L_oDDSTemp.GetDC L_nDCDXS
  782.         
  783.         ' Blit BMP from API DC into DX DC using standard API BitBlt
  784.         StretchBlt L_nDCDXS, 0, 0, L_dDXD.dwWidth, L_dDXD.dwHeight, L_nDCBitmap, 0, 0, L_dBitmap.bmWidth, L_dBitmap.bmHeight, SRCCOPY
  785.         
  786.         ' Cleanup
  787.         L_oDDSTemp.ReleaseDC L_nDCDXS
  788.         DeleteDC L_nDCBitmap
  789.         DeleteObject L_nBMBitmap
  790.         
  791.         ' Return success
  792.         Set LoadBitmapIntoDXS = L_oDDSTemp
  793.         
  794.         ' Cleanup
  795.         Set L_oDDSTemp = Nothing
  796.     
  797.     ' Error handler ...
  798.     
  799.         Exit Function
  800.     
  801. E_LoadBitmapIntoDXS:
  802.  
  803.         AppError err.Number, err.Description, "LoadBitmapIntoDXS"
  804.  
  805. End Function
  806.  
  807. Public Function MakeDXSurface(ByVal nWidth As Integer, ByVal nHeight As Integer, Optional bIs3D As Boolean) As IDirectDrawSurface3
  808.  
  809.     ' Enable error handling ...
  810.         
  811.         On Error GoTo E_MakeDXSurface
  812.     
  813.     ' Setup local variables ...
  814.  
  815.         Dim L_dDXD As DDSURFACEDESC    ' Variable holding temporary surface description
  816.  
  817.     ' Create surface ...
  818.     
  819.         ' Fill surface description
  820.         With L_dDXD
  821.            .dwSize = Len(L_dDXD)
  822.            .dwFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  823.            .DDSCAPS.dwCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_VIDEOMEMORY Or IIf(bIs3D, DDSCAPS_3DDEVICE, 0)
  824.            .dwWidth = nWidth
  825.            .dwHeight = nHeight
  826.         End With
  827.     
  828.         ' Create surface from description
  829.         G_oDDInstance.CreateSurface L_dDXD, MakeDXSurface, Nothing
  830.     
  831.         ' Check for existance of surface
  832.         If MakeDXSurface Is Nothing Then
  833.            AppError 0, "Surface could not be created", "MakeDXSurface"
  834.            Exit Function
  835.         End If
  836.     
  837.     ' Error handler ...
  838.         Exit Function
  839.     
  840. E_MakeDXSurface:
  841.  
  842.         AppError err.Number, err.Description, "MakeDXSurface"
  843.     
  844. End Function
  845.  
  846. Public Function GetDXError(nError As Long) As String
  847.  
  848.     Select Case nError
  849.         Case DDERR_DCALREADYCREATED
  850.             GetDXError = "A device context (DC) has already been returned for this surface. Only one DC can be retrieved for each surface."
  851.         Case DDERR_DIRECTDRAWALREADYCREATED
  852.             GetDXError = "A IDirectDraw object representing this driver has already been created for this process."
  853.         Case DDERR_EXCEPTION
  854.             GetDXError = "An exception was encountered while performing the requested operation."
  855.         Case DDERR_EXCLUSIVEMODEALREADYSET
  856.             GetDXError = "An attempt was made to set the cooperative level when it was already set to exclusive."
  857.         Case DDERR_HEIGHTALIGN
  858.             GetDXError = "The height of the provided rectangle is not a multiple of the required alignment."
  859.         Case DDERR_HWNDALREADYSET
  860.             GetDXError = "The IDirectDraw cooperative level window handle has already been set. It cannot be reset while the process has surfaces or palettes created."
  861.         Case DDERR_HWNDSUBCLASSED
  862.             GetDXError = "IDirectDraw is prevented from restoring state because the IDirectDraw cooperative level window handle has been subclassed."
  863.         Case DDERR_ALREADYINITIALIZED
  864.             GetDXError = "The object has already been initialized."
  865.         Case DDERR_BLTFASTCANTCLIP
  866.             GetDXError = "A IDirectDrawClipper object is attached to a source surface that has passed into a call to the IDirectDrawSurface2::BltFast method."
  867.         Case DDERR_CANNOTATTACHSURFACE
  868.             GetDXError = "A surface cannot be attached to another requested surface."
  869.         Case DDERR_CANNOTDETACHSURFACE
  870.             GetDXError = "A surface cannot be detached from another requested surface."
  871.         Case DDERR_CANTCREATEDC
  872.             GetDXError = "Windows cannot create any more device contexts (DCs)."
  873.         Case DDERR_CANTDUPLICATE
  874.             GetDXError = "Primary and 3D surfaces, or surfaces that are implicitly created, cannot be duplicated."
  875.         Case DDERR_CANTLOCKSURFACE
  876.             GetDXError = "Access to this surface is refused because an att    empt was made to lock the primary surface without DCI support."
  877.         Case DDERR_CANTPAGELOCK
  878.             GetDXError = "An attempt to page lock a surface failed. Page lock will not work on a display-m    emory surface or an     emulated primary surface."
  879.         Case DDERR_CANTPAGEUNLOCK
  880.             GetDXError = "An attempt to page unlock a surface failed. Page unlock will not work on a display-m    emory surface or an     emulated primary surface."
  881.         Case DDERR_CLIPPERISUSINGHWND
  882.             GetDXError = "An attempt was made to set a clip list for a IDirectDrawClipper object that is already monitoring a window handle."
  883.         Case DDERR_COLORKEYNOTSET
  884.             GetDXError = "No source color key is specified for this operation."
  885.         Case DDERR_CURRENTLYNOTAVAIL
  886.             GetDXError = "No support is currently available."
  887.         Case DDERR_IMPLICITLYCREATED
  888.             GetDXError = "The surface cannot be restored because it is an implicitly created surface."
  889.         Case DDERR_INCOMPATIBLEPRIMARY
  890.             GetDXError = "The primary surface creation request does not match with the existing primary surface."
  891.         Case DDERR_INVALIDCAPS
  892.             GetDXError = "One or more of the capability bits passed to the callback function are incorrect."
  893.         Case DDERR_INVALIDCLIPLIST
  894.             GetDXError = "IDirectDraw does not support the provided clip list."
  895.         Case DDERR_INVALIDDIRECTDRAWGUID
  896.             GetDXError = "The globally unique identifier (GUID) passed to the IDirectDrawCreate function is not a valid IDirectDraw driver identifier."
  897.         Case DDERR_INVALIDMODE
  898.             GetDXError = "IDirectDraw does not support the requested mode."
  899.         Case DDERR_INVALIDOBJECT
  900.             GetDXError = "IDirectDraw received a pointer that was an invalid IDirectDraw object."
  901.         Case DDERR_INVALIDPIXELFORMAT
  902.             GetDXError = "The pixel format was invalid as specified."
  903.         Case DDERR_INVALIDPOSITION
  904.             GetDXError = "The position of the overlay on the destination is no longer legal."
  905.         Case DDERR_INVALIDRECT
  906.             GetDXError = "The provided rectangle was invalid."
  907.         Case DDERR_INVALIDSURFACETYPE
  908.             GetDXError = "The requested operation could not be performed because the surface was of the wrong type."
  909.         Case DDERR_LOCKEDSURFACES
  910.             GetDXError = "One or more surfaces are locked, causing the failure of the requested operation."
  911.         Case DDERR_NO3D
  912.             GetDXError = "No 3D hardware or emulation is present."
  913.         Case DDERR_NOALPHAHW
  914.             GetDXError = "No alpha acceleration hardware is present or available, causing the failure of the requested operation."
  915.         Case DDERR_NOBLTHW
  916.             GetDXError = "No blitter hardware is present."
  917.         Case DDERR_NOCLIPLIST
  918.             GetDXError = "No clip list is available."
  919.         Case DDERR_NOCLIPPERATTACHED
  920.             GetDXError = "No IDirectDrawClipper object is attached to the surface object."
  921.         Case DDERR_NOCOLORCONVHW
  922.             GetDXError = "The operation cannot be carried out because no color-conversion hardware is present or available."
  923.         Case DDERR_NOCOLORKEY
  924.             GetDXError = "The surface does not currently have a color key."
  925.         Case DDERR_NOCOLORKEYHW
  926.             GetDXError = "The operation cannot be carried out because there is no hardware support for the destination color key."
  927.         Case DDERR_NOCOOPERATIVELEVELSET
  928.             GetDXError = "A create function is called without the IDirectDraw2::SetCooperativeLevel method being called."
  929.         Case DDERR_NODC
  930.             GetDXError = "No DC has ever been created for this surface."
  931.         Case DDERR_NODDROPSHW
  932.             GetDXError = "No IDirectDraw raster operation (ROP) hardware is available."
  933.         Case DDERR_NODIRECTDRAWHW
  934.             GetDXError = "Hardware-only IDirectDraw object creation is not possible; the driver does not support any hardware."
  935.         Case DDERR_NODIRECTDRAWSUPPORT
  936.             GetDXError = "IDirectDraw support is not possible with the current display driver."
  937.         Case DDERR_NOEMULATION
  938.             GetDXError = "Software emulation is not available."
  939.         Case DDERR_NOEXCLUSIVEMODE
  940.             GetDXError = "The operation requires the application to have exclusive mode, but the application does not have exclusive mode."
  941.         Case DDERR_NOFLIPHW
  942.             GetDXError = "Flipping visible surfaces is not supported."
  943.         Case DDERR_NOGDI
  944.             GetDXError = "No GDI is present."
  945.         Case DDERR_NOHWND
  946.             GetDXError = "Clipper notification requires a window handle, or no window handle has been previously set as the cooperative level window handle."
  947.         Case DDERR_NOMIPMAPHW
  948.             GetDXError = "The operation cannot be carried out because no mipmap texture mapping hardware is present or available."
  949.         Case DDERR_NOMIRRORHW
  950.             GetDXError = "The operation cannot be carried out because no mirroring hardware is present or available."
  951.         Case DDERR_NOOVERLAYDEST
  952.             GetDXError = "The IDirectDrawSurface2::GetOverlayPosition method is called on an overlay that the IDirectDrawSurface2::UpdateOverlay method has not been called on to establish a destination."
  953.         Case DDERR_NOOVERLAYHW
  954.             GetDXError = "The operation cannot be carried out because no overlay hardware is present or available."
  955.         Case DDERR_NOPALETTEATTACHED
  956.             GetDXError = "No palette object is attached to this surface."
  957.         Case DDERR_NOPALETTEHW
  958.             GetDXError = "There is no hardware support for 16- or 256-color palettes."
  959.         Case DDERR_NORASTEROPHW
  960.             GetDXError = "The operation cannot be carried out because no appropriate raster operation hardware is present or available."
  961.         Case DDERR_NOROTATIONHW
  962.             GetDXError = "The operation cannot be carried out because no rotation hardware is present or available."
  963.         Case DDERR_NOSTRETCHHW
  964.             GetDXError = "The operation cannot be carried out because there is no hardware support for stretching."
  965.         Case DDERR_NOT4BITCOLOR
  966.             GetDXError = "The IDirectDrawSurface object is not using a 4-bit color palette and the requested operation requires a 4-bit color palette."
  967.         Case DDERR_NOT4BITCOLORINDEX
  968.             GetDXError = "The IDirectDrawSurface object is not using a 4-bit color index palette and the requested operation requires a 4-bit color index palette."
  969.         Case DDERR_NOT8BITCOLOR
  970.             GetDXError = "The IDirectDrawSurface object is not using an 8-bit color palette and the requested operation requires an 8-bit color palette."
  971.         Case DDERR_NOTAOVERLAYSURFACE
  972.             GetDXError = "An overlay component is called for a non-overlay surface."
  973.         Case DDERR_NOTEXTUREHW
  974.             GetDXError = "The operation cannot be carried out because no texture-mapping hardware is present or available."
  975.         Case DDERR_NOTFLIPPABLE
  976.             GetDXError = "An attempt has been made to flip a surface that cannot be flipped."
  977.         Case DDERR_NOTFOUND
  978.             GetDXError = "The requested item was not found."
  979.         Case DDERR_NOTLOCKED
  980.             GetDXError = "An attempt is made to unlock a surface that was not locked."
  981.         Case DDERR_NOTPAGELOCKED
  982.             GetDXError = "An attempt is made to page unlock a surface with no outstanding page locks."
  983.         Case DDERR_NOTPALETTIZED
  984.             GetDXError = "The surface being used is not a palette-based surface."
  985.         Case DDERR_NOVSYNCHW
  986.             GetDXError = "The operation cannot be carried out because there is no hardware support for vertical blank synchronized operations."
  987.         Case DDERR_NOZBUFFERHW
  988.             GetDXError = "The operation to create a z-buffer in display memory or to perform a blit using a z-buffer cannot be carried out because there is no hardware support for z-buffers."
  989.         Case DDERR_NOZOVERLAYHW
  990.             GetDXError = "The overlay surfaces cannot be z-layered based on the z-order because the hardware does not support z-ordering of overlays."
  991.         Case DDERR_OUTOFCAPS
  992.             GetDXError = "The hardware needed for the requested operation has already been allocated."
  993.         Case DDERR_OUTOFVIDEOMEMORY
  994.             GetDXError = "IDirectDraw does not have enough display memory to perform the operation."
  995.         Case DDERR_OVERLAYCANTCLIP
  996.             GetDXError = "The hardware does not support clipped overlays."
  997.         Case DDERR_OVERLAYCOLORKEYONLYONEACTIVE
  998.             GetDXError = "An attempt was made to have more than one color key active on an overlay."
  999.         Case DDERR_OVERLAYNOTVISIBLE
  1000.             GetDXError = "The IDirectDrawSurface2::GetOverlayPosition method is called on a hidden overlay."
  1001.         Case DDERR_PALETTEBUSY
  1002.             GetDXError = "Access to this palette is refused because the palette is locked by another thread."
  1003.         Case DDERR_PRIMARYSURFACEALREADYEXISTS
  1004.             GetDXError = "This process has already created a primary surface."
  1005.         Case DDERR_REGIONTOOSMALL
  1006.             GetDXError = "The region passed to the IDirectDrawClipper::GetClipList method is too small."
  1007.         Case DDERR_SURFACEALREADYATTACHED
  1008.             GetDXError = "An attempt was made to attach a surface to another surface to which it is already attached."
  1009.         Case DDERR_SURFACEALREADYDEPENDENT
  1010.             GetDXError = "An attempt was made to make a surface a dependency of another surface to which it is already dependent."
  1011.         Case DDERR_SURFACEBUSY
  1012.             GetDXError = "Access to the surface is refused because the surface is locked by another thread."
  1013.         Case DDERR_SURFACEISOBSCURED
  1014.             GetDXError = "Access to the surface is refused because the surface is obscured."
  1015.         Case DDERR_SURFACELOST
  1016.             GetDXError = "Access to the surface is refused because the surface memory is gone. The IDirectDrawSurface object representing this surface should have the IDirectDrawSurface2::Restore method called on it."
  1017.         Case DDERR_SURFACENOTATTACHED
  1018.             GetDXError = "The requested surface is not attached."
  1019.         Case DDERR_TOOBIGHEIGHT
  1020.             GetDXError = "The height requested by IDirectDraw is too large."
  1021.         Case DDERR_TOOBIGSIZE
  1022.             GetDXError = "The size requested by IDirectDraw is too large. However, the individual height and width are OK."
  1023.         Case DDERR_TOOBIGWIDTH
  1024.             GetDXError = "The width requested by IDirectDraw is too large."
  1025.         Case DDERR_UNSUPPORTEDFORMAT
  1026.             GetDXError = "The FourCC format requested is not supported by IDirectDraw."
  1027.         Case DDERR_UNSUPPORTEDMASK
  1028.             GetDXError = "The bitmask in the pixel format requested is not supported by IDirectDraw."
  1029.         Case DDERR_UNSUPPORTEDMODE
  1030.             GetDXError = "The display is currently in an unsupported mode."
  1031.         Case DDERR_VERTICALBLANKINPROGRESS
  1032.             GetDXError = "A vertical blank is in progress."
  1033.         Case DDERR_WASSTILLDRAWING
  1034.             GetDXError = "The previous blit operation that is transferring information to or from this surface is incomplete."
  1035.         Case DDERR_WRONGMODE
  1036.             GetDXError = "This surface cannot be restored because it was created in a different mode."
  1037.         Case DDERR_XALIGN
  1038.             GetDXError = "The provided rectangle was not horizontally aligned on a required boundary."
  1039.         Case Else
  1040.             GetDXError = "Unknown Error: Out of memory or invalid parameters passed."
  1041.     End Select
  1042.     
  1043. End Function
  1044. Public Sub AppStart()
  1045.     ' Initialize application data
  1046.     Call AppInitialize
  1047.     ' Create dinput Keyboard for key polling
  1048.     directInputmodule.CreateDirectInputKeyboard directInput, Keyboarddevice2, fMain
  1049.     ' Create dinput Mouse for mouse polling
  1050.     directInputmodule.CreateDirectInputMouse directInput, MouseDevice2, fMain
  1051.     ' Start main application loop
  1052.     Call AppLoop
  1053. End Sub
  1054. Public Sub TextOutput(Text As String, X, Y)
  1055.     SetTextColor fMain.hDC, RGB(255, 255, 255)
  1056.     TextOut fMain.hDC, X, Y, Text, Len(Text)
  1057. End Sub
  1058. Public Sub AddObjects()
  1059.         'Set a random position for the flask object
  1060.     Dim RandomX As Single, RandomZ As Single
  1061.     Dim ObjectBuilder As IDirect3DRMMeshBuilder2    ' Holds and loads the mesh for the object
  1062.     Dim ObjectBuilder2 As IDirect3DRMMeshBuilder2    ' Holds and loads the mesh for the object
  1063.         
  1064.         'Create frame for object
  1065.         G_oD3DInstance.CreateFrame RMScene, ObjectFrame(1)
  1066.         G_oD3DInstance.CreateFrame RMScene, ObjectFrame(2)
  1067.         G_oD3DInstance.CreateMeshBuilder ObjectBuilder
  1068.         G_oD3DInstance.CreateMeshBuilder ObjectBuilder2
  1069.         
  1070.         ObjectBuilder.Load App.Path & "\mesh\jar.x", 0, 0, 0, 0
  1071.         ObjectFrame(1).AddVisual ObjectBuilder
  1072.         ObjectFrame(1).AddScale D3DRMCOMBINE_BEFORE, 0.1, 0.1, 0.1
  1073.         ObjectBuilder2.Load App.Path & "\mesh\face.x", 0, 0, 0, 0
  1074.         ObjectFrame(2).AddVisual ObjectBuilder2
  1075.         ObjectFrame(2).AddScale D3DRMCOMBINE_BEFORE, 0.1, 0.1, 0.1
  1076.         
  1077.         Randomize
  1078.         RandomX = 1 * Rnd - 1
  1079.         Randomize
  1080.         RandomZ = 8 * Rnd - 4
  1081.         ObjectFrame(1).SetPosition RMScene, RandomX, 0.2, RandomZ
  1082.         
  1083.         Randomize
  1084.         RandomX = 1 * Rnd - 1
  1085.         Randomize
  1086.         RandomZ = 8 * Rnd - 4
  1087.         ObjectFrame(2).SetPosition RMScene, RandomX, 0.2, RandomZ
  1088.         
  1089.         Set ObjectBuilder = Nothing
  1090.         Set ObjectBuilder2 = Nothing
  1091. End Sub
  1092. Public Sub CheckMouse()
  1093.     On Error GoTo 0
  1094.     Dim CursorPos As POINTAPI
  1095.     Dim PickedArray As IDirect3DRMPickedArray
  1096.     Dim MeshBuilder As IDirect3DRMMeshBuilder
  1097.     Dim FrameArray As IDirect3DRMFrameArray
  1098.     Dim PickDesc As D3DRMPICKDESC
  1099.     
  1100.     Dim Distance As Single
  1101.     Dim CameraPos As D3DVECTOR
  1102.     Dim World As D3DVECTOR
  1103.     Dim Screen As D3DRMVECTOR4D
  1104.     
  1105.     Dim framer As IDirect3DRMFrame
  1106.     
  1107.     MouseDevice2.Acquire
  1108.     MouseDevice2.GetDeviceState 16, MouseState
  1109.     Call GetCursorPos(CursorPos)
  1110.     If MouseState.rgbButtons(0) <> 0 Then
  1111.         RMViewport.Pick CursorPos.X, CursorPos.Y, PickedArray
  1112.         PickedArray.GetPick 0, MeshBuilder, FrameArray, PickDesc
  1113.         FrameArray.GetElement (FrameArray.GetSize - 1), framer
  1114.         framer.GetPosition RMCamera, CameraPos
  1115.         framer.LookAt RMCamera, RMScene, D3DRMCONSTRAIN_Z
  1116.         'Stupid d3d rescales object when moved???
  1117.         framer.AddScale D3DRMCOMBINE_BEFORE, 0.1, 0.1, 0.1
  1118.         framer.SetPosition framer, 0, 0, 0.3
  1119.         With PickDesc.vPosition
  1120.             Screen.X = .X
  1121.             Screen.Y = .Y
  1122.             Screen.z = .z
  1123.             Screen.w = 1
  1124.         End With
  1125.         RMViewport.InverseTransform World, Screen
  1126.         Distance = Sqr((CameraPos.X - World.X) ^ 2 + (CameraPos.z - World.z) ^ 2)
  1127.         TextOutput "Distance to object: " & Distance, CursorPos.X + 5, CursorPos.Y
  1128.     End If
  1129. End Sub
  1130.  
  1131. Public Sub AddLights()
  1132.     Dim Light As IDirect3DRMLight
  1133.     G_oD3DInstance.CreateFrame RMScene, RMLight(1)
  1134.     G_oD3DInstance.CreateFrame RMScene, RMLight(2)
  1135.     G_oD3DInstance.CreateLightRGB D3DRMLIGHT_POINT, 0, 0, 0.2, Light
  1136.     RMLight(1).AddLight Light
  1137.     G_oD3DInstance.CreateLightRGB D3DRMLIGHT_POINT, 0.3, 0, 0, Light
  1138.     RMLight(2).AddLight Light
  1139.     RMLight(1).SetPosition RMScene, 2, 4, 1
  1140.     RMLight(2).SetPosition RMScene, -3, 3, -1
  1141. End Sub
  1142.