• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JeZ-l-Lee

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

5 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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!

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0