文章目录
事件相关的函数和数据结构
- SDL_WaitEvent :等待一个事件
- SDL_PushEvent 发送一个事件
- SDL_PumpEvents(): 将硬件设备产生的时间放入事件队列 ,用于读取事件,在调用该函数之前,必须调用SDL_PumpEVents搜集键盘等事件
- SDL_PeepEvents(): 从事件队列提取一个事件
-数据结构 SDL_Event :代表一个事件
可以监控到的事件在SDL_events.h文件里面可以找到
用户自定义事件
#define FF_QUIT_EVENT (SDL_USEREVENT + 2) // 用户自定义事件
代码相关:
cpp
TEMPLATE = app
CONFIG += console thread
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.c
INCLUDEPATH += \
$$PWD/../SDL2-2.0.10/include/
LIBS += \
$$PWD/../SDL2-2.0.10/lib/x86/SDL2.lib
main.c
cpp
#include <SDL.h>
#include <stdio.h>
#define FF_QUIT_EVENT (SDL_USEREVENT + 2) // 用户自定义事件
#undef main
int main(int argc, char *argv[]) {
SDL_Window *window = NULL; // Declare a pointer
SDL_Renderer *renderer = NULL;
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
/* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
renderer = SDL_CreateRenderer(window, -1, 0);
/* Select the color for drawing. It is set to red here. */
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
/* Clear the entire screen to our selected color. */
SDL_RenderClear(renderer);
/* Up until now everything was drawn behind the scenes.
This will show the new, red contents of the window. */
SDL_RenderPresent(renderer);
SDL_Event event;
int b_exit = 0;
for (;;) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN: /* 键盘事件 */
switch (event.key.keysym.sym) {
case SDLK_a:
printf("key down a\n");
break;
case SDLK_s:
printf("key down s\n");
break;
case SDLK_d:
printf("key down d\n");
break;
case SDLK_q:
printf("key down q and push quit event\n");
SDL_Event event_q;
event_q.type = FF_QUIT_EVENT;
SDL_PushEvent(&event_q);
break;
default:
printf("key down 0x%x\n", event.key.keysym.sym);
break;
}
break;
case SDL_MOUSEBUTTONDOWN: /* 鼠标按下事件 */
if (event.button.button == SDL_BUTTON_LEFT) {
printf("mouse down left\n");
} else if (event.button.button == SDL_BUTTON_RIGHT) {
printf("mouse down right\n");
} else {
printf("mouse down %d\n", event.button.button);
}
break;
case SDL_MOUSEMOTION: /* 鼠标移动事件 */
printf("mouse movie (%d,%d)\n", event.button.x, event.button.y);
break;
case FF_QUIT_EVENT:
//如果是收到自定义的退出信号 则退出接受事件的循环
printf("receive quit event\n");
b_exit = 1;
break;
}
if (b_exit)
break;
}
// destory renderer
if (renderer)
SDL_DestroyRenderer(renderer);
// Close and destroy the window
if (window)
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}