home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / ddraw / src / fire / fire.bas next >
Encoding:
BASIC Source File  |  1999-07-20  |  6.9 KB  |  253 lines

  1. Attribute VB_Name = "FireMod"
  2. Option Explicit
  3. Public DX As New DirectX7
  4. Public DDraw As DirectDraw7
  5. Public DDSFront As DirectDrawSurface7
  6. Public DDSFrontDesc As DDSURFACEDESC2
  7. Public DDSBack As DirectDrawSurface7
  8. Public DDSBackDesc As DDSURFACEDESC2
  9. Public Clipper As DirectDrawClipper
  10. Dim Pict() As Byte
  11. Dim AlphaRect As RECT
  12. Dim X As Long, Y As Long
  13. Dim Temp As Long
  14. Dim Index As Long
  15. Dim Index2 As Long
  16. Dim Pos As Long
  17. Dim PosPlus1 As Long
  18. Dim PosPlus2 As Long
  19. Dim PosPlus3 As Long
  20. Public Pal(255) As PALETTEENTRY
  21. Public Palette As DirectDrawPalette
  22. Public BlitRect As RECT
  23. Public FullSize As Boolean
  24. Public ExitLoop As Boolean
  25. Dim Accum As Long
  26. Dim Msg(9) As String
  27. Dim Counter As Long
  28. Dim MsgIndex As Long
  29. Dim bDrawText As Boolean
  30. Dim lastTime As Long
  31. Dim XPos As Long, YPos As Long
  32. Dim wait As Long
  33. Dim Angle As Single
  34. Dim Flag As Boolean
  35. Dim Count As Long
  36. Dim CurModeActiveStatus As Boolean
  37. Dim bRestore As Boolean
  38. Dim Mode As Boolean
  39. Private Sub Main()
  40.     
  41.     InitializeDX
  42.  
  43.     'On Error Resume Next
  44.     DDSBack.SetForeColor RGB(255, 255, 255)
  45.     DDSBack.SetFont MainForm.Font
  46.     
  47.     Msg(0) = "A Simple Fiery Demo"
  48.     Msg(1) = "Demonstrating"
  49.     Msg(2) = "Direct Access"
  50.     Msg(3) = "To Video Memory"
  51.     Msg(4) = "With VB Arrays"
  52.     Msg(5) = "{Esc} to exit"
  53.  
  54.     'setup 8bit palette
  55.     
  56.     For Index = 0 To 84
  57.         Pal(Index + 1).red = Index * 3 + 3
  58.         Pal(Index + 1).green = 0
  59.         Pal(Index + 1).blue = 0
  60.     
  61.         Pal(Index + 86).red = 255
  62.         Pal(Index + 86).green = Index * 3 + 3
  63.         Pal(Index + 86).blue = 0
  64.     
  65.         Pal(Index + 171).red = 255
  66.         Pal(Index + 171).green = 255
  67.         Pal(Index + 171).blue = Index * 3 + 3
  68.     Next
  69.     
  70.     Set Palette = DDraw.CreatePalette(DDPCAPS_8BIT _
  71.         Or DDPCAPS_ALLOW256, Pal())
  72.     DDSFront.SetPalette Palette
  73.     
  74.     
  75.     AlphaRect.Right = DDSBackDesc.lWidth - 1
  76.     AlphaRect.Bottom = DDSBackDesc.lHeight - 1
  77.     
  78.     'Lock must have corresponding unlock...
  79.     DDSBack.Lock AlphaRect, DDSBackDesc, DDLOCK_WAIT, 0
  80.     
  81.     'Get locked array gives you access to
  82.     'a byte array that represents video memory.
  83.     'be aware that the color information is orgnanized
  84.     'differntly for various color depths.
  85.     '
  86.     DDSBack.GetLockedArray Pict()
  87.     
  88.     
  89.     For X = 0 To 639
  90.         For Y = 0 To 479
  91.             Pict(X, Y) = 0
  92.         Next
  93.     Next
  94.     
  95.     'Corresponding unlock
  96.     DDSBack.Unlock AlphaRect
  97.     
  98.     While Not ExitLoop
  99.         
  100.         Mode = ExModeActive
  101.         
  102.         'DoEvents
  103.         bRestore = False
  104.         Do Until ExModeActive
  105.             DoEvents
  106.             bRestore = True
  107.         Loop
  108.         DoEvents
  109.         
  110.         If bRestore Then
  111.             bRestore = False
  112.             DDraw.RestoreAllSurfaces
  113.         End If
  114.         
  115.         'Lock
  116.         DDSBack.Lock AlphaRect, DDSBackDesc, DDLOCK_WAIT, 0
  117.         
  118.         'GetVBArray
  119.         DDSBack.GetLockedArray Pict()
  120.     
  121.         For Y = 0 To 479
  122.             Pict(0, Y) = 0
  123.             Pict(639, Y) = 0
  124.         Next
  125.         For X = 0 To 639
  126.             Pict(X, 477) = Rnd * 220 + 35
  127.             Pict(X, 478) = Rnd * 220 + 35
  128.             Pict(X, 479) = Rnd * 220 + 35
  129.         Next
  130.         
  131.         Accum = 0
  132.         For X = 1 To 638
  133.             For Y = 0 To 477
  134.                 Accum = (Accum + Pict(X, Y + 1) _
  135.                     + Pict(X, Y + 2) _
  136.                     + Pict(X + 1, Y + 1) _
  137.                     + Pict(X - 1, Y + 1)) \ 5
  138.                     If Accum < 0 Then
  139.                         Accum = 0
  140.                     ElseIf Accum > 255 Then
  141.                         Accum = 255
  142.                     End If
  143.                 Pict(X, Y) = Accum
  144.             Next
  145.         Next
  146.         
  147.         For X = 0 To 639
  148.             Pict(X, 0) = 0
  149.             Pict(X, 1) = 0
  150.         Next
  151.         X = Rnd * 639
  152.         For Y = 50 To 439
  153.         Next
  154.         'Unlock
  155.         DDSBack.Unlock AlphaRect
  156.         
  157.         If DX.TickCount() - lastTime > wait Then
  158.             If Counter = 0 Then
  159.                 bDrawText = True
  160.                 Counter = 1
  161.                 XPos = Rnd * 200
  162.                 YPos = 300 + Rnd * 140
  163.                 wait = 400
  164.             ElseIf Counter = 1 Then
  165.                 MsgIndex = MsgIndex + 1
  166.                 If MsgIndex > 5 Then MsgIndex = 0
  167.                 bDrawText = False
  168.                 Counter = 0
  169.                 wait = 2000
  170.             End If
  171.             lastTime = DX.TickCount
  172.         End If
  173.         
  174.         'Draw Text to the backbuffer
  175.         If bDrawText Then
  176.             On Error Resume Next
  177.             DDSBack.DrawText XPos, YPos, Msg(MsgIndex), False
  178.             On Error GoTo 0
  179.         End If
  180.         
  181.         MainForm.Form_Paint
  182.         
  183.     Wend
  184.     
  185.     TerminateDX
  186.     End
  187. End Sub
  188. Function ExModeActive() As Boolean
  189.     Dim TestCoopRes As Long
  190.     TestCoopRes = DDraw.TestCooperativeLevel
  191.     Select Case TestCoopRes
  192.         Case DDERR_NOEXCLUSIVEMODE
  193.             ExModeActive = False
  194.         Case DD_OK
  195.             ExModeActive = True
  196.     End Select
  197. End Function
  198. Public Sub InitializeDX()
  199.      
  200.     MainForm.Left = 0
  201.     MainForm.Top = 0
  202.     MainForm.Height = 640 * Screen.TwipsPerPixelY
  203.     MainForm.Width = 480 * Screen.TwipsPerPixelX
  204.     MainForm.Show
  205.     
  206.     Set DDraw = DX.DirectDrawCreate("")
  207.     
  208.     DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN ' DDSCL_NORMAL
  209.     
  210.    ' You can set the cooperative level to normal if you are already in an 8bit mode
  211.    ' and want to debug the code
  212.    ' DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_NORMAL
  213.    
  214.     DDraw.SetDisplayMode 640, 480, 8, 0, DDSDM_DEFAULT
  215.     
  216.     With DDSFrontDesc
  217.         .lFlags = DDSD_CAPS
  218.         .ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE 'Or DDSCAPS_SYSTEMMEMORY
  219.     End With
  220.     With DDSBackDesc
  221.         .ddsCaps.lCaps = DDSCAPS_SYSTEMMEMORY
  222.         .lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
  223.         .lWidth = 640
  224.         .lHeight = 480
  225.     End With
  226.     Set DDSFront = DDraw.CreateSurface(DDSFrontDesc)
  227.     Set DDSBack = DDraw.CreateSurface(DDSBackDesc)
  228.     Set Clipper = DDraw.CreateClipper(0)
  229.     Clipper.SetHWnd MainForm.hWnd
  230.     DDSFront.SetClipper Clipper
  231.     DDSBack.SetClipper Clipper
  232.     DoEvents
  233.     Exit Sub
  234. ERRoUT:
  235.     If Not (DDraw Is Nothing) Then
  236.         DDraw.RestoreDisplayMode
  237.         DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_NORMAL
  238.         DoEvents
  239.     End If
  240.     MsgBox "Unable to initialize DirectDraw " + Chr(13) + "Your display card may not support 640x480x8 resolution " + Chr(13) + "required for this sample"
  241.     End
  242. End Sub
  243. Public Sub TerminateDX()
  244.     DDraw.RestoreDisplayMode
  245.     DDraw.SetCooperativeLevel MainForm.hWnd, DDSCL_NORMAL
  246.     DoEvents
  247.     Set Clipper = Nothing
  248.     Set DDSBack = Nothing
  249.     Set DDSFront = Nothing
  250.     Set DDraw = Nothing
  251.     Set DX = Nothing
  252. End Sub
  253.