Window 关闭太快

Window closes too fast

所以我希望这段代码创建一个 window 上面有一个图像 (hello world) 然后在 5 秒后使用 SDL2 退出,visual studio,和 C++。我认为这应该很简单。我写出了代码,当我构建它时没有错误,但问题是 window 一创建就退出。我最初认为添加 SDL_Delay(5000) 会产生预期的效果,但我想它不会那样工作。谁能告诉我这是为什么?

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

bool init();
bool load_media();
void close();
const int s_height = 300;
const int s_width = 400;

SDL_Window* new_window = NULL;
SDL_Surface* new_surface = NULL;
SDL_Surface* new_image = NULL;

using namespace std;

bool init()
{
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        cout << "couldn't initialize" << endl;
        success = false;
    }
    else
    {
        new_window = SDL_CreateWindow(
            "SDL Tutorial 2", 
            SDL_WINDOWPOS_UNDEFINED, 
            SDL_WINDOWPOS_UNDEFINED, 
            s_width, 
            s_height, 
            SDL_WINDOW_SHOWN);
        if (new_window == NULL)
        {
            cout << "there's no window" << endl;
            success = false;
        }
        else
        {
            new_surface = SDL_GetWindowSurface(new_window);
        }

    }
    return success;
}

bool load_media()
{
    bool success = true;
    new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
    if (new_image == NULL)
    {
        cout << "couldn't load image" << endl;
        success = false;
    }
    return success;
}

void close()
{
    SDL_FreeSurface(new_image);
    SDL_DestroyWindow(new_window);
    SDL_Quit;

}


int main(int argc, char *argv[])
{
    if (!init())
    {
        cout << "FAILED to Initialize!" << endl;
        if (!load_media())
        {
            cout << "FAILED to Load Media!" << endl;
        }
        else
        {
            SDL_BlitSurface(new_image, NULL, new_surface, NULL);
            SDL_UpdateWindowSurface(new_window);
            SDL_Delay(5000);
            SDL_Quit;
        }
    }

close();
return 0;
}

您只初始化了视频子系统;如果要使用 SDL_Delay 和其他与时间相关的功能,则需要初始化 Timer 子系统。

将您的 SDL_Init(SDL_INIT_VIDEO) 更改为 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)

你的第一行应该是...

if (init())

调用 'if (!init())' 仅当 init() 失败时才继续执行代码的其余部分。 我认为 window 短暂出现是因为在 init() 中创建了 window,但是您的其余代码(包括计时)被跳过并且 window 立即关闭。

我会修改为:

int main(int argc, char* argv[])
{
    if( init()) {

        //other code executes if init() is successful
        SDL_Delay(5000);
    }

    else {
        cout << "Failed to initialize!";
    }

    close();
    return 0;
}

这里有有效的代码,以及来自评论和其他答案的所有建议:

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

int const s_height = 300;
int const s_width  = 400;

SDL_Window  *new_window  = NULL;
SDL_Surface *new_surface = NULL;
SDL_Surface *new_image   = NULL;

using namespace std;

bool init(void) {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
        cout << "couldn't initialize" << endl;
        return false;
    }
    new_window = SDL_CreateWindow("SDL Tutorial 2", 
                                  SDL_WINDOWPOS_UNDEFINED, 
                                  SDL_WINDOWPOS_UNDEFINED, 
                                  s_width, 
                                  s_height, 
                                  SDL_WINDOW_SHOWN
    );
    if (new_window == NULL) {
        cout << "there's no window" << endl;
        return false;
    }

    new_surface = SDL_GetWindowSurface(new_window);
    if (new_surface == NULL) {
        cout << "there's no surface" << endl;
        return false;
    }
    return true;
}

bool load_media(void) {
    new_image = SDL_LoadBMP("SDL Tutorial 2/hello_world.bmp");
    if (new_image == NULL) {
        cout << "couldn't load image" << endl;
        return false;
    }
    return true;
}

void finish(void) {
    if (new_image) {
        SDL_FreeSurface(new_image);
    }
    if (new_window) {
        SDL_DestroyWindow(new_window);
    }
    SDL_Quit();
}

int main(int argc, char *argv[]) {
    if (init()) {
        if (load_media()) {
            SDL_BlitSurface(new_image, NULL, new_surface, NULL);
            SDL_UpdateWindowSurface(new_window);
            SDL_Delay(5000);
            finish();
        } else {
            cout << "FAILED to Load Media!" << endl;
        }
    } else {
        cout << "FAILED to Initialize!" << endl;
    }
    return 0;
}