blob: b5c5b2eba75f5258714f80b61eb7dfe56629816a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#!/usr/bin/env python
import sys
from math import ceil, log
from PIL import Image
outfile = "map.tiles"
ATLASFILE = "atlas.png"
TILESIZE = 16
WIDTH = 2**10
HEIGHT = 2**10
# 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 = ceil(log(tilecount, 256))
HEADER = "TILEFILEv1"
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'));
for y in range(HEIGHT):
for x in range(WIDTH):
index = (x + y) % 5
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())
|