diff options
author | davidovski <david@davidovski.xyz> | 2023-07-10 01:46:56 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2023-07-10 01:46:56 +0100 |
commit | 12a53d83e062fc7ec5838c94d575b94a7310d6cd (patch) | |
tree | 4a630b755c32696cd7c8fe2be7e7574b8fe7d622 /tools | |
parent | b87245927e08887af7746ce0f7a91d3f73af0572 (diff) |
add tile atlas to mapfile
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/createmap.py | 36 |
1 files changed, 31 insertions, 5 deletions
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()) |