• Create Account

## Rendering Text from *.txt file

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Mr. Lawliet  Members

1467
Like
0Likes
Like

Posted 15 February 2014 - 03:33 AM

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

But when lines are longer, like this: 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, 15 February 2014 - 03:35 AM.

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

### #2cozzie  Members

4631
Like
1Likes
Like

Posted 15 February 2014 - 04:40 AM

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

### #3Bacterius  Members

13102
Like
2Likes
Like

Posted 15 February 2014 - 05:43 AM

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

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.”

### #4MaxDZ8  Members

4976
Like
1Likes
Like

Posted 17 February 2014 - 01:07 AM

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"

### #5Mr. Lawliet  Members

1467
Like
0Likes
Like

Posted 17 February 2014 - 08:37 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.