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

Author Topic: Text Wrapping with .txt Files?  (Read 3117 times)

0 Members and 1 Guest are viewing this topic.

tiny screaming yak

  • Guest
Text Wrapping with .txt Files?
« on: August 12, 2014, 06:00:55 pm »
Okay, so I'm aware that this question has most likely been asked many times before, however after days of Google searches, I am still unable to find an answer. How would I implement some sort of text wrapping within a text file?

Here's my function for reading text files:

void openFile(ifstream &myfile, Text &text, RenderWindow & window, int loopTimes)
{
   
    Font font;
    setFont(font, text, fontSize);
    for (int i = 0; i < loopTimes + 1; i++)
    {
        string line;
        getline(myfile, line);
        text.setString(line);
        window.draw(text);
    }
}

I've tried creating another Text variable that contains "\n" and drawing it, but no luck. I've tried putting "\n" within the file, however that didn't do much either (I didn't think it'd work, but it was worth a shot). The file has multiple lines already, but when I run the code, all of the words overlap. How can I fix this?

Here's all of my code:
#include <iostream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <fstream>
#include <string>
#include "ResourcePath.hpp"

using namespace std;
using namespace sf;

int fontSize = 18; // May change later

void setFont(Font &font, Text &text, int charSize)
{
    if (!font.loadFromFile("Arial.ttf"))
    {
        cout << "Unable to load font." << endl;
    }
    text.setFont(font);
    text.setCharacterSize(charSize);
    text.setColor(Color::White);
}


void openFile(ifstream &myfile, Text &text, RenderWindow & window, int loopTimes)
{
   
    Font font;
    setFont(font, text, fontSize);
    for (int i = 0; i < loopTimes + 1; i++)
    {
        string line;
        getline(myfile, line);
        text.setString(line);
        window.draw(text);
    }
}

int main()
{
    RenderWindow window(VideoMode(800, 600), "Name here", Style::Default);
   
    window.setVerticalSyncEnabled(true);
   
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
            {
                window.close();
            }
        }
       
        window.clear(Color::Black);
       
        //Draw everything here
       
        ifstream welcomeFile ("WelcomeMenu.txt");
        Text welcomeText;
        int welcomeLoop = 3;
        openFile(welcomeFile, welcomeText, window, welcomeLoop);
       
       
        window.display();
    }
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Text Wrapping with .txt Files?
« Reply #1 on: August 12, 2014, 06:09:20 pm »
How would I implement some sort of text wrapping within a text file?
I have to admit, I'm not sure what your goal is.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

tiny screaming yak

  • Guest
Re: Text Wrapping with .txt Files?
« Reply #2 on: August 12, 2014, 09:21:26 pm »
Quote
I have to admit, I'm not sure what your goal is.

I'm sorry, I didn't read over what I wrote so my wording is off, haha. 

I mean, how would I be able to keep the text from overlapping when I run the code.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Text Wrapping with .txt Files?
« Reply #3 on: August 12, 2014, 09:28:36 pm »
1) use multiple sf::Text objects for each line and position them apropriately.
2) insert a line break (\n) in to the sf::Text object at the apropriate position.

How you determine when a line-break is appropriate for your input is left up to you ;)

tiny screaming yak

  • Guest
Re: Text Wrapping with .txt Files?
« Reply #4 on: August 21, 2014, 03:05:45 am »
Quote
2) insert a line break (\n) in to the sf::Text object at the apropriate position.

How would I place the line break in the Text object? (I apologize for my silly sounding question, but I don't understand how? I can't remember if it was covered in the tutorials or not.)

P/S: Sorry for the late reply.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Text Wrapping with .txt Files?
« Reply #5 on: August 21, 2014, 03:19:11 am »
Quote
2) insert a line break (\n) in to the sf::Text object at the apropriate position.

How would I place the line break in the Text object? (I apologize for my silly sounding question, but I don't understand how? I can't remember if it was covered in the tutorials or not.)

P/S: Sorry for the late reply.
I usually ask a lot of people for source codes and then study them in case if I need something later. But if you are too lazy to do that or you need something urgently I would suggest you to ask example of how they use it. So far I've only asked 2 people for source codes and I'm still waiting for 1 to send them to me. Just some physics simulators

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Text Wrapping with .txt Files?
« Reply #6 on: August 21, 2014, 03:36:49 am »
Quote
2) insert a line break (\n) in to the sf::Text object at the apropriate position.

How would I place the line break in the Text object? (I apologize for my silly sounding question, but I don't understand how? I can't remember if it was covered in the tutorials or not.)
//example    
sf::Font font;
    font.loadFromFile("font.ttf");

    std::string str = "line1 line2";
    std::size_t position = str.find(" ") + 1;
    str.insert(position, "\n");

    sf::Text text;
    text.setFont(font);
    text.setString(str);
Insert the \n character at the appropriate position, like Jesper said. You don't literally insert the line break inside the sf::Text, but inside the string that you give to your Text.
Or did I misunderstand your question?
//your code
void openFile(ifstream &myfile, Text &text, RenderWindow & window, int loopTimes)
{
    Font font;
    setFont(font, text, fontSize);
    string str;
    for (int i = 0; i < loopTimes + 1; i++)
    {
        string line;
        getline(myfile, line);
        str += line;
        str += '\n';
    }
    text.setString(str);
    window.draw(text);
}
And BTW, you're reading the text from your file and loading your font EVERY frame, it's very slow and useless. Do it once. :p
« Last Edit: August 21, 2014, 03:46:02 am by G. »

tiny screaming yak

  • Guest
Re: Text Wrapping with .txt Files?
« Reply #7 on: August 24, 2014, 03:46:46 pm »
Quote
I usually ask a lot of people for source codes and then study them in case if I need something later. But if you are too lazy to do that or you need something urgently I would suggest you to ask example of how they use it.
It's not that I'm being lazy, I did not know what he meant by "insert". I didn't realize there was an insert function.

Quote
//example    
sf::Font font;
    font.loadFromFile("font.ttf");

    std::string str = "line1 line2";
    std::size_t position = str.find(" ") + 1;
    str.insert(position, "\n");

    sf::Text text;
    text.setFont(font);
    text.setString(str);
Insert the \n character at the appropriate position, like Jesper said. You don't literally insert the line break inside the sf::Text, but inside the string that you give to your Text.
Or did I misunderstand your question?
This is perfect and I totally understand now. Thank you so much.

Quote
And BTW, you're reading the text from your file and loading your font EVERY frame, it's very slow and useless. Do it once. :p
Ahh, thank you!