前言
如果我们的目的是以写应用或游戏的话,那我写的第一篇Hello world SDL已经创建了一个窗口,应用的第一步就有了进展,作为游戏的第二个很重要的部分就是控制,输入控制。这次我们来捕获我们的鼠标键盘消息。
接着上一篇文章,我们把主要的框架留下来:
#include <iostream>
#include "SDL2/include/SDL.h"
const int screen_width = 800;
const int screen_height = 600;
int main(int, char**)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("YourGame",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screen_width, screen_height,
SDL_WINDOW_SHOWN);
SDL_Surface* window_surface = SDL_GetWindowSurface(window);
bool sdl_quit = false;
// TODO(yourtayu@gmail.com): Write SDL event processing logic here.
// ...
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
加上我们处理Event的逻辑代码:
SDL_Event sdl_event;
bool sdl_quit = false;
while (!sdl_quit)
{
while (SDL_PollEvent(&sdl_event))
{

本文深入讲解了如何在SDL中实现鼠标和键盘事件的捕获,包括事件处理逻辑和相关代码示例。通过实例演示了如何响应鼠标点击和键盘按键事件,并解释了SDL_PollEvent和SDL_PumpEvents的功能及使用条件。
最低0.47元/天 解锁文章
1006





