Skip to content

Instantly share code, notes, and snippets.

@tongyul
Created May 19, 2024 16:42
Show Gist options
  • Save tongyul/ef4bc6378c7b971167c57a4c70fabcd0 to your computer and use it in GitHub Desktop.
Save tongyul/ef4bc6378c7b971167c57a4c70fabcd0 to your computer and use it in GitHub Desktop.
Inconsistent resize behavior w.r.t. pygame.SCALED
from __future__ import annotations
from typing import Final, NoReturn
import dataclasses, logging, sys
import pygame
from pygame import Surface
logging.basicConfig(level=logging.DEBUG)
DEFAULT_WIDTH: Final = 1280
DEFAULT_HEIGHT: Final = 720
@dataclasses.dataclass
class GraphicsConfig:
screen_width: int
screen_height: int
fullscreen: bool
vsync: bool
fps_limit: int
@dataclasses.dataclass
class AppState:
graphics_config: GraphicsConfig
pending_exit_code: int | None = None
screen_surface: Surface | None = None
request_tick: bool = False
request_draw: bool = False
def mainloop(app_state: AppState) -> None:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# do exit stuff
app_state.pending_exit_code = 0
elif event.type == pygame.WINDOWRESIZED:
gconf = app_state.graphics_config
gconf.screen_width = event.x
gconf.screen_height = event.y
app_state.screen_surface = None
if app_state.screen_surface is None:
gconf = app_state.graphics_config # alias
app_state.screen_surface = pygame.display.set_mode(
(gconf.screen_width, gconf.screen_height),
flags=
(pygame.FULLSCREEN if gconf.fullscreen else pygame.RESIZABLE) |
pygame.SCALED, # XXX here, toggle this
vsync=gconf.vsync,
)
app_state.request_draw=True
app_state.screen_surface.fill((128, 128, 128, 255))
pygame.display.flip()
def main() -> NoReturn:
pygame.init()
app_state = AppState(
graphics_config=GraphicsConfig(
screen_width=DEFAULT_WIDTH,
screen_height=DEFAULT_HEIGHT,
fullscreen=False,
vsync=True,
fps_limit=60,
),
)
while app_state.pending_exit_code is None:
mainloop(app_state)
sys.exit(app_state.pending_exit_code)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment