SDL_Init 到底是做什么的? [英] What does SDL_Init exactly do?

查看:70
本文介绍了SDL_Init 到底是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,大约一年前我开始使用 SDL,我只是认为 SDL_Init 初始化了 SDL 的子系统(如此处所写:https://wiki.libsdl.org/SDL_Init ),并且我必须先调用它.但是今天,我意识到在一个相当大的项目中,我只是忘记了调用它,而且我从来没有遇到任何问题:一切正常.所以我只是想知道它是做什么的,因为我显然不需要它来使用库?

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 ?

推荐答案

SDL_Init 确实初始化了 SDL 的子系统.事实上,它只是转发到 SDL_InitSubSystem,正如您在源代码中看到的:

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);
}

现在,如果您不调用 SDL_InitSDL_InitSubSystem 但在使用子系统时没有遇到任何问题,那么您可能只是幸运.通过查看 SDL 源代码,您可能会发现很多函数在使用资源之前都会检查是否已初始化,并在可能的情况下进行初始化.例如,SDL_GetTicks 在需要时调用 SDL_TicksInit:

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();
    }
// [...]
}

同样,SDL_CreateWindow 会在需要时调用 SDL_VideoInit:

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.

这篇关于SDL_Init 到底是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