Commits

Daniel Pope committed c81fe47

New: progressive generation with pygame viewer

Comments (0)

Files changed (8)

+syntax: glob
+*.pyc
+*.swp
+build/
+dist/
+heightfield.egg-info/

heightfield/data/heightmap.png

Old
Old image
New
New image

heightfield/main.py

     print
 
     landscape.to_pil().save(outfile)
+
+
+ISLAND_CHAIN = [
+    # size, height, number
+    (100, 0.8, 200),
+    (75, 0.6, 300),
+    (50, 0.4, 400),
+    (25, 0.2, 500),
+]
+
+CONTINENTAL = [
+    (200, 0.7, 150),
+    (50, 0.5, 300),
+    (30, 0.2, 1000),
+]
+
+ISLANDS = [
+    (200, 1, 50),
+    (50, 0.3, 500),
+    (30, 0.1, 2000),
+]
+
+def visible_deposition():
+    from .viewer.pygameviewer import Viewer
+    from pkg_resources import resource_stream
+    SIZE = 768
+    landscape = Surface(SIZE, fill=-0.25)
+    viewer = Viewer(landscape, resource_stream(__name__, 'data/heightmap.png'))
+    viewer.start()
+
+    octaves = ISLANDS
+
+    for size, height, number in octaves:
+        number = int(number * (SIZE / 512.0) ** 2)
+        c = Surface.make_cone(size, height)
+        sys.stdout.write('.')
+        sys.stdout.flush()
+        for j in xrange(10):
+            with viewer.lock:
+                for i in range(number // 10):
+                    xpos = random.randint(-size, SIZE)
+                    ypos = random.randint(-size, SIZE)
+                    landscape.blit(c, xpos, ypos)
+
+    print "finished."

heightfield/viewer/__init__.py

Empty file added.

heightfield/viewer/pygameviewer.py

+import sys
+import pygame
+import threading
+
+pygame.display.init()
+
+class Viewer(threading.Thread):
+    def __init__(self, surface, colormapfile):
+        threading.Thread.__init__(self)
+        self.surface = surface
+        self.screen = pygame.display.set_mode((surface.size, surface.size))
+        self.lock = threading.RLock()
+        self.load_colormap(colormapfile)
+
+    def load_colormap(self, mapfile):
+        map = pygame.image.load(mapfile)
+        self.colormap = [pygame.Color('black')] * 256
+        for i in xrange(map.get_width()):
+            self.colormap[i] = map.get_at((i, 0))
+    
+    def repaint_rgb(self):
+        """Draw the surface to the screen"""
+        with self.lock:
+            self.screen.lock()
+            try:
+                for y in xrange(self.surface.size):
+                    for x in xrange(self.surface.size):
+                        h = self.surface[x, y]
+                        val = max(0, min(1.0, h * 0.1) * 255)
+                        col = self.colormap[int(val)]
+                        self.screen.set_at((x, y), col)
+            finally:
+                self.screen.unlock()
+
+    def run(self):
+        clock = pygame.time.Clock()
+        while True:
+            clock.tick(6)
+            for event in pygame.event.get():
+                if event.type == pygame.QUIT:
+                    sys.exit(0)
+            self.repaint_rgb()
+            pygame.display.flip()
+from heightfield.main import visible_deposition
+
+visible_deposition()
     entry_points={
         'console_scripts': [
             'heightfield = heightfield.main:main',
+            'mapbuilder = heightfield.main:visible_deposition',
         ]
     }
 )
Old
Old image
New
New image
          offset="0"
          id="stop2989" />
       <stop
+         id="stop3159"
+         offset="0.13169043"
+         style="stop-color:#3381f1;stop-opacity:1" />
+      <stop
          style="stop-color:#7bf6e3;stop-opacity:1"
-         offset="0.14719746"
+         offset="0.17785633"
          id="stop3028" />
       <stop
          id="stop3111"
-         offset="0.15028511"
+         offset="0.18127087"
          style="stop-color:#ffffff;stop-opacity:1" />
       <stop
          id="stop2995"
-         offset="0.15346482"
+         offset="0.18446605"
          style="stop-color:#ffff00;stop-opacity:1" />
       <stop
          style="stop-color:#d4aa00;stop-opacity:1"
      borderopacity="1.0"
      inkscape:pageopacity="0.0"
      inkscape:pageshadow="2"
-     inkscape:zoom="22.627417"
-     inkscape:cx="24.566761"
-     inkscape:cy="-1.5667928"
+     inkscape:zoom="11.313709"
+     inkscape:cx="48.364578"
+     inkscape:cy="-5.5877748"
      inkscape:document-units="px"
      inkscape:current-layer="layer1"
      showgrid="false"
-     inkscape:window-width="1854"
-     inkscape:window-height="1176"
-     inkscape:window-x="66"
+     inkscape:window-width="1680"
+     inkscape:window-height="1026"
+     inkscape:window-x="1280"
      inkscape:window-y="24"
      inkscape:window-maximized="1"
      fit-margin-top="0"
         <dc:format>image/svg+xml</dc:format>
         <dc:type
            rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
-        <dc:title></dc:title>
+        <dc:title />
       </cc:Work>
     </rdf:RDF>
   </metadata>