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

Author Topic: Strange behavior (RenderWindow::GetEvent)  (Read 2124 times)

0 Members and 1 Guest are viewing this topic.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Strange behavior (RenderWindow::GetEvent)
« on: August 10, 2010, 03:17:37 pm »
Hi, I wanted to track the events with a function like this, and then I realized that I keep getting sf::Event::KeyReleased events infinitely until the window receives another type of event. This is the code:

Code: [Select]
bool GetEvent(sf::RenderWindow &window, sf::Event &event)
{
bool b = window.GetEvent(event);
// Here you will get this event infinitely before you receive another type of event (like moving the mouse)
if(event.Type == sf::Event::KeyReleased) std::cout << "key released" << std::endl;
return b;
}

int main()
{
sf::RenderWindow window;
window.Create(sf::VideoMode(800, 600), "Bug?");
sf::Event event;
while(window.IsOpened())
{
while(GetEvent(window, event))
{
switch(event.Type)
{
// Close the window
case sf::Event::Closed:
window.Close();
break;
// Check for the key released event. Here it works fine!
case sf::Event::KeyReleased:
std::cout << "=========== KEY RELEASED ===========" << std::endl;
break;
}
}
}
}


I haven't tried with sf::Window. It's my fault?

EDIT:
Somehow, it's fixed if you change the function so it resets the event, like this:
Code: [Select]
bool GetEvent(sf::RenderWindow &window, sf::Event &event)
{
// Add this to "reset" the event
event.Type = sf::Event::Count;

bool b = window.GetEvent(event);

if(event.Type == sf::Event::KeyReleased) std::cout << "key released" << std::endl;
return b;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange behavior (RenderWindow::GetEvent)
« Reply #1 on: August 10, 2010, 03:35:01 pm »
You're not supposed to use the event if GetEvent returned false (which means that there were no event, and that the event you passed is returned unchanged).
Code: [Select]
bool b = window.GetEvent(event);

if (b)
{
   if(event.Type == sf::Event::KeyReleased) std::cout << "key released" << std::endl;
}
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Strange behavior (RenderWindow::GetEvent)
« Reply #2 on: August 10, 2010, 06:17:42 pm »
Silly me, how I didn't noticed that!

Thanks Laurent, it seems that it is always good time to pay more attention to things.

 

anything