Advice on designing a generic event system

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Void Mapper
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Jan 04, 2011 11:00 pm
Programming Language of Choice: C++

Advice on designing a generic event system

Post by Void Mapper »

I've been working on a simple 2D game library and I'm torn between the following two approaches for handling events.
SFML style

Code: Select all

Event event;
while(window.getEvents(&event)
{
     switch(event.type)
     {
         
     }
}
GLUT style

Code: Select all

void mouseDown(Button);
void keyDown(unsigned char);
void windowClosed(void);

window.mouseBtnDown = &mouseDown;
window.keyDown = &keyDown;
window.close = &windowClosed;
I'm currently leaning towards the GLUT approach because it seems like it would be much more efficient than the SFML approach. Are there any advantages to using a SFML style or disadvantages to using a GLUT style?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Advice on designing a generic event system

Post by avansc »

They really are the same thing, glut just wraps the loop in glutmainloop or whatever it is called, I would recommend not using glut or sfml, and just doing it in the os.

the os will most likely have a loop where you process all the events.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Void Mapper
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Jan 04, 2011 11:00 pm
Programming Language of Choice: C++

Re: Advice on designing a generic event system

Post by Void Mapper »

avansc wrote:They really are the same thing, glut just wraps the loop in glutmainloop or whatever it is called, I would recommend not using glut or sfml, and just doing it in the os.

the os will most likely have a loop where you process all the events.
SFML creates it own separate queue for events, I doubt glut does that. Although I'm not currently planning to make the library multi platform (mainly because I only have a windows machine) I'd still like it to be an option. Also, message handling can be a pain in windows sometimes. One of the things I wanted to do with the library was to make it simpler.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Advice on designing a generic event system

Post by avansc »

How do you plan on getting events?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Advice on designing a generic event system

Post by Ginto8 »

In order to really understand how SFML does it vs GLUT requires some more digging. I personally have gutted SFML's windowing code (for linux at least) in my pursuit to understand XLib. Basically what SFML has in the background is "handlers" that take system generated events, act upon them in some way, then decide whether to convert it to an sf::Event and pump it in to the queue. GLUT has user-provided handlers (and just user provided handlers).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Void Mapper
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Jan 04, 2011 11:00 pm
Programming Language of Choice: C++

Re: Advice on designing a generic event system

Post by Void Mapper »

Ginto8 wrote:In order to really understand how SFML does it vs GLUT requires some more digging. I personally have gutted SFML's windowing code (for linux at least) in my pursuit to understand XLib. Basically what SFML has in the background is "handlers" that take system generated events, act upon them in some way, then decide whether to convert it to an sf::Event and pump it in to the queue. GLUT has user-provided handlers (and just user provided handlers).
I've looked through SFML's windowing code for windows. I decided to look through it after I finished the current version of the windowing system I have in my library. I wanted to see how SFML did it because I wasn't happy with the way mine turned out and I was hoping to get an idea of how to make it better. The main difference between mine and SFML's is that mine doesn't create its own queue. Every time a window's getEvents function is called it calls either GetMessage or PeekMessage. Then it converts the message it gets into an Event. Then the function returns whatever the call to Get/PeekMessage returned.

The main problem I have with my current system is how it handles messages that are sent to the window procedure rather than the message queue. At that point what I have to do is post a user message to queue so I can turn it into an event. I have to do this when the user moves, resizes, or closes the window.
Post Reply