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

Author Topic: pollEvent problem  (Read 3091 times)

0 Members and 1 Guest are viewing this topic.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
pollEvent problem
« on: May 28, 2013, 04:25:06 am »
SFML forum,

 SFML 2.0  Visual c++ 2010

 I can take in keys pressed with a pollEvent in my main() successfully.

 But when I call a function that does another pollEvent to check keys pressed it always gets the last key typed from the pollEvent in the main().
  If I call the function without doing the pollEvent in main()  there is no key in the stdin buffer.

 I have read and tried all the methods for clearing the stdin buffer but none has worked.

 Any suggestions?

jerryd

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: pollEvent problem
« Reply #1 on: May 28, 2013, 07:36:12 am »
Do not poll more than once per loop. Poll in the beginning and safe the key status for later use or use sf::keyboard::isKeyPressed()
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: pollEvent problem
« Reply #2 on: May 28, 2013, 05:04:29 pm »
Kralo9,

 Thanks for the reply.

 I don't quite understand your suggestion yet.
 Here is the exact function I call from the main()

int getKey(sf::RenderWindow* screen)
{
   do {
      sf::Event event;
      if(screen->pollEvent(event)) {
         if(event.type == event.KeyPressed) {

            if (event.key.code == sf::Keyboard::Num1) {return 1;}
            if (event.key.code == sf::Keyboard::Num2) {return 2;}
            if (event.key.code == sf::Keyboard::Num3) {return 3;}
            if (event.key.code == sf::Keyboard::Num4) {return 4;}
         }
      }
   } while(1);

   return 0;
} // end int getKey(sf::RenderWindow* screen)

 I call this event many times.
 How should I change it?

 The other pollEvent function is very similar.

jerryd

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: pollEvent problem
« Reply #3 on: May 28, 2013, 06:07:27 pm »
Kralo9,

 Thanks for the reply.

 I don't quite understand your suggestion yet.
 Here is the exact function I call from the main()
int getKey(sf::RenderWindow* screen)
{
        do {
                sf::Event event;
                if(screen->pollEvent(event)) {
                        if(event.type == event.KeyPressed) {

                                if (event.key.code == sf::Keyboard::Num1) {return 1;}
                                if (event.key.code == sf::Keyboard::Num2) {return 2;}
                                if (event.key.code == sf::Keyboard::Num3) {return 3;}
                                if (event.key.code == sf::Keyboard::Num4) {return 4;}
                        }
                }
        } while(1);

        return 0;
} // end int getKey(sf::RenderWindow* screen)
 

 I call this event many times.
 How should I change it?

 The other pollEvent function is very similar.

jerryd

Please use code tags ;)

First you should not infinite loop until a key gets pressed.
Rather check all key inputs in the beginning of your loop:
while(window.isOpen()){

   checkEvents(); //<--- Here you check all keys pressed and every other events and store
                           // the information somehow
   updateMyApp(); //<--- Here you check the STORED inputs
   window.clear();
   //draw stuff
   window.display();
}
 

How I solved this: I inherited from sf::RenderWindow and added to my class an update function. Additionaly I added a isFocused() function, so I can check if my window is focused first and then check pressed keys with sf::keyboard::isKeyPressed(...)
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

Sqasher

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: pollEvent problem
« Reply #4 on: May 29, 2013, 01:27:05 am »
You should definitely read the tutorials and the documentation.
Also if you don't provide us your code or a minimal example, it's hard to find out what you are trying to do exactly.  :)

I think you might have something like this:
while (window.isOpen())
{
        sf::Event event;
        while(window.pollEvent(event))
        {
                // ...
        }
       
        // call getKey and do something with it
        int key = getKey(&window);

        // ...
       
        window.clear();
        window.draw(...);
        window.display();
}

The problem with this is, that pollEvent() returns events as long as there are events and the code will only continue if there are no events in queue. So there are no events exept the last one when getKey() is called.

Also, like kralo9 said, you loop until an key is pressed. The window can't draw anything until that happens and seems to be frozen.

You probably want real time input here:
int getKey(sf::RenderWindow* window)
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num0))
                return 0;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num1))
                return 1;
        ...
}
 

Hope that helps.  ;)
« Last Edit: May 29, 2013, 01:29:38 am by Sqasher »

 

anything