C ++ Win32:通过位图显示动画

时间:2015-08-13 14:36:40

标签: c++ function winapi animation bitmap

我已经写了一个solitare游戏,虽然逻辑是坚实和完整的,但形式缺乏。我使用位图编写了动画功能,这很奇怪。似乎位图打印功能太慢而无法跟上运行时处理速度。以下是方法:

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;
}

我不确定是否有更快的方式来显示图像。我愿意接受那里的建议。如果没有,我能做些什么来缩短处理时间吗?

0 个答案:

没有答案