Can't Render Sprites & TTF Text At Same Time?

Started by
4 comments, last by kauna 10 years, 5 months ago

Hi,

Working on a 2D DirectX 9.0c game engine now.

I am working on graphics core.

Problem I have now is that I can't seem to render sprites and TTF text simultaneously?

I can render sprites and I can render TTF texts separately, but when I try to do both I only get sprites?

Hope someone can help, thanks!

JeZ+Lee

Here is the relevant source code:


void Visuals::DrawSpriteOntoScreenBuffer(int index)
{
    Sprites[index].DXSprite->Begin(D3DXSPRITE_ALPHABLEND);

    if (Sprites[index].Smooth == false)
    {
        DXDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
        DXDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
        DXDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
    }
    else if (Sprites[index].Smooth == true)
    {
        DXDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
        DXDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
        DXDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC);
    }

    RECT spriteRect;
    spriteRect.top = 0;
    spriteRect.left = 0;
    spriteRect.bottom = Sprites[index].TextureHeight;
    spriteRect.right = Sprites[index].TextureWidth;

    D3DXVECTOR3 spritePosition;
    spritePosition.x = 0.0f - (Sprites[index].TextureWidth/2);
    spritePosition.y = 0.0f - (Sprites[index].TextureHeight/2);
    spritePosition.z = 0.0f;

    D3DXMATRIX matrixSpriteTransform;

    D3DXVECTOR2 screenPostion = D3DXVECTOR2(Sprites[index].ScreenX, Sprites[index].ScreenY);

    float rotation = Sprites[index].RotationDegree * (3.14159265f/180.0f);

    if (Sprites[index].FlipX == true)  Sprites[index].ScaleX = (Sprites[index].ScaleX*-1.0f);
    if (Sprites[index].FlipY == true)  Sprites[index].ScaleY = (Sprites[index].ScaleY*-1.0f);
    D3DXVECTOR2 scaling(Sprites[index].ScaleX, Sprites[index].ScaleY);
    if (Sprites[index].FlipX == true)  Sprites[index].ScaleX = (Sprites[index].ScaleX*-1.0f);
    if (Sprites[index].FlipY == true)  Sprites[index].ScaleY = (Sprites[index].ScaleY*-1.0f);

    D3DXMatrixTransformation2D(&matrixSpriteTransform, NULL, 0.0, &scaling, NULL, rotation, &screenPostion);
    Sprites[index].DXSprite->SetTransform(&matrixSpriteTransform);

    Sprites[index].DXSprite->Draw( Sprites[index].Texture, &spriteRect, NULL, &spritePosition,
                                   D3DCOLOR_RGBA(Sprites[index].RedHue, Sprites[index].GreenHue, Sprites[index].BlueHue, Sprites[index].Transparency) );

    Sprites[index].DXSprite->End();
}


void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, LPD3DXFONT Font, int posX, int posY,
                                       int XJustification, int textRed, int textGreen, int textBlue,
                                       int outlineRed, int outlineGreen, int outlineBlue)
{
RECT rect, textSize;

    Font[0].DrawTextA( NULL, textToDisplay, -1, &textSize, DT_CALCRECT, D3DCOLOR_RGBA(textRed, textGreen, textBlue, 255) );

    if (XJustification == JustifyLeft)
    {
        // Do nothing...
    }
    else if (XJustification == JustifyCenter)
    {
        posX = (800 / 2) - (textSize.right / 2) - 3;
    }
    else if (XJustification == JustifyRight)
    {
        posX = (800 - posX) - textSize.right - 3;
    }
    else if (XJustification == JustifyCenterOnPoint)
    {
        posX = posX - (textSize.right / 2) - 3;
    }

    for (int screenY = -2; screenY < 3; screenY++)
    {
        for (int screenX = -2; screenX < 3; screenX++)
        {
            SetRect( &rect, posX+screenX, posY+screenY, 0, 0 );
            if (screenY != 0 && screenX != 0)
                Font[0].DrawTextA( NULL, textToDisplay, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(outlineRed, outlineGreen, outlineBlue, 255) );
        }
    }

    SetRect( &rect, posX, posY, 0, 0 );
    Font[0].DrawTextA( NULL, textToDisplay, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(textRed, textGreen, textBlue, 255) );
}


