Jump to content

  • Log In with Google      Sign In   
  • 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.

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

#1 Vidar son of Odin   Members   -  Reputation: 1322

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.

Check out my portfolio : https://bratie.wordpress.com

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

 


Sponsor:

#2 cozzie   Members   -  Reputation: 1611

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

#3 Bacterius   Crossbones+   -  Reputation: 8852

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.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#4 Krohm   Crossbones+   -  Reputation: 3117

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 Vidar son of Odin   Members   -  Reputation: 1322

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.


Check out my portfolio : https://bratie.wordpress.com

"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