Reputation: 1021
Say I have this simple event handling code and I want to get the X & Y value of a left click:
SDL_Event = event;
SDL_PollEvent(&event); //get the event
switch (event.type) {
case SDL_QUIT://if the X is pressed then end the game
isRunning = false;
break;
case SDL_MOUSEBUTTONDOWN:
int x, y;
if (event.button.button == SDL_BUTTON_LEFT) {
SDL_GetMouseState(&x, &y);
std::cout << "Xpos is: " << x << std::endl;
std::cout << "Ypos is: " << y << std::endl;
}
default:
break;
}
Why do I have to call .button
twice on event.button.button
to be able to read the X and Y coordinate?
Upvotes: 2
Views: 877
Reputation: 3025
SDL_Event
is defined as a union, so saying event.button
is the way you access the event data as an SDL_MouseButtonEvent
. Then, SDL_MouseButtonEvent::button
gives you the actual mouse button index.
You could clarify this a bit like so:
SDL_Event = event;
SDL_PollEvent(&event); //get the event
switch (event.type) {
. . .
case SDL_MOUSEBUTTONDOWN:
{
const SDL_MouseButtonEvent &mouse_event = event.button;
int x, y;
if (mouse_event.button == SDL_BUTTON_LEFT) {
SDL_GetMouseState(&x, &y);
std::cout << "Xpos is: " << x << std::endl;
std::cout << "Ypos is: " << y << std::endl;
}
break;
}
. . .
}
Upvotes: 1
Reputation: 52167
SDL_Event
is a big 'ole union:
typedef union SDL_Event
{
Uint32 type; /**< Event type, shared with all events */
SDL_CommonEvent common; /**< Common event data */
SDL_DisplayEvent display; /**< Window event data */
SDL_WindowEvent window; /**< Window event data */
SDL_KeyboardEvent key; /**< Keyboard event data */
SDL_TextEditingEvent edit; /**< Text editing event data */
SDL_TextInputEvent text; /**< Text input event data */
SDL_MouseMotionEvent motion; /**< Mouse motion event data */
SDL_MouseButtonEvent button; /**< Mouse button event data */
SDL_MouseWheelEvent wheel; /**< Mouse wheel event data */
SDL_JoyAxisEvent jaxis; /**< Joystick axis event data */
SDL_JoyBallEvent jball; /**< Joystick ball event data */
SDL_JoyHatEvent jhat; /**< Joystick hat event data */
SDL_JoyButtonEvent jbutton; /**< Joystick button event data */
SDL_JoyDeviceEvent jdevice; /**< Joystick device change event data */
SDL_ControllerAxisEvent caxis; /**< Game Controller axis event data */
SDL_ControllerButtonEvent cbutton; /**< Game Controller button event data */
SDL_ControllerDeviceEvent cdevice; /**< Game Controller device event data */
SDL_AudioDeviceEvent adevice; /**< Audio device event data */
SDL_SensorEvent sensor; /**< Sensor event data */
SDL_QuitEvent quit; /**< Quit request event data */
SDL_UserEvent user; /**< Custom event data */
SDL_SysWMEvent syswm; /**< System dependent window event data */
SDL_TouchFingerEvent tfinger; /**< Touch finger event data */
SDL_MultiGestureEvent mgesture; /**< Gesture event data */
SDL_DollarGestureEvent dgesture; /**< Gesture event data */
SDL_DropEvent drop; /**< Drag and drop event data */
/* This is necessary for ABI compatibility between Visual C++ and GCC
Visual C++ will respect the push pack pragma and use 52 bytes for
this structure, and GCC will use the alignment of the largest datatype
within the union, which is 8 bytes.
So... we'll add padding to force the size to be 56 bytes for both.
*/
Uint8 padding[56];
} SDL_Event;
The first button
selects the SDL_MouseButtonEvent
member:
typedef struct SDL_MouseButtonEvent
{
Uint32 type; /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
Uint32 timestamp; /**< In milliseconds, populated using SDL_GetTicks() */
Uint32 windowID; /**< The window with mouse focus, if any */
Uint32 which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
Uint8 button; /**< The mouse button index */
Uint8 state; /**< ::SDL_PRESSED or ::SDL_RELEASED */
Uint8 clicks; /**< 1 for single-click, 2 for double-click, etc. */
Uint8 padding1;
Sint32 x; /**< X coordinate, relative to window */
Sint32 y; /**< Y coordinate, relative to window */
} SDL_MouseButtonEvent;
The second button
is the actual mouse button index in SDL_MouseButtonEvent
.
Upvotes: 4