Jump to content
  • Advertisement
Sign in to follow this  
Stompy9999

Question regarding SDL_ttf

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

Okay, So I'm currently making a Turn based Strategy game with C++ and SDL, and I'm using SDL_ttf for text output. I'm going to make a briefing screen before each level that tells the player info about that level. Now before I go about doing this, I have a question. Does SDL_ttf recognize the newline character when it is drawing text? I'm wondering because I'm going to be reading the briefing text in from a text file, and I what all of the word wrapping in there automaticly. So does SDL_ttf recognize newline characters, or will I have to write my own word wrapping routine? Thanks for any help, and sorry if this is a dumb question.[grin]

Share this post


Link to post
Share on other sites
Advertisement
Natively, I don't believe so. But you can always parse the string for '\n' and upon detection, render two different SDL_surfaces and then stick them together in a single surface. This is how my video programmer did it just this morning, but I haven't tested it myself yet. (We use OpenGL with our SDL app, so your mileage may vary)


//-----------------------------------------------------------------------------
// DrawText: non unicode version
//-----------------------------------------------------------------------------

bool GameVideo::DrawText(const string &txt, float x, float y)
{
if(txt.empty())
{
if(VIDEO_DEBUG)
cerr << "VIDEO ERROR: tried to draw empty text string!" << endl;
}


TTF_Font *font = _fontMap[_currentFont];

_PushContext();

if(font)
{
int32 lineSkip = TTF_FontLineSkip(_fontMap[GetFont()]);
// use a temporary string since the code changes it
string text = txt;

do
{
size_t newlinePos = text.find("\n");
if(newlinePos != string::npos)
{
if(!_DrawTextHelper(text.substr(0, newlinePos).c_str(), NULL, x, y))
{
_PopContext();
return false;
}
text = text.substr(newlinePos+1, text.length()-newlinePos);
y -= lineSkip;
}
else
{
if(!_DrawTextHelper(text.c_str(), NULL, x, y))
{
_PopContext();
return false;
}
text = "";
}
} while(text != "");
}

_PopContext();

return true;
}



You can view the rest of the video engine in our CVS here. The code is GPL'ed. The snippet above I grabbed from the file `text.cpp`


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!