Anonymous avatar Anonymous committed 6625feb

fix issue 88
take suggestions from stefanor

Comments (0)

Files changed (1)

         change visible to 1, you must set dirty to 1 for it to be erased from
         the screen.)
 
-    layer = 0
+    _layer = 0
         A READ ONLY value, it is read when adding it to the LayeredUpdates
         group. For details see documentation of sprite.LayeredUpdates.
 
 
     def remove_internal(self, sprite):
         r = self.spritedict[sprite]
-        if r is not 0:
+        if r:
             self.lostsprites.append(r)
         del self.spritedict[sprite]
 
             for r in self.lostsprites:
                 bgd(surface, r)
             for r in self.spritedict.values():
-                if r is not 0:
+                if r:
                     bgd(surface, r)
         else:
             surface_blit = surface.blit
             for r in self.lostsprites:
                 surface_blit(bgd, r, r)
             for r in self.spritedict.values():
-                if r is not 0:
+                if r:
                     surface_blit(bgd, r, r)
 
     def empty(self):
        for s in self.sprites():
            r = spritedict[s]
            newrect = surface_blit(s.image, s.rect)
-           if r is 0:
-               dirty_append(newrect)
-           else:
+           if r:
                if newrect.colliderect(r):
                    dirty_append(newrect.union(r))
                else:
                    dirty_append(newrect)
                    dirty_append(r)
+           else:
+               dirty_append(newrect)
            spritedict[s] = newrect
        return dirty
 
 
     """
 
+    _init_rect = Rect(0, 0, 0, 0)
+
     def __init__(self, *sprites, **kwargs):
         """initialize an instance of LayeredUpdates with the given attributes
 
         You can set the default layer through kwargs using 'default_layer'
         and an integer for the layer. The default layer is 0.
 
-        If the sprite you add has an attribute layer, then that layer will be
+        If the sprite you add has an attribute _layer, then that layer will be
         used. If **kwarg contains 'layer', then the passed sprites will be
-        added to that layer (overriding the sprite.layer attribute). If neither
-        the sprite nor **kwarg has a 'layer', then the default layer is used to
-        add the sprites.
+        added to that layer (overriding the sprite._layer attribute). If
+        neither the sprite nor **kwarg has a 'layer', then the default layer is
+        used to add the sprites.
 
         """
         self._spritelayers = {}
         It is used by the group to add a sprite internally.
 
         """
-        self.spritedict[sprite] = Rect(0, 0, 0, 0) # add a old rect
+        self.spritedict[sprite] = self._init_rect
 
         if layer is None:
             try:
 
         LayeredUpdates.add(*sprites, **kwargs): return None
 
-        If the sprite you add has an attribute layer, then that layer will be
+        If the sprite you add has an attribute _layer, then that layer will be
         used. If **kwarg contains 'layer', then the passed sprites will be
-        added to that layer (overriding the sprite.layer attribute). If neither
-        the sprite nor **kwarg has a 'layer', then the default layer is used to
-        add the sprites.
+        added to that layer (overriding the sprite._layer attribute). If
+        neither the sprite nor **kwarg has a 'layer', then the default layer is
+        used to add the sprites.
 
         """
 
         """
         self._spritelist.remove(sprite)
         # these dirty rects are suboptimal for one frame
-        self.lostsprites.append(self.spritedict[sprite]) # dirty rect
+        r = self.spritedict[sprite]
+        if r is not self._init_rect:
+            self.lostsprites.append(r) # dirty rect
         if hasattr(sprite, 'rect'):
             self.lostsprites.append(sprite.rect) # dirty rect
 
-        self.spritedict.pop(sprite, 0)
-        self._spritelayers.pop(sprite)
+        del self.spritedict[sprite]
+        del self._spritelayers[sprite]
 
     def sprites(self):
         """return a ordered list of sprites (first back, last top).
         dirty = self.lostsprites
         self.lostsprites = []
         dirty_append = dirty.append
+        init_rect = self._init_rect
         for spr in self.sprites():
             rec = spritedict[spr]
             newrect = surface_blit(spr.image, spr.rect)
-            if rec is 0:
+            if rec is init_rect:
                 dirty_append(newrect)
             else:
                 if newrect.colliderect(rec):
         if bgd is not None:
             self._bgd = bgd
         _bgd = self._bgd
+        init_rect = self._init_rect
 
         _surf.set_clip(_clip)
         # -------
                         i = _union_rect_collidelist(_update)
                     _update_append(_union_rect.clip(_clip))
 
-                    _union_rect = _rect(_old_rect[spr])
-                    _union_rect_collidelist = _union_rect.collidelist
-                    _union_rect_union_ip = _union_rect.union_ip
-                    i = _union_rect_collidelist(_update)
-                    while -1 < i:
-                        _union_rect_union_ip(_update[i])
-                        del _update[i]
+                    if _old_rect[spr] is not init_rect:
+                        _union_rect = _rect(_old_rect[spr])
+                        _union_rect_collidelist = _union_rect.collidelist
+                        _union_rect_union_ip = _union_rect.union_ip
                         i = _union_rect_collidelist(_update)
-                    _update_append(_union_rect.clip(_clip))
+                        while -1 < i:
+                            _union_rect_union_ip(_update[i])
+                            del _update[i]
+                            i = _union_rect_collidelist(_update)
+                        _update_append(_union_rect.clip(_clip))
             # can it be done better? because that is an O(n**2) algorithm in
             # worst case
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.