Sign in to follow this  

Eventsystem with SDL

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Has anyone here written and custom eventsystem based on SDL_UserEvent? I have several threads in my game, the most important ones are client (containing the main loop and processing all userinput and graphics) and server (doing only gamelogic stuff). What Im currently doing is writing message structs which I can put into a SDL_UserEvent and also easily serialize to send them over the internet. My question now is whats the best way for threads other than the mainloop in the client (which uses SDL_PollEvent and a gigantic switch case for everything) to check for SDL_UserEvents. My best guess is using SDL_PeepEvent in the other threads, and pushing all events that arent processed in the thread back in the queue. SDL_EventFilters seems not to be the right choice, for it doesnt filter events which were pushed manually with SDL_PushEvent. So this would be something like:
//client thread
while(client_running)
{
    SDL_Event event;
    SDL_PollEvent(&event);
    if(event.user.code == 2) // 2 is client event
    {
        //further proceed the event 
    }
    else
    {
        SDL_PushEvent(event);   //event is not for the client, so push it back
    }
}

//server thread
while(server_running)
{
    SDL_Event event;
    SDL_PeepEvent(&event,1,SDL_GETEVENT,SDL_EVENTMASK(SDL_UserEvent));
    if(event.user.code == 1)  //1 is server event
    {
        //further proceed the event 
    }
    else
    {
        SDL_PushEvent(event);   //event is not for the server, so push it back
    }
Is this an efficient way to do this? Or did I get something in the SDL documentation wrong?

Share this post


Link to post
Share on other sites
I'd say it is generally a bad idea to retrieve messages from the message queue from two or more different threads (I don't know if the event queue functions are even thread save). Apart from that your implementation is wrong too since you can't query the even.user.code variable unless the type variable indicates a user event.

I'd suggest to look for another way to communicate between client and server.

Share this post


Link to post
Share on other sites
@Trenki

SDL_PushEvent is documented as being thread safe.

@OP

From my understanding, the SDL Event Queue is not intended for bi-directional communication.

I am currently using a simple system for bi-directional events in my application. For messages from auxiliary threads to the main thread, I use the event queue. For messages going the other direction, I use a locked command queue. This works well for me because there are few messages being passed (its a ray tracer, so a given thread typically gets one message and will spend a few seconds processing it, before sending a response message). For a game with a high event frequency, you might consider a lockless queue.

You might be interested in Bob Pendleton's Fast Event library. I can't remember if it offers bi-directional communication, but I recall it to be an interesting read either way.

Share this post


Link to post
Share on other sites
Ok, just checked and it seems the even queue functions are thread save. This means it would be possible to read from the queue from two different threads. One would still have to watch out for thread related issues in ones own code though.

Share this post


Link to post
Share on other sites

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this