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

Transparency with pygame.draw

Open oddbookworm opened this issue 1 year ago • 1 comments
trafficstars

pygame.draw doesn't apply transparency, but the docs imply it should

reproducer

import pygame

screen = pygame.display.set_mode((500, 500))

OPAQUE = (255, 0, 0, 255)
TRANSLUCENT = (255, 0, 0, 128)
TRANSPARENT = (255, 0, 0, 0)

opaque_rect = pygame.Rect(0, 0, 100, 100)
translucent_rect = pygame.Rect(100, 0, 100, 100)
transparent_rect = pygame.Rect(200, 0, 100, 100)


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("black")
    pygame.draw.rect(screen, OPAQUE, opaque_rect)
    pygame.draw.rect(screen, TRANSLUCENT, translucent_rect)
    pygame.draw.rect(screen, TRANSPARENT, transparent_rect)
    pygame.display.flip()

image

oddbookworm avatar Feb 08 '24 03:02 oddbookworm

draw functions do apply the alpha, but in SDL's BLENDMODE_NONE fashion, which means it replaces target alpha. The display surface doesn't have an alpha channel so that gets ignored. If you draw to a SRCALPHA surface and blit that, it will blend properly (as is default behaviour for blitting).

I guess this might benefit from clarification in the docs (maybe an example), but it's not a bug.

Here is a relevant issue about adding a blendmode kwarg to draw functions: #2220

Matiiss avatar Feb 08 '24 10:02 Matiiss