sprites, raster and masks
the sprite
let us take a step away from the actual coding and have a good look at a very central aspect of almost any graphical game, the sprite. the sprite or rather the sprites are the small things that move around the gaming field, either controlled by a human or by the computer. as you have probably already guessed, you use the bitblt function to draw sprites onto the gaming field from a specified source. since we are blitting a rectangular area this would all be very nice and neat, if all sprites were rectangular but they are not. in fact they are usually anything but rectangular, running the gamut from simple blobs to elaborate spaceships. so how can we make the rectangular area we just blitted any other shape we want? the simple answer is, we can not, but we can make it appear as though they are.
raster operations
the technique we will use to make the sprites appear to have the shape we want involves the use of raster operations. in the previous sample, we simply drew what was in the picturebox onto the screen. this means that we set the destination pixel = source pixel. in other words, when we blit the picture we are specifying that each of the destination pixels be exactly like its corresponding source pixel. we specify the raster operation we want to apply using the last parameter of the bitblt function: dwrop.
before showing you how this can be used to make sprites of any shape, or more accurately, to make unwanted areas of the bitmap disappear (also known as transparency), lets take a closer look at raster operations.
when we are drawing an image from one device context to another, bitblt is actually copying the value of each pixel in the source picture, and performs a specified raster operation on the destination with this copied value. to simplify, if we could hear bitblt thinking, we might hear something like this "ok, so now i have this black pixel from the source and i am going to copy it to the destination which is gray. do you want me to mix the two colors, or place one on top of the other, or do something else to them?" the way we tell bitblt how to handle the operation is by using the dwrop parameter we mentioned earlier.
so the raster operation involves both the current value of the destination and the copied value from the source. the values used in this context, are the color values of the pixels. each pixel in an image or device context is made of a number of bits, and the raster operations combine these bits to form a new pixel on the destination. the basis of the raster operations are the logical boolean operators, not, and, or & xor.
visual basic provides several constants for raster operations, here are a few of the most frequently used ones;
| vbblackness | sets destination = 0 (black) |
| vbdstinvert | inverts the destination rectangle (not destination) |
| vbmergecopy | combines the source with the destination using and |
| vbmergepaint | combines the source with the destination using or |
| vbnotsrccopy | inverts the source and then copies it to destination |
| vbnotsrcerase | inverts the result of combining the source and destination using or |
| vbsrcand | combines the source and the destination with the and operator |
| vbsrccopy | copies the source directly to the destination |
| vbsrcerase | combines the inverted destination with the source using and |
| vbsrcinvert | combines source and destination with xor |
| vbsrcpaint | combines the source and the destination with or |
| vbwhiteness |
destination is set to white |