双'button'SDL_Event会员权限?

Double 'button' SDL_Event member access?

假设我有这个简单的事件处理代码,我想获得左键单击的 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;
}

为什么我必须在 event.button.button 上调用 .button 两次才能读取 X 和 Y 坐标?

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;

第一个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;

第二个buttonSDL_MouseButtonEvent中的实际鼠标按键索引。

SDL_Eventdefined 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;
    }
    . . .
}