简体   繁体   English

SDL2 window 不会立即关闭?

[英]SDL2 window doesn't immediately close?

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>

const int width = 800, height = 600;

int main(int argc, char* argv[]) 
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        std::cout << "SDL couldn't initialise! Error: " << SDL_GetError() << std::endl;
    }

    SDL_Window* window = SDL_CreateWindow("New Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_ALLOW_HIGHDPI);

    if (window == NULL) 
    {
        std::cout << "Window couldn't be created! Error: " << SDL_GetError() << std::endl;
        return EXIT_FAILURE;
    }

    SDL_Event windowEvent;

    while (true)
    {
        if (SDL_PollEvent(&windowEvent))
        {
            if (SDL_QUIT == windowEvent.type)
            {
                break;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

So in this little program, how does the window stay open?那么在这个小程序中,window 是如何保持打开状态的呢? I have experimented what keeps it open and found out that it's this part:我已经尝试了保持它打开的方法,并发现它是这部分:

    while (true)
    {
        if (SDL_PollEvent(&windowEvent))
        {
            if (SDL_QUIT == windowEvent.type)
            {
                break;
            }
        }
    }

More specifically, it's the fact that the loop loops while "true".更具体地说,循环在“真”时循环。 But I don't get what's true nor how it makes it loop.但我不明白什么是真的,也不知道它是如何循环的。 Can someone explain how it works?有人可以解释它是如何工作的吗? I'd highly appreciate it if someone basically explained what everything here is step by step.如果有人基本上逐步解释了这里的所有内容,我将不胜感激。 (eg what "SDL_Window" is, what SDL_Event is etc.) (例如“SDL_Window”是什么,SDL_Event 是什么等)

Unless I'm reading the question wrong, this seems to be a very very general c++ language question mixed with an SDL question.除非我读错了问题,否则这似乎是一个非常非常笼统的 c++ 语言问题和一个 SDL 问题。 I'd just note here, SDL requires adequate knowledge of c++ so you may hit more hurdles very soon if you aren't confident.我只想在这里指出,SDL 需要对 c++ 有足够的了解,因此如果您没有信心,可能很快就会遇到更多障碍。

Anyway, in c++ a while(expression) {... } loop will continue to loop while the 'expression' in the brackets evaluate to true.无论如何,在 c++ 中,while(expression) {... } 循环将继续循环,而括号中的“表达式”评估为真。 I'd you want to loop forever, you can just force the expression to true by writing this: while(true) {... }.如果你想永远循环,你可以通过写这个来强制表达式为真:while(true) {...}。 The o ly way to exit from this loop is to call the "break" command from inside the loop.退出这个循环的唯一方法是从循环内部调用“break”命令。 Once the program has broken out of the loop, the program will reach the main "return 0;"一旦程序跳出循环,程序将到达主“return 0;”。 and exit safely.并安全退出。

As for this line:至于这条线:

if (SDL_PollEvent(&windowEvent))

Every time this line is ran, SDL checks if there is any "event" ready for processing.每次运行此行时,SDL 都会检查是否有任何“事件”准备好进行处理。 An event is a "message" sent from SDL or your operating system TO your application to get your application to do something.事件是从 SDL 或您的操作系统发送到您的应用程序的“消息”,以使您的应用程序执行某些操作。 In your code, you are checking "IE polling" for a window event.在您的代码中,您正在检查 window 事件的“IE 轮询”。 To be clear, when the user(you) clicks a windows close button, minimise, maximises, resizes the window, etc etc, those events will be sent to your application.需要明确的是,当用户(您)单击 windows 关闭按钮、最小化、最大化、调整 window 的大小等时,这些事件将被发送到您的应用程序。 This line will return true when a one of those messages has been received.当收到其中一条消息时,此行将返回 true。 It's your job to figure out what the event is and what you want to do with it.弄清楚事件是什么以及你想用它做什么是你的工作。

These lines:这些行:

if (SDL_QUIT == windowEvent.type)
{
    break;
}

Will check the type of event received.将检查收到的事件类型 If the event type is the SDL_QUIT (IE user pressed the close button on the window), then the "break" command will be executed.如果事件类型是 SDL_QUIT(IE 用户按下窗口上的关闭按钮),则“break”命令将被执行。 As mentioned earlier, if the break is called, it will exit from the loop, and the application will close.如前所述,如果调用了break,它将退出循环,应用程序将关闭。

So in summary, your code will enter a infinite for loop due to its expression always evaluating to true (true == true).所以总而言之,你的代码将进入一个无限循环,因为它的表达式总是评估为真(真 ==真)。 Inside the loop we check if we got a new event.在循环中,我们检查是否有新事件。 If we did get an event, figure out what it is.如果我们确实得到了一个事件,请弄清楚它是什么。 If it's a SDL_QUIT, then break from the loop.如果它是 SDL_QUIT,则从循环中中断。 If we *didnt" get an event, then just re-loop and check for an event again.如果我们“*没有”得到一个事件,那么只需重新循环并再次检查一个事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM