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

Author Topic: Text Wrapping in SFML  (Read 2564 times)

0 Members and 1 Guest are viewing this topic.

Funky

  • Newbie
  • *
  • Posts: 1
    • View Profile
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:

#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;
}

 
« Last Edit: January 12, 2017, 05:07:02 pm by Funky »

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Text Wrapping in SFML
« Reply #1 on: January 13, 2017, 12:36:57 am »
I can't actually see where it is that you've declared the 'width' variable. Is this code actually the same as the code in your program?