Jump to content
  • Advertisement
Sign in to follow this  

SOLVED:After a day of going through my code, I found what was slowing everything down

This topic is 4720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First topic I have the following function that updates and draws the text while one is playing my Tetris clone. It updates the current level, score, and the amount of lines one has cleared. Here's what I have: NOTE:This is not exactly how everything is formatted. I have externs and everything here is spread across a number of files. This is just all that's part of the problem. Global Vars
CEngine Engine
SDL_Surface *tScore, *tLevel, *tLines, *tNext, *tPiece
SLD_Color c_WHITE;
TTF_Font *TTF_Arial;
char ScoreString[255];
char LevelString[255];
char LineString[255];
Engine Fucntions
void CEngine::DrawImageToScreen(SDL_Surface *surface, int xPos, int yPos)
{
	SDL_Rect dest;
	dest.x = xPos;
	dest.y = yPos;
	SDL_BlitSurface(surface, NULL, this->screen, &dest);
}

SDL_Surface* CEngine::SetTextSurface(TTF_Font* font, char* string, SDL_Color color)
{
	SDL_Surface *surface = TTF_RenderText_Solid(font, string, color);
	return surface;
}
Text Functions
void InitGameText()
{
	tGameOver =  Engine->SetTextSurface(TTF_Arial, "Game Over", c_WHITE);
	tLevel =  Engine->SetTextSurface(TTF_Arial, LevelString, c_WHITE);
	tLines =  Engine->SetTextSurface(TTF_Arial, LineString, c_WHITE);
	tNext =  Engine->SetTextSurface(TTF_Arial, "Next", c_WHITE);
	tPiece = Engine->SetTextSurface(TTF_Arial, "Piece: ", c_WHITE);
	tScore =  Engine->SetTextSurface(TTF_Arial, ScoreString, c_WHITE);
}

void UpdateGameText()
{
	sprintf(LevelString, "Level: %i", Level);
	sprintf(LineString, "Lines: %i", Lines);
	sprintf(ScoreString, "Score: %i", Score);
	tLevel = Engine->SetTextSurface(TTF_Arial, LevelString, c_WHITE);
	tLines = Engine->SetTextSurface(TTF_Arial, LineString, c_WHITE);
	tScore = Engine->SetTextSurface(TTF_Arial, ScoreString, c_WHITE);
}

void DrawGameText()
{
	UpdateGameText();
	Engine->DrawImageToScreen(tScore, 270, 240);
	Engine->DrawImageToScreen(tLevel, 270, 140);
	Engine->DrawImageToScreen(tLines, 270, 340);
	Engine->DrawImageToScreen(tNext, 285, -10);
	Engine->DrawImageToScreen(tPiece, 270, 40);
}
I'm thinking it has to do something with the UpdateGameText() function with the sprintf's? Help is much appreciated. [Edited by - Ekim_Gram on June 18, 2005 2:26:20 PM]

Share this post


Link to post
Share on other sites
Advertisement
I've never used SDL, but the calls to TTF_RenderText_Solid might be your culprit. If it is doing a full-blown font rendering (and not just copying images of letters from a back surface), then that is most likely your problem. Rendering Windows fonts and the like take a long time, and it usually isn't a good idea to re-render text each frame. Instead, you could try rendering it to a buffer and copying it when drawing the frame. Then, you would only need to render the text when it changes.

Share this post


Link to post
Share on other sites
The main problem though, is that it starts off fine. If you'd like to test it and see exactly what I mean I'd gladly upload it. But after 2 or 3 minutes of play, my entire computer begins to slow down drasticly.

Share this post


Link to post
Share on other sites

CEngine Engine;
SDL_Surface *tScore, *tLevel, *tLines, *tNext, *tPiece;
SDL_Color c_WHITE;
TTF_Font *TTF_Arial;
char ScoreString[255];
char LevelString[255];
char LineString[255];


I'm pretty sure I'm talking outta my @$$ but shouldn't all the SDL_Surfaces be pointers? Or is tScore the only surface pointer?

Share this post


Link to post
Share on other sites
Remember that everytime you call TTF_RenderText_Solid it actually creates a new SDL_Surface which you must remember to free! I had this problem as well and wondered why my program memory usage kept growing and growing...

Share this post


Link to post
Share on other sites
Quote:
Original post by MindFlayer
Remember that everytime you call TTF_RenderText_Solid it actually creates a new SDL_Surface which you must remember to free! I had this problem as well and wondered why my program memory usage kept growing and growing...


How would I go about doing that then. I would think the way I went about programming that function and the way to use it would just keep that from happening since it's set as a parameter.

EDIT: And as of the documentation, it returns a pointer to an SDL_Surface.

Quote:

Returns: a pointer to a new SDL Surface. NULL is returned on errors.

Share this post


Link to post
Share on other sites
I did it the lazy way (create & release the surface in one single function). It's definitely not the fastest way but works for me at the moment. Here's some code from my font class:


/// Renders ONE line of text at (x, y) with the given text color

void CFont::RenderSinglelineText(const std::string & Text, int x, int y, Uint8 Red, Uint8 Green, Uint8 Blue) const

{

// Create SDL surface

SDL_Color Color = { Red, Green, Blue, 0 };

SDL_Surface * TextSurface = TTF_RenderText_Solid(m_Font, Text.c_str(), Color);



// Continue only if we got a proper SDL surface

if (TextSurface)

{

// Blit surface at given coordinates

SDL_Rect DestRect = { x, y, 0, 0 };

SDL_BlitSurface(TextSurface, 0, m_Graphics->m_ScreenSurface, &DestRect);



// Free the surface

SDL_FreeSurface(TextSurface);

}

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The easy way to do it would be to just free all of the text surfaces after drawing them.

Share this post


Link to post
Share on other sites
Yes, I just figured it out about freeing the surface after drawing it. And now my problem is solved!!! Thank you so much everybody.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!