Выбрать главу

We've set the timer to elapse every 50 milliseconds, which results in approximately 20 frames per second. Approximately because like I said, SetTimer() is a little inaccurate, but this isn't critical code, and a few milliseconds here or there won't kill us.

Animating in WM_TIMER

Now when we get WM_TIMER we want to calculate the new position for the ball and draw it's updated position.

case WM_TIMER:

 {

  RECT rcClient;

  HDC hdc = GetDC(hwnd);

  GetClientRect(hwnd, &rcClient);

  UpdateBall(&rcClient);

  DrawBall(hdc, &rcClient);

  ReleaseDC(hwnd, hdc);

 }

 break;

I've put the code for updating and drawing the ball in their own functions. This is good practice, and it lets us draw the ball from either WM_TIMER or WM_PAINT without duplicating code, note that the method we use to get the HDC in each case is different, so it's best to leave this code in the message handlers and pass the result into the DrawBall() function.

void UpdateBall(RECT* prc) {

 g_ballInfo.x += g_ballInfo.dx;

 g_ballInfo.y += g_ballInfo.dy;

 if (g_ballInfo.x < 0) {

  g_ballInfo.x = 0;

  g_ballInfo.dx = BALL_MOVE_DELTA;

 } else if(g_ballInfo.x + g_ballInfo.width > prc->right) {

  g_ballInfo.x = prc->right - g_ballInfo.width;

  g_ballInfo.dx = -BALL_MOVE_DELTA;

 }

 if(g_ballInfo.y < 0) {

  g_ballInfo.y = 0;

  g_ballInfo.dy = BALL_MOVE_DELTA;

 } else if(g_ballInfo.y + g_ballInfo.height > prc->bottom) {

  g_ballInfo.y = prc->bottom - g_ballInfo.height;

  g_ballInfo.dy = -BALL_MOVE_DELTA;

 }

}

All this does is some basic math, we add the delta value to the x position to move the ball. If the ball goes outside the client area, move it back in range and change the delta value to the opposite direction so that the ball "bounces" off the sides.

void DrawBall(HDC hdc, RECT* prc) {

 HDC hdcBuffer = CreateCompatibleDC(hdc);

 HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);

 HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);

 HDC hdcMem = CreateCompatibleDC(hdc);

 HBITMAP hbmOld = SelectObject(hdcMem, g_hbmMask);

 FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH));

 BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND);

 SelectObject(hdcMem, g_hbmBall);

 BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);

 BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);

 SelectObject(hdcMem, hbmOld);

 DeleteDC(hdcMem);

 SelectObject(hdcBuffer, hbmOldBuffer);

 DeleteDC(hdcBuffer);

 DeleteObject(hbmBuffer);

}

This is essentially the same drawing code as the past few examples, with the exception that it gets the position and dimentions of the ball from the BALLINFO structure. There is however one important difference…

Double Buffering

When doing your drawing directly to the HDC of the window, it's entirely possible that the screen will get updated before you're done… for example after you draw the mask and before you draw the colour image over top, the user might see a flicker of the back background before your program has a chance to draw over it in colour. The slower your computer and the more drawing operations that you do, the more flicker will be apparent and eventually it will look like a big jumbled mess.

This is terribly distracting, and we can solve it simply by doing all the drawing in memory first, and then copying the completed masterpiece to the screen in a single BitBlt() so that the screen is updated directly from the old image, to the complete new image with none of the individual operations visible.

To do this, we create a temporary HBITMAP in memory that is the exact size of the area we are going to draw to on the screen. We also need an HDC so that we can BitBlt() to the bitmap.

HDC hdcBuffer = CreateCompatibleDC(hdc);

HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);

HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);

Now that we have a place to draw to in memory, all of the drawing operations use hdcBuffer instead of hdc (the window) and the results are stored on the bitmap in memory untill we are complete. We can now copy the whole thing over to the window in one shot.

BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);

That's it, and we clean up our HDCs and HBITMAP s as usual.

Faster Double Buffering

In this example I am creating and destroying the bitmap used for double buffering each frame, I did this basically because I wanted to be able to size the window so it's easier to just always create a new buffer than to track when the window position changes and resize the buffer. It would be more efficient to create a global double buffer bitmap and either not allow the window to resize or only resize the bitmap when the window resized, instead of creating it and destroying it all the time. It's up to you to implement this if you want to optimize the drawing for a game or something.

Killing the Timer

When our window is destroyed, it's a good idea to release all resources we used, and in this case that includes the timer we set. To stop it, we simply call KillTimer() and pass in the ID that we used when we created it.

KillTimer(hwnd, ID_TIMER);

Text and Fonts

Example: font_one

Loading Fonts

The Win32 GDI has some remarkable capabilites for dealing with vastly different typefaces, styles, languages and characters sets. One of the drawbacks of this is that dealing with fonts can look rather intimidating to the newcomer. CreateFont(), the primary API when it comes to fonts, has 14 parameters for specifying height, style, weight, family, and various other attributes.

Fortunately, it's not really has hard as it might appear, and a large portion of the work involved is taken care of my sensible default values. All but 2 of the parameters to CreateFont() can be set to 0 or NULL, and the system will simply use a default value giving you a plain ordinary font.