SFML community forums

Help => Window => Topic started by: dove96 on March 13, 2016, 08:00:41 am

Title: how to create "input fields/ text fields"
Post by: dove96 on March 13, 2016, 08:00:41 am
Hello everyone, i am an absolute beginner with c++ and sfml, i was tasked to create an Artillery Game, 2 player.
i really need help on how to create a Text field area where a player will enter their name, ive seen tutorials read docs, and forums, but still i am not able to do this. i really need some help.

how should i display the TextEntered on the window?
for example: Player presses 'J', that 'J' is immediately drawn on screen..

any help??if this is even possible??
Title: Re: how to create "input fields/ text fields"
Post by: Laurent on March 13, 2016, 09:20:31 am
Quote
Player presses 'J', that 'J' is immediately drawn on screen..
sf::String playerInput;
sf::Text playerText;

...

if (event.type == sf::Event::TextEntered)
{
    playerInput +=event.text.unicode;
    playerText.setString(playerInput);
}

...

window.draw(playerText);

You see, nothing really complicated ;)
Title: Re: how to create "input fields/ text fields"
Post by: dove96 on March 17, 2016, 08:59:32 pm
Thank you so much. you're my savior laurent.. ;D ;D ;D ;D
Title: Re: how to create "input fields/ text fields"
Post by: dove96 on March 18, 2016, 04:10:32 am
Hi Laurent, im sorry im here to bug you again. my program is working, but it does not display the characters typed on the window. here's my code. Can you please go over it and see what may i be doing wrong?

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int entername()
{
            sf::RenderWindow window(sf::VideoMode(1180, 600), "PANZER_WAR:TPL_GAME", sf::Style::None);
            sf::Font font;
            if (!font.loadFromFile("emulogic.ttf"))
            return EXIT_FAILURE;

            sf::Event event;
            sf::String playerInput;
            sf::Text playerText;
            playerText.setPosition(60,300);
            playerText.setColor(sf::Color::Red);

    while (window.isOpen())
        {
             while(window.pollEvent(event))
                {
                        if (event.type == sf::Event::TextEntered)
                            {
                                if(event.text.unicode < 128)
                                    {
                                        playerInput +=event.text.unicode;
                                        playerText.setString(playerInput);
                                    }
                            }
                            window.draw(playerText);
                }
        window.display();
        }
    return 0;
}
Title: Re: how to create "input fields/ text fields"
Post by: Hapax on March 18, 2016, 04:17:11 am
You need to draw the text object to the window at all times, not just when it is updated/when there is an event.
The text object is the graphical representation of that text and it needs to be drawn on every frame that you wish it to be seen.
(click to show/hide)

EDIT: corrected "explicit fix"
Title: Re: how to create "input fields/ text fields"
Post by: dove96 on March 18, 2016, 04:27:54 am
"move  window.draw(playerText); so that it is directly before window.draw(playerText);"

Hi there Hapax, im really new to this, so where should i put window.draw(playerText);?

tried to move it before the poll event but no luck.

Title: AW: how to create "input fields/ text fields"
Post by: eXpl0it3r on March 18, 2016, 07:52:42 am
He meant window.display(), i.e. you should move the draw() infront of the display().
And before that you need to call window.clear().

Everything is explained in the tutorials, please study them in-depth.
Title: Re: how to create "input fields/ text fields"
Post by: Hapax on March 18, 2016, 04:51:17 pm
Sorry. That was a typo. I've fixed my post but eXpl0it3r has explained what needs to be done.
Title: Re: how to create "input fields/ text fields"
Post by: dove96 on March 19, 2016, 05:45:38 am
 Good Gracious!!! Thank you for your support.
i did move it in that area. but still it didnt work. only then when i defined

sf::Text playerText;

to

sf::Text playerText(''',font,size);

im really sorry, that should have been obvious to me. grateful for your help guys. sky five!
Title: Re: how to create "input fields/ text fields"
Post by: Hapax on March 21, 2016, 12:27:18 pm
You could set those parts of the text separately; it's not required that they're set in the constructor.