home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter09 / SpriteTest / Sprite.bas < prev    next >
Encoding:
BASIC Source File  |  2004-11-06  |  3.0 KB  |  123 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.     'create sprite handler object
  69.     Set spr.spriteObject = d3dx.CreateSprite(dev)
  70.     
  71.     'set initial values for the sprite
  72.     spr.CurrentFrame = 0
  73.     spr.FramesPerRow = 1
  74.     spr.FrameCount = 1
  75.     spr.AnimCount = 0
  76.     spr.AnimDelay = 0
  77.     spr.ScaleFactor = 1
  78.     spr.x = 0
  79.     spr.y = 0
  80.     spr.width = 0
  81.     spr.height = 0
  82.     spr.SpeedX = 0
  83.     spr.SpeedY = 0
  84.     spr.DirX = 0
  85.     spr.DirY = 0
  86. End Sub
  87.  
  88. Public Sub DrawSprite(ByRef tex As Direct3DTexture8, ByRef spr As TSPRITE, ByVal alpha As Long)
  89.     'set the sprite's position
  90.     Dim vecPos As D3DVECTOR2
  91.     vecPos.x = spr.x
  92.     vecPos.y = spr.y
  93.     
  94.     'set the sprite's scale factor
  95.     Dim vecScale As D3DVECTOR2
  96.     vecScale.x = spr.ScaleFactor
  97.     vecScale.y = spr.ScaleFactor
  98.     
  99.     'set the sprite's rotation value (not used)
  100.     Dim vecRot As D3DVECTOR2
  101.     vecRot.x = 0
  102.     vecRot.y = 0
  103.     
  104.     'start drawing
  105.     spr.spriteObject.Begin
  106.     
  107.     'set the source rect
  108.     Dim r As DxVBLibA.RECT
  109.     r.Left = (spr.CurrentFrame Mod spr.FramesPerRow) * spr.width
  110.     r.Top = (spr.CurrentFrame \ spr.FramesPerRow) * spr.height
  111.     r.Right = r.Left + spr.width
  112.     r.bottom = r.Top + spr.height
  113.     
  114.     'draw the sprite
  115.     spr.spriteObject.Draw tex, r, vecScale, vecRot, 0, vecPos, alpha
  116.     
  117.     'stop drawing
  118.     spr.spriteObject.End
  119. End Sub
  120.  
  121.  
  122.  
  123.