SDL_GetWindowFlags()返回看似随机的值

时间:2017-10-20 18:43:10

标签: c++ sdl-2 enum-flags

我需要我的SDL2程序才能知道窗口是否是全屏的,我想我可以使用SDL_GetWindowFlags()来获取该信息。默认情况下,我用两个标志初始化我的窗口:SDL_WINDOW_SHOWN和SDL_WINDOW_BORDERLESS,它们分别等于16和4。所以我希望函数返回20,但我得到532.有时候1556,甚至在运行时在重新初始化窗口几次后变为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;
}

1 个答案:

答案 0 :(得分:3)

根据SDL_WINDOW_INPUT_FOCUSSDL_WINDOW_MOUSE_FOCUS的状态,您的旗帜值会发生变化。但这并不重要。标志值始终在变化。你不应该担心旗帜的总价值。您只需要知道正在观看的标志位的值。当值为SDL_WINDOW_SHOWNSDL_WINDOW_BORDERLESS时,5321556标记仍会设置(如果您查看二进制文件)。

只需获取位标志的值:

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