Commit dcaf928a authored by talljosh's avatar talljosh

Correctly handle window resizes with Pygame 2.

parent 0c354b504ca7
......@@ -222,7 +222,8 @@ class PygameRunnerAPI(Protocol):
raise NotImplementedError
def resize_window(
self, window_size: Tuple[int, int], full_screen: Optional[bool] = None) -> None:
self, window_size: Tuple[int, int], full_screen: Optional[bool] = None,
from_event=False) -> None:
'''
Resizes the currently showing Pygame window.
......@@ -230,6 +231,8 @@ class PygameRunnerAPI(Protocol):
:param full_screen: If provided, controls whether the window
will be shown in full-screen mode. If not provided, the
current value will be used.
:param from_event: Should be True iff this call is in response
to a VIDEORESIZE event.
:raises:
PygameWindowNotOpen: if the Pygame window is not open
'''
......@@ -297,26 +300,39 @@ class PygameRunner(PygameRunnerAPI):
return surface.get_size()
def resize_window(
self, window_size: Tuple[int, int], full_screen: Optional[bool] = None) -> None:
self, window_size: Tuple[int, int], full_screen: Optional[bool] = None,
from_event=False) -> None:
if not self.is_showing():
raise PygameWindowNotOpen('No pygame window currently open')
if full_screen is None:
full_screen = self.full_screen_requested
self.set_display_mode(window_size, full_screen)
self.set_display_mode(window_size, full_screen, from_event=from_event)
self.on_resize()
def set_display_mode(self, size, full_screen):
def set_display_mode(self, size, full_screen, from_event=False):
if pygame.vernum[0] >= 2 and from_event:
self.last_screen_size = size
self.full_screen_requested = full_screen
if size == self.last_screen_size and full_screen == self.full_screen_requested:
if self.is_showing():
return
self.last_screen_size = size
self.full_screen_requested = full_screen
kwargs = {}
if full_screen:
flags = pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE
else:
flags = pygame.RESIZABLE
if pygame.vernum[0] >= 2:
# Ask for vertical sync if possible
kwargs['vsync'] = True
pygame.display.init()
self.set_default_icon()
return pygame.display.set_mode(size, flags)
return pygame.display.set_mode(size, flags, **kwargs)
def set_default_icon(self):
icon = pygame.image.load(str(ICON_FILE))
......@@ -380,7 +396,7 @@ class PygameRunner(PygameRunnerAPI):
if event.type == pygame.QUIT:
return
if event.type == pygame.VIDEORESIZE:
self.resize_window(event.size)
self.resize_window(event.size, from_event=True)
try:
self.on_tick(now - last_time)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment