Jump to content

  • Log In with Google      Sign In   
  • Create Account


JeZ-l-Lee

Member Since 03 Jul 2010
Offline Last Active Nov 18 2013 10:00 PM
-----

Topics I've Started

DirectX 9.0c: DrawTextA() Onto DirectX Texture?

11 November 2013 - 07:18 AM

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);
}


DirectX 9.0c: Draw TTF Text On DirectX Sprite?

10 November 2013 - 10:09 AM

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);
}


DirectX 9.0c: Handling "Lost Device"?

09 November 2013 - 07:19 AM

Hi,

 

What is the best method to handle a "lost device" in DirectX 9.0c?

Please look at the source code below and tell me if I am doing it correctly or not.

I "Reset" the DirectX device and reload the DirectX Sprites...

 

Thanks!

 

JeZ+Lee

 

 

Sprite Loading Function:

bool Visuals::LoadSpritesIntoMemoryAndInitialize(bool DeviceReset)
{
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;

            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);

            if (DeviceReset == false)
            {
                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;
            }
        }
    }

    return(true);
}

Screen Render Function:

void Visuals::DisplayScreenBufferOntoDisplay(void)
{
HRESULT result;

    result = DXDevice->Present( NULL, NULL, NULL, NULL );

    if (result != D3D_OK)
    {
        D3DDISPLAYMODE d3ddm;
        DXD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory( &d3dpp, sizeof(d3dpp) );
        d3dpp.Windowed               = TRUE;
        d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat       = d3ddm.Format;
        d3dpp.EnableAutoDepthStencil = TRUE;
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
        d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
        DXDevice->Reset(&d3dpp);

        for (int index = 0; index < NumberOfSprites; index++)
        {
            if (Sprites[index].DXSprite != NULL)  Sprites[index].DXSprite->Release();
            if (Sprites[index].Texture != NULL)  Sprites[index].Texture->Release();
        }

        LoadSpritesIntoMemoryAndInitialize(true);
    }
}

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

04 November 2013 - 11:26 AM

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;
    }
}


"DrawTextA": Width of TTF Text In Pixels?

04 November 2013 - 06:52 AM

Hi,

 

I'm designing a 2D game engine using DirectX 9.0c.

I am currently working on the graphics core.

 

Before using "DrawTextA", how would I get the size of TTF text width in pixels?

(I need to know it for horizontal positioning on the screen)

 

Thanks!

 

JeZ+Lee

 

Here is my source code:

//-------------------------------------------------------------------------------------------------------------------------------
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;

// Horizonal positioning would go here...
//#define JustifyLeft             0
//#define JustifyCenter           1
//#define JustifyRight            2
//#define JustifyCenterOnPoint    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) );
}

//-------------------------------------------------------------------------------------------------------------------------------


PARTNERS