[Python] Square distortion during window resizing problem

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to make a square that maintains its size and aspect ratio during window resizing using SDL3 and SDL_SetRenderLogicalPresentation() with the SDL_LOGICAL_PRESENTATION_LETTERBOX mode:

    import sdl3

    # Function to draw a square
    def draw_square(renderer, x, y, size):
    sdl3.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255) # White color
    rect = sdl3.SDL_FRect(x=x, y=y, w=size, h=size)
    sdl3.SDL_RenderFillRect(renderer, rect)

    # Initialize SDL3
    if sdl3.SDL_Init(sdl3.SDL_INIT_VIDEO) < 0:
    print(f"SDL initialization error: {sdl3.SDL_GetError().decode()}")
    exit(1)

    # Create window
    window = sdl3.SDL_CreateWindow(b"Hello World", 640, 480, sdl3.SDL_WINDOW_RESIZABLE)
    if not window:
    print(f"Window creation error: {sdl3.SDL_GetError().decode()}")
    exit(1)

    # Create renderer
    renderer = sdl3.SDL_CreateRenderer(window, b"metal")
    if not renderer:
    print(f"Renderer creation error: {sdl3.SDL_GetError().decode()}")
    exit(1)

    # Set logical rendering size
    logical_width, logical_height = 640, 480
    sdl3.SDL_SetRenderLogicalPresentation(renderer, logical_width, logical_height, sdl3.SDL_LOGICAL_PRESENTATION_LETTERBOX)

    # Parameters for positioning the square
    square_size = 100 # Fixed square size
    square_x = (logical_width - square_size) / 2 # Center horizontally
    square_y = (logical_height - square_size) / 2 # Center vertically

    running = True
    while running:
    # Event handling
    event = sdl3.SDL_Event()
    while sdl3.SDL_PollEvent(event):
    if event.type == sdl3.SDL_EVENT_QUIT:
    running = False

    # Rendering
    sdl3.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) # Black background
    sdl3.SDL_RenderClear(renderer)
    # Draw square with fixed size and centered position
    draw_square(renderer, square_x, square_y, square_size)
    sdl3.SDL_RenderPresent(renderer)

    # Cleanup
    sdl3.SDL_DestroyRenderer(renderer)
    sdl3.SDL_DestroyWindow(window)
    sdl3.SDL_Quit()


    Currently, the square only resets to its original width and height after I release the mouse button (when resizing ends). During the resizing process itself, the square gets stretched or squashed — it deforms.

    Is this the expected behavior of SDL_SetRenderLogicalPresentation? How can I make the square that never deforms at all, no matter how the window is resized? Are there workarounds or best practices for this? (Mac user)

    For example, would converting the square into a texture or something similar help keep its aspect ratio stable during resizing?

    My code written in Python but feel free to answer in any programming language, main goal is to get the way how to do window resizing without distortion.

    Continue reading...

Compartilhe esta Página