2012-07-23 35 views
0

我一直在使用Direct X编写我自己的库,并遇到了一个奇怪的问题。虽然试图呈现一个生动活泼的精灵,我只是看到一个大黑方:Direct X Sprite渲染问题

Direct X Rendering Issue

我已经通过代码加强痴迷,并得出结论认为,它必须是一些有关的实际精灵的加载,因为一切我可以在我的代码中看到很好。显然,我不能进入BltFast这样的函数,所以无法分辨我的精灵表面是否成功地传递到后端缓冲器。

这里是我的加载和渲染功能精灵:

雪碧:: LOAD

/** 
* loads a bitmap file and copies it to a directdraw surface 
* 
* @param pID    wait 
* @param pFileName  name of the bitmap file to load into memory 
*/ 
void Sprite::Load (const char *pID, const char *pFileName) 
{ 
    // initialises the member variables with the new image id and file name 
    mID     = pID; 
    mFileName   = pFileName; 

    // creates the necessary variables 
    HBITMAP    tHBM; 
    BITMAP    tBM; 
    DDSURFACEDESC2  tDDSD; 
    IDirectDrawSurface7 *tDDS; 

    // stores bitmap image into HBITMAP handler 
    tHBM     = static_cast<HBITMAP> (LoadImage (NULL, pFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION)); 

    GetObject (tHBM, sizeof (tBM), &tBM); 

    // create surface for the HBITMAP to be copied onto 
    ZeroMemory (&tDDSD, sizeof (tDDSD)); 
    tDDSD.dwSize   = sizeof (tDDSD); 
    tDDSD.dwFlags   = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; 
    tDDSD.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; 
    tDDSD.dwWidth   = tBM.bmWidth; 
    tDDSD.dwHeight   = tBM.bmHeight; 
    DirectDraw::GetInstance()->DirectDrawObject()->CreateSurface (&tDDSD, &tDDS, NULL); 

    // copying bitmap image onto surface 
    CopyBitmap(tDDS, tHBM, 0, 0, 0, 0); 

    // deletes bitmap image now that it has been used 
    DeleteObject(tHBM); 

    // stores the new width and height of the image 
    mSpriteWidth   = tBM.bmWidth; 
    mSpriteHeight   = tBM.bmHeight; 

    // sets the address of the bitmap surface to this temporary surface with the new bitmap image 
    mBitmapSurface   = tDDS; 
} 

雪碧::渲染

/** 
* renders the sprites surface to the back buffer 
* 
* @param pBackBuffer  surface to render the sprite to 
* @param pX    x co-ordinate to render to (default is 0) 
* @param pY    y co-ordinate to render to (default is 0) 
*/ 
void Sprite::Render (LPDIRECTDRAWSURFACE7 &pBackBuffer, float pX, float pY) 
{ 
    if (mSpriteWidth > 800)  mSpriteWidth = 800; 

    RECT   tFrom; 

    tFrom.left  = tFrom.top  = 0; 
    tFrom.right  = mSpriteWidth; 
    tFrom.bottom = mSpriteHeight; 

    // bltfast parameters are (position x, position y, dd surface, draw rect, wait flag) 
    // pBackBuffer->BltFast (0 + DirectDraw::GetInstance()->ScreenWidth(), 0, mBitmapSurface, &tFrom, DDBLTFAST_WAIT); 
    pBackBuffer->BltFast (static_cast<DWORD>(pX + DirectDraw::GetInstance()->ScreenWidth()), 
     static_cast<DWORD>(pY), mBitmapSurface, &tFrom, DDBLTFAST_WAIT); 
} 

回答

0

表面很根本不兼容的格式。

这里的固定copybitmap功能,我现在在负载的功能调用:

extern "C" HRESULT 
DDCopyBitmap(IDirectDrawSurface7 * pdds, HBITMAP hbm, int x, int y, 
      int dx, int dy) 
{ 
    HDC      hdcImage; 
    HDC      hdc; 
    BITMAP     bm; 
    DDSURFACEDESC2   ddsd; 
    HRESULT     hr; 

    if (hbm == NULL || pdds == NULL) 
     return E_FAIL; 
    // 
    // Make sure this surface is restored. 
    // 
    pdds->Restore(); 
    // 
    // Select bitmap into a memoryDC so we can use it. 
    // 
    hdcImage = CreateCompatibleDC(NULL); 
    if (!hdcImage) 
     OutputDebugString("createcompatible dc failed\n"); 
    SelectObject(hdcImage, hbm); 
    // 
    // Get size of the bitmap 
    // 
    GetObject(hbm, sizeof(bm), &bm); 
    dx = dx == 0 ? bm.bmWidth : dx;  // Use the passed size, unless zero 
    dy = dy == 0 ? bm.bmHeight : dy; 
    // 
    // Get size of surface. 
    // 
    ddsd.dwSize = sizeof(ddsd); 
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH; 
    pdds->GetSurfaceDesc(&ddsd); 

    if ((hr = pdds->GetDC(&hdc)) == DD_OK) 
    { 
     StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, x, y, 
        dx, dy, SRCCOPY); 
     pdds->ReleaseDC(hdc); 
    } 
    DeleteDC(hdcImage); 
    return hr; 
}