SFML community forums

Help => Window => Topic started by: Yelnats on December 06, 2010, 02:21:26 am

Title: Event Polling not working
Post by: Yelnats 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.
Title: Event Polling not working
Post by: Laurent on December 06, 2010, 08:05:52 am
Nop, it should work. Can you show a complete and minimal code that reproduces this problem?
Title: Event Polling not working
Post by: Yelnats 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";
}
}
Title: Event Polling not working
Post by: Hiura 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:
Title: Event Polling not working
Post by: Yelnats 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.
Title: Event Polling not working
Post by: Hiura 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 ?
Title: Event Polling not working
Post by: Groogy 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
Title: Event Polling not working
Post by: Yelnats 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.
Title: Event Polling not working
Post by: Groogy on December 07, 2010, 10:15:46 pm
Have you set a max framerate?
Title: Event Polling not working
Post by: Yelnats 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.
Title: Event Polling not working
Post by: Drektar 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?
Title: Event Polling not working
Post by: Yelnats on December 08, 2010, 12:47:02 am
Held it down.
Title: Event Polling not working
Post by: Drektar 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 (http://www.sfml-dev.org/tutorials/1.6/window-events.php)
Title: Event Polling not working
Post by: Yelnats 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.
Title: Event Polling not working
Post by: Terrydil 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?
Title: Event Polling not working
Post by: Yelnats on December 08, 2010, 04:54:05 am
In the 700s.
Title: Event Polling not working
Post by: Groogy on December 08, 2010, 08:47:01 am
700 frames per second? That's your problem, you are running at full throttle with the main-loop. Because you do that it will starve the thread processing events in the background resulting in a huge delay in the events getting registered since the event thread don't get any opportunity to run since your main thread is busy working it's ass off trying to pull events that will never be registered.

Like I said before, easy fix is setting a framerate or just simply calling sf::Sleep.
Title: Event Polling not working
Post by: Laurent on December 08, 2010, 08:51:17 am
Quote
That's your problem, you are running at full throttle with the main-loop. Because you do that it will starve the thread processing events in the background resulting in a huge delay in the events getting registered since the event thread don't get any opportunity to run since your main thread is busy working it's ass off trying to pull events that will never be registered.

There's no event thread running in parallel/background, events are processed when calling GetEvent.
Title: Event Polling not working
Post by: Groogy on December 08, 2010, 10:52:50 am
Quote from: "Laurent"
Quote
That's your problem, you are running at full throttle with the main-loop. Because you do that it will starve the thread processing events in the background resulting in a huge delay in the events getting registered since the event thread don't get any opportunity to run since your main thread is busy working it's ass off trying to pull events that will never be registered.

There's no event thread running in parallel/background, events are processed when calling GetEvent.


You might not have a thread for it but the OS has. I get the same problem if my main-thread never sleeps.
Title: Event Polling not working
Post by: Laurent on December 08, 2010, 11:06:58 am
Quote
You might not have a thread for it but the OS has. I get the same problem if my main-thread never sleeps.

Hmm, never seen this problem before (and I've already seen apps running at thousands of FPS). I don't think the OS runs an event thread inside your process. Maybe it has a global process for that but then it's probably smart enough to handle priorities, otherwise any application that runs at 100% CPU would totally block the whole OS.
Title: Event Polling not working
Post by: Yelnats on December 08, 2010, 03:44:14 pm
Ok I guess I'll just use sf::Input with a bool check to see if key was unpressed.
Title: Event Polling not working
Post by: Laurent on December 08, 2010, 04:05:08 pm
Quote from: "Yelnats"
Ok I guess I'll just use sf::Input with a bool check to see if key was unpressed.

Is it different? sf::Input uses events internally.
Title: Event Polling not working
Post by: Yelnats on December 08, 2010, 10:36:14 pm
Well sf::Input is and has been working for me since I started, for WSAD controls and such.