SDL_GetWindowFlags() returns 看似随机的值

SDL_GetWindowFlags() returns seemingly random values

我需要我的 SDL2 程序知道 window 是否全屏,我想我可以使用 SDL_GetWindowFlags() 获取该信息。默认情况下,我用两个标志初始化 window:SDL_WINDOW_SHOWN 和 SDL_WINDOW_BORDERLESS,分别等于 16 和 4。所以我期望函数为 return 20,但我得到的是 532。有时甚至是 1556,甚至在重新初始化 window 几次后在运行时变为 532。然而,532 在运行时永远不会更改为 1556。

这些标志是如何工作的?

bool init( int windowflags )
{
    bool success = true;
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Video initialization failed: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        gWindow = SDL_CreateWindow( "VIRGULE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_W, WIN_H, SDL_WINDOW_SHOWN + windowflags );
        if( gWindow == NULL )
        {
            printf( "Window could not be created: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED + SDL_RENDERER_TARGETTEXTURE );
            if( gRenderer == NULL )
            {
                printf( "Renderer could not be created: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                gTexture = SDL_CreateTexture( gRenderer, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_TARGET, SCR_W, SCR_H );
                if( gTexture == NULL )
                {
                    printf( "Texture creation failed: %s\n", SDL_GetError() );
                    success = false;
                }
            }
        }
    }
    printf( "%i\n", SDL_GetWindowFlags( gWindow ) );
    //this is either prints 1556 or 532
    return success;
}

看起来您的标志值正在根据 SDL_WINDOW_INPUT_FOCUSSDL_WINDOW_MOUSE_FOCUS 的状态发生变化。不过没关系。标志值一直在变化。您不必担心标志的总价值。你只需要知道你正在观察的标志位的值。当值是 5321556 时,SDL_WINDOW_SHOWNSDL_WINDOW_BORDERLESS 标志仍然被设置(如果你查看二进制文件)。

直接抓取位标志的值:

int flags             = SDL_GetWindowFlags( gWindow );
int window_shown      = ( flags & SDL_WINDOW_SHOWN ) ? true : false;
int window_borderless = ( flags & SDL_WINDOW_BORDERLESS ) ? true : false;
int window_fullscreen = ( flags & SDL_WINDOW_FULLSCREEN ) ? true : false;

您可以使用以下函数查看根据值设置了哪些标志:

void show_flags(int flags);

int main()
{
    show_flags(20);
    show_flags(532);
    show_flags(1556);

    return 0;
}

void show_flags(int flags) {

    printf("\nFLAGS ENABLED: ( %d )\n", flags);
    printf("=======================\n");
    if(flags & SDL_WINDOW_FULLSCREEN)         printf("SDL_WINDOW_FULLSCREEN\n");
    if(flags & SDL_WINDOW_OPENGL)             printf("SDL_WINDOW_OPENGL\n");
    if(flags & SDL_WINDOW_SHOWN)              printf("SDL_WINDOW_SHOWN\n");
    if(flags & SDL_WINDOW_HIDDEN)             printf("SDL_WINDOW_HIDDEN\n");
    if(flags & SDL_WINDOW_BORDERLESS)         printf("SDL_WINDOW_BORDERLESS\n");
    if(flags & SDL_WINDOW_RESIZABLE)          printf("SDL_WINDOW_RESIZABLE\n");
    if(flags & SDL_WINDOW_MINIMIZED)          printf("SDL_WINDOW_MINIMIZED\n");
    if(flags & SDL_WINDOW_MAXIMIZED)          printf("SDL_WINDOW_MAXIMIZED\n");
    if(flags & SDL_WINDOW_INPUT_GRABBED)      printf("SDL_WINDOW_INPUT_GRABBED\n");
    if(flags & SDL_WINDOW_INPUT_FOCUS)        printf("SDL_WINDOW_INPUT_FOCUS\n");
    if(flags & SDL_WINDOW_MOUSE_FOCUS)        printf("SDL_WINDOW_MOUSE_FOCUS\n");
    if(flags & SDL_WINDOW_FULLSCREEN_DESKTOP) printf("SDL_WINDOW_FULLSCREEN_DESKTOP\n");
    if(flags & SDL_WINDOW_FOREIGN)            printf("SDL_WINDOW_FOREIGN\n");
}

可以在此处找到更多标志:https://wiki.libsdl.org/SDL_WindowFlags

输出:

FLAGS ENABLED: ( 20 )  
=======================
SDL_WINDOW_SHOWN       
SDL_WINDOW_BORDERLESS  

FLAGS ENABLED: ( 532 ) 
=======================
SDL_WINDOW_SHOWN       
SDL_WINDOW_BORDERLESS  
SDL_WINDOW_INPUT_FOCUS 

FLAGS ENABLED: ( 1556 )
=======================
SDL_WINDOW_SHOWN       
SDL_WINDOW_BORDERLESS  
SDL_WINDOW_INPUT_FOCUS 
SDL_WINDOW_MOUSE_FOCUS