我正在使用未定义 SDL_main 的 SDL 函数。这样好吗?

I'm using the SDL functions without the SDL_main be defined. Is that fine?

这是我的代码:

Lib.h

#ifdef ExportLib
    #define Lib __declspec(dllexport)
#else
    #define Lib __declspec(dllimport)
#endif
extern void Lib Launch();

Lib.cpp

#include <SDL/SDL.h>
#include "Lib.h"
void Launch() {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* win = SDL_CreateWindow("Untitle", 100, 100, 400, 400, 0);
    SDL_DestroyWindow(win);
    SDL_Quit();
}

我将此代码构建到静态库中。然后我创建了一个新的源文件并使用了这个库。

main.cpp

#include "Lib.h"

int main() {
    Launch();
    return 0;
}

最后,我使用没有定义 SDL_main 和 SDL 依赖项的静态库编译 main.cpp。效果很好,window 出现了。

但是真的可以吗?我失去了哪些功能?

SDL_main is for SDL's automatic initialization and cleanup. It's mostly so you don't need to do it manually, though it also goes through the effort of properly setting everything up for a windowed application on the platform where it's compiled, but it's fine#defineSDL_MAIN_HANDLED#includeing SDL.h 之前,这将阻止 SDL 将 main 变成宏for SDL_main 只需确保在您自己的代码中正确初始化和退出 SDL。

如果您想确保自己正确地进行了必要的初始化,您可以just check the source code并模拟其中的内容。

编辑:

在某些平台上,如果您不使用 SDL_main,则 SDL_Init will fail。您可以通过在 SDL_Init 之前调用 SDL_SetMainReady 来禁用此失败,但请注意,这将禁用 SDL 的错误处理,如果您在调用 SDL_SetMainReady 后不正确地初始化 SDL,您将无法获得最清晰的错误信息。

退出 SDL 是 much more straightforward(如果您不使用 SDL_main 也需要这样做):

使用完 SDL 后,只需调用 SDL_Quit。这将正确关闭当前处于活动状态的任何 SDL 子系统。

I'm using the SDL functions without the SDL_main be defined. Is that fine?

可能,也可能不会。请改用 SDL_SetMainReady() 以确保:

SDL_SetMainReady: Use this function to circumvent failure of SDL_Init() when not using SDL_main() as an entry point.