即使在初始化失败后我也应该使用 SDL_Quit() 吗?

Should I use SDL_Quit() even after a failed initialisation?

使用 SDL2.0 时,如果 SDL 无法创建 window 或渲染器,不使用 SDL_Quit() 停止程序是否合理?

例如,我可以写(在 SDL 初始化、window 和渲染器创建之后):

if (!renderer)
{
    fprintf("Error while creating the renderer: %s\n", SDL_GetError());
    return -1;
}

SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;

或者我应该写成:

if (!renderer)
{
    fprintf("Error while creating the renderer: %s\n", SDL_GetError());
    SDL_DestroyWindow(window);
    SDL_Quit();
    return -1;
}

SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;

我认为第二种选择更好,但我不确定关闭 SDL 并销毁 window 是否真的有用,即使未创建渲染器。

来自 the SDL Wiki :

You should call it upon all exit conditions.

因此,假设您之前调用了 SDL_Init,这是使用任何其他 SDL 函数所必需的,那么无论如何您都应该调用 SDL_Quit

此外,来自同一页面:

It is safe to call this function even in the case of errors in initialization.

这意味着您甚至不必担心调用错误。

为方便起见,您可以将 SDL_Quitatexit 一起使用,但这更多是个人喜好问题。