Jump to content
  • Advertisement
Sign in to follow this  
JeZ-l-Lee

DirectX 9.0c: Draw TTF Text On DirectX Sprite?

This topic is 2077 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 am working on a DirectX 9.0c 2D game engine now.

I am currently designing the interface core.

 

My question is:

Can I draw TTF text onto a DirectX Sprite?

Imagine that I have a single blank button PNG image.

And I load it 7 times and draw the following texts on it:

[START!]

[Options]

[How To Play]

[High Scores]

[About]

[Exit]

[Back]

 

The 7 button PNG images are loaded as DirectX Sprites.

 

Any help would be appreciated!

Thank you!

 

JeZ+Lee

 

TC5-Buttons_Have_No_Text.png

//-------------------------------------------------------------------------------------------------------------------------------
bool Visuals::LoadSpritesIntoMemoryAndInitialize(void)
{
HRESULT result;
char filePath[256];

    strcpy_s(filePath, "~\0");

    for (int index = 0; index < NumberOfSprites; index++)
    {
        switch(index)
        {
            case 0:
                strcpy_s(filePath, "Data/Visuals/Screen-Fade-Black-Box.png");
                break;

            case 5:
                strcpy_s(filePath, "Data/Visuals/16BitSoft-Logo.png");
                break;

            case 6:
                strcpy_s(filePath, "Data/Visuals/Title-BG.png");
                break;

            case 7:
                strcpy_s(filePath, "Data/Visuals/TC5-Logo.png");
                break;


            case 1001:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1002:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1003:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1004:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1005:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1006:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;

            case 1007:
                strcpy_s(filePath, "Data/Visuals/Button.png");
                break;


            default:
                strcpy_s(filePath, "~");
                break;
        }

        strcat_s(filePath, "\0");

        if (filePath[0] != '~')
        {
            D3DXIMAGE_INFO imageInfo;
            result = D3DXGetImageInfoFromFileA(filePath, &imageInfo);
//            if FAILED (hResult){
//              return false;
//              }

            D3DXCreateTextureFromFileExA(DXDevice, filePath,  imageInfo.Width, imageInfo.Height, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
                                         D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &Sprites[index].Texture);

            result = D3DXCreateSprite(DXDevice, &Sprites[index].DXSprite);

            Sprites[index].ScreenX = 400.0f;
            Sprites[index].ScreenY = 240.0f;
            Sprites[index].ScaleX = 1.0f;
            Sprites[index].ScaleY = 1.0f;
            Sprites[index].RotationDegree = 0.0f;
            Sprites[index].RedHue = 255;
            Sprites[index].GreenHue = 255;
            Sprites[index].BlueHue = 255;
            Sprites[index].Transparency = 255;
            Sprites[index].Smooth = true;
            Sprites[index].FlipX = false;
            Sprites[index].FlipY = false;
            Sprites[index].OriginalWidth = imageInfo.Width;
            Sprites[index].OriginalHeight = imageInfo.Height;

            D3DSURFACE_DESC textureInfo;
            Sprites[index].Texture->GetLevelDesc(0, &textureInfo);
            Sprites[index].TextureWidth = textureInfo.Width;
            Sprites[index].TextureHeight = textureInfo.Height;

            Sprites[index].AnimationTimer = -1.0f;

            if (index > 1000 && index < 1008)
            {
                RECT rect, textSize;
                int posX, posY;

                textSize.top = 0;
                textSize.left = 0;
                textSize.right = textureInfo.Width;
                textSize.bottom = textureInfo.Height;

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

                Font[0]->DrawTextA( NULL, "START!", -1, &textSize, DT_CALCRECT, D3DCOLOR_RGBA(255, 255, 255, 255) );

                posX = (textureInfo.Width / 2) - (textSize.right / 2) - 3;
                posY = textureInfo.Height / 2;

                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( Sprites[index].DXSprite, "START!", -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(100, 100, 100, 255) );
                    }
               }

               SetRect( &rect, posX, posY, 0, 0 );
               Font[0]->DrawTextA( Sprites[index].DXSprite, "START!", -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(255, 255, 255, 255) );
            }
        }
    }

    return(true);
}

Edited by JeZ-l-Lee

Share this post


Link to post
Share on other sites
Advertisement

You're already on the right track, it sorta looks like. D3DX provides the ID3DXFont interface for loading system fonts and drawing with them, and your code seems to be calling into that interface, just not at the right time. You can't draw to the textures/sprites as you load them, if that's what you're asking (without render targets or direct texture editing) but you can draw the text over the buttons when it comes time to render everything.

 

Just call the ID3DXFont:DrawText method when you're calling the draw code, after you've drawn your buttons. 

Share this post


Link to post
Share on other sites
Can I draw TTF text onto a DirectX Sprite?

Imagine that I have a single blank button PNG image.

And I load it 7 times and draw the following texts on it:

 

D3DXSprite is a hepler interface that allows you to render textured quads on the screen quite easily. Under the hood it creates a vertex buffer and draws it with the texture.

So, you cannot literally draw a text on a sprite. But you could, if you wanted, draw a text on a texture which you then use to draw the sprite. As Flimflam said - you could either create a render target or simply lock the texture and get the text inside by using GDI for example. But in most cases, this won't be the best solution, because you can very easily render the text directly on the screen each frame - and here comes D3DXFont.

Draw the empty button sprites (you already have this) and then simply draw the texts on the screen over the buttons.

 

Btw, D3DXFont isn't very fast, so maybe you'll want to use some other text-rendering class or create your own. But it's also possible that D3DXFont will be absolutely enough for you and then it's OK to use it.

Edited by Tom KQT

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.

GameDev.net 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!