void Visuals::DisplayScreenBufferOntoDisplay(void)
{
    DXDevice->EndScene();
    DXDevice->Present( NULL, NULL, NULL, NULL );

    DXDevice->BeginScene();
}

void Screens::ApplyScreenFadeTransition(void)
{
    if (ScreenTransitionStatus == FadeIn)
    {
        if (ScreenFadeTransparency > 0)
        {
            ScreenFadeTransparency -= 1;
            ScreenIsDirty = true;
        }
        else
        {
            ScreenFadeTransparency = 0;
            ScreenTransitionStatus = FadeNone;
        }
    }
    else if (ScreenTransitionStatus == FadeOut)
    {
        if (ScreenFadeTransparency < 255)
        {
            ScreenFadeTransparency += 1;
            ScreenIsDirty = true;
        }
        else
        {
            ScreenFadeTransparency = 255;
            ScreenTransitionStatus = FadeAll;
        }
    }

    if (ScreenTransitionStatus != FadeNone)
    {
        visuals->Sprites[0].ScreenX = 400;
        visuals->Sprites[0].ScreenY = 240;
        visuals->Sprites[0].Transparency = ScreenFadeTransparency;
        visuals->DrawSpriteOntoScreenBuffer(0);
    }
}


void Screens::ProcessScreenToDisplay(void)
{
    switch(ScreenToDisplay)
    {
        case SixteenBitSoftScreen:
            DisplaySixteenBitSoftScreen();
            break;

        case TitleScreen:
            DisplayTitleScreen();
            break;

        default:
            break;
    }

    ApplyScreenFadeTransition();

    if (ScreenIsDirty == true)
    {
        visuals->DisplayScreenBufferOntoDisplay();
        if (ScreenTransitionStatus != FadeIn && ScreenTransitionStatus != FadeOut)  ScreenIsDirty = false;
    }
}


void Screens::DisplaySixteenBitSoftScreen(void)
{
    if (ScreenTransitionStatus == FadeAll)
    {
        ScreenDisplayTimer = 370;
        ScreenTransitionStatus = FadeIn;
    }

    if (ScreenIsDirty == true)
    {
        visuals->ClearScreenBufferWithColor(0, 0, 0, 255);

        visuals->Sprites[5].ScreenX = 400;
        visuals->Sprites[5].ScreenY = 210;
        visuals->Sprites[5].ScaleX = 5.0;
        visuals->Sprites[5].ScaleY = 8.0;
        visuals->Sprites[5].Smooth = false;
        visuals->DrawSpriteOntoScreenBuffer(5);

        visuals->DrawTextOntoScreenBuffer("TM", visuals->Font[0], 55, 295, JustifyRight, 1, 255, 1, 1, 1, 1);

        visuals->DrawTextOntoScreenBuffer("Bringing back old memories from the 16bit era!", visuals->Font[0], 0, 307, JustifyCenter, 1, 255, 1, 1, 1, 1);

        visuals->DrawTextOntoScreenBuffer("www.16BitSoft.com", visuals->Font[0], 0, 347, JustifyCenter, 1, 255, 1, 1, 90, 1);
    }

    if (ScreenTransitionStatus == FadeOut && ScreenFadeTransparency == 255)
    {
        ScreenTransitionStatus = FadeAll;
        ScreenToDisplay = TitleScreen;
    }
}

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

Advertisement

Hi Again,

Still haven't solved this issue.

In: "void Visuals::DrawSpriteOntoScreenBuffer(int index)" function, if I comment out:

"D3DXMatrixTransformation2D(&matrixSpriteTransform, NULL, 0.0, &scaling, NULL, rotation, &screenPostion);"

the text draws?

Anyone know why using "D3DXMatrixTransformation2D" in another function before DrawTextA makes text not render?

Thanks in advance!

JeZ+Lee

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

I bet that it isn't actually the D3DXMatrixTransformation2D that is the problem.

In my opinion the problem is that you set a transform matrix with the function SetTransform(...)

