SDL user events?

Started by
1 comment, last by DeadXorAlive 18 years, 2 months ago
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;

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.
Thanks a lot evolutional, that was exactly what I was doing wrong.

This topic is closed to new replies.

Advertisement