Rendering Text from *.txt file

Started by
3 comments, last by bratiefanut 10 years, 2 months ago

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.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

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 :)

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


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.

Previously "Krohm"

I kind of solved the problem by making the lines in my txt file smaller. I've done a bit of hardcoding and its ok now.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement