SDL窗口在启动时关闭并返回0

时间:2023-01-29 12:08:31

The window closes as soon as i start the program. this is the main function:

一旦我启动程序,窗口就会关闭。这是主要功能:

int main(int argc, char* argv[])
{
    if (!init())
    {
        printf("Could not initialize!");
    }
    else
    {
        bool quit = false;
        SDL_Event ev;

        while(!quit)
        {
            while(SDL_PollEvent(&ev))
            {
                if(ev.type = SDL_QUIT)
                {
                    quit = true;
                }
            }
        }
    }
    close();
    return 0;
}

Adding printf() statements narrowed it down to this section

添加printf()语句将其缩小到本节

while(SDL_PollEvent(&ev))
{
    if(ev.type = SDL_QUIT)
    {
        quit = true;
    }
}

If i change while(SDL_PollEvent(&ev)) to while(!SDL_PollEvent(&ev)) or while(SDL_PollEvent(&ev) != 0) the window stays open but closes as soon as i mouse over it or try to move it.

如果我将while(SDL_PollEvent(&ev))更改为while(!SDL_PollEvent(&ev))或while(SDL_PollEvent(&ev)!= 0),则窗口保持打开状态,但只要将鼠标悬停在其上或尝试移动它就会关闭。

the SDL documentation says that SDL_PollEvent only returns 1 (true) if there is a pending event, and since the program returns 0 it seems like SDL_PollEvent must have returned 1 somehow and also that ev.type was set to SDL_QUIT without clicking the X button, which I find unlikely. So I probably did something wrong but I can't figure out what it is, and I have been trying to find a solution.

SDL文档说如果有挂起事件,SDL_PollEvent只返回1(true),并且由于程序返回0,似乎SDL_PollEvent必须以某种方式返回1并且ev.type被设置为SDL_QUIT而不单击X按钮,我觉得不太可能。所以我可能做错了但我无法弄清楚它是什么,我一直在努力寻找解决方案。

Also, here is the init() function.

此外,这是init()函数。

bool init()
{
    bool success = true;
    if( SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL failed to initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        window = SDL_CreateWindow("Image Encrypter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                  SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if(window == NULL)
        {
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            screenSurface = SDL_GetWindowSurface(window);
            if(screenSurface == NULL)
            {
                printf("Screen surface could not be created! SDL Error: %s\n", SDL_GetError());
            }
        }
    }
    return success;
}

The console does not output any of the printf statements in the init() function, so I don't think that's where the problem is.

控制台不会在init()函数中输出任何printf语句,因此我认为这不是问题所在。

1 个解决方案

#1


A common error here:

这里常见的错误:

if(ev.type = SDL_QUIT)

- this is an assignment, not a comparison. The first version of your code should work then.

- 这是一项任务,而非比较。您的代码的第一个版本应该可以工作。

#1


A common error here:

这里常见的错误:

if(ev.type = SDL_QUIT)

- this is an assignment, not a comparison. The first version of your code should work then.

- 这是一项任务,而非比较。您的代码的第一个版本应该可以工作。