简体   繁体   中英

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? 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.)

Unless I'm reading the question wrong, this seems to be a very very general c++ language question mixed with an SDL question. I'd just note here, SDL requires adequate knowledge of c++ so you may hit more hurdles very soon if you aren't confident.

Anyway, in c++ a while(expression) {... } loop will continue to loop while the 'expression' in the brackets evaluate to true. I'd you want to loop forever, you can just force the expression to true by writing this: while(true) {... }. The o ly way to exit from this loop is to call the "break" command from inside the loop. Once the program has broken out of the loop, the program will reach the main "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. An event is a "message" sent from SDL or your operating system TO your application to get your application to do something. In your code, you are checking "IE polling" for a window event. 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. This line will return true when a one of those messages has been received. 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. As mentioned earlier, if the break is called, it will exit from the loop, and the application will close.

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. If we *didnt" get an event, then just re-loop and check for an event again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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