问题
So, I started using SDL about a year ago, and I just thought SDL_Init initialized SDL's subsytems ( as written here : https://wiki.libsdl.org/SDL_Init ), and that I HAD to call it before anything else. But today, I realized that in a fairly big project, I just forgot to call it, and I never had any problem : everything works perfectly. So I just wonder what it does, since I apparently don't need it to use the library ?
回答1:
SDL_Init
does indeed initialize the SDL's subsystems. In fact, it simply forwards to SDL_InitSubSystem
as you can see in the source code:
int
SDL_Init(Uint32 flags)
{
return SDL_InitSubSystem(flags);
}
Now, if you don't call either SDL_Init
or SDL_InitSubSystem
but don't experience any problems when using subsystems, you might just be lucky. By looking around in the SDL source code, you may find that a lot of functions check whether a resource is initialized before using it, and initialize if possible. For example, SDL_GetTicks
calls SDL_TicksInit
if needed:
Uint32
SDL_GetTicks(void)
{
// [...]
if (!ticks_started) {
SDL_TicksInit();
}
// [...]
}
Similarly, SDL_CreateWindow
calls SDL_VideoInit
if needed:
SDL_Window *
SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
{
// [...]
if (!_this) {
/* Initialize the video system if needed */
if (SDL_VideoInit(NULL) < 0) {
return NULL;
}
}
// [...]
}
The problem is that you might someday encounter a bug due to something not being initialized, and you won't be able to find the cause easily. In short, initialize the subsystems you use. It doesn't cost you anything and could potentially save you a lot of trouble.
来源:https://stackoverflow.com/questions/53571360/what-does-sdl-init-exactly-do