Please help, SDL ttf IS eating up all my memory =(

high,

sorry for the double “post”. but i just confirmed that my function does
indeed eat up memory. could someone please explain to me what the hell im
doing wrong? i just dont understand. lets say i want to draw the word
"Score:" then next to that i want to draw the score. how the hell do i
update this score without updating the SDL_Surface*??? this is what im
talking about:

first i declare these variables and initialize everything:

//surface for text
SDL_Surface* text;

//font
TTF_Font* font;
TTF_Font* font_big;

//initialize SDL_ttf
TTF_Init();

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

font_big = TTF_OpenFont("arial.ttf",50);

now, i draw the score with this function:

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

}

now i just call this function whenver i want to draw something. i do this
with Score, Lives, Level, Time. ETC. so this functions getting called
multiple times every frame. i do stuff like this:

Draw_Txt(“Score:”,0,0,1);

//points is a array of characters that stores the players score.
Draw_Txt(points,75,0,1);

i do this with lives,level, ETC. so how come this is bogging down my
computer and hogging all my memory? when i comment out my font drawing,
memory usage goes from 7k to 3k! could someone PLEASE explain to me what im
doing wrong? thanks for any help!!_________________________________________________________________
Check out MSN PC Safety & Security to help ensure your PC is protected and
safe. http://specials.msn.com/msn/security.asp

Just glancing, I think you want to free once you’re done blitting.
Otherwise, you’re keeping a copy of EVERY surface you create with the
TTF Render call :wink:

-bill!On Thu, Apr 01, 2004 at 08:39:06PM +0000, Graveyard Filla wrote:

high,

sorry for the double “post”. but i just confirmed that my function does
indeed eat up memory.

Quoting Graveyard Filla :

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

See these docs:

http://jcatki.no-ip.org/SDL_ttf/SDL_ttf_28.html#SEC28

You never free the memory returned from TTF_RenderText_Solid,
which returns a new surface every call. After your blit call

SDL_FreeSurface(text);

Good luck!

-daniel

You should also not forget to call TTF_CloseFont(font);,
TTF_Closefont(big_font); and TTF_Quit(); before you exit.

Daniel Hedrick wrote:> Quoting Graveyard Filla :

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

See these docs:

http://jcatki.no-ip.org/SDL_ttf/SDL_ttf_28.html#SEC28

You never free the memory returned from TTF_RenderText_Solid,
which returns a new surface every call. After your blit call

SDL_FreeSurface(text);

Good luck!

-daniel


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

Must we free all allocated memory for font and font rendering surfaces
each loop ? or only on program exit

My application does this on program exit only, as the rendering surface
is always the same each loop, and only it’s content change.

Am i Wrong or right ?

You ve all confused me a little I must tell.

les.

Bernhard Bliem a ?crit :> You should also not forget to call TTF_CloseFont(font);,

TTF_Closefont(big_font); and TTF_Quit(); before you exit.

Daniel Hedrick wrote:

Quoting Graveyard Filla :

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

See these docs:

http://jcatki.no-ip.org/SDL_ttf/SDL_ttf_28.html#SEC28

You never free the memory returned from TTF_RenderText_Solid,
which returns a new surface every call. After your blit call

SDL_FreeSurface(text);

Good luck!

-daniel


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


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

Les HauSsebons wrote:

Must we free all allocated memory for font and font rendering surfaces

each loop ? or only on program exit

it’s simple:

for each call to

text = TTF_RenderText_Solid(font,message,color);

there must be a call to

SDL_FreeSurface(text);

on programm exit or when you do not need the font anymore you call

TTF_Closefont(big_font);

when you’re done using the ttf library (usually at exit) do

TTF_Quit();

clemens