SDL_Event union is blowing my mind

Hi all,
I’m a relatively new SDL programmer (hell, I’m a relatively new
programmer), and I gotta say that SDL rocks. I’ve also gotta say that
the SDL_Event union is confusing the hell out of me. It’s not really
a practical question, just a conceptual one, that must be answered if
my sanity is to be preserved. The SDL_Event union is a union of all
the even structures in SDL, and a Uint8 type. Whenever you want to
check what type of event is in a given SDL_Event union, you check to
see what event.type is equal to. And then, you can do stuff like
using the value in event.key.keysym.sym and such. My question is, how
is that possible??? If SDL_Event is a union, it should be able to
hold information in either Uint8 type, or the keyboard structure, but
not both!!! At least not at the same time. If somebody could take the
time to enlighten a noob, I would be grateful for eternity.

Peace,
=Pete=

Peter wrote:

My question is, how is that possible???

Through a crude but useful hack :slight_smile: (IMHO)

If SDL_Event is a union, it should be able to hold
information in either Uint8 type, or the keyboard structure, but
not both!!! At least not at the same time.

Yep… so they ensure that the ‘type’ member is also represented in each
of the union’s other possible states. If you look through all the
structs like SDL_ActiveEvent, SDL_KeyboardEvent, SDL_MouseMotionEvent
and so on, their first member is a Uint8 also called type. So you know
that first byte has always got an event type in it.

Why do this? I’m guessing that the ‘logical’ alternative (to have had
SDL_Event as a struct containing the type plus a union for the
type-specific state) would have been a little bit more awkward because
you would have had to specify the union’s name in every operation that
used anything but the event type.–
Kylotan
http://pages.eidosnet.co.uk/kylotan

Every individual struct in that union also begins with a “Uint8 type”,
so you can look at the union from the perspective of any of the structs
or as a plain Uint8 and the type will always be there.

ex.

struct foo { int x; };
union fum { foo a; int y; }

fum u;
u.y = 42;
assert(u.a.x == 42);On Sun, Oct 20, 2002 at 09:33:30PM +0000, Peter wrote:

using the value in event.key.keysym.sym and such. My question is, how
is that possible??? If SDL_Event is a union, it should be able to
hold information in either Uint8 type, or the keyboard structure, but
not both!!! At least not at the same time. If somebody could take the


Glenn Maynard

Hi all,
I’m a relatively new SDL programmer (hell, I’m a relatively new
programmer), and I gotta say that SDL rocks. I’ve also gotta say that
the SDL_Event union is confusing the hell out of me. It’s not really
a practical question, just a conceptual one, that must be answered if
my sanity is to be preserved. The SDL_Event union is a union of all
the even structures in SDL, and a Uint8 type. Whenever you want to
check what type of event is in a given SDL_Event union, you check to
see what event.type is equal to. And then, you can do stuff like
using the value in event.key.keysym.sym and such. My question is, how
is that possible??? If SDL_Event is a union, it should be able to
hold information in either Uint8 type, or the keyboard structure, but
not both!!! At least not at the same time. If somebody could take the
time to enlighten a noob, I would be grateful for eternity.

The first item in the event specific structures is always a Uint8, so
the Unit8 in the SDL_Event maps to the same byte as the Uint8 at the
start of the each of the type specific structures. So, no matter what,
there is always a Uint8 named “type” at the first byte of each
structure.

It always helps to look one level deeper.

	Bob PendletonOn Sun, 2002-10-20 at 16:33, Peter wrote:

Peace,
=Pete=


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-----------------------------------+

This is a pretty standard C programming trick: where a structure can represent
one of several different data structures, you can use a union to encompass
every possible one. The way you tell the difference is that each structure
inside the union starts with a common header, in this case a Uint8, so you’ve
got:

union
{
Uint8 type;
struct { Uint8 type; … } event1;
struct { Uint8 type; … } event2;

}

so however you reference that “type” field it’ll always refer to the same
point in the data, i.e. the start.

If you’ve got any questions on the finer points of C data structure
organisation, you might get a more in-depth (and doubtless nerdier) answer on
the USENET comp.lang.c newsgroup. Also keep a copy of the “C Programming
Language” handy if you’ve not already got it, because it tells you everything
you’ll need to know in a very small number of pages:

http://www.amazon.com/exec/obidos/tg/detail/-/0131103628/

cheers,On Sunday 20 October 2002 22:33, Peter wrote:

My question is, how
is that possible??? If SDL_Event is a union, it should be able to
hold information in either Uint8 type, or the keyboard structure, but
not both!!! At least not at the same time. If somebody could take the
time to enlighten a noob, I would be grateful for eternity.


Matthew > http://www.soup-kitchen.net/
> ICQ 19482073