SFML community forums

Help => General => Topic started by: aanthonyz on March 27, 2014, 03:28:57 pm

Title: Displaying Text using .txt files
Post by: aanthonyz on March 27, 2014, 03:28:57 pm
Hello, I am creating a Visual Novel by hand and I have gotten to the point where I should start inputting text. Is there anyway I can write all my text into, .txt files and SFML will read it? Ive looked at all the formatting already and at most I can have 5 lines at a time and each line can have 77 characters maximum. Should I use fstream or do people recommend something else
Title: Re: Displaying Text using .txt files
Post by: Nexus on March 27, 2014, 03:33:44 pm
Yes, use std::fstream for file I/O.

SFML is only responsible for drawing text on the screen.
Title: Re: Displaying Text using .txt files
Post by: Peteck on March 27, 2014, 03:34:09 pm
Can you please provide some code?
Because it should be easy to implement with a simple stream.
Title: Re: Displaying Text using .txt files
Post by: aanthonyz on March 27, 2014, 04:12:10 pm
So far I have this:

sf::Text text2("One moment I'm going out with this cute girl and the next, I get stabbed in the chest with", font, 50); //77 Characters
        text2.setPosition(100,790);
        sf::Text text3("a spear. What to do now...just wait? Hmm, I wish I had more time to at least say goodbye", font, 50); //77 Characters
        text3.setPosition(100,860);

using file stream, is it possible to simplify it down to something where it would read a line at a time and once it has read 77 characters, it would move on and fill text3, then text4, etc?
Title: Re: Displaying Text using .txt files
Post by: eXpl0it3r on March 27, 2014, 05:02:13 pm
Yes, use a vector for your texts.

To me it seems you have little knowledge and rarely any experience with C++ and/or programming. You first should learn the basics with a good C++ book. (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ;)
Title: Re: Displaying Text using .txt files
Post by: Peteck on March 27, 2014, 05:03:28 pm
What you want is called wordwrapping.
But here some code I made for you, that hopefully may help you get going

#include <fstream>
#include <sstream>
#include <list>
#include <SFML\Graphics\Text.hpp>
#include <SFML\Graphics\Rect.hpp>

int main()
{
        std::string line;
        std::ifstream file;
        std::list<sf::Text> textList;
        sf::Text text("", font, 50); //Load your font and replace 'font'


        file.open("text.txt"); //Set your path here
        if(file.is_open())
        {
                while(std::getline(file, line))
                {
                        //Getting every line of the .txt file and putting it in the 'line' string
                        text.setString(line);
                        textList.push_back(text);
                }
        }

        float i = 0;
        for(std::list<sf::Text>::iterator it = textList.begin(); it != textList.end(); ++it)
        {
                sf::Text& text = *it;
                sf::FloatRect boundings = text.getLocalBounds();
                text.setPosition(0.f, i * (boundings.height + 5));
                ++i;

                //draw or something here
        }
}
Title: Re: Displaying Text using .txt files
Post by: aanthonyz on March 27, 2014, 05:19:30 pm
Yes, use a vector for your texts.

To me it seems you have little knowledge and rarely any experience with C++ and/or programming. You first should learn the basics with a good C++ book. (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ;)

Yeah, I know some programming not enough to say I can do a entire game without asking for help. Currently im taking a Fundamentals of Programming course in college and im doing this as a side project