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