HRESULT CreateTextureFromSurface (LPCTSTR tszTextureFile)
{
HRESULT hr;
D3DDISPLAYMODE d3ddm;
RECT rc;
D3DLOCKED_RECT d3dRect;
BYTE* pbyImage = NULL;
D3DSURFACE_DESC d3dDesc;
IDirect3DSurface8* pSurface = NULL;
g_pD3D->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &d3ddm);
hr = pDevice->CreateImageSurface (1024, 768, d3ddm.Format, &pSurface);
if FAILED (hr)
{
MessageBox (NULL, "CreateImageSurface () failed!", "Error", MB_OK);
return hr;
}
if FAILED (hr = g_pDevice->CreateTexture (256, 256, 1, 0, d3ddm.Format, D3DPOOL_MANAGED, &m_pBackgroundTexture))
{
MessageBox (NULL, "CreateTexture () failed!", "Error", MB_OK);
pSurface->Release ();
pSurface = NULL;
return hr;
}
if FAILED (hr = D3DXLoadSurfaceFromFile (pSurface, NULL, NULL, tszTextureFile, NULL, D3DX_FILTER_LINEAR, 0, NULL))
{
MessageBox (NULL, "D3DXLoadSurfaceFromFile () failed!", "Error", MB_OK);
pSurface->Release ();
pSurface = NULL;
return hr;
}
SetRect (&rc, 0, 0, 256, 256);
pSurface->GetDesc (&d3dDesc);
pbyImage = new BYTE[d3dDesc.Size];
pSurface->LockRect (&d3dRect, &rc, D3DLOCK_READONLY);
memcpy (pbyImage, d3dRect.pBits, d3dDesc.Size * sizeof (BYTE));
pSurface->UnlockRect ();
g_pBackgroundTexture->GetLevelDesc (0, &d3dDesc);
g_pBackgroundTexture->LockRect (0, &d3dRect, NULL, 0);
memcpy (d3dRect.pBits, pbyImage, d3dDesc.Size * sizeof (BYTE));
g_pBackgroundTexture->UnlockRect (0);
delete[] pbyImage;
pSurface->Release ();
pSurface = NULL;
return S_OK;
}
With that method the bitmap won''t be displayed. But if I set the size of the surface to 256x256 there are no problems and the bitmap will be displayed. My question is: Why ?
The bitmap has a size of 1024x768. I would copy 12 pieces of size 256x256 of the bitmap into 12 textures. But to do that I need a surface that fits the size of the bitmap.
- LongingForDeath
Copying Surfaces to Textures
Hi,
I think it is better to post the code first:
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement