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

Author Topic: Displaying the sf::Text in window immediately after pressing a button.  (Read 1518 times)

0 Members and 1 Guest are viewing this topic.

chips1331

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hello guys, it's my first post here. I started to play with SFML and I have quite strange problem. I want to write letters '1' or '0' in my board. I want it to show immediately after I press 1 or 0 but can't really do it well. Here's my void. It should draw 1 or 0 in a position immediately after I press1 or 0 and it actually does, but after I want to print another number the previous one is getting hidden.
void tablica::rysowanie_cyfr(sf::RenderWindow & window)
{
        int a = 50;
        int b = 50;
        sf::Event event;
        bool wait = true;
                for (int i = 0;  i<4; i++) {
                        for (int j = 0;  j<4; j++) {
                                sf::Text *text = new sf::Text;
                                text = set_properties(window);
                                while (wait) {
                                        while (window.pollEvent(event)) {
                                                if (event.type == sf::Event::KeyReleased) {
                                                        if (event.key.code == sf::Keyboard::Num1) {
                                                                text->setPosition(a, b);
                                                                text->setString("1");
                                                                wait = false;
                                                                window.draw(*text);
                                                       
                                                        }
                                                        else if (event.key.code == sf::Keyboard::Num0)
                                                        {
                                                                text->setPosition(a, b);
                                                                text->setString("0");
                                                                wait = false;
                                                                window.draw(*text);
                                                        }
                                                }
                                        }
                                }
                                delete text;
                                wait = true;
                                a += 100;
                                window.display();
                        }b += 100; a = 50;
        }
}
It is summoned in a main loop like this
if (wait == true) {
                        tabela.rysowanie_cyfr(window);
                }
 
wait is set at false and when i press a button I set wait as true.
Please, help me :(
Also sorry for my english it's not my native language. If you have problems with understanding my problem I can explain it futher.
« Last Edit: March 17, 2017, 12:58:34 pm by chips1331 »

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
You're doing setString("0") or setString("1") which will set the string to EXACTLY "0" or "1".
If you want to add numbers afterwards, you need to do text.setString(text.getString() + "whatevertextyouwanthere").

Also, PLEASE do not do

if (booleanVariable == true)

just do

if (booleanVariable)

It's just cleaner.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
For what are the i and j loops?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything