SDL_TTF text drawing performance issues

SDL_TTF text drawing performance issues…

Hi,

I am using SDL_TTF for text drawing in my game.
The text drawing seems to be alittle slow.
Below is the text drawing code.
Any way to make it faster?
(Note: it draws text with a black outline.)-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void Visuals::DrawTextOnScreenBuffer(Uint8 fontNumber, char *text,
Sint32 red, Sint32 green, Sint32 blue, Sint32 alpha, Sint32 xScreenPos,
Sint32 yScreenPos, Uint8 xJustification)
{
Sint32 x, y;
int textWidth;
int textHeight;
SDL_Surface *sText;
SDL_Rect rcDest;

if (xJustification == TextCenterX)
{
    TTF_SizeText(FontLibrary[fontNumber], text, &textWidth, 

&textHeight);
xScreenPos = (640 - textWidth) / 2;
}
else if (xJustification == TextRightX)
{
TTF_SizeText(FontLibrary[fontNumber], text, &textWidth,
&textHeight);
xScreenPos = ( (640 - textWidth) - xScreenPos );
}

SDL_Color textColor = {0 , 0 , 0, 0};

sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text, 

textColor );

for (y = -3; y < 4; y++)
{
    for (x = -3; x < 4; x++)
    {
        sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text, 

textColor );
rcDest.x = xScreenPos+x;
rcDest.y = yScreenPos+y;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
SDL_FreeSurface(sText);
}
}

SDL_Color textColor2 = {red , green , blue, alpha};
sText = TTF_RenderText_Blended( FontLibrary[fontNumber], text, 

textColor2 );
rcDest.x = xScreenPos;
rcDest.y = yScreenPos;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
SDL_FreeSurface(sText);
}

Thanks…

Jesse “JeZ+Lee” Palser, BS, A+, Network+, MCP
SLNTHERO at aol.com
Silent Hero Productions®
Video Game Design Studio
http://www.SilentHeroProductions.com

Hi,

---------------------- void Visuals::DrawTextOnScreenBuffer(Uint8
fontNumber, char *text, Sint32 red, Sint32 green, Sint32 blue, Sint32
alpha, Sint32 xScreenPos, Sint32 yScreenPos, Uint8 xJustification)

IMO function DrawTextOnScreenBuffer should only draw rendered text. It means
render text once and store it in surface. When ‘char *text’ or font will
change, render new text. Game loop would be like this:

char *text;
SDL_Surface *sTextSolid=NULL, *sTextBlend=NULL;
while (…) {
if text or font has changed {
if( sText…!=NULL ) SDL_FreeSurface(sText…
sTextSolid = TTF_RenderText_Solid(…
sTextBlend = TTF_RenderText_Blended(…
}

// drawing section
if( sTextSolid!=NULL) {
SDL_BlitSurface(…
}
}
if( sText…!=NULL ) SDL_FreeSurface(sText…

Than if text is still the same you will only blit it without rendering.

Of course it’s very simply model, I would store surface, text, color and
coordinates for each text instance in object with methods SetPos(x,y),
RenderSolid(char *txt), RenderBlended(char *txt), Draw(), SetColor(), Show(),
Hide(), SetFont(…)…

In your code You can easly speed up executing:

for (y = -3; y < 4; y++)
{
    for (x = -3; x < 4; x++)
    {
        sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text,

textColor );
rcDest.x = xScreenPos+x;
rcDest.y = yScreenPos+y;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
SDL_FreeSurface(sText);
}
}

sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text, textColor );
for (y = -3; y < 4; y++)
{
for (x = -3; x < 4; x++)
{
rcDest.x = xScreenPos+x;
rcDest.y = yScreenPos+y;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
}
}
SDL_FreeSurface(sText);–
Regards,
Dominik Dagiel
@Dominik_Dagiel
www.dagiel.pl

Hi Dominik,

Big thanks, that worked and solved my text drawing performance issues.

Thanks…

JeZ+Lee
of
Silent Hero Productions®

Dominik Dagiel wrote:> Hi,

---------------------- void Visuals::DrawTextOnScreenBuffer(Uint8
fontNumber, char *text, Sint32 red, Sint32 green, Sint32 blue, Sint32
alpha, Sint32 xScreenPos, Sint32 yScreenPos, Uint8 xJustification)

IMO function DrawTextOnScreenBuffer should only draw rendered text. It means
render text once and store it in surface. When ‘char *text’ or font will
change, render new text. Game loop would be like this:

char *text;
SDL_Surface *sTextSolid=NULL, *sTextBlend=NULL;
while (…) {
if text or font has changed {
if( sText…!=NULL ) SDL_FreeSurface(sText…
sTextSolid = TTF_RenderText_Solid(…
sTextBlend = TTF_RenderText_Blended(…
}

// drawing section
if( sTextSolid!=NULL) {
SDL_BlitSurface(…
}
}
if( sText…!=NULL ) SDL_FreeSurface(sText…

Than if text is still the same you will only blit it without rendering.

Of course it’s very simply model, I would store surface, text, color and
coordinates for each text instance in object with methods SetPos(x,y),
RenderSolid(char *txt), RenderBlended(char *txt), Draw(), SetColor(), Show(),
Hide(), SetFont(…)…

In your code You can easly speed up executing:

for (y = -3; y < 4; y++)
{
    for (x = -3; x < 4; x++)
    {
        sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text,

textColor );
rcDest.x = xScreenPos+x;
rcDest.y = yScreenPos+y;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
SDL_FreeSurface(sText);
}
}

sText = TTF_RenderText_Solid( FontLibrary[fontNumber], text, textColor );
for (y = -3; y < 4; y++)
{
for (x = -3; x < 4; x++)
{
rcDest.x = xScreenPos+x;
rcDest.y = yScreenPos+y;
SDL_BlitSurface( sText, NULL, Screen, &rcDest );
}
}
SDL_FreeSurface(sText);