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

Author Topic: Typewriter text effect  (Read 2951 times)

0 Members and 1 Guest are viewing this topic.

Nemac

  • Newbie
  • *
  • Posts: 3
    • View Profile
Typewriter text effect
« on: December 05, 2016, 11:06:29 pm »
I am trying to make a typewriter text effect. Here's the simplified code:

#include <SFML/Graphics.hpp>

void typewriter(sf::Text& something, sf::Font& f, sf::RenderWindow& w, std::string text, unsigned int characterSize, int positionX, int positionY, sf::Time td)
{
    something.setCharacterSize(characterSize);
    something.setPosition(positionX, positionY);
    something.setFillColor(sf::Color::White);
    something.setFont(f);
    std::string s = "";
    for (char c : text)
    {
        s = s + c;
        something.setString(s);
        w.clear();
        w.draw(something);
        sf::sleep(td);
        w.display();
    }
}

int main()
{
    bool tw = true; //should typewriter be called
    bool j = false;
    std::string u;
    //window
    sf::RenderWindow window(sf::VideoMode(640, 480), "Problem with input", sf::Style::Default);
    window.setFramerateLimit(60); //framerate limit is set to 60fps
    //font
    sf::Font font;
    font.loadFromFile("oldFont.ttf");
    //text
    sf::Text enterSomething;
    sf::Text input;
    input.setFont(font);
    input.setCharacterSize(25);
    input.setPosition(75, 400);
    input.setFillColor(sf::Color::White);
    //events
    sf::Event event;

    //main loop
    while (window.isOpen())
    {
        window.clear();
        if (tw)
        {
            typewriter(enterSomething, font, window, "Enter something:", 25, 200, 100, sf::milliseconds(200));
            tw = false;
            j = true;
        }
        else
        {
            window.draw(enterSomething);
        }
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                    window.close();
            }
            else if (event.type == sf::Event::TextEntered && j)
            {
                if (event.text.unicode >= 32 && event.text.unicode <= 126)
                {
                    u = u + static_cast<char>(event.text.unicode);
                }
                else if (event.text.unicode == 8 && !u.empty())
                {
                    u.pop_back();
                }
            }
        }
        input.setString(u);
        window.draw(input);
        window.display();
    }
}
 
This is the problem: if the user starts typing while "Enter something:" is being displayed, whatever he/she typed at that moment will be later displayed to the screen.

I tried placing TextEntered block into another event block but the result was same (probably because I didn't do it properly).
« Last Edit: December 06, 2016, 03:28:57 pm by Nemac »

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Typewriter text effect
« Reply #1 on: December 06, 2016, 02:07:45 am »
create another boolean flag for the input and activate when typewriter is finished. also, the sf::clock inside for-loop is really inefficient better to use sf::sleep instead.
« Last Edit: December 06, 2016, 03:32:31 am by Mortal »

Nemac

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Typewriter text effect
« Reply #2 on: December 06, 2016, 03:29:31 pm »
create another boolean flag for the input and activate when typewriter is finished. also, the sf::clock inside for-loop is really inefficient better to use sf::sleep instead.

Thanks for the reply but unfortunately that didn't solve my problem. Check the first post for the updated code.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Typewriter text effect
« Reply #3 on: December 06, 2016, 04:19:30 pm »
This is the problem: if the user starts typing while "Enter something:" is being displayed, whatever he/she typed at that moment will be later displayed to the screen.
This is not what you want? What would you prefer to happen?

Your typewriter effect is hogging the window for its entire duration. If this is what you want to happen. the typewrite function should also be polling for events.
If you poll events during the function but ignore them, anything typed during the effect will no longer be waiting in the event queue.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nemac

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Typewriter text effect
« Reply #4 on: December 06, 2016, 05:23:12 pm »
This is the problem: if the user starts typing while "Enter something:" is being displayed, whatever he/she typed at that moment will be later displayed to the screen.
This is not what you want? What would you prefer to happen?

Your typewriter effect is hogging the window for its entire duration. If this is what you want to happen. the typewrite function should also be polling for events.
If you poll events during the function but ignore them, anything typed during the effect will no longer be waiting in the event queue.

Thanks, it works now.
« Last Edit: December 06, 2016, 05:26:02 pm by Nemac »