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();
}
}