pythonpygamebinary-operators

Using python's boolean & operator to check for flag in pygame surface doesnt return expected flag


While writing my game in pygame I was trying to use the XOR (^) binary operator to 'toggle' flags any flag I passed to a function. However I kept running into Overflow errors (and Warning: re-creating window in toggle_fullscreen action() errors). To debug this I tried to use the & operator to check if the flags were being properly detected and compare it to the constant value provided by pygame.

print(pygame.display.get_surface().get_flags() & exampleflag, exampleflag)

While this worked when checking for pygame.NOFRAME (the result was 0 32 when not in borderless mode and 32 32 when in borderless.
But when trying to check for pygame.SCALED which is always enabled in my game it would always output 0 512 even though pygame.SCALED was present and I was expecting 512 512.
Also when checking for pygame.FULLSCREEN (I was toggling it using pygame.display.toggle_fullscreen() if this helps in any way) the result was 0 -2147483648 (was expecting 0 0 since the window was not fullscreen) which is obviously not correct and probably why I was getting Overflow errors.

I don't really understand how pygame flags work beyond what the basic binary operators are meant to do to them since it is not documented very well (the fact that you can check for flags using & itself was discovered by me on another question on stack overflow). Can someone explain why this is happening?


Solution

  • pygame surface flags are completely different from the flags you pass to pygame.display.set_mode. SCALED, FULLSCREEN, and NOFRAME are set_mode flags. You cannot test for them with Surface.get_flags().

    These are the surface flags, as documented under Surface.get_flags:

    SWSURFACE      0x00000000    # Surface is in system memory
    HWSURFACE      0x00000001    # (obsolete in pygame 2) Surface is in video memory
    ASYNCBLIT      0x00000004    # (obsolete in pygame 2) Use asynchronous blits if possible
    HWACCEL        0x00000100    # Blit uses hardware acceleration
    SRCCOLORKEY    0x00001000    # Blit uses a source color key
    RLEACCELOK     0x00002000    # Private flag
    RLEACCEL       0x00004000    # Surface is RLE encoded
    SRCALPHA       0x00010000    # Blit uses source alpha blending
    PREALLOC       0x01000000    # Surface uses preallocated memory
    

    (That SWSURFACE value is bizarre, but 0 appears to be correct. It's not pygame's fault - this weird value appears to be from SDL.)