SDL_Init()代码阅读

来源:互联网 发布:三网合一建站系统源码 编辑:程序博客网 时间:2024/05/29 10:04

首先调用的是:

intSDL_Init(Uint32 flags){    return SDL_InitSubSystem(flags);}

所以就需要看看这个SDL_InitSubSystem(flasg)到底做了什么

#define SDL_InitSubSystem SDL_InitSubSystem_REAL

跳转到宏定义了,看来只能找SDL_InitSubSystem_REAL这个函数了

intSDL_InitSubSystem(Uint32 flags){

if (!SDL_MainIsReady) {        SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");        return -1;    }
那么这个SDL_MainIsReady 到底是什么东西?看看这个宏定义

/* The initialized subsystems */#ifdef SDL_MAIN_NEEDEDstatic SDL_bool SDL_MainIsReady = SDL_FALSE;#elsestatic SDL_bool SDL_MainIsReady = SDL_TRUE;#endif

这个注释很清楚了,没说的

    /* Clear the error message */    SDL_ClearError();

#if SDL_VIDEO_DRIVER_WINDOWSif ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {if (SDL_HelperWindowCreate() < 0) {return -1;}}#endif

看看这个函数干什么了?

/* * Creates a HelperWindow used for DirectInput events. */intSDL_HelperWindowCreate(void){    HINSTANCE hInstance = GetModuleHandle(NULL);    WNDCLASS wce;    /* Make sure window isn't created twice. */    if (SDL_HelperWindow != NULL) {        return 0;    }    /* Create the class. */    SDL_zero(wce);    wce.lpfnWndProc = DefWindowProc;    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;    wce.hInstance = hInstance;    /* Register the class. */    SDL_HelperWindowClass = RegisterClass(&wce);    if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {        return WIN_SetError("Unable to create Helper Window Class");    }    /* Create the window. */    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,                                      SDL_HelperWindowName,                                      WS_OVERLAPPED, CW_USEDEFAULT,                                      CW_USEDEFAULT, CW_USEDEFAULT,                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,                                      hInstance, NULL);    if (SDL_HelperWindow == NULL) {        UnregisterClass(SDL_HelperWindowClassName, hInstance);        return WIN_SetError("Unable to create Helper Window");    }    return 0;}

上一幅图介绍下


就是创建了一个窗口

接着看下面的代码

#if !SDL_TIMERS_DISABLED    SDL_TicksInit();#endif

依然是看看这个函数到底做了什么

voidSDL_TicksInit(void){    if (ticks_started) {        return;    }    ticks_started = SDL_TRUE;    /* Set first ticks value */#ifdef USE_GETTICKCOUNT    start = GetTickCount();#else    /* QueryPerformanceCounter has had problems in the past, but lots of games       use it, so we'll rely on it here.     */    if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {        hires_timer_available = TRUE;        QueryPerformanceCounter(&hires_start_ticks);    } else {        hires_timer_available = FALSE;#ifdef __WINRT__        start = 0;            /* the timer failed to start! */#else        timeSetPeriod(1);     /* use 1 ms timer precision */        start = timeGetTime();        SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,                            SDL_TimerResolutionChanged, NULL);#endif /* __WINRT__ */    }#endif /* USE_GETTICKCOUNT */}



继续下面的代码 , 根据给定的标志再加一定的标志

    if ((flags & SDL_INIT_GAMECONTROLLER)) {        /* game controller implies joystick */        flags |= SDL_INIT_JOYSTICK;    }    if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {        /* video or joystick implies events */        flags |= SDL_INIT_EVENTS;    }


继续,看注释  初始化事件系统

    /* Initialize the event subsystem */    if ((flags & SDL_INIT_EVENTS)) {#if !SDL_EVENTS_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {            if (SDL_StartEventLoop() < 0) {                return (-1);            }            SDL_QuitInit();        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);#else        return SDL_SetError("SDL not built with events support");#endif    }

继续,
    /* Initialize the timer subsystem */    if ((flags & SDL_INIT_TIMER)){#if !SDL_TIMERS_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {            if (SDL_TimerInit() < 0) {                return (-1);            }        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);#else        return SDL_SetError("SDL not built with timer support");#endif    }


下面是SDL_TimerInit()的代码逻辑


下面应该是重点啦

    /* Initialize the video subsystem */    if ((flags & SDL_INIT_VIDEO)){#if !SDL_VIDEO_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {            if (SDL_VideoInit(NULL) < 0) {                return (-1);            }        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);#else        return SDL_SetError("SDL not built with video support");#endif    }


关键函数SDL_VideoInit(NULL)

/* * Initialize the video and event subsystems -- determine native pixel format */intSDL_VideoInit(const char *driver_name){    SDL_VideoDevice *video;    const char *hint;    int index;    int i;    SDL_bool allow_screensaver;    /* Check to make sure we don't overwrite '_this' */    if (_this != NULL) {        SDL_VideoQuit();    }#if !SDL_TIMERS_DISABLED    SDL_TicksInit();#endif    /* Start the event loop */    if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0 ||        SDL_KeyboardInit() < 0 ||        SDL_MouseInit() < 0 ||        SDL_TouchInit() < 0) {        return -1;    }    /* Select the proper video driver */    index = 0;    video = NULL;    if (driver_name == NULL) {        driver_name = SDL_getenv("SDL_VIDEODRIVER");    }    if (driver_name != NULL) {        for (i = 0; bootstrap[i]; ++i) {            if (SDL_strncasecmp(bootstrap[i]->name, driver_name, SDL_strlen(driver_name)) == 0) {                if (bootstrap[i]->available()) {                    video = bootstrap[i]->create(index);                    break;                }            }        }    } else {        for (i = 0; bootstrap[i]; ++i) {            if (bootstrap[i]->available()) {                video = bootstrap[i]->create(index);                if (video != NULL) {                    break;                }            }        }    }    if (video == NULL) {        if (driver_name) {            return SDL_SetError("%s not available", driver_name);        }        return SDL_SetError("No available video device");    }    _this = video;    _this->name = bootstrap[i]->name;    _this->next_object_id = 1;    /* Set some very sane GL defaults */    _this->gl_config.driver_loaded = 0;    _this->gl_config.dll_handle = NULL;    SDL_GL_ResetAttributes();    _this->current_glwin_tls = SDL_TLSCreate();    _this->current_glctx_tls = SDL_TLSCreate();    /* Initialize the video subsystem */    if (_this->VideoInit(_this) < 0) {        SDL_VideoQuit();        return -1;    }    /* Make sure some displays were added */    if (_this->num_displays == 0) {        SDL_VideoQuit();        return SDL_SetError("The video driver did not add any displays");    }    /* Add the renderer framebuffer emulation if desired */    if (ShouldUseTextureFramebuffer()) {        _this->CreateWindowFramebuffer = SDL_CreateWindowTexture;        _this->UpdateWindowFramebuffer = SDL_UpdateWindowTexture;        _this->DestroyWindowFramebuffer = SDL_DestroyWindowTexture;    }    /* Disable the screen saver by default. This is a change from <= 2.0.1,       but most things using SDL are games or media players; you wouldn't       want a screensaver to trigger if you're playing exclusively with a       joystick, or passively watching a movie. Things that use SDL but       function more like a normal desktop app should explicitly reenable the       screensaver. */    hint = SDL_GetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER);    if (hint) {        allow_screensaver = SDL_atoi(hint) ? SDL_TRUE : SDL_FALSE;    } else {        allow_screensaver = SDL_FALSE;    }    if (!allow_screensaver) {        SDL_DisableScreenSaver();    }    /* If we don't use a screen keyboard, turn on text input by default,       otherwise programs that expect to get text events without enabling       UNICODE input won't get any events.       Actually, come to think of it, you needed to call SDL_EnableUNICODE(1)       in SDL 1.2 before you got text input events.  Hmm...     */    if (!SDL_HasScreenKeyboardSupport()) {        SDL_StartTextInput();    }    /* We're ready to go! */    return 0;}


    /* Initialize the joystick subsystem */    if ((flags & SDL_INIT_JOYSTICK)){#if !SDL_JOYSTICK_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {           if (SDL_JoystickInit() < 0) {               return (-1);           }        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);#else        return SDL_SetError("SDL not built with joystick support");#endif    }    if ((flags & SDL_INIT_GAMECONTROLLER)){#if !SDL_JOYSTICK_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {            if (SDL_GameControllerInit() < 0) {                return (-1);            }        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);#else        return SDL_SetError("SDL not built with joystick support");#endif    }    /* Initialize the haptic subsystem */    if ((flags & SDL_INIT_HAPTIC)){#if !SDL_HAPTIC_DISABLED        if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {            if (SDL_HapticInit() < 0) {                return (-1);            }        }        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);#else        return SDL_SetError("SDL not built with haptic (force feedback) support");#endif    }    return (0);}


主要就是各种初始化


附上一个写的比较全面的地址:http://blog.csdn.net/leixiaohua1020/article/details/40680907   非常详细

0 0
原创粉丝点击