Sign in to follow this  

Strange surface disapperance...

This topic is 2844 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

I have been working on ths program using SDL on Windows, I have displayed a numerous amount of text using TrueType fonts, after about 5 mins of being open all my text disappears and I am not sure what is happening. All my other surface that are images are still displayed, IE background, colored boxes, etc... Anyone have any clue to what is happening, below is my render code for the text and my game loop it is displayed in. I have two differnt drawText and drawInteger that is why I have a separate render_text function.
SDL_Surface *render_text(const char *text, SDL_Color color)
{
	return TTF_RenderText_Solid(font, text, color);
}

void drawText(int x, int y, char *text, SDL_Color colors)
{
	SDL_Surface *textSurface = render_text(text, colors);
	apply_surface(x, y, textSurface, screen);
}

I have hundreds of different instance of text so i cut out one of them.
void programScreen()
{
        drawText(17, 477, "Alpha", white);
}

Game loop...
while(running)
{
	//Fill screen cornflower blue
	SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 100, 149, 237));

	//While there's events to handle
	while(SDL_PollEvent(&event))
	{
		//Handles the diffent button input
		handleInput();

		//If the user has Xed out the window
		if(event.type == SDL_QUIT)
		{
			//Quit the program
			running = false;
		}
	} 

	//Key to close window
	if(keystate[SDLK_ESCAPE])
		running = false;

	//switch to decide which part of the program to run
	switch(gamestate)
	{
	case splash:
		splashScreen();
		break;
	case program:
		programScreen();
		break;
	}

	//Update screen
	updateScreen();
}

Any suggestions would be helpful, thanks in advance.

Share this post


Link to post
Share on other sites
I've never used SDL_ttf myself, but it's my understanding that TTF_RenderText_Solid() creates and returns a new SDL surface, which the caller is responsible for freeing.

In this code:
void drawText(int x, int y, char *text, SDL_Color colors)
{
SDL_Surface *textSurface = render_text(text, colors);
apply_surface(x, y, textSurface, screen);
}
You appear to be creating a new SDL surface on each call, but never freeing it, which is a memory leak. I noticed that you're not doing any error checking here, but if you were to add a check for NULL to the above code, you might find that at the point where the text usually stops rendering, TTF_RenderText_Solid() begins to return NULL (that's just a guess, but an out-of-memory error doesn't seem too unlikely, given the circumstances).

The easiest fix would probably be to change your code as follows:
void drawText(int x, int y, char *text, SDL_Color colors)
{
SDL_Surface *textSurface = render_text(text, colors);
apply_surface(x, y, textSurface, screen);
SDL_FreeSurface(textSurface);
}
In cases where you're rendering the same text repeatedly, however, it might be more efficient to create the corresponding surface once and then just render it as needed.

Share this post


Link to post
Share on other sites
Thanks your help that fixed the issue. I just had to free up the surface in my drawText function, which is funny because I have a drawInteger one and I free'd the surface on that as well, must have just had a brain lapse. Again, thank you! Very much appreciated.

Share this post


Link to post
Share on other sites

This topic is 2844 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this