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

Author Topic: Multiple Events Running At the Same Time?  (Read 6383 times)

0 Members and 1 Guest are viewing this topic.

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Multiple Events Running At the Same Time?
« on: December 15, 2011, 06:17:15 pm »
Hello,
I'm wondering if SFML supports multiple events running at the same time, and if so, how I can implement it.
Say, for example, you had a game with a man driving a car.  To accelerate, you press Spacebar.  To turn, you use the left and right arrow keys.  What if someone wanted to accelerate and turn at the same time?
From what I've seen, it appears SFML can only handle one event at a time.

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Multiple Events Running At the Same Time?
« Reply #1 on: December 15, 2011, 07:38:43 pm »
Using a while loop with PollEvent function (GetEvent in SFML1.6) lets you to manage all events in one frame. Don't use if statements for this.

Cheers

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Multiple Events Running At the Same Time?
« Reply #2 on: December 15, 2011, 08:07:36 pm »
Ah I see, that must be what I'm doing wrong.  I'm using the while(Window.GetEvent(Event)) system from the tutorials.  What should I replace the if statements with?

LB

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Multiple Events Running At the Same Time?
« Reply #3 on: December 15, 2011, 09:08:06 pm »
Use it as is explained on tutorials and everything should work fine. To events that happens at the same time will generate two events, but one will always be the first. As all available events are handled every frame, they will be processed at the same time, in the point-of-view of the end-user of your application.

Code: [Select]
sf::Event event;
while (window.GetEvent(event)) // It must be a while.
{
    // Process your event here, you can use ifs, or a switch to check event type.
}

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Multiple Events Running At the Same Time?
« Reply #4 on: December 15, 2011, 11:39:28 pm »
Quote from: "julen26"
Don't use if statements for this.

What's wrong with using if? Unless you meant else if?

LB

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Multiple Events Running At the Same Time?
« Reply #5 on: December 15, 2011, 11:49:25 pm »
Quote from: "Contadotempo"
What's wrong with using if? Unless you meant else if?


Suppose the user pressed two keys very fast, faster than one frame. If you use if to check new events, this way:

Code: [Select]
while (running)
{
  sf::Event event;
  if (window.GetEvent(event))
  {
    // Process event here
  }
  // Drawing here
  window.Display();
}


You will get only one event per frame. There will be a delay of one frame between the time when the user pressed the second key and the time when the program process it. If you use while:

Code: [Select]
while (running)
{
  sf::Event event;
  while (window.GetEvent(event))
  {
    // Process event here
  }
  // Drawing here
  window.Display();
}


All available events will be processed every frame. No event will remains to be processed by the next frame. This is the right way.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Multiple Events Running At the Same Time?
« Reply #6 on: December 16, 2011, 09:43:36 am »
I misread. Thought julen26 meant if on the event processing. i.e. if(sf::Keyboard::KeyPressed == something).
Sorry about that.