You could use a Clock to control the time between characters, and use a variable to get a substring of the string.
You shouldn't use sleep functions, because they would pause the game every iteration.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
// Create a graphical text to display
sf::Font font;
if (!font.LoadFromFile("arial.ttf"))
return EXIT_FAILURE;
sf::Text text("", font, 50);
sf::Clock timer;
unsigned int character = 0;
std::string str = "This is an example of typewriter \neffect, it uses one clock to control \nthe time. Check out the code, it's \neasy to understand!";
// Start the game loop
while (window.IsOpened())
{
// Process events
sf::Event event;
while (window.PollEvent(event))
{
// Close window : exit
if (event.Type == sf::Event::Closed || (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape))
window.Close();
}
if (timer.GetElapsedTime() > 50 && character < dialog.size())
{
timer.Reset();
character++;
text.SetString( sf::String( str.substr(0, character) ) );
}
// Clear screen
window.Clear();
// Draw the string
window.Draw(text);
// Update the window
window.Display();
}
return EXIT_SUCCESS;
}
It's very easy to understand.
Cheers.