Jump to content
  • Advertisement
Sign in to follow this  
bratiefanut

Rendering Text from *.txt file

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

Hello everybody,

 

I am a begginer with SFML and I want to get some text from a file and render it on the screen. I did this. With SFML is simple and intuitive. 

It is something like this:

........
std::ifstream file("Data/chapter.txt");
std::string chapter[11];
if(file.is_open())
{	
	for(int i = 0; i < 10; i++)
	{
		std::getline(file, chapter[i]);
	}
	file.close();
}
sf::Font font;
font.loadFromFile("Data/Font/comic.ttf");
sf::Text Chapter[11];
for(int i = 0; i < 10; i++)
{
	Domenii[i].setFont(font);
	Domenii[i].setColor(sf::Color::Black);
	Domenii[i].setString(chapter[i]);
	Domenii[i].setCharacterSize(26);
}
..... and then window.draw(Chapter[0]);

I have no problem when txt files that look like this:[attachment=19956:Capture.PNG]

But when lines are longer, like this:[attachment=19957:Capture.PNG] and I want to print the text, it goes out of screen.

What I need to know is that if is a way you can make a new line to format the text that it won't print outside the screen boundaries?. 

 

I hope you guys can understand what I wrote.. I am still learning english.

Thank you.

Edited by Bratie Fanut

Share this post


Link to post
Share on other sites
Advertisement
A newline in your text itself can be defined as \n.
Where the slash is the escape character from the text itself, n for newline, and for example t for a tab.

Maybe you can do it with the code itself, for example by splitting the text if it has more then x characters, where x is the max you want on one line on the screen.
That way you don't have to keep line lenghts in mind when making the nice stories in your text files :)

Share this post


Link to post
Share on other sites

A newline in your text itself can be defined as \n.
Where the slash is the escape character from the text itself, n for newline, and for example t for a tab.

Maybe you can do it with the code itself, for example by splitting the text if it has more then x characters, where x is the max you want on one line on the screen.
That way you don't have to keep line lenghts in mind when making the nice stories in your text files smile.png

 

This isn't quite accurate when the font is not monospace, though, e.g. "lll" is shorter than "mmm" in terms of pixels. Most font frameworks have a function similar to "getWidth(string)" which returns the width in pixels of the given string for a given font, which lets you do some more precise text width calculations. But what you are really looking for is some SFML component which can render text inside a bounding box with different styles like left-justify, right-justify, center, etc... Namely, a text area.

 

Regrettably, SFML does not provide that (because it's too "high level", according to the forums). One possible approach is to build lines from your list of words (in the text file) iteratively and checking if the line becomes longer than a certain width in pixels, and, if so, draw that line and go to the next one. Another approach is to use a GUI library, preferably built on top of SFML, for instance SFGUI, TGUI... google it. I would strongly advise not to implement your own, as text rendering operations are notoriously hard to implement properly and flexibly, and if you're writing a game you probably don't want to spend time rolling your own.

Share this post


Link to post
Share on other sites


text rendering operations are notoriously hard to implement properly and flexibly, and if you're writing a game you probably don't want to spend time rolling your own
QFE. On Windows, Uniscribe can encapsulate you from most problems. On *nix, it's either Cairo or Harfbuzz, it is my understanding the FreeFont library is not enough. I wouldn't touch those unless forced to.

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!