# Directx text stays under sprites

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

## Recommended Posts

Nowadays I'm creating my gui system(I am asking one question per day ^^,so some of you should know it lol)anyway as my title says when I want to write the captions of buttons, the sprites "override" that text,although I am drawing text later than sprite.. Here is the code how I render my label..
void Label::Draw(LPD3DXSPRITE &d3dspt,LPD3DXFONT &dxfont)
{
static D3DXMATRIX translation,scale,identity;

D3DXMatrixIdentity(&translation);
D3DXMatrixIdentity(&scale);
D3DXMatrixIdentity(&identity);

D3DXMatrixTranslation(&translation, X, Y, 0.9f);
D3DXMatrixScaling(&scale, ScaleX, ScaleY, 1);

d3dspt->SetTransform(&(scale*translation));
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 position(0.0f, 0.0f, 0.0f);
d3dspt->Draw(sprite, NULL, &center,&position, D3DCOLOR_ARGB(200, 255, 255, 255));

d3dspt->SetTransform(&identity);

RECT textbox;
SetRect(&textbox, X, Y,X+Width, Y+Height);

dxfont->DrawTextA(NULL,
Caption.c_str(),
Caption.length(),
&textbox,
DT_CENTER | DT_VCENTER,
D3DCOLOR_ARGB(255, 255, 255, 255));
}


I'll provide if you need additional information like how I initialize dxfont :S thanks.. [Edited by - Eralp on May 8, 2008 2:20:48 PM]

##### Share on other sites
It doesn't matter always if you draw "later", there is also z-coordinate.

for example: D3DXMatrixTranslation(&translation, X, Y, 0.0f);
where Z = 0;

In 2D space when you draw from 0.0 to 1.0, 1.0 z coordinate is further from you 0.0 is near.
try to put: 3DXMatrixTranslation(&translation, X, Y, 0.9f); It should help.

##### Share on other sites
thanks for the fast reply, that is logical but it didnt work.I also made the scaleZ = 1 that it wont multiple my depth with 0, but it didnt work.I must be missing something more

##### Share on other sites
You can also turn off Z writings with SetRenderState, your draws will be shown on screen in order you draw them. When you finish turn on back Z-Writings.

##### Share on other sites
Ok, its not my origonal question, but Ill soon be running into the same problem...

You say "when you finish". When I finish drawing each frame or, just set it back in my directx cleanup func?

##### Share on other sites
Quote:
 Original post by BreathOfLifeOk, its not my origonal question, but Ill soon be running into the same problem...You say "when you finish". When I finish drawing each frame or, just set it back in my directx cleanup func?

Noo [smile] You can always call SetRenderState in middle of the drawing
BeginScene();....DrawSprite();//pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);DrawText();pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);EndScene();

With which parameters are you calling BeginSprite function? Begin sprite sets bunch of renderstates...

This is a little trick that will allow you to draw some things in front/back of other.

To notice some things: you don't need to call on translation & scale, you are already fill that with D3DXMatrixTranslation & D3DXMatrixScaling.

You can do that this way too:
m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);	D3DXVECTOR2 vPos(x,y);	D3DXVECTOR2 vCnt(center_x,_center_y);	D3DXVECTOR2 vScale(scale,scale);        D3DXMATRIX mat;	// rotation, translation	D3DXMatrixTransformation2D(&mat,&vCnt,0,&vScale,&vCnt,rotate,&vPos);	// Tell the sprite about the matrix	m_pSprite->SetTransform(&mat);		D3DXVECTOR3 ps(0,0,z); // z coordinate	m_pSprite->Draw(stripe->texture,&srcRect,NULL,&ps,col);        // put back	D3DXMatrixIdentity(&mat);	m_pSprite->SetTransform(&mat);m_pSprite->End();

Notice D3DXMatrixTransformation2D, rotation, scaling and translation are set with one call.

Oh I noticed one more thing. Don't draw text or anything inside sprite begin/end block.
So
m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
m_pSprite->Draw(...
m_pSprite->End();

dxfont->DrawTextA(
is right way.
As I said BeginSprite call sets bunch of renderstates and dxfont->DrawTextA will act unpredictable if inside of begin/end block of the sprite.

##### Share on other sites
I think you got the problem, it acts weird because it is inside "d3dsptbegin and end", I'm gonna add DrawText function to my gui objects.Then I should be okk, at least I hope ^^ thanks for your answers