据我所知,C中的联合一次只能保存一个值,而且我也不真正理解C中的这段代码为什么有意义,因为event.window不能与event.type同时填充
while(SDL_PollEvent(&event)) {
switch(event.type)
{
case SDL_WINDOWEVENT:
switch(event.window.event)该事件被定义为:
typedef union SDL_Event
{
Uint32 type; /**< Event type, shared with all events */
SDL_CommonEvent common; /**< Common 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_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;发布于 2015-08-31 11:35:22
每个聚合(structs可能)成员SDL_CommonEvent common;,SDL_WindowEvent window;,SDL_KeyboardEvent key;等.union SDL_Event的开头是一些Uint32字段,给出了type,而该公共type字段在每个联合成员中具有相同的地址和大小。
因此,虽然一个联合在内存中同时只包含一个字段(换句话说,所有的联合成员都有相同的地址),但每个成员都以type开头,而event.type是有意义的;它获取的是type。
在C语言中,这种成语是实现有标记的工会的常用方法。
发布于 2015-08-31 11:35:54
SDL_Event联盟的每个成员都以相同的两个成员( Uint32 type和Uint32 timestamp )开始。C标准明确指出,如果一个联合当前持有一个struct类型的值,但将其读取为另一个结构类型,其第一个成员与另一个struct类型相匹配,则可以读取那些匹配的成员。
发布于 2015-08-31 11:36:44
所有其他SDL_X类型都以32位的类型开头。实际上,它们在开始时似乎都包括SDL_CommonEvent中的字段。这是为了便于加入所有子结构的共同要素。然后,通过event.common.x,您可以访问所有公共元素,而不必区分事件的确切类型。
https://stackoverflow.com/questions/32310238
复制相似问题