SDL_PollEvent不起作用

SDL_PollEvent does not work

本文关键字:不起作用 PollEvent SDL      更新时间:2023-10-16

我正在学习本教程,介绍游戏开发中的 SDL。程序在查找输入时应离开主循环,但它不会:

SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gXOut = NULL;
int main(int argc, char* argv[]) {
    if (!init()){
        printf("Failed to initialize.n");
    }
    else {
        //Load media
        if (!loadMedia()){
            printf("Failed to load media.n");
        }
        else {
            //Main loop flag
            bool quit = false;
            //Event handler
            SDL_Event e;
            //While application is runnig
            while (!quit){
                //Handles events on queue
                while (SDL_PollEvent(&e) != 0){
                    //User requests quit
                    if (e.type == SDL_QUIT){
                        quit = true;
                    }
                }
                //Aply the image
                SDL_BlitSurface(gXOut, NULL, gScreenSurface, NULL);
                //Update the surface
                SDL_UpdateWindowSurface(gWindow);
            }
        }
    }
    //Free resources and close SDL
    close();
    return 0;

我尝试为SDL_KEYPRESSED更改SDL_QUIT,但它也不起作用。知道为什么吗?

SDL2 中不存在SDL_KEYPRESSED。如果要使用键盘输入退出程序,请在循环中使用SDL_KEYDOWN或SDL_KEYUP,如下所示:

while (SDL_PollEvent(&e) != 0){
    if (e.type == SDL_KEYDOWN){  // or SDL_KEYUP
       quit = true;
    }

请查看维基页面。

以下是代码查找您要实现的目标的方式:

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char* args[])
{
    //Main loop flag
    bool quit = false;
    //Event handler
    SDL_Event e;
    SDL_Surface* gXOut = NULL;
    //The window we'll be rendering to
    SDL_Window* window = NULL;
    //The surface contained by the window
    SDL_Surface* screenSurface = NULL;
    //Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %sn",                 SDL_GetError());
    }
    else
    {
        //Create window
        window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL)
        {
            printf("Window could not be created! SDL_Error: %sn",         SDL_GetError());
        }
        else
        {
            //Get window surface
            screenSurface = SDL_GetWindowSurface(window);
            //Fill the surface white
            SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
            //Update the surface
            SDL_UpdateWindowSurface(window);
            //Wait two seconds
            SDL_Delay(2000);
        }
    }
        //While application is running
        while (!quit)
        {
                //Handle events on queue
                while (SDL_PollEvent(&e) != 0)
                {
                    //User requests quit
                    if (e.type == SDL_QUIT) //can change the SD_QUIT event to whatever you want to customize with.
                    {
                        quit = true;
                    }
                }
                //Apply the image
                SDL_BlitSurface(gXOut, NULL, screenSurface, NULL);
            //Update the surface
            SDL_UpdateWindowSurface(window);
        }
    //Destroy window
    SDL_DestroyWindow(window);
    //Quit SDL subsystems
    SDL_Quit();
    return 0;
}

SDL_QUIT 在按下窗口上的 X 时接受输入,或者如果您想在应用程序中使用其他键退出,请将键更改为其他键。