流媒体开发13SDL事件响应
Posted 叮咚咕噜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了流媒体开发13SDL事件响应相关的知识,希望对你有一定的参考价值。
SDL事件的响应,先给大家一个直观上的理解,通过事件响应我们可以获取到当前键盘敲击的键值,可以获取到鼠标在窗口的位置,可以获取到鼠标当前按下的是左键还是右键等;功能还是比较强大的,ffplay就是使用的SDL来实现一些播放快捷键的。
基本介绍
- 函数:
- SDL_WaitEvent():等待一个事件
- SDL_PushEvent():发送一个事件
- SDL_PumpEvents():将硬件设备产生的事件放入事件队列,用于读取事件,在调用该函数之前,必须调用SDL_PumpEvents搜集键盘等事件
- SDL_PeepEvents():从事件队列提取一个事件
本节主要介绍前面两个函数的使用
- 数据结构:
- SDL_Event:代表一个事件
- 补充:
- 可以查看头文件的中枚举SDL_EventType,了解支持的响应事件
- 并且我们用户层可以定义自己的响应事件(SDL_USEREVENT+n),只要枚举值不超过0xFFFF即可(下面demo有实现,可以参考)
代码运行结果
- 可以根据下面执行结果看出 ,打印出了鼠标的位置,键盘按下的键值,以及鼠标点击情况
- 右侧是创建的SDL窗口,左侧是事件响应打印的一些调试信息
代码实现
#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;
}
以上是关于流媒体开发13SDL事件响应的主要内容,如果未能解决你的问题,请参考以下文章