C++ SDL2事件处理 C++ SDL2事件处理
配置请参照前面的笔记https://www.cnblogs.com/zzr-stdio/p/14514043.html
这里主要介绍SDL_PollEvent函数,以及SDL_Event的结构数据
示例程序:
#include <iostream>
#include<SDL.h>
#include<SDL_image.h>
using namespace std;
int main(int argc, char* argv[])
{
::SDL_Init(SDL_INIT_VIDEO);//初始化SDL
::SDL_Window* window = ::SDL_CreateWindow("SDL test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN);//创建窗体
::SDL_Surface* surface = ::SDL_GetWindowSurface(window);//得到窗体的SDL_Surface
//::SDL_Surface* image = ::SDL_LoadBMP("r.bmp");//加载图片
::SDL_Surface* image = ::IMG_Load("2.png");//加载png图片
::SDL_Rect rect;
rect.x = 100;//显示位置
rect.y = 0;
bool quit = false;
::SDL_Event event;
while (quit == false)
{
while (::SDL_PollEvent(&event))//对当前待处理事件进行轮询
{
if (event.type == ::SDL_QUIT)//退出事件
{
quit = true;
}
else if (event.type == SDL_MOUSEBUTTONDOWN) //鼠标按下事件
{
if (event.button.button == SDL_BUTTON_LEFT)//判断是鼠标左键
{
cout << "左" << endl;
}
else if (event.button.button == SDL_BUTTON_RIGHT)//判断是鼠标右键
{
cout << "右" << endl;
}
}
else if (event.type == SDL_KEYDOWN)//键盘事件
{
cout << (char)event.key.keysym.sym << endl;//输出键盘字符
}
else if(event.type == SDL_DROPFILE)//文件拖动事件
{
image = ::IMG_Load(event.drop.file);
cout << event.drop.file << endl;
}
}
::SDL_FillRect(surface, nullptr, 0);
::SDL_BlitSurface(image, nullptr, surface, &rect);//把图片贴到窗体上
::SDL_UpdateWindowSurface(window);//刷新窗口,不调用则显示不了图片。
}
::SDL_DestroyWindow(window);//销毁窗体
::SDL_Quit();//退出SDL
return 0;
}
SDL_Event的定义为:
typedef union SDL_Event
{
Uint32 type; //事件类型
SDL_CommonEvent common; /**< Common event data */
SDL_DisplayEvent display; /**< Display event data */
SDL_WindowEvent window; /**< Window event data */
SDL_KeyboardEvent key; //键盘事件,键盘按下和释放
SDL_TextEditingEvent edit; /**< Text editing event data */
SDL_TextInputEvent text; /**< Text input event data */
SDL_MouseMotionEvent motion; //鼠标移动事件
SDL_MouseButtonEvent button; //鼠标按键事件
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_ControllerTouchpadEvent ctouchpad; /**< Game Controller touchpad event data */
SDL_ControllerSensorEvent csensor; /**< Game Controller sensor event data */
SDL_AudioDeviceEvent adevice; /**< Audio device event data */
SDL_SensorEvent sensor; /**< Sensor event data */
SDL_QuitEvent quit; //退出事件
SDL_UserEvent user; //用户自定义事件
SDL_SysWMEvent syswm; //平台相关的系统事件
SDL_TouchFingerEvent tfinger; /**< Touch finger event data */
SDL_MultiGestureEvent mgesture; /**< Gesture event data */
SDL_DollarGestureEvent dgesture; /**< Gesture event data */
SDL_DropEvent drop; //拖拽事件
/* 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;