diff nagslang/level.py @ 118:c02a99502a90

Tweak 'draw exterior' logic to handle surrounded polygons better
author Neil Muller <drnlmuller@gmail.com>
date Mon, 02 Sep 2013 15:44:57 +0200
parents 93256a0987a2
children 02423600d958
line wrap: on
line diff
--- a/nagslang/level.py	Mon Sep 02 15:29:37 2013 +0200
+++ b/nagslang/level.py	Mon Sep 02 15:44:57 2013 +0200
@@ -128,14 +128,28 @@
         """Fill the exterior of the level with black"""
         if self._exterior and not force:
             return
+        white = pygame.color.THECOLORS['white']
+        black = pygame.color.THECOLORS['black']
         surface = pygame.surface.Surface((self.x, self.y), pgl.SRCALPHA)
-        surface.fill(pygame.color.THECOLORS["black"])
+        surface.fill(black)
         for index, polygon in self.polygons.items():
             if len(polygon) > 1:
                 pointlist = [self.point_to_pygame(p) for p in polygon]
                 # filled polygons
-                pygame.draw.polygon(surface, pygame.color.THECOLORS['white'],
-                                    pointlist, 0)
+                color = white
+                # If a polygon overlaps on of the existing polygons,
+                # it is treated as negative
+                # This is not a complete inversion, since any overlap
+                # triggers this (inversion is easy enough, but the
+                # behaviour doesn't seem useful)
+                # We also only check the vertexes - not breaking this
+                # assumption is left to the level designers
+                surface.lock()
+                for p in pointlist:
+                    if surface.get_at(p) == white:
+                        color = black
+                surface.unlock()
+                pygame.draw.polygon(surface, color, pointlist, 0)
         self._surface.blit(surface, (0, 0), special_flags=pgl.BLEND_RGBA_MULT)
         self._exterior = True