VBfx / Tile tutorial / Step 3

Write and Read

Now we're going back to specialized programming for our Types. We need a Write and Read function for both modules mMap and mTiles, however mMap will contain the main functions we can call later. This is because the map is dependant on the tiles while the tile set is more likely a stand-alone thing and independant from the map.

WriteData

As you can see in the following code we're putting the tile set into the same file as the map goes, this is just fine for small projects but later you might want to put it into it's own file. This part of the code goes into mMap:

Public Sub WriteData( iFileNumber as Integer )
    'Write tile set
    mTiles.WriteData iFileNumber
    
    With Map
        'Write map size
        Put iFileNumber, , .w
        Put iFileNumber, , .h
        
        'Write map data
        Put iFileNumber, , .Data
    End With
End Sub

Writing the tiles is a little longer but not that complicated. Note that we're going through each tile and writing it's properties to the file instead of just putting the whole array there. This gets handy when adding properties later because we can exactly control what data we want to read from the file:

Public Sub WriteData( iFileNumber as Integer )
    Dim A as Long
    
    'Write array size
    Put iFileNumber, , TileCount
    
    For A = 0 To TileCount
        With Tile( A )
            'Put the file name
            WriteString iFileNumber, .PictureFile
            
            'Animation info
            Put iFileNumber, , .FrameCount
            Put iFileNumber, , .FrameDelay
            
            'Game properties
            Put iFileNumber, , .Walkable
        End With
    Next
End Sub

Compared to the tTile Type we left out several properties here (Bitmap, ActFrame and NextTick). Tile.Bitmap of course won't always be the same number, we have to get this index from the mBitmap manager each time we load the pictures. Storing the current frame number (ActFrame) wouldn't hurt but on the other hand we don't care if the animation starts over from the beginning when loading the level.

Storing Tile.NextTick however would cause problems because this property represents a absolute time in ticks (1/1000 second). Imagine a computer that is running for 2 hours now, where GetTickCount returns something like 8,640,020. We store this value in the file, reboot the computer and load the same map again. Now what happens? GetTickCount would return 180,000 or so and the animation wouldn't continue for 2 hours! See the problem? Even if we use GetTickCount for relative timing we still have to watch for it returning absolute values (compared to the computer's running time).

ReadData

Reading back the data is likely the same, except that we have to create buffers for the data first (allocate memory).

Public Sub ReadData( iFileNumber as Integer )
    'Read tile set
    mTiles.ReadData iFileNumber
        
    With Map
        'Read m size
        Get iFileNumber, , .w
        Get iFileNumber, , .h
        
        'Allocate memory
        ReDim .Data( .w * .h )
        
        'Read map data
        Get iFileNumber, , .Data
    End With
End Sub

And finally the complement function to mTile.WriteData:

Public Sub ReadData( iFileNumber as Integer )
    Dim A as Long
    
    'Read array size and allocate memory
    Get iFileNumber, , TileCount
    ReDim Tile( TileCount )
    
    For A = 0 To TileCount
        With Tile( A )
            'Get the file name
            ReadString iFileNumber, .PictureFile
            
            'Animation info
            Get iFileNumber, , .FrameCount
            Get iFileNumber, , .FrameDelay
            
            'Game properties
            Get iFileNumber, , .Walkable
        End With
    Next
End Sub

It's important to read the data in the same order as we wrote it into the file, you can guess why. So, basically you can just copy the WriteData function, replace Put by Get and add the line to create the buffer.

Navigation