/* GDI Double buffer sample */ #include #define SCRW 1280 #define SCRH 720 #define FPS 60 #define TM_COUNT1 101 #define LPCLASSNAME TEXT("DoubleBufferSample") #define WINDOW_TITLE TEXT("Double Buffer Sample") LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { static HBITMAP hBitmap; static HBITMAP hBitmap_mask; static HBITMAP hBitmap_bg; static HBITMAP offScrnBitmap; static int wdw_w, wdw_h; static int imgw, imgh; static int bgw, bgh; static int bgx, bgy; static float x, y; static float dx, dy; switch (msg) { case WM_CREATE: // get window size { RECT rc; GetClientRect(hwnd, &rc); wdw_w = rc.right - rc.left; wdw_h = rc.bottom - rc.top; } // create off screen bitmap { HDC hdc; hdc = GetDC(hwnd); offScrnBitmap = CreateCompatibleBitmap(hdc, wdw_w, wdw_h); ReleaseDC(hwnd, hdc); // GetDC() -> ReleaseDC() } // load bitmaps from resource hBitmap = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BALL_IMG")); hBitmap_mask = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BALL_MSK")); hBitmap_bg = LoadBitmap(((LPCREATESTRUCT)lp)->hInstance, TEXT("BG_IMG")); // get images size { BITMAP bp; // get ball image size GetObject(hBitmap, sizeof(BITMAP), &bp); imgw = bp.bmWidth; imgh = bp.bmHeight; // get bg image size GetObject(hBitmap_bg, sizeof(BITMAP), &bp); bgw = bp.bmWidth; bgh = bp.bmHeight; } // init work x = wdw_w / 2; y = wdw_h / 2; dx = (float)wdw_w / (float)FPS; dy = dx * 0.2; bgx = 0; bgy = 0; SetTimer(hwnd, TM_COUNT1, (int)(1000 / FPS), NULL); return 0; case WM_TIMER: // main loop x += dx; y += dy; if (x <= 0 || (x + imgw) >= wdw_w) dx *= -1; if (y <= 0 || (y + imgh) >= wdw_h) dy *= -1; bgx -= 1; bgy -= 1; if (bgx <= -(bgw)) bgx += bgw; if (bgy <= -(bgh)) bgy += bgh; if (0) { // clear background InvalidateRect(hwnd, NULL, TRUE); } else { // not clear background InvalidateRect(hwnd, NULL, FALSE); } break; case WM_PAINT: // draw window { HDC hdc, hBmpDC, hMemDC; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); // hdc = GetDC(hwnd); // set off-screen bitmap hMemDC = CreateCompatibleDC(hdc); SelectObject(hMemDC, offScrnBitmap); // draw bg to off-screen hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap_bg); { int by = bgy; while (by <= wdw_h) { int bx = bgx; while (bx <= wdw_w) { BitBlt(hMemDC, bx, by, bgw, bgh, hBmpDC, 0, 0, SRCCOPY); bx += bgw; } by += bgh; } } DeleteDC(hBmpDC); // draw mask to off-screen hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap_mask); BitBlt(hMemDC, (int)x, (int)y, imgw, imgh, hBmpDC, 0, 0, SRCAND); DeleteDC(hBmpDC); // draw source image to off-screen hBmpDC = CreateCompatibleDC(hdc); SelectObject(hBmpDC, hBitmap); BitBlt(hMemDC, (int)x, (int)y, imgw, imgh, hBmpDC, 0, 0, SRCPAINT); DeleteDC(hBmpDC); // drawing to off-screen is finished // draw off-screen bitmap to window BitBlt(hdc, 0, 0, wdw_w, wdw_h, hMemDC, 0, 0, SRCCOPY); DeleteDC(hMemDC); // Create*() -> DeleteDC() EndPaint(hwnd, &ps); } return 0; case WM_DESTROY: KillTimer(hwnd, TM_COUNT1); // kill timer DeleteObject(offScrnBitmap); DeleteObject(hBitmap); DeleteObject(hBitmap_mask); DeleteObject(hBitmap_bg); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wp, lp); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = LPCLASSNAME; if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( LPCLASSNAME, WINDOW_TITLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, SCRW, SCRH, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) return -1; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }