Читать «Применение Windows API» онлайн - страница 50

А. И. Легалов

The two pictures below are that of a sprite (my personal pug, Fanny) and its mask. The mask is a monochrome bitmap that has black areas where we want the sprite to be transparent. The sprite, on the other hand, must be white in these areas. What we want is to be able to see the background through these areas.

The trick is to first blit the background, then blit the mask using logical OR, and then blit the sprite over it using logical AND.

ORing a black mask pixel (all zero bits) with a background pixel will give back the background pixel. ORing a white mask pixel (all one bits) with a background will give a white pixel. So after blitting the mask, we'll have a white ghost in the shape of our sprite floating over the background.

ANDing a white sprite pixel (all ones) with a background pixel will give back the background pixel. ANDing a non-white sprite pixel with the white (all ones) background (the ghost from previous step) will give the sprite pixel. We'll end up with the sprite superimposed on the background.

What remains is to implement the animation. The naive implementation would be to keep re-drawing the image: background, mask and sprite, changing the position of the mask and the sprite in each frame. The problem with this approach is that it results in unacceptable flutter. The trick with good animation is to prepare the whole picture off-line, as a single bitmap, and then blit it to screen in one quick step. This technique is called double buffering — the first buffer being the screen buffer, the second one — our bitmap.

We'll also use Windows timer to time the display of frames.

class WinTimer {

public:

 WinTimer(HWND hwnd = 0, int id = -1) : _hwnd (hwnd), _id (id) {}

 void Create(HWND hwnd, int id) {

  _hwnd = hwnd;

  _id = id;

 }

 void Set(int milliSec) {

  ::SetTimer(_hwnd, _id, milliSec, 0);

 }

 void Kill() {

  ::KillTimer(_hwnd, _id);

 }

 int GetId() const {

  return _id;

 }

private:

 HWND _hwnd;

 int _id;

};

We'll put the timer in our Controller object and initialize it there.

class Controller {

public:

 Controller(HWND hwnd, CREATESTRUCT* pCreate);

 ~Controller();

 void Timer(int id);

 void Size(int x, int y);

 void Paint();

 void Command(int cmd);

private:

 HWND _hwnd;

 WinTimer _timer;

 View _view;

};

Controller::Controller(HWND hwnd, CREATESTRUCT * pCreate) : _hwnd (hwnd), _timer (hwnd, 1), _view(pCreate->hInstance) {

 _timer.Set (100);

}

Once set, the timer sends our program timer messages and we have to process them.

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

 Controller* pCtrl = WinGetLong<Controller*>(hwnd);

 switch (message) {

 ...

 case WM_TIMER:

  pCtrl->Timer(wParam);

  return 0;

 ...

 }

 return ::DefWindowProc(hwnd, message, wParam, lParam);

}

void Controller::Timer(int id) {

 _timer.Kill();

 _view.Step();

 UpdateCanvas canvas(_hwnd);

 _view.Update(canvas);

 ::InvalidateRect(_hwnd, 0, FALSE);