c++sdl-2enum-flags

SDL_GetWindowFlags() returns seemingly random values


I need my SDL2 program to know whether a window is fullscreen, and I thought I could get that info using SDL_GetWindowFlags(). By default I initialize my window with two flags: SDL_WINDOW_SHOWN and SDL_WINDOW_BORDERLESS, which are equal to 16 and 4 respectively. So I expected the function to return 20, but instead I get 532. And also sometimes 1556, which even changes to 532 during runtime after reinitializing the window a few times. 532 never changes to 1556 during runtime however.

How do these flags work?

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

Solution

  • Looks like your flag value is changing based on the states of SDL_WINDOW_INPUT_FOCUS and SDL_WINDOW_MOUSE_FOCUS. But it doesn't matter. Flag values change all the time. You shouldn't worry about the total value of the flags. You only need to know the value of the flag bit you are watching. The SDL_WINDOW_SHOWN and SDL_WINDOW_BORDERLESS flags are still set when the values are 532 and 1556 (if you look in binary).

    Just grab the value of the bit flag:

    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;
    

    Here's a function you can use to see what flags are set based on the value:

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

    More flags can be found here: https://wiki.libsdl.org/SDL_WindowFlags.

    Output:

    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