Re: accessing C union members...
- Posted by gbonvehi Jan 10, 2013
- 1092 views
Adding to what Matt said:
The union type tells basically that it can contain any of the types specified in it's declaration. Don't get confused, it will only hold one value at a time, either a type (Uint8), a key (SDL_KeyboardEvent), a jaxis (SDL_JoyAxisEvent), etc.
A Uint8 type uses 1 byte in memory (8-bits). Knowing this and that the initial offset is 0, you can calculate the offsets you will need for each var in the event structure:
typedef struct{ Uint8 type; Uint8 which; Uint8 button; Uint8 state; Uint16 x, y; } SDL_MouseButtonEvent;
peek(sdl_event) -- type peek(sdl_event+1) -- which peek(sdl_event+2) -- button peek(sdl_event+3) -- state peek2u(sdl_event+4) -- x peek2u(sdl_event+6) -- y (Uint16 takes 2 bytes in memory)
Cheers,
Guillermo