Liberty BASIC Help Online

Add a Mask
The code provided below opens a sprite image and automatically adds a proper mask.  If the resulting image is correct, it can be saved to disk.
A sprite starts with a drawn image that has a black background, like this:
Image bm7.GIF
This image can be opened in the mask maker and a proper mask is added.  It will look like this while processing:
Image bm24.GIF
If the mask/sprite image above is saved, it will look like this:
Image bm11.GIF
Here is the code.  Copy and paste into the Liberty BASIC Editor to run the "Add Masks to Sprites" program.  See a Step by Step sprite coding explanation.
'small utility to add masks to the
'top of sprite images for use in LB3
if val(Version$)<3 then
    notice "This program is for LB3 only!"
end if
bmpheight=0     'bitmap height
bmpwidth=0      'bitmap width
bitmap$=""      'bitmap file name
savefile$=""    'save file name
hBitmap=0       'handle for loaded bitmap
hWindow=0       'window handle
menu #1, "&File", "&Open Sprite",[openSprite],_
    "&Save As...",[saveAs],|,"E&xit",[quit]
open "Add Masks to Sprites" for graphics_fs_nsb as #1
    print #1, "trapclose [quit]"
    print #1, "down;place 20 40"
    print #1, "|Open the desired sprite image."
    print #1, "|A mask will be added to the sprite "
    print #1, "|as you watch."
    print #1, "|This might take time for large images."
    print #1, "|Images larger than the window "
    print #1, "|will be cut off."
    print #1, "|If it is satisfactory, choose "
    print #1, "|'Save As...' from the File menu."
    input aVar$
    close #1:end
    if hBitmap<>0 then
        unloadbmp ("bm")
        print #1, "cls"
    end if
    filedialog "Open Sprite","*.bmp",bitmap$
    if bitmap$="" then
        notice "No bitmap chosen!"
        goto [loop]
    end if
    print #1, "cls"
    loadbmp "bm" , bitmap$
    print #1, "down;drawbmp bm 0 0"
    print #1, "drawbmp bm 0 ";bmpheight
    call MakeMask bmpwidth, bmpheight, hWindow
    goto [loop]
    print #1, "getbmp SpriteMask 0 0 ";bmpwidth;" ";2*bmpheight
    filedialog "Save As... ","*.bmp",savefile$
    if savefile$="" then
        notice "No filename specified!"
        goto [loop]
    end if
    bmpsave "SpriteMask",savefile$
    notice "Sprite and mask saved as ";savefile$
    goto [loop]
function WidthBitmap(name$)
    open name$ for input as #pic
    close #pic
    WidthBitmap = asc(mid$(pic$,19,1)) + _
        (asc(mid$(pic$,20,1)) * 256)
  end function
function HeightBitmap(name$)
    open name$ for input as #pic
    close #pic
    HeightBitmap = asc(mid$(pic$,23,1)) + _
        (asc(mid$(pic$,24,1)) * 256)
  end function
sub MakeMask wide, high, hWnd
    cursor hourglass
    open "user32" for dll as #user
    Open "gdi32"for DLL as #gdi
    CallDll #user, "GetDC",_
        hWnd as long,_
        hDC as long
    for i = 0 to wide-1
        for j = 0 to high-1
            CallDll #gdi, "GetPixel",_
                hDC as long,_
                i as long,_
                j as long,_
                pColor as long
            if pColor=black then
            end if
            CallDll #gdi, "SetPixel",_
                hDC as long,_
                i as long, _
                j as long, _
                newColor as long, _
                r as long
        next j
    next i
    CallDll #user, "ReleaseDC",_
            hWnd as long,_
            hDC as long,_
            r as long
    close #user
    close #gdi
    cursor normal
  end sub

Copyright (C) 2003 Shoptalk Systems
Liberty BASIC -