Browse Source

Tile map fixes, regression tests, Travis first cut

James Bowman 2 years ago
parent
commit
2a75c41e66
7 changed files with 448 additions and 11 deletions
  1. 9 0
      .travis.yml
  2. 20 11
      gameduino2/prep.py
  3. 385 0
      testdata/MagicLand.tmx
  4. BIN
      testdata/golden-tiledemo.png
  5. BIN
      testdata/magiclanddizzy_tiles.gif
  6. 7 0
      travistests
  7. 27 0
      unit.py

+ 9 - 0
.travis.yml

@@ -0,0 +1,9 @@
+before_install:
+  - sudo apt-get update -qq
+  - sudo apt-get install -qq python-imaging
+language: python
+python:
+  - "2.6"
+  - "2.7"
+install: python setup.py install
+script: python unit.py

+ 20 - 11
gameduino2/prep.py

@@ -229,7 +229,8 @@ class AssetBin(gameduino2.base.GD2):
             self.define("%s_SIZE" % name, vsz)
             (vw, vh) = (vsz, vsz)
         self.BitmapSize(filter, gd2.BORDER, gd2.BORDER, vw, vh);
-        self.inits.append("static const shape_t %s_SHAPE = {%d, %d, %d, %d};" % (name, self.handle, w, h, vsz))
+        if name is not None:
+            self.inits.append("static const shape_t %s_SHAPE = {%d, %d, %d, %d};" % (name, self.handle, w, h, vsz))
 
         # aw is aligned width
         # For L1, L2, L4 formats the width must be a whole number of bytes
@@ -320,7 +321,7 @@ class AssetBin(gameduino2.base.GD2):
         widths = ([0] * 32) + [w for (w, _) in sizes]
         self.load_font(name, ims, widths, format)
 
-    def load_tiles(self, name, file_name, scale = 1.0):
+    def load_tiles(self, name, file_name, scale = None, preview = False):
         world_map = gameduino2.tmxreader.TileMapParser().parse_decode(file_name)
 
         # print("loaded map:", world_map.map_file_name)
@@ -349,6 +350,7 @@ class AssetBin(gameduino2.base.GD2):
         used = set()
         for layer in layers:
             used |= set(layer.decoded_content)
+        # used = sorted(used)
         used = sorted(used - set([0]))
 
         def reindex(i):
@@ -372,26 +374,27 @@ class AssetBin(gameduino2.base.GD2):
                     for y in range(4):
                         for x in range(4):
                             t = fetchtile(layer, i + x, j + y)
+                            # if i < (480 / 16) and j < (272 / 16): print 16 * (i + x), 16 * (j + y), "tile", t
                             if t is not None:
                                 eve.Vertex2ii(stw * x, sth * y, t / 128, t % 128)
                             else:
                                 eve.Nop()
+        # assert 0
         stride = ((w + 3) / 4)
         self.add(name, struct.pack("6H", w * stw, h * sth, stw * 4, sth * 4, stride, len(layers)) + eve.d)
         self.tile_files = world_map.tile_sets[0].images[0].source
+
         # print 'Size of tiles: %d (compressed %d)' % (len(eve.d), len(zlib.compress(eve.d)))
         # print 'Tile size', (tw, th)
+
         im = pma(Image.open(self.tile_files))
-        def extract(i):
-            w = im.size[0] / 72
-            x = 72 * (i % w)
-            y = 72 * (i / w)
-            return im.crop((x + 0, y + 0, x + 70, y + 70)).resize((32, 32))
         def extract(i):
             if hasattr(ts, 'columns'):
                 w = int(ts.columns)
-            else:
+            elif not hasattr(ts, 'spacing'):
                 w = im.size[0] / tw
+            else:
+                w = (im.size[0] + ts.spacing) / (tw + ts.spacing)
             x = ts.margin + (tw + ts.spacing) * (i % w)
             y = ts.margin + (th + ts.spacing) * (i / w)
             r = im.crop((x + 0, y + 0, x + tw, y + th))
@@ -399,11 +402,17 @@ class AssetBin(gameduino2.base.GD2):
                 r = r.resize((stw, sth), Image.ANTIALIAS)
             return r
         for i,g128 in enumerate(chunker(used, 128)):
-            # print 'g128', len(g128), g128
-            for j,t in enumerate(g128):
-                extract(t - 1).save("xx_%d_%d.png" % (i, j))
             self.load_handle(None, [extract(t - 1) for t in g128], gd2.ARGB4, dither=0)
 
+        if preview:
+            pv = Image.new("RGB", (tw * w, th * h))
+            layer = layers[0]
+            for y in range(pv.size[1] / th):
+                for x in range(pv.size[0] / tw):
+                    t = fetchtile(layer, x, y)
+                    pv.paste(extract(used[t] - 1), (16 * x, 16 * y))
+            return pv
+
     """
     def dxt1(self, imagefile):
         import numpy

File diff suppressed because it is too large
+ 385 - 0
testdata/MagicLand.tmx


BIN
testdata/golden-tiledemo.png


BIN
testdata/magiclanddizzy_tiles.gif


+ 7 - 0
travistests

@@ -0,0 +1,7 @@
+#!/bin/sh
+
+set -e
+set -x
+python setup.py build_ext
+sudo python setup.py install
+python unit.py

+ 27 - 0
unit.py

@@ -0,0 +1,27 @@
+import unittest
+from PIL import Image
+import gameduino2 as gd2
+
+
+class TestPreptools(unittest.TestCase):
+    def test_tile_magicland(self):
+        class Tiledemo(gd2.prep.AssetBin):
+            asset_file = "tiledemo.gd2"
+            def addall(self):
+                self.target_810()
+
+                if 0:
+                    self.load_tiles("TILEMAP", "gameart2d-desert.tmx", 0.5)
+                    desert = Image.open("desert-bg.png").resize((480, 272), Image.ANTIALIAS)
+                    self.load_handle("BACKGROUND", [desert], gd2.RGB565)
+                else:
+                    self.preview = self.load_tiles("TILEMAP", "testdata/MagicLand.tmx", preview = True)
+        td = Tiledemo()
+        td.make()
+        p = td.preview
+        self.assertEqual(p.mode, "RGB")
+        self.assertEqual(p.size, (7360, 1200))
+        self.assert_(p.tostring() == Image.open("testdata/golden-tiledemo.png").tostring())
+
+if __name__ == '__main__':
+    unittest.main()