|
|
发表于 2007-1-4 10:34:00
|
显示全部楼层
Re:请问绘制2D图象设置透明后的黑色背景怎么改,那来
int GetBmpWidth(const int bmWidth)
{
int nnop = bmWidth%4;
if (nnop)
{
return bmWidth + 4 - nnop;
}
else
{
return bmWidth;
}
}
IDirect3DSurface9 * LoadSurfaceFromFile(IDirect3DDevice9 * Device, char * FileName)
{
if (!Device) return 0;
HBITMAP hb = (HBITMAP)LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if (!hb) return 0;
BITMAP bmp;
GetObject(hb, sizeof(bmp), &bmp);
IDirect3DSurface9 * pSurface;
if (FAILED( Device->CreateOffscreenPlainSurface(bmp.bmWidth, bmp.bmHeight, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSurface, NULL)
)
)
{
DeleteObject(hb);
return 0;
}
D3DLOCKED_RECT lr;
if (FAILED( pSurface->LockRect(&lr, NULL, D3DLOCK_DISCARD)
)
)
{
DeleteObject(hb);
return 0;
}
D3DCOLOR * dest = (D3DCOLOR*)lr.pBits;
BYTE * source = (BYTE*)bmp.bmBits;
int nWidth = GetBmpWidth(bmp.bmWidth);
for (int y=0;y<bmp.bmHeight;++y)
{
for (int x=0;x<bmp.bmWidth;++x)
{
dest[(bmp.bmHeight - 1 - y)*(lr.Pitch/4)+x] = D3DCOLOR_ARGB(255,
source[(y*nWidth+x)*3+2],
source[(y*nWidth+x)*3+1],
source[(y*nWidth+x)*3]
);
}
}
pSurface->UnlockRect();
DeleteObject(hb);
return pSurface;
}
IDirect3DTexture9 * LoadTextureFromFile(IDirect3DDevice9 * Device, char * FileName)
{
if (!Device) return 0;
HBITMAP hb = (HBITMAP)LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if (!hb)
{
MessageBox(0, "LoadImage Failed!", NULL, MB_ICONSTOP);
return 0;
}
BITMAP bmp;
GetObject(hb, sizeof(bmp), &bmp);
IDirect3DTexture9 * pTexture;
HRESULT hr = Device->CreateTexture(bmp.bmWidth, bmp.bmHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
if (FAILED(hr
//if (FAILED( Device->CreateTexture(bmp.bmWidth, bmp.bmHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL)
//if (FAILED(D3DXCreateTextureFromFile(Device, FileName, &pTexture)
)
)
{
MessageBox(0, "Create Texture Failed!", NULL, MB_ICONSTOP);
DeleteObject(hb);
return 0;
}
D3DLOCKED_RECT lr;
if (FAILED( pTexture->LockRect(0, &lr, NULL, 0)
)
)
{
MessageBox(0, "Lock Texture Failed!", NULL, MB_ICONSTOP);
DeleteObject(hb);
return 0;
}
D3DCOLOR * dest = (D3DCOLOR*)lr.pBits;
BYTE * source = (BYTE*)bmp.bmBits;
int nWidth = GetBmpWidth(bmp.bmWidth);
for (int y=0;y<bmp.bmHeight;++y)
{
for (int x=0;x<bmp.bmWidth;++x)
{
dest[(bmp.bmHeight - 1 - y)*(lr.Pitch/4)+x] = D3DCOLOR_ARGB(255*((source[(y*nWidth+x)*3+2]!=0)|(source[(y*nWidth+x)*3+1]!=0)|(source[(y*nWidth+x)*3]!=0)),
source[(y*nWidth+x)*3+2],
source[(y*nWidth+x)*3+1],
source[(y*nWidth+x)*3]
);
}
}
pTexture->UnlockRect(0);
DeleteObject(hb);
return pTexture;
} |
|