Advertisement Jump to content
Sign in to follow this  

HDC from IDirect3DTexture9

This topic is 4904 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

Hi! I'm presently moving my 2D engine from DirectDraw to Direct 3D. I'm very happy about the result but I have a last important thing to do : How to get an HDC from an IDirect3DTexture9? Thanks you, Joe.

Share this post

Link to post
Share on other sites
(1) Use IDirect3DTexture9::GetSurfaceLevel( 0 ) to retrieve a surface on the texture.

(2) Use IDirect3DSurface9::GetDC() to get the device context.

Share this post

Link to post
Share on other sites
Thanks, it work well, but now, I have a Transparancy/Color Keying problem:

// Members & initiation
IDirect3DSurface9 *pds;
d3d.m_pd3dDevice->CreateTexture(800, 600, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);

// Set vertex for 2D on 3D...

// Draw the Texture with GDI function, and then, display it
pTexture->GetSurfaceLevel(0, &pds);
if( pds->GetDC( &hDC ) )
TextOut(hDC, 10, 10, "Hello!", 6);
.... Other GDI draw.....
pds->ReleaseDC( hDC );

//Set texture
d3d.m_pd3dDevice->SetTexture(0, pTexture);
d3d.m_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
d3d.m_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
d3d.m_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x01);
d3d.m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);

// End drawing...

It's work well, but it's show the entire 800x600 texture. (Black background) Can someone explain me why there's no transparency?


[Edited by - Coder on August 21, 2005 2:57:15 PM]

Share this post

Link to post
Share on other sites
I can think of the following solutions. Smarter ones most probably exist:
1) Don't use GDI to draw the texture, do it another way.
2) Use a pixel shader, and emit an alpha of 0 if the input color's the same as your colorkey (a pixel shader constant).
3) Lock the texture after you draw to it, and copy it to another texture with alpha.

Share this post

Link to post
Share on other sites
I've done more than enough messing about with texture memory to get to/from formats... and in my experience the "built in" functionality (be it GDI/DC's or the StretchRect/CopyRect stuff) is good so long as you stick to using it the way THEY want you to use it.

As soon as you step outside of the designated use you're pretty much back to square one.

In those cases, or at least my experience of those cases, you usually have to resort to locking the texture and reading out the data as appropriate.

If I were you, double check all the available tools with regards to converting textures to suitable formats, and if you can't get what you want you might just have to bite the bullet and implement your own routine [headshake]


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!