Jump to content
  • Advertisement
Sign in to follow this  
DeadXorAlive

SDL user events?

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

How would I go about using the SDL_Event structure for my own events? I have come this far in that I can send messages, but am stuck in how to use the provided void pointers (see below) for transferring data. My intend is to use these as parameters. Is this a good (enough) idea anyway, or will it be 'unsafe'? Example taken from the docs:
//This is how to push your own message on the queue:
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = my_event_code;
event.user.data1 = significant_data; // This is where I'm stuck, so far only crashes...
event.user.data2 = 0;
SDL_PushEvent(&event);

//And the definition of the struct to use for user events is:
typedef struct{
  Uint8 type;
  int code;
  void *data1;
  void *data2;
} SDL_UserEvent;

Share this post


Link to post
Share on other sites
Advertisement
You need to provide a pointer to some object of yours. If you're doing as it seems you're doing, you're passing a pointer to an object on the stack which becomes void when the function exits. What you should be doing instead is to pass a heap-allocated (or otherwise 'safe' pointer along).

When you consume your event, you can then safely cast it back to a pointer of your known type and carry on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!