Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

How to create masks and sprites

Last updated: February 02, 1998
Article ID: RS10552

The information in this article applies to:

SUMMARY

This article explains how to create 256 color bitmap masks and sprites using the Win32 API.

MORE INFORMATION

Good animation techniques rely on fast API BitBlt'ing. This type of technique requires the use of mask and sprite images. First the mask image is laid over the background which cuts a hole in the background image then a sprite image is placed on top of that. The two images are AND'd together to create a transparent mix of images for a realistic animated image. The following article describes how through code you can create the mask and the sprite image from a source image using palette manipulation. This technique is considerably faster to process because you only need to cycle through a possible 256 palette entries instead of cycling through every pixel in the image.

Step-by-Step Instructions

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a new module to the project.
  3. Paste the following code in the new module:
    Option Explicit
    
    
    
    Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    
    Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    
    Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
    
    
    
    Type BITMAP
    
        bmType As Long
    
        bmWidth As Long
    
        bmHeight As Long
    
        bmWidthBytes As Long
    
        bmPlanes As Integer
    
        bmBitsPixel As Integer
    
        bmBits As Long
    
    End Type
    
    
    
    Public RetValue As Long
    
    
  4. Drop two picturebox controls (picture1 and picture2) on the form (Form1).
  5. Set the picturebox properties (both controls): autoredraw=true, scalemode=pixel
  6. Paste the following code into the forms code editor:
    Option Explicit
    
    
    
    Dim PictureObject As Picture
    
    Public Sub MakeSprite()
    
    
    
        Dim Bytes() As Byte
    
        Dim TempBMP As BITMAP
    
        Dim wid As Long, hgt As Long
    
        Dim PaletteEntry As Byte
    
        Dim i As Long, j As Long
    
        
    
        Set PictureObject = LoadPicture(App.Path & "\temp.bmp")
    
        
    
        RetValue = GetObject(PictureObject.Handle, Len(TempBMP), TempBMP)
    
        With TempBMP
    
            wid = .bmWidthBytes
    
            hgt = .bmHeight
    
        End With
    
        ReDim Bytes(1 To wid, 1 To hgt)
    
        RetValue = GetBitmapBits(PictureObject.Handle, wid * hgt, Bytes(1, 1))
    
        PaletteEntry = Bytes(1, 1)
    
        
    
        For i = 1 To wid
    
            For j = 1 To hgt
    
                If Bytes(i, j) = PaletteEntry Then
    
                    ' set background color to black
    
                    Bytes(i, j) = 0
    
                End If
    
            Next
    
        Next
    
        
    
        RetValue = SetBitmapBits(PictureObject.Handle, wid * hgt, Bytes(1, 1))
    
        Erase Bytes
    
        ReDim Bytes(0)
    
        
    
        ' size the image to the picturebox control
    
        Picture2.PaintPicture PictureObject, 0, 0, Picture2.ScaleWidth, Picture2.ScaleHeight, 0, 0, wid, hgt
    
    
    
    End Sub
    
    
    
    Public Sub MakeMask()
    
    
    
        Dim Bytes() As Byte
    
        Dim TempBMP As BITMAP
    
        Dim wid As Long, hgt As Long
    
        Dim PaletteEntry As Byte
    
        Dim i As Long, j As Long
    
        
    
        Set PictureObject = LoadPicture(App.Path & "\temp.bmp")
    
        
    
        RetValue = GetObject(PictureObject.Handle, Len(TempBMP), TempBMP)
    
        With TempBMP
    
            wid = .bmWidthBytes
    
            hgt = .bmHeight
    
        End With
    
        ReDim Bytes(1 To wid, 1 To hgt)
    
        RetValue = GetBitmapBits(PictureObject.Handle, wid * hgt, Bytes(1, 1))
    
        PaletteEntry = Bytes(1, 1)
    
        
    
        For i = 1 To wid
    
            For j = 1 To hgt
    
                If Bytes(i, j) = PaletteEntry Then
    
                    ' set background color to white
    
                    Bytes(i, j) = 255
    
                Else
    
                    ' set mask to black
    
                    Bytes(i, j) = 0
    
                End If
    
            Next
    
        Next
    
        
    
        RetValue = SetBitmapBits(PictureObject.Handle, wid * hgt, Bytes(1, 1))
    
        Erase Bytes
    
        ReDim Bytes(0)
    
        
    
        ' size the image to the picturebox control
    
        Picture1.PaintPicture PictureObject, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, 0, 0, wid, hgt
    
    
    
    End Sub
    
    
    
    
    
    Private Sub Form_Paint()
    
    
    
        MakeMask
    
        MakeSprite
    
        
    
    End Sub
    
    
  7. Save the project.
  8. Run the program (or press the F5 key). When the paint event is triggered for the form, the mask and sprite are displayed in their repective pictureboxes.

THE INFORMATION PROVIDED IN THE RABIT SOFTWARE KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. RABIT SOFTWARE DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL RABIT SOFTWARE OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF RABIT SOFTWARE OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.