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

  1. Attribute VB_Name = "Sprite"
  2.  
  3. Option Explicit
  4. Option Base 0
  5.  
  6.  
  7. 'sprite properties
  8. Public Type TSPRITE
  9.     spriteObject As D3DXSprite
  10.     x As Long
  11.     y As Long
  12.     width As Long
  13.     height As Long
  14.     FramesPerRow As Long
  15.     StartFrame As Long
  16.     FrameCount As Long
  17.     CurrentFrame As Long
  18.     Animating As Boolean
  19.     AnimSeq As Long
  20.     AnimDelay As Long
  21.     AnimCount As Long
  22.     SpeedX As Long
  23.     SpeedY As Long
  24.     DirX As Long
  25.     DirY As Long
  26.     ScaleFactor As Single
  27. End Type
  28.  
  29. Public Function LoadTexture(ByRef dev As Direct3DDevice8, ByVal filename As String) As Direct3DTexture8
  30.     On Local Error GoTo error1
  31.     
  32.     Dim d3dx As New D3DX8
  33.     Dim tex As Direct3DTexture8
  34.     
  35.     'load the source bitmap file into a texture
  36.     Set tex = d3dx.CreateTextureFromFileEx( _
  37.         dev, _
  38.         filename, _
  39.         D3DX_DEFAULT, _
  40.         D3DX_DEFAULT, _
  41.         1, 0, _
  42.         D3DFMT_UNKNOWN, _
  43.         D3DPOOL_MANAGED, _
  44.         D3DX_FILTER_NONE, _
  45.         D3DX_FILTER_NONE, _
  46.         &HFF00FF, _
  47.         ByVal 0, ByVal 0)
  48.         
  49.     If tex Is Nothing Then
  50.         MsgBox "Error loading " & filename, vbOKOnly, "Error"
  51.         Set LoadTexture = Nothing
  52.     Else
  53.         Set LoadTexture = tex
  54.     End If
  55.     
  56.     Exit Function
  57.  
  58. error1:
  59.     MsgBox "Error loading " & filename, vbOKOnly, "Error"
  60.     Set LoadTexture = Nothing
  61. End Function
  62.  
  63. Public Sub InitSprite(ByRef dev As Direct3DDevice8, ByRef spr As TSPRITE)
  64.     
  65.     Set spr.spriteObject = d3dx.CreateSprite(dev)
  66.     
  67.     spr.StartFrame = 0
  68.     spr.CurrentFrame = 0
  69.     spr.FramesPerRow = 1
  70.     spr.FrameCount = 1
  71.     spr.Animating = False       'added in chapter 12
  72.     spr.AnimCount = 0
  73.     spr.AnimDelay = 0
  74.     spr.ScaleFactor = 1
  75.     spr.x = 0
  76.     spr.y = 0
  77.     spr.width = 0
  78.     spr.height = 0
  79.     spr.SpeedX = 0
  80.     spr.SpeedY = 0
  81.     spr.DirX = 0
  82.     spr.DirY = 0
  83.     
  84. End Sub
  85.  
  86. Public Sub DrawSprite(ByRef tex As Direct3DTexture8, ByRef spr As TSPRITE, ByVal alpha As Long)
  87.     Dim vecScale As D3DVECTOR2
  88.     vecScale.x = spr.ScaleFactor
  89.     vecScale.y = spr.ScaleFactor
  90.     
  91.     Dim pos As D3DVECTOR2
  92.     pos.x = spr.x
  93.     pos.y = spr.y
  94.     
  95.     Dim vecRot As D3DVECTOR2
  96.     vecRot.x = 0
  97.     vecRot.y = 0
  98.     
  99.     'enable sprite drawing
  100.     spr.spriteObject.Begin
  101.     
  102.     'set the source rect
  103.     Dim r As RECT
  104.     r.Left = (spr.CurrentFrame Mod spr.FramesPerRow) * spr.width
  105.     r.Top = (spr.CurrentFrame \ spr.FramesPerRow) * spr.height
  106.     r.Right = r.Left + spr.width
  107.     r.bottom = r.Top + spr.height
  108.     
  109.     'draw the sprite
  110.     spr.spriteObject.Draw tex, r, vecScale, vecRot, 0, pos, alpha
  111.     
  112.     'stop sprite drawing
  113.     spr.spriteObject.End
  114. End Sub
  115.  
  116. Public Sub AnimateSprite(ByRef spr As TSPRITE, ByRef img As Direct3DTexture8)
  117.     Dim frameindex As Long
  118.     
  119.     With spr
  120.         'increment the animation counter
  121.         .AnimCount = .AnimCount + 1
  122.         
  123.         'has the animation counter waited long enough?
  124.         If .AnimCount > .AnimDelay Then
  125.             .AnimCount = 0
  126.             
  127.             'okay, go to the next frame
  128.             .CurrentFrame = .CurrentFrame + 1
  129.             
  130.             'loop through the frames
  131.             frameindex = .AnimSeq * .FrameCount
  132.             
  133.             If (.CurrentFrame < frameindex) Or (.CurrentFrame > frameindex + .FrameCount - 1) Then
  134.                 .CurrentFrame = frameindex
  135.                 .Animating = False
  136.             End If
  137.         End If
  138.     End With
  139.     
  140. End Sub
  141.  
  142.  
  143.  
  144.