home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter14 / CollisionTest / Sprite.bas < prev    next >
Encoding:
BASIC Source File  |  2004-11-11  |  5.0 KB  |  204 lines

  1. Attribute VB_Name = "Sprite"
  2. '---------------------------------------------------------------
  3. ' Visual Basic Game Programming for Teens
  4. ' Sprite Support File
  5. '---------------------------------------------------------------
  6.  
  7. Option Explicit
  8. Option Base 0
  9.  
  10.  
  11. 'sprite properties
  12. Public Type TSPRITE
  13.     spriteObject As D3DXSprite
  14.     x As Long
  15.     y As Long
  16.     width As Long
  17.     height As Long
  18.     FramesPerRow As Long
  19.     StartFrame As Long
  20.     FrameCount As Long
  21.     CurrentFrame As Long
  22.     Animating As Boolean
  23.     AnimSeq As Long
  24.     AnimDelay As Long
  25.     AnimCount As Long
  26.     SpeedX As Long
  27.     SpeedY As Long
  28.     DirX As Long
  29.     DirY As Long
  30.     ScaleFactor As Single
  31. End Type
  32.  
  33. Public Function LoadTexture(ByRef dev As Direct3DDevice8, ByVal filename As String) As Direct3DTexture8
  34.     On Local Error GoTo error1
  35.     
  36.     Dim d3dx As New D3DX8
  37.     Dim tex As Direct3DTexture8
  38.     
  39.     'load the source bitmap file into a texture
  40.     Set tex = d3dx.CreateTextureFromFileEx( _
  41.         dev, _
  42.         filename, _
  43.         D3DX_DEFAULT, _
  44.         D3DX_DEFAULT, _
  45.         1, 0, _
  46.         D3DFMT_UNKNOWN, _
  47.         D3DPOOL_MANAGED, _
  48.         D3DX_FILTER_NONE, _
  49.         D3DX_FILTER_NONE, _
  50.         &HFF00FF, _
  51.         ByVal 0, ByVal 0)
  52.         
  53.     If tex Is Nothing Then
  54.         MsgBox "Error loading " & filename, vbOKOnly, "Error"
  55.         Set LoadTexture = Nothing
  56.     Else
  57.         Set LoadTexture = tex
  58.     End If
  59.     
  60.     Exit Function
  61.  
  62. error1:
  63.     MsgBox "Error loading " & filename, vbOKOnly, "Error"
  64.     Set LoadTexture = Nothing
  65. End Function
  66.  
  67. Public Sub InitSprite(ByRef dev As Direct3DDevice8, ByRef spr As TSPRITE)
  68.     
  69.     Set spr.spriteObject = d3dx.CreateSprite(dev)
  70.     
  71.     spr.StartFrame = 0
  72.     spr.CurrentFrame = 0
  73.     spr.FramesPerRow = 1
  74.     spr.FrameCount = 1
  75.     spr.Animating = False
  76.     spr.AnimCount = 0
  77.     spr.AnimDelay = 0
  78.     spr.ScaleFactor = 1
  79.     spr.x = 0
  80.     spr.y = 0
  81.     spr.width = 0
  82.     spr.height = 0
  83.     spr.SpeedX = 0
  84.     spr.SpeedY = 0
  85.     spr.DirX = 0
  86.     spr.DirY = 0
  87.     
  88. End Sub
  89.  
  90. Public Sub DrawSprite(ByRef tex As Direct3DTexture8, ByRef spr As TSPRITE, ByVal alpha As Long)
  91.     Dim vecScale As D3DVECTOR2
  92.     vecScale.x = spr.ScaleFactor
  93.     vecScale.y = spr.ScaleFactor
  94.     
  95.     Dim pos As D3DVECTOR2
  96.     pos.x = spr.x
  97.     pos.y = spr.y
  98.     
  99.     Dim vecRot As D3DVECTOR2
  100.     vecRot.x = 0
  101.     vecRot.y = 0
  102.     
  103.     'enable sprite drawing
  104.     spr.spriteObject.Begin
  105.     
  106.     'set the source rect
  107.     Dim r As RECT
  108.     r.Left = (spr.CurrentFrame Mod spr.FramesPerRow) * spr.width
  109.     r.Top = (spr.CurrentFrame \ spr.FramesPerRow) * spr.height
  110.     r.Right = r.Left + spr.width
  111.     r.bottom = r.Top + spr.height
  112.     
  113.     'draw the sprite
  114.     spr.spriteObject.Draw tex, r, vecScale, vecRot, 0, pos, alpha
  115.     
  116.     'stop sprite drawing
  117.     spr.spriteObject.End
  118. End Sub
  119.  
  120. Public Sub AnimateSprite(ByRef spr As TSPRITE, ByRef img As Direct3DTexture8)
  121.     Dim frameindex As Long
  122.     
  123.     With spr
  124.         'increment the animation counter
  125.         .AnimCount = .AnimCount + 1
  126.         
  127.         'has the animation counter waited long enough?
  128.         If .AnimCount > .AnimDelay Then
  129.             .AnimCount = 0
  130.             
  131.             'okay, go to the next frame
  132.             .CurrentFrame = .CurrentFrame + 1
  133.             
  134.             'loop through the frames
  135.             frameindex = .AnimSeq * .FrameCount
  136.             
  137.             If (.CurrentFrame < frameindex) Or (.CurrentFrame > frameindex + .FrameCount - 1) Then
  138.                 .CurrentFrame = frameindex
  139.                 .Animating = False
  140.             End If
  141.         End If
  142.     End With
  143.     
  144. End Sub
  145.  
  146. Public Sub PrintText( _
  147.     ByRef fontImg As Direct3DTexture8, _
  148.     ByRef fontSpr As TSPRITE, _
  149.     ByVal x As Long, _
  150.     ByVal y As Long, _
  151.     ByVal color As Long, _
  152.     ByVal sText As String)
  153.     
  154.     Dim n As Long
  155.     For n = 1 To Len(sText)
  156.         PrintChar fontImg, fontSpr, x + (n - 1) * fontSpr.width, y, color, Asc(Mid$(sText, n, 1))
  157.     Next n
  158.     
  159. End Sub
  160.  
  161. Public Sub PrintChar( _
  162.     ByRef fontImg As Direct3DTexture8, _
  163.     ByRef fontSpr As TSPRITE, _
  164.     ByVal x As Long, _
  165.     ByVal y As Long, _
  166.     ByVal color As Long, _
  167.     c As Byte)
  168.     
  169.     fontSpr.x = x
  170.     fontSpr.y = y
  171.     fontSpr.CurrentFrame = c - 32
  172.     DrawSprite fontImg, fontSpr, color
  173.     
  174. End Sub
  175.  
  176. Public Function Collision(ByRef sprite1 As TSPRITE, ByRef sprite2 As TSPRITE) As Boolean
  177.     Dim dest As RECT
  178.     Dim rect1 As RECT
  179.     Dim rect2 As RECT
  180.     
  181.     'set up the first rect
  182.     rect1.Left = sprite1.x
  183.     rect1.Top = sprite1.y
  184.     rect1.Right = sprite1.x + sprite1.width
  185.     rect1.bottom = sprite1.y + sprite1.height
  186.     
  187.     'set up the second rect
  188.     rect2.Left = sprite2.x
  189.     rect2.Top = sprite2.y
  190.     rect2.Right = sprite2.x + sprite2.width
  191.     rect2.bottom = sprite2.y + sprite2.height
  192.     
  193.     'check for collision
  194.     If IntersectRect(dest, rect1, rect2) <> 0 Then
  195.         Collision = True
  196.     Else
  197.         Collision = False
  198.     End If
  199. End Function
  200.  
  201.  
  202.  
  203.  
  204.