DirectX 9.0c: DrawTextA() Onto DirectX Texture?

Started by
2 comments, last by JeZ-l-Lee 10 years, 5 months ago

Hi,

I am trying without success to draw TTF text onto a DirectX 9 texture.

I am using the DirectX 9 "DrawTextA()" function.

Please look at the below code and tell me what is wrong with it.

(focus on the source at the bottom)

Thanks in advance.

JeZ+Lee


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)
            {
                LPDIRECT3DSURFACE9 pRenderSurface = NULL;
                Sprites[index].Texture->GetSurfaceLevel(0, &pRenderSurface);
                DXDevice->SetRenderTarget(0, pRenderSurface);

                DXDevice->BeginScene();
                DXDevice->SetTexture(0, Sprites[index].Texture);

                RECT rect, textSize;
                int posX, posY;

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

                posX = (Sprites[index].TextureWidth / 2) - (textSize.right / 2) - 3;
                posY = Sprites[index].TextureHeight / 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(NULL, "START!", -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(100, 100, 100, 255) );
                    }
                }

                SetRect( &rect, posX, posY, 0, 0 );
                Font[0]->DrawTextA( NULL, "START!", -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(255, 255, 255, 255) );

                DXDevice->EndScene();
            }
        }
    }

    return(true);
}

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

Advertisement


Please look at the below code and tell me what is wrong with it.

Where to begin?

Magic numbers everywhere for one.

Allow me to introduce you to the “enum” keyword in C++. It allows you to name numbers.

Then there is the fact that GetSurfaceLevel() increases the reference count but you never call pRenderSurface->Release(), leaking resources.

If there was something more you were expecting, such as, “Why doesn’t this work?”, then perhaps you should say what it does vs. the desired result instead of ambiguously asking what is “wrong” with it.

Why don’t you use PIX and see how far it goes before an unexpected result appears?

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Didn't you ask the very same question yesterday? Why make a new thread?

Hi,

I've been trying without success to do the following:

(1) Create a DirectX 9 texture

(2) Render a loaded Button.png texture onto the created DirectX 9 texture

(3) Render TTF text on top of the rendered Button.png texture on the created DirectX 9 texture

(4) Render the new button with text DirectX 9 texture to the screen

Code is below, any help would be greatly appreciated...

(Sprites[1000] is the loaded Button.png texture)

JeZ+Lee


char buttonText[256];
if (index > 1000 && index < 1008)
{
    LPDIRECT3DSURFACE9 backBuffer = NULL;
    DXDevice->GetRenderTarget(0, &backBuffer);

    DXDevice->CreateTexture(Sprites[1000].OriginalWidth, Sprites[1000].OriginalHeight, 0, D3DUSAGE_RENDERTARGET,
                            D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &Sprites[index].Texture, NULL);

    LPDIRECT3DSURFACE9 pRenderSurface = NULL;
    Sprites[index].Texture->GetSurfaceLevel(0, &pRenderSurface);
    DXDevice->SetRenderTarget(0, pRenderSurface);

    if (index == 1001)  strcpy_s(buttonText, "START!");
    if (index == 1002)  strcpy_s(buttonText, "Options");
    if (index == 1003)  strcpy_s(buttonText, "How To Play");
    if (index == 1004)  strcpy_s(buttonText, "High Scores");
    if (index == 1005)  strcpy_s(buttonText, "About");
    if (index == 1006)  strcpy_s(buttonText, "Exit");
    if (index == 1007)  strcpy_s(buttonText, "Back");

    DXDevice->BeginScene();

    DXDevice->SetTexture(0, Sprites[index].Texture);

    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ),
                              (float)Sprites[1000].OriginalWidth / (float)Sprites[1000].OriginalHeight, 0.1f, 100.0f );

    DXDevice->SetTransform( D3DTS_PROJECTION, &matProj );

    Sprites[1000].DXSprite->Begin(D3DXSPRITE_ALPHABLEND);

    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[1000].TextureHeight;
    spriteRect.right = Sprites[1000].TextureWidth;

    D3DXVECTOR3 spritePosition;
    spritePosition.x = 0.0f;
    spritePosition.y = 0.0f;
    spritePosition.z = 0.0f;

    D3DXMATRIX matrixSpriteTransform;

    D3DXVECTOR2 screenPostion = D3DXVECTOR2(0, 0);

    float rotation = 0.0f;

    D3DXVECTOR2 scaling(1.0f, 1.0f);

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

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

    Sprites[1000].DXSprite->End();

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

    RECT rect, textSize;
    int posX, posY;

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

    posX = (Sprites[1000].OriginalWidth / 2) - (textSize.right / 2);
    posY = (Sprites[1000].OriginalHeight / 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(NULL, buttonText, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(100, 100, 100, 255) );
        }
    }

    SetRect( &rect, posX, posY, 0, 0 );
    Font[0]->DrawTextA( NULL, buttonText, -1, &rect, DT_NOCLIP, D3DCOLOR_RGBA(255, 255, 255, 255) );

    DXDevice->EndScene();

    pRenderSurface->Release();

    DXDevice->SetRenderTarget(0, backBuffer);

    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ), 800.0f / 480.0f, 0.1f, 100.0f );
    DXDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}

JeZxLee
Fallen Angel Software
www.FallenAngelSoftware.com

This topic is closed to new replies.

Advertisement