Jump to content
  • Advertisement
Sign in to follow this  
Catkill

Displaying text

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

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 this post


Link to post
Share on other sites
Advertisement
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. ;)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!