pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Maximizing a window invalidates the window surface

Open Starbuck5 opened this issue 1 year ago • 7 comments
trafficstars

OS: Windows 10, Python: 3.9

import pygame
from pygame._sdl2 import Window

window = Window(resizable=True)
screen = window.get_surface()

clock = pygame.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            raise SystemExit
        
        if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
            window.maximize()
        
    screen.fill("white")
    window.flip()

When I press 1, get pygame.error: Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface

Starbuck5 avatar Dec 15 '23 07:12 Starbuck5

Relevant to #2603

Starbuck5 avatar Dec 15 '23 07:12 Starbuck5

The bug can be fixed if you add a pygame.init(), because the init hook of window module is called when display module init, then the init hook sets the event watch.

yunline avatar Dec 16 '23 07:12 yunline

Ah, I didn't notice it was just about the resizability!

Using pygame.display.set_mode with pygame.RESIZABLE this problem never surfaces, so it probably shouldn't here?

Starbuck5 avatar Dec 16 '23 07:12 Starbuck5

Using pygame.display.set_mode with pygame.RESIZABLE this problem never surfaces

The resize event watch of display module is set in set_mode(), so it's not influenced by this bug.

yunline avatar Dec 16 '23 07:12 yunline

Sure. I’m saying that since this error can’t occur with pygame.display, it shouldn’t be able to occur with pygame.window.

Starbuck5 avatar Dec 16 '23 07:12 Starbuck5

I see two ways to resolve this

  • Window constructor calls pygame.display.init()
  • Window constructor requires pygame.display.init() to be called already, and error otherwise

I prefer the second way. pygame.display.set_mode actually goes the first way however

ankith26 avatar Dec 21 '23 18:12 ankith26

The way I see it, the modern way is to make initializations more automatic. And it's precedented in display.set_mode.

But either option would be better than the current setup.

Starbuck5 avatar Dec 23 '23 09:12 Starbuck5