Image Mask Tutorial
Component Pascal
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
readme_images
.gitignore
BackTile.bmp
ETFace.bmp
ETMask.bmp
MaskTutor.dpr
MaskTutor.res
README.md
Unit1.ddp
Unit1.dfm
Unit1.pas

README.md

image_mask_tutorial

Image Mask Tutorial

On web pages, there are a lot of transparent images (png, gif).
On windows, it is a bit harder to obtain this effect.

You can use a TImage, but the perfs are very slow.
If you want to build a game, you can't use a TImage for each sprite, you'll get an horrible framerate.

Here is the solution: use image masks.

You'll need:

  • the image (sprite) on black background
    image on black background
  • the mask, a black & white picture where the background is white and the sprite is black
    mask image
  • a background image
    background image

Then, once the background is drawn, you just need to draw the mask in "And" mode, then the image in "Paint" mode :

procedure TForm1.DrawImage;
begin
  Canvas.CopyMode := cmSrcAnd;
  Canvas.Draw(XPos, YPos, Mask);
  Canvas.CopyMode := cmSrcPaint;
  Canvas.Draw(XPos, YPos, Bitmap);
end;

Here is the result:
result

You can resize the window, the sprite will be redrawn immediatly.


Dark Skull Software
http://www.darkskull.net