Directx Direct X Sprite渲染问题
我一直在使用DirectX编写自己的库,遇到了一个奇怪的问题。在尝试渲染动画精灵时,我只看到一个大的黑色正方形: 我痴迷地浏览了代码,并得出结论,这一定与实际精灵的加载有关,因为我在代码中看到的一切都很好。显然,我无法单步执行BltFast之类的函数,因此无法判断我的精灵曲面是否已成功地Blite到backbuffer上 以下是我的精灵加载和渲染功能: SPRITE::LOADDirectx Direct X Sprite渲染问题,directx,rendering,sprite,Directx,Rendering,Sprite,我一直在使用DirectX编写自己的库,遇到了一个奇怪的问题。在尝试渲染动画精灵时,我只看到一个大的黑色正方形: 我痴迷地浏览了代码,并得出结论,这一定与实际精灵的加载有关,因为我在代码中看到的一切都很好。显然,我无法单步执行BltFast之类的函数,因此无法判断我的精灵曲面是否已成功地Blite到backbuffer上 以下是我的精灵加载和渲染功能: SPRITE::LOAD /** * loads a bitmap file and copies it to a directdraw
/**
* 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;
}
/**
*加载位图文件并将其复制到directdraw曲面
*
*@param pID等待
*@param pFileName要加载到内存中的位图文件的名称
*/
void Sprite::Load(常量字符*pID,常量字符*pFileName)
{
//使用新的映像id和文件名初始化成员变量
mID=pID;
mFileName=pFileName;
//创建必要的变量
HBITMAP-tHBM;
位图隧道掘进机;
DDSURFACEDESC2-tDDSD;
IDirectDrawSurface7*tDDS;
//将位图图像存储到HBITMAP处理程序中
tHBM=静态_cast(LoadImage(NULL,pFileName,IMAGE_位图,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION));
GetObject(tHBM、sizeof(tBM)和tBM);
//为要复制到的HBITMAP创建曲面
零内存(&tDDSD,sizeof(tDDSD));
tDDSD.dwSize=sizeof(tDDSD);
tDDSD.dwFlags=DDSD_CAPS | DDSD_高度| DDSD_宽度;
tDDSD.ddsCaps.dwCaps=ddsCaps\u OFFSCREENPLAIN;
tDDSD.dwWidth=tBM.bmWidth;
tDDSD.dwHeight=tBM.bmHeight;
DirectDraw::GetInstance()->DirectDrawObject()->CreateSurface(&tDDSD,&tDDS,NULL);
//将位图图像复制到曲面上
复制位图(tDDS、tHBM、0、0、0);
//删除已使用的位图图像
删除对象(tHBM);
//存储图像的新宽度和高度
mSpriteWidth=tBM.bmWidth;
mSpriteHeight=tBM.bmHeight;
//使用新位图图像将位图曲面的地址设置为此临时曲面
MbitMassurface=tDDS;
}
SPRITE::RENDER
/**
* 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);
}
/**
*将精灵曲面渲染到后缓冲区
*
*@param pBackBuffer surface将精灵渲染到
*要渲染到的@param pX x坐标(默认值为0)
*要渲染到的@param pY y坐标(默认值为0)
*/
void Sprite::Render(LPDIRECTDRAWSURFACE7&pBackBuffer、float pX、float pY)
{
如果(mSpriteWidth>800)mSpriteWidth=800;
RECT-tFrom;
tFrom.left=tFrom.top=0;
tFrom.right=mSpriteWidth;
tFrom.bottom=mSpriteHeight;
//bltfast参数为(位置x、位置y、dd曲面、绘制矩形、等待标志)
//pBackBuffer->BltFast(0+DirectDraw::GetInstance()->ScreenWidth(),0,mbitmassurface,&tFrom,DDBLTFAST_WAIT);
pBackBuffer->BltFast(静态转换(pX+DirectDraw::GetInstance()->ScreenWidth()),
静态广播(pY)、MbitMassurface和tFrom、DDBLTFAST\u等待);
}
曲面的格式根本不兼容
下面是我现在在load函数中调用的fixed 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;
}