From 12a53d83e062fc7ec5838c94d575b94a7310d6cd Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 10 Jul 2023 01:46:56 +0100 Subject: add tile atlas to mapfile --- tools/createmap.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/createmap.py b/tools/createmap.py index 59fb315..36f6ca9 100755 --- a/tools/createmap.py +++ b/tools/createmap.py @@ -1,12 +1,33 @@ #!/usr/bin/env python import sys +from math import ceil, log +from PIL import Image outfile = "map.tiles" -WIDTH = 500 -HEIGHT = 100 +ATLASFILE = "atlas.png" + +TILESIZE = 16 +WIDTH = 1024 +HEIGHT = 1024 + +# create atlas bytes +image = Image.open(ATLASFILE).convert("RGBA") + + +# check if atlas is suitable +if image.width % TILESIZE != 0 or image.height % TILESIZE != 0: + print(f"Atlas image must be divisible by {TILESIZE}", file=sys.err) + sys.exit(1) + +# calculate number of possible tiles (adding 1 for the empty tile) +tilecount = (image.width * image.height) / (TILESIZE**2) + 1 + +atlas_width = image.width // TILESIZE +atlas_height = image.width // TILESIZE + # number of bytes each tile needs to represent -TILEBYTES = 25 +tilebytes = ceil(log(tilecount, 256)) HEADER = "TILEFILEv1" @@ -14,10 +35,15 @@ with open(outfile, "wb") as file: file.write(bytes(HEADER, "ascii")) file.write(WIDTH.to_bytes(4, 'big')); file.write(HEIGHT.to_bytes(4, 'big')); - file.write(TILEBYTES.to_bytes(4, 'big')); + file.write(tilebytes.to_bytes(4, 'big')); + for y in range(HEIGHT): for x in range(WIDTH): index = (x + y) % 5 - file.write(index.to_bytes(TILEBYTES, 'big')); + file.write(index.to_bytes(tilebytes, 'big')); + file.write(TILESIZE.to_bytes(4, 'big')); + file.write(atlas_width.to_bytes(4, 'big')); + file.write(atlas_height.to_bytes(4, 'big')); + file.write(image.tobytes()) -- cgit v1.2.1