pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
941 stars 156 forks source link

Add warning when using negative rect dimensions in draw module #3158

Open mzivic7 opened 1 month ago

mzivic7 commented 1 month ago

Using negative rect dimension values have some undesirable results as shown in #2727. This PR adds deprecation warning to these draw functions: draw.rect, draw.ellipse, draw.arc when using negative value for rect width and height. And prevents anything from being drawn at wrong position. Warnings are also added to docs and tests. This warning will also be added to draw.aaellipse in #3016 Closes #2727

Sample code ```py import pygame pygame.init() screen = pygame.display.set_mode((300, 300)) clock = pygame.time.Clock() run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False screen.fill((0, 0, 0)) pygame.draw.rect(screen, "white", (150, 150, -50, 30), 0) pygame.draw.ellipse(screen, "white", (150, 150, -50, 30), 0) pygame.draw.arc(screen, "white", (150, 150, -50, 30), 0, 180, 0) pygame.display.flip() clock.tick(60) pygame.quit() ```
ankith26 commented 1 month ago

while thinking about this, I realised that some other draw API also takes in rect style arguments. How are negative dims handled everywhere else. Whatever fix we do, we should try to be consistent with the approach everywhere

mzivic7 commented 1 month ago

I added same warning, docs and tests to draw.rect and draw.arc

mzivic7 commented 3 weeks ago

Sure, here it comes.

Starbuck5 commented 3 weeks ago

Did some testing,

I still think the most straightforward way to fix this is normalize the rect arguments when they come in. (https://pyga.me/docs/ref/rect.html#pygame.Rect.normalize)

mzivic7 commented 3 weeks ago

Why is normalizing input rects bad:

So I will remove warning and only disable drawing if rect width/height is negative.