1
Graphics / Text Wrapping in SFML
« on: January 12, 2017, 05:04:38 pm »
Hi! I'm currently just beginning to learn SFML. I wanted to know how to wrap the text around the window screen so I added a new line whenever the global bound of the text is less than the screen dimensions. But I don't know how to make the global bound to return to 0 to something like that. but after the first line, it continuously add new line for every character.
Here is my code:
Here is my code:
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
#include <ctime>
#include<cstdlib>
using namespace std;
int main()
{
sf::Vector2i screenDimensions(800, 600);
sf::Vector2i blockDimensions(10, 10);
sf::RenderWindow window(sf::VideoMode( screenDimensions.x, screenDimensions.y), "SFML Tutorial", sf::Style::Close | sf::Style::Titlebar);
//window.setKeyRepeatEnabled(false);
sf::Font font;
if (!font.loadFromFile("OCRAStd.otf"))
{
cout << "Can't find the font file" << endl;
}
sf::String sentence;
sf::Text text(sentence, font, 40);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setFillColor(sf::Color(44, 127, 255));
text.setOutlineColor(sf::Color(44, 127, 255));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case(sf::Event::Closed):
window.close();
break;
case(sf::Event::KeyPressed) :
if (event.key.code == sf::Keyboard::Escape)
window.close();
break;
case sf::Event::TextEntered:
if (width >= screenDimensions.x)
{
sentence.insert(sentence.getSize(), "\n");
width = 0;
}
if (event.text.unicode >= 32 && event.text.unicode <= 126)
sentence += (char)event.text.unicode;
else if (event.text.unicode == 8 && sentence.getSize() > 0)
sentence.erase(sentence.getSize() - 1, sentence.getSize());
text.setString(sentence);
break;
}
}
window.draw(text);
window.display();
window.clear();
}
return 0;
}
#include <iostream>
#include <string>
#include <ctime>
#include<cstdlib>
using namespace std;
int main()
{
sf::Vector2i screenDimensions(800, 600);
sf::Vector2i blockDimensions(10, 10);
sf::RenderWindow window(sf::VideoMode( screenDimensions.x, screenDimensions.y), "SFML Tutorial", sf::Style::Close | sf::Style::Titlebar);
//window.setKeyRepeatEnabled(false);
sf::Font font;
if (!font.loadFromFile("OCRAStd.otf"))
{
cout << "Can't find the font file" << endl;
}
sf::String sentence;
sf::Text text(sentence, font, 40);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setFillColor(sf::Color(44, 127, 255));
text.setOutlineColor(sf::Color(44, 127, 255));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case(sf::Event::Closed):
window.close();
break;
case(sf::Event::KeyPressed) :
if (event.key.code == sf::Keyboard::Escape)
window.close();
break;
case sf::Event::TextEntered:
if (width >= screenDimensions.x)
{
sentence.insert(sentence.getSize(), "\n");
width = 0;
}
if (event.text.unicode >= 32 && event.text.unicode <= 126)
sentence += (char)event.text.unicode;
else if (event.text.unicode == 8 && sentence.getSize() > 0)
sentence.erase(sentence.getSize() - 1, sentence.getSize());
text.setString(sentence);
break;
}
}
window.draw(text);
window.display();
window.clear();
}
return 0;
}