SDL事件相关

文章目录

事件相关的函数和数据结构

  • 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;
}
相关推荐
橘子味的茶二1 个月前
SDL线程
sdl
橘子味的茶二1 个月前
SDL渲染器和纹理
sdl
橘子味的茶二1 个月前
SDL打开YUV视频
ffmpeg·音视频·sdl
布丁小站3 个月前
SDL录制音频并播放
音视频·sdl
程序员黄老师3 个月前
Windows下SDL2创建最简单的一个窗口
c++·windows·sdl·sdl2
程序员黄老师3 个月前
Windows与Linux下 SDL2的第一个窗口程序
c++·windows·ffmpeg·sdl
烈日下的奔跑4 个月前
SDL直接渲染yuv视频帧数据
ffmpeg·音视频·sdl
melonbo4 个月前
SDL 与 OpenGL 的关系
opengl·sdl
烈日下的奔跑4 个月前
SDL库自适应窗口大小及遇到的坑
ffmpeg·sdl