This topic is now archived and is closed to further replies.


DX8 - BMP to Texture

Recommended Posts

Do you absolutely need the Bitmap in HBITMAP format? I have no idea how the HBITMAP works, but the same may apply to that if you can parse it. (Else, I suggest skipping using windows to load the bitmap).

Get yourself the BMP file format from
Using any file function (I use FILE *) open the BMP in binary and read the header.
Parse the BMP header, to find information about the data, e.g. height, width, offset to the location the data is, etc.
SEEK to the offset found in the header.
Read the data into a locked Dx8 texture (which I assume you will know about, including the "Pitch" problem that may occur).

However, as I said before, if you derive this info from a HBITMAP, then you could probably use that, but this is much more convienient.


Share this post

Link to post
Share on other sites
The only problem with locking a LPDIRECT3DTEXTURE8 structure is that you can only lock one file format!! Anyone know how to get around this? Maybe with locking the resource surface?

"If I wanted to hear the pitter patter of little feet I would put shoes on my cat"

Share this post

Link to post
Share on other sites
What do you mean? Locking only one surface format? Do you mean there are no format-conversions as you would if you could get the DC (in Dx7) and then use the GDI to draw it?

I use D3DFMT_A8R8G8B8 for my textures (and so is my screen).


Share this post

Link to post
Share on other sites
Hehe, well, they made that alot harder in DX8.
What you will have to do is to create a DIB section with a format matching the texture's. Then you will have to copy it manually. Here's some code from my "engine". It is an OO engine, so I hope you know how that works

int texWidth, texHeight;
D3DCAPS8 d3dc;
HDC hdc_DIB, hdcImage;
int y;
CTexture* lpCTexture;

GetObject(hImage, sizeof(BITMAP), &bm);
texWidth = bm.bmWidth;
texHeight = bm.bmHeight;

// Step one, create a texture.
lpCTexture = new CTexture();

if (d3dc.TextureCaps & D3DPTEXTURECAPS_POW2)
for(texWidth = 1; bm.bmWidth > texWidth; texWidth <<= 1);
for(texHeight = 1; bm.bmHeight > texHeight; texHeight <<= 1);

if (texWidth > texHeight) texHeight = texWidth;
else texWidth = texHeight;

switch (m_textureDepth)
case 16 :
m_lp3dDevice->CreateTexture(texWidth, texHeight, 1, 0, D3DFMT_A1R5G5B5,
D3DPOOL_MANAGED, &lpCTexture->m_lp3dTexture);

case 32 :
m_lp3dDevice->CreateTexture(texWidth, texHeight, 1, 0, D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED, &lpCTexture->m_lp3dTexture);

// Step three, create a matching DIB section with the same depth as the
// texture.
ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = texWidth;
bmi.bmiHeader.biHeight = texHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biBitCount = m_textureDepth;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;

hdc_DIB = CreateCompatibleDC(NULL);
hbm_DIB = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS,
(VOID**)&lpDIB, NULL, 0);

// Step four, copy the image into the newly created DIB section.
hdcImage = CreateCompatibleDC(NULL);
SelectObject(hdcImage, hImage);
SelectObject(hdc_DIB, hbm_DIB);
StretchBlt(hdc_DIB, 0, 0, texWidth, texHeight, hdcImage, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);

// Step five, copy the DIB data into the texture.
lpCTexture->m_lp3dTexture->LockRect( 0, &d3dlr, NULL, NULL);
lpCTexture->m_lp3dTexture->GetLevelDesc(0, &d3sd);

for (y = 0; y < texHeight; ++y)
CopyMemory((LPBYTE)d3dlr.pBits + (y * d3dlr.Pitch), lpDIB + ((texHeight-1-y) * texWidth * (m_textureDepth / 8)), texWidth * (m_textureDepth / 8));



Sorry about the indentation, but I'm too lazy to replace all tabs

// Poppeman.

Edited by - poppeman on April 25, 2001 1:29:59 PM

Share this post

Link to post
Share on other sites
Poppeman just did! You have to copy the contents of an HBITMAP structure to a texture structure...

"If I wanted to hear the pitter patter of little feet I would put shoes on my cat"

Share this post

Link to post
Share on other sites