Sign in to follow this  

SDL_Event - global or not

This topic is 3463 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

Hey all I have a question. I have a lot of classes that will be using SDL_Events. Would it be better to have a global SDL_Event that they all use, or should I remake the structure every time I create the class or should I remake it every time I call the event polling function?
/// Option 1 - global


SDL_Event event;

// ...

void
CFoo::
handleEvents()
{
    while ( SDL_PollEvent( &event ) ) {
        // ...
    }
}

// ...

void
CBar::
handleEvents()
{
    while ( SDL_PollEvent( &event ) ) {
        // ...
    }
}
/// Option 2 - Private member


class CFoo : public CParent
{
  public:

    CFoo();
    ~CFoo();

    handleEvents();
    // ...

  private:

    SDL_Event m_event;

};

// ...

class CBar : public CParent
{
  public:

    CBar();
    ~CBar();

    handleEvents();
    // ...

  private:

    SDL_Event m_event;

};

// ...

void
CFoo::
handleEvents()
{
    while ( SDL_PollEvent( &m_event ) ) {
        // ...
    }
}

// ...

void
CBar::
handleEvents()
{
    while ( SDL_PollEvent( &m_event ) ) {
        // ...
    }
}
/// Option 3 - In scope


void
CFoo::
handleEvents()
{
    SDL_Event event;

    while ( SDL_PollEvent( &event ) ) {
        // ...
    }
}

// ...

void
CBar::
handleEvents()
{
    SDL_Event event;

    while ( SDL_PollEvent( &event ) ) {
        // ...
    }
}
I encountered option 3 in some example code I'm using to learn from. Somehow it seems wrong to recreate that structure all the time. Wouldn't it be killing my processing power? Also, wouldn't it make my event polling less acurate, because only events registered between the declaration and the while loop would be polled? Option 1 was my first alternative idea, but whenever I think of the word global, I feel it is wrong, like for example using goto. Option 2 looks a little less bad, but I wouldn't know whether I should make the event part of each child, or just part of the parent. Then, I wouldn't know whether to change the inheritance to private or to make the event structure public. All these questions, I'm sure someone can help me. Thanks!

Share this post


Link to post
Share on other sites
Option 3 is definately the best.

SDL_PollEvent looks at what events are pending on some internal SDL data structure that you can't see, and copies the first into the event structure. You then do whatever you want with that event. After that, you poll the next event (because of the while loop), which fills the event structure again, and so on until there are no more pending events on this internal datastructure.

Note that deciding where you place your event structure (global, heap, stack) has absolutely NO influence on what events are returned by SDL_PollEvent. And since there is (in a well written application) no need to keep the event structure around after you've processed it, option three is the best.

Note: 'making' the structure is trivial. It will take no additional time (as you'll probably have other stack variables as well) and take up a negliable amount of stack space.

Share this post


Link to post
Share on other sites
In general you will be polling events in a centralised location, and handing events to any objects that need them (or using some kind of callback system, mapping events to functions).

If you aren't sure what you are doing, then I would advise against having more than a single place where you poll events unless you can come up with a good explanation.

As for your question, option #3 is best, in general.

Share this post


Link to post
Share on other sites
I'm going by this article. I'm quite interested in this seperation of states the way it's done here. It's hard to tell whether a gamestate should be checking for events, or whether I should poll the events already in the engine and then pass them on. I think you amy be right, and the latter may be the best, because in that case I can make sure that SDL_QUIT will always do what I want.

Share this post


Link to post
Share on other sites
In this case, it is probably ok to poll in each specific state, because (presumably) only one state will be "active" and receiving events at a time.

The alternative is to slightly modify the code in the article:

// main loop
while ( game.Running() )
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
// modify this function to take a const SDL_Event reference
game.HandleEvent(event);
}
game.Update();
game.Draw();
}



This reduces the amount of code duplication (you don't need to write the PollEvent loop in every derived state). In addition, you could handle important events - like perhaps SDL_QUIT - in one place.

Share this post


Link to post
Share on other sites

This topic is 3463 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