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

Author Topic: Event handling advice\best prectices  (Read 6116 times)

0 Members and 1 Guest are viewing this topic.

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Event handling advice\best prectices
« on: August 31, 2011, 11:27:38 am »
i wonder about event handling in my game. how am i supposed to use sfml? - tutorials provide very terse information on this. For example

Code: [Select]
// Start the game loop
     while (window.IsOpened())
     {
         // Process events
         sf::Event event;
         while (window.PollEvent(event))
         {
             // Close window : exit
             if (event.Type == sf::Event::Closed)
                 window.Close();
         }
 
         // Clear screen
         window.Clear();
 
         // Draw the sprite
         window.Draw(sprite);
 
         // Draw the string
         window.Draw(text);
 
         // Update the window
         window.Display();
     }


Game logic goes somewhere between window.Clear(); and window.Draw(sprite); . So how in this case handle events? For example i have several user interface windows and only one is active - when user clicks "Escape" I want to close my window - not the game itself. But when no window is shown i want to close the game.

Any suggestions? or samples

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Event handling advice\best prectices
« Reply #1 on: August 31, 2011, 11:32:41 am »
You could have a generic event handler that dispatches events from the main event loop, to the appropriate object(s) depending which one has the focus, is active, etc. Then, each object can block the event (most likely when it processed it), or propagate it to children / listeners.

Event dispatching rules in user interfaces can be tricky, you should read more about these. I don't know if there are tutorials or articles about it, but at least you can have a look at the source code of simple GUI libraries if you're really lost.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Event handling advice\best prectices
« Reply #2 on: August 31, 2011, 12:16:11 pm »
There are multiple possibilities. The direct (and least beautiful) one is to have a huge switch statement with all reactions of game logics to events.

Another option, still procedural, is to store the relevant events each frame and pass the state information to different game entities which react correspondingly.

Even less decoupled is an object-oriented event-handler. This can be an abstract base class with a virtual OnEvent() function, but also callback systems like Boost.Signals are possible. Then you can link different game entities directly to concrete events.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

omeg

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • http://omeg.pl/
Event handling advice\best prectices
« Reply #3 on: August 31, 2011, 12:27:45 pm »
Or use C# which has built-in support for events. :wink:

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Event handling advice\best prectices
« Reply #4 on: August 31, 2011, 01:53:13 pm »
i was hoping i won't have to write something on top of sfml and can use some kind of an existing mechanism.

dydya-stepa

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Event handling advice\best prectices
« Reply #5 on: August 31, 2011, 02:01:02 pm »
by the way, Laurent, is there any game discussions happening around? i mean within these sfml forums. sfml is a good library but for those who're making games not engines that might be helpful. (in the context of the sfml)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Event handling advice\best prectices
« Reply #6 on: August 31, 2011, 02:41:50 pm »
Quote from: "dydya-stepa"
i was hoping i won't have to write something on top of sfml and can use some kind of an existing mechanism.
I wrote an event-handling system in my library. If you are interested in reusing existing functionality, you could take a look at the tutorial to get an impression.

Quote from: "dydya-stepa"
by the way, Laurent, is there any game discussions happening around? i mean within these sfml forums.
Yes, there are frequently discussions about game design and structure. You can try to search for the term "design", for example...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything