Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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.

  • You cannot reply to this topic
4 replies to this topic

#1 Mr. Lawliet   Members   -  Reputation: 1455

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;
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:Capture.PNG

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

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

 


Sponsor:

#2 cozzie   Members   -  Reputation: 3361

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


#3 Bacterius   Crossbones+   -  Reputation: 11532

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


#4 Krohm   Crossbones+   -  Reputation: 4000

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.

#5 Mr. Lawliet   Members   -  Reputation: 1455

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.



PARTNERS