ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
BitBlt Tutorial - By Gary Beebe
The first thing I suggest you do is add a Module, if you haven't added
one already. In the Declarations section of that Module you need
to add the following code.
Declare Function BitBlt Lib "gdi32" Alias
"BitBlt" (ByVal hDestDC As Long, _
ByVal X As Long, ByVal Y As Long, ByVal
nWidth As Long, _
ByVal nHeight As Long, ByVal hSrcDC As
Long, ByVal xSrc As Long, _
ByVal ySrc As Long, ByVal dwRop As Long)
As Long
Public Const SRCCOPY = &HCC0020
Public Const SRCPAINT = &HEE0086
Public Const SRCAND = &H8800C6
Public RetVal as Long
The hDestDC is the destination object where
you wish to put the picture. The destination here is referred to by
its hDC.
The X and Y
are the coordinates where you want the top left corner of that picture
to be on the destination.
The nWidth and nHeight
are the width and height of the picture that you want to put on the destination.
The hSrcDC is the source object that you
are getting the picture from. The source here is refered to by its
hDC.
The xSrc and ySrc
are the coordinates of the top left corner of the area in the source picture
where you want the copying to begin.
The dwRop is the raster operation, also
known as a BitwiseOperator. Its value specifies how to copy the area
defined as the source to the area defined as the destination. Those
value are as follows:
SRCCOPY Copy the image exactly how it is.
SRCPAINT Copy the image except what is
black.
SRCAND Copy the image except what is white.
The RetVal is just a Return Value that
we are using to get the action started. it should become a non zero
(meaning any number greater than zero) after the BitBlt statement has been
executed. If it is zero than that is telling you that the execution
of the statement has failed (You should check the statement for any errors).
Ok, so what do we need?
Fig 1
First we'll have a background picture. We don't need a background
picture, but in this example it will make explaining the operations
a lot easier. we will say our background image is Picture1
(being in a Picture Box), and as we can already see, it has a picture of
a disco ball room on it. So, this is where our sprite will be standing.
The picture is 320 x 200 pixels, without the black box that you see around
it. We will refer to it in our source as Picture1.hDC.
Fig 2
This, groovy person, is our sprite. Notice the black background.
This is the area that is "see through" or "invisible" like the areas under
his arms. This image is 110 x 189 pixels, without the black box around
it which is hard to see. We will say that this picture is called
DiscoSpr and will be refered to as DiscoSpr.hDC.
Fig 3
This is our mask. A monochrome (black and white) copy of our sprite.
Think of it as a silhouette of our groovy person. Notice how the
background (what we see as black on the original sprite) is white here
and under his arms, and how the actual object is all black. Our mask
is the exact same size as our sprite, 110 x 189 pixels, without the black
box around it which is easier to see. We will say that this picture is
called DiscoMsk and will be refered to as
DiscoMsk.hDC.
Now we need a surface object. In this example we will be using
Form1. You don't have to use Form1.
You can use any object that has an hDC.
Fig 4 - 5
The first step we do is to put Picture1 on
Form1. We do this by executing the command:
Retval = Bitblt (Form1.hDC, 0, 0, 320, 200,
Picture1.hDC, 0, 0, SRCCOPY)
This will put the picture in Picture1 (Fig
1) in the top left most corner of Form1.
The next thing we need to do is lay down the mask for the sprite.
The mask is important, it lets the computer know what areas are to be shown
(In Fig 3, What is to be shown is black). We do this by executing
this command:
Retval = Bitblt (Form1.hDC, 10, 10, 110, 189,
DiscoMsk.hDC, 0, 0, SRCAND)
This will put the mask at the corner 10,
10 of Form1.
And last, we need to lay the sprite over the mask. Remember how the
black background of DiscoSpr (Fig 2) tells us what isn't to be seen, and
how the white of DiscoMsk (Fig 3) isn't to be seen? Well the mask
also tells us what in the sprite should be seen (the black parts of the
mask). If we don't lay down the mask first the the colors behind
the sprite, on the background, would bleed through. To lay down the
Sprite we execute this code:
RetVal = Bitblt (Form1.hDC, 10, 10, 110, 189,
DiscoSpr.hDC, 0, 0, SRCPAINT)
The first Picture Fig 4 show what the Form1
looks like after we place the background down and the mask on it.
The second picture (Fig 5) shows what Form1
looks like after we place the sprite over the mask.
You may try this exactly how I have shown you above by saving these
images to your hard drive. To get more familiar with BitBlt you
may want to use your own images.
Fig 6

Also you don't have to make a million drawings or have a million
picture boxes you can put them all in one drawing and all in one picture
box. All you need to do with the source code is change the xSrc and
ySrc. You shouldn't have to change the nWidth and nHeight if you are following
along or changing everything to one picture because the Width and Height
stays the same.
|
|