home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter08 / ScrollWorld / TileScroller.bas < prev   
Encoding:
BASIC Source File  |  2004-11-03  |  5.1 KB  |  201 lines

  1. Attribute VB_Name = "TileScroller"
  2. '---------------------------------------------------------------
  3. ' Visual Basic Game Programming for Teens
  4. ' Tile Scrolling Support File
  5. '---------------------------------------------------------------
  6.  
  7. Option Explicit
  8. Option Base 0
  9.  
  10.  
  11. 'tile scroller surfaces
  12. Public scrollbuffer As Direct3DSurface8
  13. Public tiles As Direct3DSurface8
  14.  
  15. 'map data
  16. Public mapdata() As Integer
  17. Public mapwidth As Long
  18. Public mapheight As Long
  19.  
  20. 'scrolling values
  21. Public ScrollX As Long
  22. Public ScrollY As Long
  23. Public SpeedX As Integer
  24. Public SpeedY As Integer
  25.  
  26.  
  27. Public Sub UpdateScrollPosition()
  28.     'update horizontal scrolling position and speed
  29.     ScrollX = ScrollX + SpeedX
  30.     If (ScrollX < 0) Then
  31.         ScrollX = 0
  32.         SpeedX = 0
  33.     ElseIf ScrollX > GAMEWORLDWIDTH - WINDOWWIDTH Then
  34.         ScrollX = GAMEWORLDWIDTH - WINDOWWIDTH
  35.         SpeedX = 0
  36.     End If
  37.  
  38.     'update vertical scrolling position and speed
  39.     ScrollY = ScrollY + SpeedY
  40.     If ScrollY < 0 Then
  41.         ScrollY = 0
  42.         SpeedY = 0
  43.     ElseIf ScrollY > GAMEWORLDHEIGHT - WINDOWHEIGHT Then
  44.         ScrollY = GAMEWORLDHEIGHT - WINDOWHEIGHT
  45.         SpeedY = 0
  46.     End If
  47. End Sub
  48.  
  49.  
  50. Public Sub DrawTiles()
  51.     Dim tilex As Long
  52.     Dim tiley As Long
  53.     Dim columns As Long
  54.     Dim rows As Long
  55.     Dim X As Long
  56.     Dim Y As Long
  57.     Dim tilenum As Long
  58.     
  59.     'calculate starting tile position
  60.     'integer division drops the remainder
  61.     tilex = ScrollX \ TILEWIDTH
  62.     tiley = ScrollY \ TILEHEIGHT
  63.     
  64.     'calculate the number of columns and rows
  65.     'integer division drops the remainder
  66.     columns = WINDOWWIDTH \ TILEWIDTH
  67.     rows = WINDOWHEIGHT \ TILEHEIGHT
  68.     
  69.     'draw tiles onto the scroll buffer surface
  70.     For Y = 0 To rows
  71.         For X = 0 To columns
  72.             
  73.             '*** This condition shouldn't be necessary. I will try to
  74.             '*** resolve this problem and make the change during AR.
  75.             If tiley + Y = mapheight Then tiley = tiley - 1
  76.             
  77.             tilenum = mapdata((tiley + Y) * mapwidth + (tilex + X))
  78.             DrawTile tiles, tilenum, TILEWIDTH, TILEHEIGHT, 16, scrollbuffer, _
  79.                 X * TILEWIDTH, Y * TILEHEIGHT
  80.         Next X
  81.     Next Y
  82. End Sub
  83.  
  84. Public Sub DrawScrollWindow()
  85.     Dim r As DxVBLibA.RECT
  86.     Dim point As DxVBLibA.point
  87.     Dim partialx As Long
  88.     Dim partialy As Long
  89.  
  90.     'calculate the partial sub-tile lines to draw
  91.     partialx = ScrollX Mod TILEWIDTH
  92.     partialy = ScrollY Mod TILEHEIGHT
  93.     
  94.     'set dimensions of the source image
  95.     r.Left = partialx
  96.     r.Top = partialy
  97.     r.Right = partialx + WINDOWWIDTH
  98.     r.bottom = partialy + WINDOWHEIGHT
  99.         
  100.     'set the destination point
  101.     point.X = 0
  102.     point.Y = 0
  103.     
  104.     'draw the scroll window
  105.     d3ddev.CopyRects scrollbuffer, r, 1, backbuffer, point
  106. End Sub
  107.  
  108. Public Sub DrawTile( _
  109.     ByRef source As Direct3DSurface8, _
  110.     ByVal tilenum As Long, _
  111.     ByVal width As Long, _
  112.     ByVal height As Long, _
  113.     ByVal columns As Long, _
  114.     ByVal dest As Direct3DSurface8, _
  115.     ByVal destx As Long, _
  116.     ByVal desty As Long)
  117.     
  118.     'create a RECT to describe the source image
  119.     Dim r As DxVBLibA.RECT
  120.     
  121.     'set the upper left corner of the source image
  122.     r.Left = (tilenum Mod columns) * width
  123.     r.Top = (tilenum \ columns) * height
  124.     
  125.     'set the bottom right corner of the source image
  126.     r.Right = r.Left + width
  127.     r.bottom = r.Top + height
  128.     
  129.     'create a POINT to define the destination
  130.     Dim point As DxVBLibA.point
  131.     
  132.     'set the upper left corner of where to draw the image
  133.     point.X = destx
  134.     point.Y = desty
  135.     
  136.     'draw the source bitmap tile image
  137.     d3ddev.CopyRects source, r, 1, dest, point
  138.  
  139. End Sub
  140.  
  141. Public Sub LoadMap(ByVal filename As String)
  142.     Dim num As Long
  143.     Dim line As String
  144.     Dim buffer As String
  145.     Dim s As String
  146.     Dim value As String
  147.     Dim index As Long
  148.     Dim pos As Long
  149.     Dim buflen As Long
  150.  
  151.     'open the map file
  152.     num = FreeFile()
  153.     Open filename For Input As num
  154.  
  155.     'read the width and height
  156.     Input #num, mapwidth, mapheight
  157.  
  158.     'read the map data
  159.     While Not EOF(num)
  160.         Line Input #num, line
  161.         buffer = buffer & line
  162.     Wend
  163.  
  164.     'close the file
  165.     Close num
  166.  
  167.     'prepare the array for the map data
  168.     ReDim mapdata(mapwidth * mapheight)
  169.     index = 0
  170.     buflen = Len(buffer)
  171.  
  172.     'convert the text data to an array
  173.     For pos = 1 To buflen
  174.     
  175.         'get next character
  176.         s = Mid$(buffer, pos, 1)
  177.         
  178.         'tiles are separated by commas
  179.         If s = "," Then
  180.             If Len(value) > 0 Then
  181.             
  182.                 'store tile # in array
  183.                 mapdata(index) = CInt(value - 1)
  184.                 index = index + 1
  185.             End If
  186.             
  187.             'get ready for next #
  188.             value = ""
  189.             s = ""
  190.         Else
  191.             value = value & s
  192.         End If
  193.     Next pos
  194.     
  195.     'save last item to array
  196.     mapdata(index) = CInt(value - 1)
  197. End Sub
  198.  
  199.  
  200.  
  201.