欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > SDL事件相关

SDL事件相关

2025/4/20 5:11:59 来源:https://blog.csdn.net/weixin_52243202/article/details/143616284  浏览:    关键词:SDL事件相关

文章目录

    • 事件相关的函数和数据结构
      • 用户自定义事件
      • 代码相关:

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

  • SDL_WaitEvent :等待一个事件
  • SDL_PushEvent 发送一个事件
  • SDL_PumpEvents(): 将硬件设备产生的时间放入事件队列 ,用于读取事件,在调用该函数之前,必须调用SDL_PumpEVents搜集键盘等事件
  • SDL_PeepEvents(): 从事件队列提取一个事件
    -数据结构 SDL_Event :代表一个事件

可以监控到的事件在SDL_events.h文件里面可以找到
在这里插入图片描述

用户自定义事件

#define FF_QUIT_EVENT (SDL_USEREVENT + 2) // 用户自定义事件
在这里插入图片描述

代码相关:

TEMPLATE = app
CONFIG += console thread
CONFIG -= app_bundle
CONFIG -= qtSOURCES += \main.cINCLUDEPATH += \$$PWD/../SDL2-2.0.10/include/LIBS += \$$PWD/../SDL2-2.0.10/lib/x86/SDL2.lib

main.c

#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 pointerSDL_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 titleSDL_WINDOWPOS_UNDEFINED, // initial x positionSDL_WINDOWPOS_UNDEFINED, // initial y position640, // width, in pixels480, // height, in pixelsSDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS // flags - see below);// Check that the window was successfully createdif (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 rendererif (renderer)SDL_DestroyRenderer(renderer);// Close and destroy the windowif (window)SDL_DestroyWindow(window);// Clean upSDL_Quit();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词