Bitmap::Bitmap(Canvas& canvas, int dx, int dy) : _hBitmap (0) {
CreateCompatible(canvas, dx, dy);
}
void Bitmap::CreateCompatible(Canvas& canvas, int width, int height) {
Free();
_hBitmap = ::CreateCompatibleBitmap(canvas, width, height);
}
How do you display the bitmap on screen? You have to blit it. Blit stands for "block bit transfer" or something like that. When you blit a bitmap, you have to specify a lot of parameters, so we'll just encapsulate the blitting request in a separate object, the blitter. This is a very handy object that sets the obvious defaults for blitting, but at the same time lets you override each and any of them.
A blitter transfers a rectangular area of the bitmap into a rectangular area of the screen. The meaning of various parameters is the following:
• Source position: pixel coordinates of the upper left corner of the bitmap area, to be transferred. The default is the upper left corner of the bitmap.
• Destination position: pixel coordinates within the target window of the upper left corner of the transferred area. The default is upper left corner of the window.
• Area dimensions: the dimensions of the rectangular area to be transferred. The default is the dimensions of the bitmap.
• Transfer mode. The way bitmap pixels are combined with existing window pixels. The default, SRCCOPY, copies the pixels over existing pixels. You may also specify more involved logical operations, like SRCAND (Boolean AND), SRCPAINT (Boolean OR), etc. (see your compiler's help on BitBlt).
class Blitter {
public:
Blitter(Bitmap& bmp) : _bmp (bmp), _mode (SRCCOPY), _xDst (0), _yDst (0), _xSrc (0), _ySrc (0) {
bmp.GetSize(_width, _height);
}
void SetMode(DWORD mode) {
_mode = mode;
}
void SetDest(int x, int y) {
_xDst = x;
_yDst = y;
}
void SetSrc(int x, int y) {
_xSrc = x;
_ySrc = y;
}
void SetArea(int width, int height) {