SFML community forums
Help => Window => Topic started 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:
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.
-
Nop, it should work. Can you show a complete and minimal code that reproduces this problem?
-
while(App.GetEvent(events)){
if(events.Type == sf::Event::KeyPressed){
if (events.Key.Code == sf::Key::Q) std::cout << "Working";
}
}
-
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:
-
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.
#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.
-
There is no Display() so it may have some side effect on event, maybe.
ATI card ?
sf::Key::Q
You mean sf::Key::S, right ?
-
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
-
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.
-
Have you set a max framerate?
-
I have not for both my program and the test program. Yet test works and my program doesn't.
-
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?
-
Held it down.
-
I think your problem is normal...
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: 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)
-
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.
-
I know you said you didn't set a max framerate but have you checked to see what framerate you are actually getting?
-
In the 700s.
-
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.
-
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.
-
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.
-
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.
-
Ok I guess I'll just use sf::Input with a bool check to see if key was unpressed.
-
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.
-
Well sf::Input is and has been working for me since I started, for WSAD controls and such.