VBfx / Tile tutorial / Step 7

Implement it

We had a .DC property in the tiles Type yet but now it's time to replace this by the Bitmap index we get from our new bitmap manager! Since we're loading from files now we also have to add a FileName property, so the new Type looks like this:

    Public Type tTile
        'Tile info
        PictureFile as String
        Bitmap as Long

(we're cutting here because the rest stays the same)

Now that we changed the Type we also have to update the AddTile function, we no longer need to pass the DC but the file name instead. It's not worth showing the sample code here, however if you need it download the sample code at the end of this tutorial.

Loading resources

When all tiles are added there are still no bitmaps loaded but that's what we're going to do now. The following function goes through all tiles and loads the specified bitmap, storing the returned index in the Bitmap property of each tile. Note that even if 2 or more tiles have the same bitmap our manager will recognize the file name and just give out the same index again.

Public Sub LoadResources()
    Dim A as Long
    
    For A = 1 To TileCount
        'Load the bitmap and get it's index
        Tile( A ).Bitmap = mBitmap.LoadBitmap( App.Path & "\SOURCE\" & Tile( A ).PictureFile )
    Next
End Sub

In this functon I pre-coded that path to the tiles, it matches the sample project's path. You're supposed to change this when using the code, eg. you should make one directory for all pictures, one for all levels, and so on.

Draw function

In case you didn't remember: Since we removed the DC property of the tiles we have to modify the Draw function again. We now get the DC from the Bitmap.Picture.DC located by the Tile.Bitmap property in the Bitmap() array. As before only the selected line from the BitBlt call has changed:


                'Draw tile
                BitBlt Camera.FrontDC, _
                    x * TileSize, y * TileSize, _
                    TileSize, TileSize, _
                    Bitmap( Tile( TileIndex ).Bitmap ).Picture.DC, _
Tile( TileIndex ).ActFrame * TileSize, 0, _ vbSrcCopy

Conclusion

Finally we're done implementing the manager but remember that it's a stand-alone Module! You could easily start a new project and use the mBitmap Module to load and release pictures. Just remember to call the Initialize and Reset functions properly, meaning before loading bitmaps and when exiting the application. As usual you can download the updated sample code here:

Navigation