Since the Direct3D is a state machine, you'll need to restore a proper transform matrix before using the text function, since they seem to be affected by a transform.

The transform matrix that you'll need to set before drawing text is probably an identity matrix.

Cheers!

I bet that it isn't actually the D3DXMatrixTransformation2D that is the problem.

In my opinion the problem is that you set a transform matrix with the function SetTransform(...)

Since the Direct3D is a state machine, you'll need to restore a proper transform matrix before using the text function, since they seem to be affected by a transform.

The transform matrix that you'll need to set before drawing text is probably an identity matrix.

Cheers!

Hi,

Thank you for your reply.

I changed the source code of the text render function to below, but it still does not display?

What am I doing wrong?

JeZ+Lee


void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, LPD3DXFONT Font, int posX, int posY,
                                       int XJustification, int textRed, int textGreen, int textBlue,
                                       int outlineRed, int outlineGreen, int outlineBlue)
{
RECT rect, textSize;

    D3DXMATRIX matrixFixTextAfterSpriteDrawing;
    matrixFixTextAfterSpriteDrawing._11 = 1;
    matrixFixTextAfterSpriteDrawing._12 = 0;
    matrixFixTextAfterSpriteDrawing._13 = 0;
    matrixFixTextAfterSpriteDrawing._14 = 0;
    matrixFixTextAfterSpriteDrawing._21 = 0;
    matrixFixTextAfterSpriteDrawing._22 = 1;
    matrixFixTextAfterSpriteDrawing._23 = 0;
    matrixFixTextAfterSpriteDrawing._24 = 0;
    matrixFixTextAfterSpriteDrawing._31 = 0;
    matrixFixTextAfterSpriteDrawing._32 = 0;
    matrixFixTextAfterSpriteDrawing._33 = 1;
    matrixFixTextAfterSpriteDrawing._34 = 0;
    matrixFixTextAfterSpriteDrawing._41 = 0;
    matrixFixTextAfterSpriteDrawing._42 = 0;
    matrixFixTextAfterSpriteDrawing._43 = 0;
    matrixFixTextAfterSpriteDrawing._44 = 1;
    D3DXMatrixIdentity(&matrixFixTextAfterSpriteDrawing);

    Font[0].DrawTextA( NULL, textToDisplay, -1, &textSize, DT_CALCRECT, D3DCOLOR_RGBA(textRed, textGreen, textBlue, 255) );

    if (XJustification == JustifyLeft)
    {
        // Do nothing...
    }
    else if (XJustification == JustifyCenter)
    {
        posX = (800 / 2) - (textSize.right / 2) - 3;
    }
    else if (XJustification == JustifyRight)
    {
        posX = (800 - posX) - textSize.right - 3;
    }
    else if (XJustification == JustifyCenterOnPoint)
    {
        posX = posX - (textSize.right / 2) - 3;
    }

    for (int screenY = -2; screenY < 3; screenY++)
    {
        for (int screenX = -2; screenX < 3; screenX++)
        {
            SetRect( &rect, posX+screenX, posY+screenY, 0, 0 );
            if (screenY != 0 && screenX != 0)
                Font[0].DrawTextA( DXSpriteForTextOutline, textToDisplay, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(outlineRed, outlineGreen, outlineBlue, 255) );
        }
    }

    SetRect( &rect, posX, posY, 0, 0 );
    Font[0].DrawTextA( DXSpriteForText, textToDisplay, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(textRed, textGreen, textBlue, 255) );
}

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

Hi,

Got it working!

Added:


   D3DXMATRIX matrixFixTextAfterSpriteDrawing;
   D3DXMatrixTransformation2D(&matrixFixTextAfterSpriteDrawing, NULL, NULL, NULL, NULL, NULL, NULL);
   D3DXMatrixIdentity(&matrixFixTextAfterSpriteDrawing);

to top of text render function.

Thank you for your help!

JeZ+Lee

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

I don't see you calling SetTransform at any point before rendering text. Setting one matrix in memory to identity doesn't change the matrix for D3D.

Cheers!

This topic is closed to new replies.

Advertisement