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

Author Topic: Clock controlled loop, delayed text output  (Read 2318 times)

0 Members and 1 Guest are viewing this topic.

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Clock controlled loop, delayed text output
« on: August 27, 2012, 04:47:58 am »
Hi all,

I've been trying to display a string character by character, with a short delay between each characters. So far, I set up an array to contain the string, and defined an sf::Text constructor:
char testString[] = "This is a message.";
sf::Text text;

Then, in the main loop, I assign "testString" to "text", and draw it character by character like this:
for(int i = 0; i < sizeof(testString); i++)
{
        text.setString(testString[i]);
        text.setPosition((float)i * 10, 0);
        window.draw(text);
}

It works, however, as mentioned earlier, I'd like to have some delay between the characters. My first idea was to control the loop with a clock. I set up a clock, then replaced "i++" (sorry, I have no idea how this part of the loop initializer is called in english...) with "i += clock.restart().asSeconds()". Of course, it failed miserably, so it did with "clock.restart().asMilliseconds()".

So, what should be the correct way to achieve this? I've seen many videos of various SFML apps doing delayed thext output, but neither of these had public source access, so I couldn't look at their code.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Clock controlled loop, delayed text output
« Reply #1 on: August 27, 2012, 06:01:34 am »
This is probably overkill and nooby but maybe you can reverse-engineer something out of this:
header:
class EEDelayedText : public sf::Text
{
private:
        sf::Clock m_Clock;
        sf::Time m_time,m_delay;
        std::string m_string,m_buffer;
        unsigned int current;
public:
        EEDelayedText(const std::string& gstring,const sf::Font& gfont,unsigned int gsize);
        ~EEDelayedText(void);
        void setString(const std::string& string);//covering text's setter
        void setDelay(sf::Time gtime);
        const EEDelayedText& update(void);
};
source:
#include "EEDelayedText.h"
EEDelayedText::EEDelayedText(const std::string& gstring,const sf::Font& gfont,unsigned int gsize):
sf::Text("",gfont,gsize),//can be changes probably, my sfml 2.0 is not latest so I miss some ctors
current(0),
m_string(gstring)
{
}
EEDelayedText::~EEDelayedText(void)
{
}
void EEDelayedText::setString(const std::string& gstring)
{
        m_buffer.clear();
        m_string=gstring;
        current=0;
}
void EEDelayedText::setDelay(sf::Time gtime)
{
        m_delay=gtime;
}
const EEDelayedText& EEDelayedText::update(void)
{
        m_time+=m_Clock.restart();
        while (m_time>=m_delay)
        {
                m_time-=m_delay;
                if(current<m_string.length())
                {
                        m_buffer+=m_string[current];
                        ++current;
                }
        }
        sf::Text::setString(m_buffer);//text's setString is covered, must call this way
        return *this;
}
use:
   
sf::RenderWindow app(sf::VideoMode(600,600),"EEDelayedText");
        sf::Font font;
        font.loadFromFile("Resources/Main/arial.ttf");
        EEDelayedText txt("Gott mit uns!",font,25);
        txt.setDelay(sf::seconds(1.f/10.f));
        while(1)
        {      
                app.clear();
                app.draw(txt.update());
                app.display();
        }
 
Add includes for sftext/whatever as needed, vc++ is a bit weird about that so there might be some missing.
This class is kind of ugly tbh, especially that lolzy .update() returning reference that window can use for drawing and that's about what you can do with it really because it's const reference.
Don't blame me if something explodes. :)
« Last Edit: August 27, 2012, 03:53:47 pm by FRex »
Back to C++ gamedev with SFML in May 2023

N_K

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Clock controlled loop, delayed text output
« Reply #2 on: August 27, 2012, 11:24:13 pm »
Nice, it works as advertised, and nothing exploded so far. :)

Thank you for your help!