Pygame - is there any way to only blit or update in a mask

后端 未结 2 2009
鱼传尺愫
鱼传尺愫 2020-12-03 08:57

Is there any way in pygame to blit something to the screen inside a mask. Eg: if you had a mask where all the bits were set to 1 except for the topleft corner and a fully bl

2条回答
  •  我在风中等你
    2020-12-03 09:53

    So if I understand correctly, the question was - can one use bit-masks for blitting. The answer is yes and no. AFAIK, there is no support for 1-bit masks in pygame, that means, you must use 32-bpp RGBA (SRCALPHA flag) for source surface to blit with transparency and in this case you use 8-bit mask = 256 grades of transparency per pixel and this is described in previous answer. The other option is to use 'colorkey' blitting. In this case it is more similar to 1-bit mask and is used with standard 24-bpp RGB surfaces. You just set what color in source surface you do not want to write to destination. See 'Surface.set_colorkey' in documentation. The problem with colorkey method however is that you can accidentally have some pixels in source, wich have same color as colorkey, but which you still want to draw. So you must be careful. I personally use RGBA surfaces for masking, it is more powerful, but almost always I don't need 8-bits, 1-bit mask would be enough. So it seems to me kind of strange that there is no support for per bit-mask blitting. The same is with buffer flipping - you can describe the areas of the screen buffer to update with a list of rectangles only. More flexible and straightforward would be to use bit masks, but afaik that is not possible too. Probably converting a bit mask to a list of rectangles is the way, but I didn't try it. Third options is not to use pygame's surface and use Numpy for your data, and then use array blitting:

    pygame.surfarray.blit_array(Destination_surface, my_array)
    

    Then you are free to make anything you want with you data, but that is another story.

提交回复
热议问题