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

Author Topic: typewriter effect like RPG games  (Read 6844 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
typewriter effect like RPG games
« on: December 14, 2011, 01:21:32 pm »
Hello
I want to make a typewriter effect where text get displayed letter by letter like in RPG games.

I did some coding and got it to work in windows console but i can't figure out how to do it in SFML.

here is an example on how i want this.


here is the code working
Code: [Select]

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main() {

string Text = "This Text should be display letter by letter like a typewriter :D\n if this works I would be freak'n happy xD";

for (int x = 0; x < Text.length(); x++) {
          cout << Text[x];
          Sleep(100);
     }

     system("pause");
     return 0;
}

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
typewriter effect like RPG games
« Reply #1 on: December 14, 2011, 04:48:02 pm »
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.

Code: [Select]
#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.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
typewriter effect like RPG games
« Reply #2 on: December 14, 2011, 05:14:04 pm »
Quote from: "julen26"
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.

Code: [Select]
#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.


thank you very much that was very very helpful :D
I just modified the code to work with sfml 1.6 because I'm still using the 1.6 xD. Here is the code modified for 1.6 for anyone else who want to use it.

Code: [Select]

#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

sf::String text;

    sf::Clock timer;
    unsigned int character = 0;
    std::string str = "This is an example of typewriter \n effect, it uses one clock to control \n the time. Check out the code, it's \n easy to understand!";

//text.SetText(str);
text.SetSize(20);

    // Start the game loop
    while (window.IsOpened())
    {
window.SetFramerateLimit(60);

        sf::Event Event;
while (window.GetEvent(Event))
        {
            // Close window : exit
if (Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape))
window.Close();
        }

if (timer.GetElapsedTime() > 0.1 && character < str.length())
        {
            timer.Reset();
            character++;
text.SetText( str.substr(0, character) ) ;
        }

        window.Clear();
        window.Draw(text);
        window.Display();
    }

    return 0;
}



again thank you very much.

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
typewriter effect like RPG games
« Reply #3 on: December 14, 2011, 05:47:26 pm »
Oh, i'm so sorry, I should have adviced about the SFML version.
 :wink: