C++ Win32: Displaying Animation via bitmap

2019-09-09 13:30发布

问题:

I've written a solitare game and while the logic is solid and complete, the form is lacking. I wrote a animation function using bitmaps, and it's glitchy. It seems as if the bitmap printing function is too slow to keep up with the runtime processing speed. Here are the methods:

BOOL DrawBitmap(HDC hDC, INT x, INT y, INT width, INT height, HBITMAP hBitmap, DWORD dwROP)
{
    HDC       hDCBits;
    BITMAP    Bitmap;
    BOOL      bResult;

    if (!hDC || !hBitmap)
        return FALSE;

    hDCBits = CreateCompatibleDC(hDC);
    GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
    SelectObject(hDCBits, hBitmap);
    // Replace with StretchBlt call
    //bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP);
    bResult = StretchBlt(hDC, x, y, width, height,
        hDCBits, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, dwROP);
    DeleteDC(hDCBits);

    return bResult;
}
bool distortBMP (LPCWSTR szFileName, bool perlog, int xcoor, int ycoor, int h, int w, HDC hWinDC) {
    HBITMAP hBitmap;
    hBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
        LR_LOADFROMFILE);

    if (hBitmap == NULL) {
        ::MessageBox(NULL, __T("LoadImage Failed"), __T("Error"), MB_OK);
        return false;
    }

    if (perlog) {
        DrawBitmap(hWinDC, xcoor, ycoor, w, h, hBitmap, SRCCOPY);
    }
    else {
        BITMAP qBitmap;
        int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP),
            reinterpret_cast<LPVOID>(&qBitmap));
        if (!iReturn) {
            ::MessageBox(NULL, __T("GetObject Failed"), __T("Error"), MB_OK);
            return false;
        }
        DrawBitmap(hWinDC, xcoor, ycoor, qBitmap.bmWidth * w, qBitmap.bmWidth * h, hBitmap, SRCCOPY);
    }
    return true;
}
bool animateCard(HDC hdc, LPCWSTR szFileName, int flip, int orix, int oriy, int desx, int desy, int frames) {
    float dx = (float) desx - orix;
    float dy = (float) desy - oriy;


    int i = 0;
    float percent = 0;
    while (i < frames && flip == 0) {
        i++;
        percent = (float) (i * 100) / frames;
        wchar_t secon[50];
        wsprintf(secon, _T("%i, %i, %i, %i"), i,(int) percent, (int)dx, (int)dy);
        //MessageBox(NULL, secon, _T("Alert"), MB_OK);
        HBITMAP hBitmap;
        hBitmap = (HBITMAP)::LoadImage(NULL, _T("screenShot.bmp"), IMAGE_BITMAP, 0, 0,
            LR_LOADFROMFILE);
        //Where "screenShot.bmp" represents a screenshot captures before the animation take place
        BITMAP qBitmap;
        int iReturn = GetObject(reinterpret_cast<HGDIOBJ>(hBitmap), sizeof(BITMAP),
            reinterpret_cast<LPVOID>(&qBitmap));
        if (!iReturn) {
            ::MessageBox(NULL, __T("GetObject Failed"), __T("Error"), MB_OK);
            return false;
        }
        HBITMAP rBitmap;
        rBitmap = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
            LR_LOADFROMFILE);
        BITMAP zBitmap;
        iReturn = GetObject(reinterpret_cast<HGDIOBJ>(rBitmap), sizeof(BITMAP),
            reinterpret_cast<LPVOID>(&zBitmap));
        if (!iReturn) {
            ::MessageBox(NULL, __T("GetObject Failed"), __T("Error"), MB_OK);
            return false;
        }
        HBITMAP BitmapName;
        BitmapName = (HBITMAP)::LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0,
            LR_LOADFROMFILE);

        if (BitmapName == NULL) {
            ::MessageBox(NULL, __T("LoadImage Failed"), __T("Error"), MB_OK);
            return false;
        }
        DrawBitmap(hdc, 0, 0, qBitmap.bmWidth, qBitmap.bmHeight, hBitmap, SRCCOPY);
        DrawBitmap(hdc, (int)(orix + (dx * percent) / 100), (int)(oriy + (dy * percent) / 100), zBitmap.bmWidth, zBitmap.bmHeight, BitmapName, SRCCOPY);

        //distortBMP(szFileName, false, (int) (orix + (dx * percent) / 100), (int) (oriy + (dy * percent) / 100), 1, 1, hdc);
    }
    return true;
}

I'm not sure if there is a faster way of displaying images. I'm open to suggestions there. If not, is there anything i can do to minimize processing time?