SDL_ttf question (does this re-allocated a surface each time?)

high,

i understand that you should never really load in an image to the same
surface multiple times, or if you do that, you have to Free it as many times
as you loaded it. so whats the deal with SDL_ttf then? you make a
SDL_Surface, and then everytime you draw with it, you re-assign a value to
this surface. is this hogigng up memory and allocating a new surface every
time i draw text (which i do like 5 times each frame)

here is what my Text drawing function looks like:

void Messages::Draw_Txt(char* message,int x,int y,int size)
{
//make it white
SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;

//render text if i sent 1 make small text else make big text
if(size == 1)
	text = TTF_RenderText_Solid(font,message,color);
else text = TTF_RenderText_Solid(font_big,message,color);

//rectangles
SDL_Rect rcSrc;
SDL_Rect rcDst;

//source rectangle
rcSrc.x = rcSrc.y = 0;
rcSrc.w = text->w;
rcSrc.h = text->h;

//destination rectangle
rcDst = rcSrc;
rcDst.x = x;
rcDst.y = y;

//blit the surface
SDL_BlitSurface(text,&rcSrc,data.screen,&rcDst);

}

text is an SDL_Surface. font and font_big are TTF_Font*'s and initialized
like this:

//open font
font = TTF_OpenFont(“arial.ttf”,25);

font_big = TTF_OpenFont(“arial.ttf”,50);

as you can see, i call this in the function:

if(size == 1)
text = TTF_RenderText_Solid(font,message,color);

else text = TTF_RenderText_Solid(font_big,message,color);

you see how i re-assign a value to text? this happends each time i call this
function, which i call multple times per frame. this means im re-assigning
memory to that SDL_Surface possibly thousands of times? there no way i have
to call SDL_FreeSurface 10000 times, do i? im just a little confused. i
appreciate any help you give me. thanks!_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar ? get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/

Hello.

I’ve find a way to manage this kind of things for my engine.

The way I do it is a simple, but I think it is one of the best.

I load all the fonz with their the size I will use them at the begining of
my programm. This way I do only one TTF_OpenFont per font/size couples. And
one appopriate TTF_CloseFont at the exit of my program.

For the text rendering text I do the same, I’m making an adventure based
engine (like SCUMM) so when I load a scene, I’ll prerender all the text that
will be required, and store them. So this is the same way as for Font
opening.

To store both kind of datas, I used chained lists with the appropriate sets
of functions for my engine.

To make it simple. I use this kind of function set :

initTxtList
addTxtList
drawTxtList
freeTxtList

So at the begining, before the rendering phase of a scene, I do one
initTxtList and many addTxtList.

In my main rendering loop I call each time drawTxtList which draw every
texts I need.

And when I exit the game, or change of scene I call freeTxtList to release
the ressources.

In fact when I add a text, I specify the font name which I get from my
fontList loaded at the init of my Engine, I use the text, and record it’s
position.

So in fact I do everything you do in your function execpt that I did not do
the blitting operation but only reccord the SDL_Image surface I’ll use and
the position to draw the text.

Therefore, my drawTxtList function will draw the text an dis only doing
blitting, no reallocation of memory …

I hope this help you, I think this is working pretty well.

Wedge

-----Message d’origine-----
De?: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org] De la part de
Graveyard Filla
Envoy??: jeudi 1 avril 2004 22:01
??: sdl at libsdl.org
Objet?: [SDL] SDL_ttf question (does this re-allocated a surface each time?)

high,

i understand that you should never really load in an image to the same
surface multiple times, or if you do that, you have to Free it as many times

as you loaded it. so whats the deal with SDL_ttf then? you make a
SDL_Surface, and then everytime you draw with it, you re-assign a value to
this surface. is this hogigng up memory and allocating a new surface every
time i draw text (which i do like 5 times each frame)

here is what my Text drawing function looks like:

void Messages::Draw_Txt(char* message,int x,int y,int size)
{
//make it white
SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;

//render text if i sent 1 make small text else make big text
if(size == 1)
	text = TTF_RenderText_Solid(font,message,color);
else text = TTF_RenderText_Solid(font_big,message,color);

//rectangles
SDL_Rect rcSrc;
SDL_Rect rcDst;

//source rectangle
rcSrc.x = rcSrc.y = 0;
rcSrc.w = text->w;
rcSrc.h = text->h;

//destination rectangle
rcDst = rcSrc;
rcDst.x = x;
rcDst.y = y;

//blit the surface
SDL_BlitSurface(text,&rcSrc,data.screen,&rcDst);

}

text is an SDL_Surface. font and font_big are TTF_Font*'s and initialized
like this:

//open font
font = TTF_OpenFont(“arial.ttf”,25);

font_big = TTF_OpenFont(“arial.ttf”,50);

as you can see, i call this in the function:

if(size == 1)
text = TTF_RenderText_Solid(font,message,color);

else text = TTF_RenderText_Solid(font_big,message,color);

you see how i re-assign a value to text? this happends each time i call this

function, which i call multple times per frame. this means im re-assigning
memory to that SDL_Surface possibly thousands of times? there no way i have
to call SDL_FreeSurface 10000 times, do i? im just a little confused. i
appreciate any help you give me. thanks!_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar ? get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl