Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Displaying Text using .txt files  (Read 7344 times)

0 Members and 2 Guests are viewing this topic.

aanthonyz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Displaying Text using .txt files
« 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Displaying Text using .txt files
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Displaying Text using .txt files
« Reply #2 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.

aanthonyz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Displaying Text using .txt files
« Reply #3 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Displaying Text using .txt files
« Reply #4 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Displaying Text using .txt files
« Reply #5 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
        }
}

aanthonyz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Displaying Text using .txt files
« Reply #6 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. ;)

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

 

anything