home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter18 / CelticCrusader3 / Sprite.bas < prev    next >
Encoding:
BASIC Source File  |  2004-11-12  |  5.1 KB  |  203 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. 'added in chapter12--needs to be inserted back into chapter 10
  121. Public Sub AnimateSprite(ByRef spr As TSPRITE)
  122.     Dim frameindex As Long
  123.     
  124.     With spr
  125.         'increment the animation counter
  126.         .AnimCount = .AnimCount + 1
  127.         
  128.         'has the animation counter waited long enough?
  129.         If .AnimCount > .AnimDelay Then
  130.             .AnimCount = 0
  131.             
  132.             'okay, go to the next frame
  133.             .CurrentFrame = .CurrentFrame + 1
  134.             
  135.             frameindex = .AnimSeq * .FrameCount
  136.             If (.CurrentFrame < frameindex) Or (.CurrentFrame > frameindex + .FrameCount - 1) Then
  137.                 .CurrentFrame = frameindex
  138.                 .Animating = False
  139.             End If
  140.         End If
  141.     End With
  142.     
  143. End Sub
  144.  
  145. Public Sub PrintText( _
  146.     ByRef fontImg As Direct3DTexture8, _
  147.     ByRef fontSpr As TSPRITE, _
  148.     ByVal x As Long, _
  149.     ByVal y As Long, _
  150.     ByVal color As Long, _
  151.     ByVal sText As String)
  152.     
  153.     Dim n As Long
  154.     For n = 1 To Len(sText)
  155.         PrintChar fontImg, fontSpr, x + (n - 1) * fontSpr.width, y, color, Asc(Mid$(sText, n, 1))
  156.     Next n
  157.     
  158. End Sub
  159.  
  160. Public Sub PrintChar( _
  161.     ByRef fontImg As Direct3DTexture8, _
  162.     ByRef fontSpr As TSPRITE, _
  163.     ByVal x As Long, _
  164.     ByVal y As Long, _
  165.     ByVal color As Long, _
  166.     c As Byte)
  167.     
  168.     fontSpr.x = x
  169.     fontSpr.y = y
  170.     fontSpr.CurrentFrame = c - 32
  171.     DrawSprite fontImg, fontSpr, color
  172.     
  173. End Sub
  174.  
  175. Public Function Collision(ByRef sprite1 As TSPRITE, ByRef sprite2 As TSPRITE) As Boolean
  176.     Dim dest As RECT
  177.     Dim rect1 As RECT
  178.     Dim rect2 As RECT
  179.     
  180.     'set up the first rect
  181.     rect1.Left = sprite1.x + sprite1.width / 4
  182.     rect1.Top = sprite1.y + sprite1.height / 4
  183.     rect1.Right = rect1.Left + sprite1.width / 2
  184.     rect1.bottom = rect1.Top + sprite1.height / 2
  185.     
  186.     'set up the second rect
  187.     rect2.Left = sprite2.x + sprite2.width / 4
  188.     rect2.Top = sprite2.y + sprite2.height / 4
  189.     rect2.Right = rect2.Left + sprite2.width / 2
  190.     rect2.bottom = rect2.Top + sprite2.height / 2
  191.     
  192.     'check for collision
  193.     If IntersectRect(dest, rect1, rect2) <> 0 Then
  194.         Collision = True
  195.     Else
  196.         Collision = False
  197.     End If
  198. End Function
  199.  
  200.  
  201.  
  202.  
  203.