lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
527 stars 191 forks source link

screen.draw.text displaying a white block instead of text #258

Open al-kee opened 3 years ago

al-kee commented 3 years ago

Completely new to pgzero, helping my kids with the book Coding Games in Python. On one of the first exercise that just should print out the Hello, instead of Hello we're getting a white block. I've tried changing the font, and that does not change the result. Attaching a screenshot.

I'm running this on MacOS Catalina and everything looks installed properly.
Image 2021-07-08 at 9 03 PM

lordmauve commented 2 years ago

I don't have a Mac to develop on, but I escalated this with Pygame maintainers on Discord and they said that it might be a Mac OS X quirk with video start-up but nobody has been able to diagnose it, and it goes away if you redraw. It might be fixed in 126f7cf21e6c0cd493ddd14468f934f7506fa343 where we handle the VIDEOEXPOSE event.

It would be useful if someone could test this on Mac and if this is reproducible, get closer to a root cause.

Note that your script ought to have a screen.clear() at the start of draw() - but what you wrote should also work.

lordmauve commented 2 years ago

This may not end up relevant but @geofft just pointed out that the screen buffer starts initialised to (0, 0, 0, 0) on Mac OS but (0, 0, 0, 255) elsewhere. It shouldn't create this effect though; the alpha blending equations would produce greys, blacks and whites as expected.

a = (0, 0, 0, 0)
b = (1, 1, 1, x)

out_rgb = b.rgb * b.a + (1 - b.a) * a.rgb = (x, x, x)
out_a = b.a + (1 - b.a) * a.a = x

It would only be white if the screen was being rendered to the display by dividing the resulting premultiplied buffer and then discarding the alpha channel - which seems like an odd thing to do; you'd either just discard the alpha channel or you'd use it for blending, either of which would avoid this effect,

r0the commented 2 years ago

I can reproduce the problem on macOS 12 Monterey with

with the following test program:

import pgzrun
def draw():
    print(screen.surface.get_at((0, 0)))
    screen.draw.text("H e l l o", topleft=(10, 10))
pgzrun.go()

The buffer is initialised as (0, 0, 0, 0)

However, when i upgrade to pygame 2.1.1, the program works as expected:

Bildschirmfoto 2021-12-24 um 23 19 23

and the buffer is initialised as (0, 0, 0, 255)

So, upgrading pygame seems to fix this problem.

geofft commented 2 years ago

Wow, 2.1.1 was released earlier today... In fact, upgrading fixes my issue too.

I bet this was pygame/pygame#2859. The patch looks relevant, and the problem looks like the same one as this issue (white rectangle instead of graphics).