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

Author Topic: Event Polling not working  (Read 9731 times)

0 Members and 1 Guest are viewing this topic.

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« on: December 06, 2010, 02:21:26 am »
I am trying to get events and it isn't working. Here is the code:

Code: [Select]
sf::Event events;

while(App.GetEvent(events)){
if(events.Type == sf::Event::KeyPressed){
if (events.Key.Code == sf::Key::S) mainchinventory.save(this, maps);


It's an excerpt so it isn't closed loop wise. It compiles, just when I press any button it doesn't accept any input. Has the format changed?

This is SFML 2.0.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Event Polling not working
« Reply #1 on: December 06, 2010, 08:05:52 am »
Nop, it should work. Can you show a complete and minimal code that reproduces this problem?
Laurent Gomila - SFML developer

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #2 on: December 06, 2010, 10:53:59 pm »
Code: [Select]
while(App.GetEvent(events)){
if(events.Type == sf::Event::KeyPressed){
if (events.Key.Code == sf::Key::Q) std::cout << "Working";
}
}

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Event Polling not working
« Reply #3 on: December 06, 2010, 11:12:42 pm »
a complete and minimal code means :
> one file or so – most of the time it means only the int main(int, char**) function.
> compile out of the box.
> reproduce the (un)desired problem.

here the second point is not fulfilled  :wink:
SFML / OS X developer

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #4 on: December 07, 2010, 01:50:59 am »
For some reason I cannot recreate the code, so there probably is a problem before there. Another question, why does the game crash when you close the render window? This is the code. I exist with code -1073741819. Using Visual Studio 2010 debug.
Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main(){
sf::Event events;

sf::RenderWindow App(sf::VideoMode(640, 800, 32), "test");
while(App.IsOpened()){
while (App.GetEvent(events)){
if(events.Type == sf::Event::KeyPressed){
if (events.Key.Code == sf::Key::S) std::cout << "sssSSSsss";
else if (events.Key.Code == sf::Key::Escape) App.Close();
}
}
}
return 0;
}


Oh I found out that my code DOES work, but the event detection takes a few seconds to kick in, and then it doesn't repeated for a few seconds more. Why is that? As in if the even is for sf::Key::Q it will wait for a few seconds before detecting it, then again before detecting it again.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Event Polling not working
« Reply #5 on: December 07, 2010, 06:59:56 pm »
There is no Display() so it may have some side effect on event, maybe.

ATI card ?

Quote
sf::Key::Q
You mean sf::Key::S, right ?
SFML / OS X developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Event Polling not working
« Reply #6 on: December 07, 2010, 09:09:45 pm »
If there's a huge delay between the event pulling then you have to be doing something very hard for the CPU in-between each frame so that you get SPF instead of FPS which you do NOT want. What do you else do in your main-loop except check for events? Also there can be a problem if you just go full-throtthle in a loop without any sleep when a frame is done. A easy way to fix this is just to call window.Display() at the end of each frame. the Display function got a framerate cap built in to it. For more information check this out: http://www.sfml-dev.org/documentation/1.6/classsf_1_1Window.htm#5544031f1d2965c00532fb5660763f33
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #7 on: December 07, 2010, 09:58:34 pm »
It is an ATi card, I said Q in one and S in the other, but I was mindful of this when pressing the buttons, so it doesn't really matter. There is a Display() in the main loop. And I don't want to throttle frame rate. Woops I see what you meant about Q and S. Yeah I meant S. Oh and I do have a Display() in my actual program, but the code that I showed with the main loop completely works. I mimicked it in my program but to no avail.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Event Polling not working
« Reply #8 on: December 07, 2010, 10:15:46 pm »
Have you set a max framerate?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #9 on: December 07, 2010, 11:08:19 pm »
I have not for both my program and the test program. Yet test works and my program doesn't.

Drektar

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Event Polling not working
« Reply #10 on: December 07, 2010, 11:40:26 pm »
When you said you have to wait before the program detect your key again... You pressed the button,release,pressed,release etc... or you keep the button pressed?

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #11 on: December 08, 2010, 12:47:02 am »
Held it down.

Drektar

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Event Polling not working
« Reply #12 on: December 08, 2010, 01:10:53 am »
I think your problem is normal...
Quote
This event system is good enough for reacting to events like window closing, or a single key press. But if you want to handle, for example, the continous motion of a character when you press an arrow key, then you will soon see that there is a problem : there will be a delay between each movement, the delay defined by the operating system when you keep on pressing a key.


For your utilization you have to use sf::Input

An example:
Code: [Select]
   while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        if (App.GetInput().IsKeyDown(sf::Key::Left))
        {
            //do something
        }
        if (App.GetInput().IsKeyDown(sf::Key::S))
        {
            //Do something else
        }


You can also look in the tutorial : Handling events

Yelnats

  • Newbie
  • *
  • Posts: 27
    • View Profile
Event Polling not working
« Reply #13 on: December 08, 2010, 01:38:50 am »
That is exactly what I don't want. I want to monitor single key presses, not held keys. They problem is that it only reacts or registers IF you hold the key down for a few seconds, instead of registering the moment you press.

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Event Polling not working
« Reply #14 on: December 08, 2010, 04:48:22 am »
I know you said you didn't set a max framerate but have you checked to see what framerate you are actually getting?

 

anything