说我有这个简单的事件处理代码,我想获得鼠标左键的X&Y值:
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;
}
为什么我必须在.button
上两次调用event.button.button
才能读取X和Y坐标?
答案 0 :(得分:4)
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;
第一个button
选择SDL_MouseButtonEvent
成员:
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;
第二个button
是SDL_MouseButtonEvent
中的实际鼠标按钮索引。
答案 1 :(得分:1)
SDL_Event
是defined as a union,所以说event.button
是将事件数据作为SDL_MouseButtonEvent
访问的方式。然后,SDL_MouseButtonEvent::button
为您提供实际的鼠标按钮索引。
您可以这样澄清一下:
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;
}
. . .
}