Jump to content
  • Advertisement


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


DX8 - BMP to Texture

This topic is 6359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I take an existing bitmap image in either an HBITMAP structure or an HDC, and make it useable as a texture in DirectX 8?

Share this post

Link to post
Share on other sites
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 wotsit.org
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
Um, thanks for all the advice, but that wasn''t my question. I want to copy a HBITMAP or an HDC to a Dx8 texture. anyone?

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!