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

Author Topic: Can't display typed text  (Read 4127 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Can't display typed text
« on: September 23, 2015, 11:21:36 pm »
Hello

I'm having a beginner problem, I've already searched Google, Youtube, and the SFML documentation.
All I want to do, is catching what the user types and output (cout) that to the console window (I'll do it to the GUI later on).

Code:
void msgBoxTxt() {
                gui gui; //Class where the function is in
                sf::Event msgBoxEvent = sf::Event();

                cout << "Text: ";
                while (true) {
                        if (msgBoxEvent.type == sf::Event::TextEntered) {
                                cout << static_cast<char>(msgBoxEvent.text.unicode) <<endl;
                        }
                }
        }

The function is triggered correctly because "Text: " is displaying.
What am I doing wrong here?

Thanks for helping!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't display typed text
« Reply #1 on: September 24, 2015, 12:25:20 am »
http://www.sfml-dev.org/tutorials/2.3/window-events.php#the-sfevent-type
Quote
sf::Event instances are filled by the pollEvent (or waitEvent) function of the sf::Window class. Only these two functions can produce valid events, any attempt to use an sf::Event which was not returned by successful call to pollEvent (or waitEvent) will result in the same undefined behavior that was mentioned above.
Amount of call to pollEvent in your code: 0.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #2 on: September 24, 2015, 12:14:03 pm »
void msgBoxTxt() {
                gui gui;
                sf::Event msgBoxEvent = sf::Event();

                cout << "Text";
                while (window.pollEvent(msgBoxEvent)) {
                        if (msgBoxEvent.type == sf::Event::TextEntered) {
                                cout << static_cast<char>(msgBoxEvent.text.unicode) <<endl;
                        }
                }
        }

Tried window and gui.window with pollEvent, none is working.
However, text is displayed multiple times when keeping clicked.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't display typed text
« Reply #3 on: September 24, 2015, 01:09:22 pm »
Do you have an sf::Window or sf::RenderWindow in your program? You need one.
In the tutorial it is assumed that the type of the variable named "window" is sf::Window (or sf::RenderWindow), if you named your sf::Window differently then replace "window" by whatever its name is.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #4 on: September 24, 2015, 09:53:16 pm »
Yes, I have one, which is called window.
This is a more complete code:

class wrapper {public:
sf::RenderWindow window;
void x() {
window.create(sf::VideoMode(720, 385));
}
void checkMouse() {
//PSEUDO OF COURSE!!!
if (mousePosition == onTextBox) {
msgBoxTxt();
}
}
        void msgBoxTxt() {
wrapper wrapper;
                sf::Event msgBoxEvent;

                cout << "Text";
                while (wrapper.window.pollEvent(msgBoxEvent)) {
                        if (msgBoxEvent.type == sf::Event::TextEntered) {
                                cout << static_cast<char>(msgBoxEvent.text.unicode) <<endl;
                        }
                }
        }
};

This semi-pseudo code is a better representation of my whole program.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Can't display typed text
« Reply #5 on: September 24, 2015, 10:28:33 pm »
Consider reading http://sscce.org/ & http://www.catb.org/esr/faqs/smart-questions.html

As for your code:
 I can recommend trying to maintain a consistent style to keep it readable. A tool such as clang-format can help (to be honest; what you just posted looks pretty horrible and is hard to read).
 What's the point of "wrapper"?
 You create the window in the "x" function but you never call that function.
 
« Last Edit: September 24, 2015, 10:32:33 pm by Jesper Juhl »

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #6 on: September 24, 2015, 10:44:17 pm »
That isn't my actual code, and of course not my actual format at all (thank god).
I've written that here in the forum textbox, it's just a representation.

Everything is working perfect, don't worry about that. Except that one function msgboxTxt(), I just added the other code quickly (AS PSEUDO) to make sure that e.g classes don't play a function in this problem.
« Last Edit: September 24, 2015, 10:46:01 pm by Niely »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Can't display typed text
« Reply #7 on: September 24, 2015, 10:56:48 pm »
That isn't my actual code
Then why don't you post your actual code so we can actually help you?

and of course not my actual format at all (thank god).
If that's not how you write code normally, then why do we have to read it like that? Please spend a little effort to make sure you present your code in a way that make people want to read it and help you. You know, like how you'd format it for yourself in your "actual format".
The forum has 'code' tags available to make code look nice - please use them.

I've written that here in the forum textbox, it's just a representation.

Everything is working perfect, don't worry about that. Except that one function msgboxTxt(), I just added the other code quickly (AS PSEUDO) to make sure that e.g classes don't play a function in this problem.
Try posting real code rather than pseudo code. Pseudo code is rarely useful, real code is.  Again; please read the two links from my previous post.
« Last Edit: September 24, 2015, 11:05:26 pm by Jesper Juhl »

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #8 on: September 25, 2015, 12:19:53 pm »
Is it also possible to PM my code to you/someone? Don't want it to be public on the Internet before release.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can't display typed text
« Reply #9 on: September 25, 2015, 12:23:20 pm »
Just post a minimal complete example. Nobody wants to read your entire code if the problem only concerns < 1% of it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't display typed text
« Reply #10 on: September 25, 2015, 03:13:53 pm »
In your code (or at least the one you posted here) you don't create the window.
It's even possible that you have multiple (useless) windows and you're not polling the right one.
Or that you're polling the same window multiple times at different places.

+1 Nexus.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #11 on: September 25, 2015, 11:39:22 pm »
Thanks a lot!
That's working.

        void msgBoxTxt() {
                gui gui;
                gui.window.create(sf::VideoMode(0, 0),"EasyChat");
                sf::Event msgBoxEvent;

                cout << "Text";
                while (gui.window.isOpen()) {
                        while (gui.window.pollEvent(msgBoxEvent)) {
                                if (msgBoxEvent.type == sf::Event::TextEntered) {
                                        cout << static_cast<char>(msgBoxEvent.text.unicode);
                                }
                        }
                }
        }
 

The only problem now is, that there opens a new window, every time the user types an extra window shows up. How to disable that? I already tried gui.window.setVisible(false);, but that ain't working, typed text is not outputted then.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Can't display typed text
« Reply #12 on: September 26, 2015, 01:47:04 am »
Events are triggered for the window which has the focus.
If you already have a window and you don't want a new one, then don't create a new one. ;)

You can pass your window by reference or by pointer to use an already existing one inside your function. (or directly use it if it's in the scope)

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Can't display typed text
« Reply #13 on: September 26, 2015, 01:55:12 pm »
I tried that, but failed.
I can't seem to figure out how to access the other window. className.window is not working, and so are the pointer/reference examples on Google.

 

anything