0

When I try to use fill with a Surface using both the rect and special_flags arguments, pygame lags for a few seconds, then crashes without notification, if I have named rect dimensions that escape the Surface's area.

Here're the results of a test using IDLE:

>>> import pygame
>>> test_surf = pygame.Surface((50,50))
>>> test_rgba = 50,100,150,200 # arbitrary
>>> test_rect = 33,33,33,33 # these dimensions are 16px too large on each axis.
>>> test_surf.fill(test_rgba, test_rect, pygame.BLEND_RGBA_MULT)

>>> ================================ RESTART ================================
>>>

This restart occurs regardless of how I interact, or do not interact, with the software. It occurs whether I call pygame.init() after import pygame or not.

If a rect argument entirely within the surface's bounds (in this case, (0,0,50,50)) is used, it functions normally. Alternatively, omitting or sending 0 for the special_flags argument does not trigger this problem; pixels contained by the Surface are altered as normal and any that 'spill' off the edge are ignored.

What I would like to know is what causes pygame to crash in this mysterious and unexplained way?

I'm using pygame 1.9.1 and python 2.7.10.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Augusta
  • 7,171
  • 5
  • 24
  • 39
  • 2
    I can't replicate this. I tried in both Python 2.7 and 3.5. However, I'm using Pygame 1.9.2 - maybe you need to use a more recent version? – Chris Sep 22 '16 at 02:46
  • @Chris I updated to `1.9.2pre` a while ago, but it cut my framerate to about 70% of what `1.9.1release` gave me so I went back. If that's the cause of this bug though, then I guess I'll have to endure it... :/ I'll try it on a different computer, as well. Thanks, though-- that's a huge clue. Maybe there'll be a changelog entry or something. – Augusta Sep 22 '16 at 02:57
  • Seems to be happening on a different computer with 1.9.2pre/2.7.10 as well. On that computer, even the weird `test_surf.fill(test_rgba, (0,0,0,0), pygame.BLEND_RGBA_MULT)` fix doesn't help it... – Augusta Sep 22 '16 at 03:14
  • @Chris Perchance, are you on a Mac? For some reason I have the sense that this may be a C++ thing and possibly that's the difference here. – Augusta Sep 22 '16 at 03:16
  • 1
    I am on a Mac, although I just tried it on Linux with no problem either. I'm afraid I don't have any windows machine to test with. – Chris Sep 22 '16 at 04:39
  • @Chris No worries. Both machines I've experienced this with are Windows (7 and 8), and they both have issues with it, so that could be related. Valuable information! I'll wait and see if other people can reproduce this as well. – Augusta Sep 22 '16 at 04:47

1 Answers1

0

For any future reader who also suffers from this problem, the problem can be sidestepped in one of at least three ways. I'll share them with you here:

  • Use a clipped rect argument.

Replacing rect argument in fill with test_surf.get_rect().clip(test_rect). clip will constrain test_rect to the bounds of the destination Surface; if there are no common pixels, it will return a safe rect with shape parameters <0, 0, 0, 0>.

  • Use a second surface and blit instead.

    Create a new Surface, fill it with the rgb value you want (excluding alpha) without using a BLEND mode, use set_alpha to apply the desired opacity to the Surface, and finally blit the new surface to test_surf, or whatever your target is, using the appropriate dest value (in my case, (33,33)) and BLEND mode (for me, pygame.BLEND_RBGA_MULT).

  • Fight bugs with bugs.

    A third and more-puzzling solution is to call test_surf.fill(test_rgba, (0,0,0,0), pygame.BLEND_RGBA_MULT) before running the out-of-bounds rect. Whatever this does seems to fix pygame in such a way that you can safely go out of bounds with subsequent BLENDed fills without trouble. Moreover, attempting out-of-bounds, BLENDed fills on any Surface thereafter seems safe, and whatever the zero-pixel BLENDed-fill does seems to correct the dysfunctional element in pygame so that the crash cannot occur again that session.

    Be aware that this third solution has not been thoroughly tested!!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174