• 12
• 12
• 9
• 10
• 13

Displaying 2D Bitmap in DirectX 9.0c

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

Recommended Posts

Hello, I am looking for a good Example/Tutorial (Source code)for displaying Bitmaps (BMP/JPG) on a 3D Screen. Windowed, Fullscreen and KeyColor for BMP. I am ussing Directdraw in DX 7.0. Works fine. But how in DirectX 9.0c. Thanks for helping me. Regards Henk.

Share on other sites
Hi,

Bare in mind that you're making a huge jump from 2D with DX7 to 2D with DX9. There are lots of new things to be considering (texture sizes being one, hardware caps another). Do you have an existing D3D9 framework?

You probably want to look into some basic D3D9 tutorials to get your framework started, and then do some research into ID3DXSprite's capabilities. This interface is designed pretty much specifically for what you want to be doing.

I dont have any links to hand, but you might find some in the Forum FAQ, or another person round here can put in some recommendations [smile]

hth
Jack

Share on other sites
The sprite interface is good for textures with dimensions that are powers of 2. If they don't fit that, then create a LPDIRECT3DSURFACE9 with CreateOffscreenPlainSurface and D3DXLoadSurfaceFromFile and pass it the name of the texture file. Then use StretchRect to draw it. This means you'll need to save the backbuffer as a surface as well though and draw to it in the StretchRect call.

[Edited by - njpaul on April 27, 2005 12:22:26 AM]

Share on other sites
Quote:
 Original post by njpaulThe sprite interface is good for textures with dimensions that are powers of 2. If they don't fit that, then create a LPDIRECT3DSURFACE9 and put a texture on it that you get from a call to LoadTextureFromFile. Then use StretchRect to draw it. This means you'll need to save the backbuffer as a surface as well though and draw to it in the StretchRect call.

Thanks, I think that is fine. Do you have a example with source?
If so? My mail adress is descartes@hetnet.nl

regards Henk,

Share on other sites
I sent you the source. Basically the code for the sprite is:

// assumes you have a LPD3DXSPRITE interface, lets say g_Globals.lpSprite...// get to the part where you want to load the texure. Pretend// all variables are initialized. lpDevice is the graphics device// and m_pTexture is LPDIRECT3DTEXTURE9D3DXCreateTextureFromFile(g_Globals.lpDevice,"..\\Images\\SampleSprite.png",&m_pTexture);...// get to the part where you want to render the spriteg_Globals.lpSprite->Begin(D3DXSPRITE_ALPHABLEND);// draws the sprite. pretend the other vars are initializedg_Globals.lpSprite->Draw(m_pTexture,&m_pSrcRect,			 &m_v3Center,&m_v3Trans,m_color);... // make as many draw calls as you can here to improve performanceg_Globals.lpSprite->End();

Note that if the image dimensions aren't a power of 2, the texture will stretch to become one. Ex. if you load a 150x20 sprite, it will become 256x32. Now for the other technique:

// I make a this function to simplify things. Note this is not something that// already exists in DirectXLPDIRECT3DSURFACE9 GetSurfaceFromFile(const char* filename){	LPDIRECT3DSURFACE9	pSurface = 0;	D3DXIMAGE_INFO		imageInfo;	if(FAILED(D3DXGetImageInfoFromFile(filename, &imageInfo)))		return 0;	if(FAILED(g_Globals.lpDevice->		CreateOffscreenPlainSurface(imageInfo.Width,imageInfo.Height,			D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&pSurface,0)))	{		return 0;	}	if(FAILED(D3DXLoadSurfaceFromFile(pSurface,0,0,filename,					0,D3DX_DEFAULT,0,0)))	{		return 0;	}	return pSurface;}// now we get to the part where we want to create the sprite.// Note that m_pBackground is a LPDIRECT3DSURFACE9m_pBackground = GetSurfaceFromFile("..\\Images\\Background.png");...// the to render. note that you need the backbufferg_Globals.lpDevice->StretchRect(m_pBackground,0,				g_Globals.lpBackBuffer,0,D3DTEXF_NONE);

Thats all there is to it. The helper function really saves time. If anyone wants the full source to a sample application that uses it, just email me.