Public Group

# Displaying text

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

## Recommended Posts

Ive recently started trying SDL and have started making my first game. I've written a few "helper functions" but after adding this one:
void DisplayText(std::string text,int x,int y, int size,int fR, int fG, int fB,SDL_Surface* target)
{
TTF_Font* font = TTF_OpenFont("arial.ttf",size);

SDL_Color foreground = {fR,fG,fB};

SDL_Surface* temp = TTF_RenderText_Solid(font, text.c_str(), foreground);

apply_surface(x,y,temp,target); //This is another helper function

SDL_FreeSurface(temp);

TTF_CloseFont(font);
}

Im getting a segmentation fault, It was working fine until this was added. Any ideas? Thanks Catkill

##### Share on other sites
Maybe try checking if font is NULL after your call to OpenFont? Could be not finding your arial.ttf?

##### Share on other sites
When exactly does your program crash? What does your debugger say? The documentation notes that if you pass a null-pointer to TTF_RenderText_Solid for it's font argument, you will get a segfault, so that's likely the problem here. Where is this 'arial.ttf' file located? What folder are you running the executable from?

As for the code itself, this function loads a font, creates a surface and then removes that surface, every time you call it. That's not efficient. You'll only want to load that font once, and you'll only want to recreate that surface when you're drawing a different string. In other words, you'll want to remember some things across multiple calls. That makes a function a poor choice - a class would be more suitable. Just thought I'd mention it. ;)

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×