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

Author Topic: how to create "input fields/ text fields"  (Read 28411 times)

0 Members and 1 Guest are viewing this topic.

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
how to create "input fields/ text fields"
« 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??
« Last Edit: March 13, 2016, 08:03:26 am by dove96 »
-dove96-

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: how to create "input fields/ text fields"
« Reply #1 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 ;)
Laurent Gomila - SFML developer

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: how to create "input fields/ text fields"
« Reply #2 on: March 17, 2016, 08:59:32 pm »
Thank you so much. you're my savior laurent.. ;D ;D ;D ;D
-dove96-

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: how to create "input fields/ text fields"
« Reply #3 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;
}
« Last Edit: March 18, 2016, 04:20:08 am by dove96 »
-dove96-

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: how to create "input fields/ text fields"
« Reply #4 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"
« Last Edit: March 18, 2016, 04:49:51 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: how to create "input fields/ text fields"
« Reply #5 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.

« Last Edit: March 18, 2016, 04:31:39 am by dove96 »
-dove96-

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
AW: how to create "input fields/ text fields"
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: how to create "input fields/ text fields"
« Reply #7 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: how to create "input fields/ text fields"
« Reply #8 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!
-dove96-

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: how to create "input fields/ text fields"
« Reply #9 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything