Jump to content
  • Advertisement
Sign in to follow this  
firegod666

How do I write text into a texture with Alpha

This topic is 4048 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 have some code that writes text into a texture which is displayed using DirectX9. The code words fine if the object is not rendered with Alpha blending enabled. If I render the object with Alpha the text written out cookie cuts the texture. Ie. it's like the text has no alpha component or if it does have one it's Zero. Heres my code...Any ideas???? I've just been experimenting, blitting a white rectangle does the same and just cuts a hole in the texture.
   
void texture_print(TEXTURE_ID tid,FONT_ID fid,INT32 x,INT32 y,GTEXT *tptr)
{
  HDC hdc=NULL;
  IDirect3DSurface9 *sur;
  IDirect3DTexture9 *tex = (IDirect3DTexture9 *)tid;
  FONT  *fptr = (FONT *)fid;
  COLORREF  oldfgcol,oldbgcol;
  INT32	oldbgmode;
  HGDIOBJ oldfont;

  if(!fptr->hfont)						     /* check we have the font loaded */
    return;  
  if(!tex)							     /* check we have a texture to drwa into */
    return;

  tex->GetSurfaceLevel(0,&sur);                                      /* get the surface from the texture */
  sur->GetDC(&hdc);
  if(hdc)
  {
    oldfgcol=SetTextColor(hdc,fptr->fgcolour);		             /* set colour text colour */
    if(fptr->flags & FONT_TRANS)
      oldbgmode=SetBkMode(hdc,TRANSPARENT);			     /* turn on transparent background */
    else
    {
      oldbgmode=SetBkMode(hdc,OPAQUE);				     /* turn off transparent background */
    } 
    oldbgcol=SetBkColor(hdc,fptr->bgcolour);                       /* set up background colour if opaque mode */
    oldfont = SelectObject(hdc,fptr->hfont);                         /* set to use this font */

    ExtTextOut(hdc,x,y,0,NULL,tptr,lstrlen(tptr),NULL);		     /* render text to surface */

    SelectObject(hdc,oldfont);					     /* restore font */
    SetBkMode(hdc,oldbgmode);					     /* restore background mode */
    SetBkColor(hdc,oldbgcol);					     /* restore background colour */
    SetTextColor(hdc,oldfgcol);					     /* restore colour */
    sur->ReleaseDC(hdc);                                             /* give back the dc */
  }
  sur->Release();

}


[Edited by - firegod666 on June 14, 2007 9:41:10 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm surprised this doesn't give you an error (or maybe it does and you ignore it?). GetDC shouldn't work on a surface with alpha.

I'd suggest you use D3DX services to draw text.

Share this post


Link to post
Share on other sites
Quote:
Original post by ET3D
I'm surprised this doesn't give you an error (or maybe it does and you ignore it?). GetDC shouldn't work on a surface with alpha.

I'd suggest you use D3DX services to draw text.


But I need to draw the text into a texture dynamically, I can't figure out how to do this with D3DX, I can't find an example anywhere.

Strangly GetDC does work but any operation on it ignores the Alpha channel. I've got around it now by filling the Alpha channel manually after the text has been written.

Share this post


Link to post
Share on other sites
Quote:
Original post by firegod666
But I need to draw the text into a texture dynamically, I can't figure out how to do this with D3DX, I can't find an example anywhere.

Create the texture as a render target, use SetRenderTarget, then draw into it as normal. That's the short of it. If you need more details, google for them or ask.

Share this post


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

  • 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!