diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/mkasset.py | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/tools/mkasset.py b/tools/mkasset.py index fd2ead7..ae27ead 100644 --- a/tools/mkasset.py +++ b/tools/mkasset.py | |||
@@ -51,10 +51,15 @@ def convert_tsx(input_filepath, output_filepath): | |||
51 | print(f"Max width: {max_tile_width}") | 51 | print(f"Max width: {max_tile_width}") |
52 | print(f"Max height: {max_tile_height}") | 52 | print(f"Max height: {max_tile_height}") |
53 | 53 | ||
54 | pixels = [] # List of byte arrays | ||
55 | pixels_offset = 0 | ||
56 | |||
54 | with open(output_filepath, 'bw') as output: | 57 | with open(output_filepath, 'bw') as output: |
58 | # Write the header. | ||
55 | output.write(ctypes.c_uint16(tile_count)) | 59 | output.write(ctypes.c_uint16(tile_count)) |
56 | output.write(ctypes.c_uint16(max_tile_width)) | 60 | # output.write(ctypes.c_uint16(max_tile_width)) |
57 | output.write(ctypes.c_uint16(max_tile_height)) | 61 | # output.write(ctypes.c_uint16(max_tile_height)) |
62 | output.write(ctypes.c_uint16(0)) # Pad. | ||
58 | 63 | ||
59 | num_tile = 0 | 64 | num_tile = 0 |
60 | for tile in root: | 65 | for tile in root: |
@@ -72,12 +77,23 @@ def convert_tsx(input_filepath, output_filepath): | |||
72 | tile_height = int(image.attrib["height"]) | 77 | tile_height = int(image.attrib["height"]) |
73 | tile_path = image.attrib["source"] | 78 | tile_path = image.attrib["source"] |
74 | 79 | ||
75 | output.write(ctypes.c_uint16(tile_width)) | 80 | assert (tile_width > 0) |
76 | output.write(ctypes.c_uint16(tile_height)) | 81 | assert (tile_height > 0) |
77 | 82 | ||
83 | tile_pixels_offset = pixels_offset | ||
78 | with Image.open(tile_path) as im: | 84 | with Image.open(tile_path) as im: |
79 | bytes = im.convert('RGBA').tobytes() | 85 | bytes = im.convert('RGBA').tobytes() |
80 | output.write(bytes) | 86 | pixels.append(bytes) |
87 | pixels_offset += len(bytes) | ||
88 | |||
89 | # Write the tile. | ||
90 | output.write(ctypes.c_uint16(tile_width)) | ||
91 | output.write(ctypes.c_uint16(tile_height)) | ||
92 | output.write(ctypes.c_uint32(tile_pixels_offset)) | ||
93 | |||
94 | # Write the pixel data. | ||
95 | for bytes in pixels: | ||
96 | output.write(bytes) | ||
81 | 97 | ||
82 | 98 | ||
83 | def convert_tmx(input_filepath, output_filepath): | 99 | def convert_tmx(input_filepath, output_filepath): |
@@ -96,23 +112,27 @@ def convert_tmx(input_filepath, output_filepath): | |||
96 | print(f"Tile width: {base_tile_width}") | 112 | print(f"Tile width: {base_tile_width}") |
97 | print(f"Tile height: {base_tile_height}") | 113 | print(f"Tile height: {base_tile_height}") |
98 | 114 | ||
99 | with open(output_filepath, 'bw') as output: | 115 | tileset_path = None |
100 | output.write(ctypes.c_uint16(map_width)) | ||
101 | output.write(ctypes.c_uint16(map_height)) | ||
102 | output.write(ctypes.c_uint16(base_tile_width)) | ||
103 | output.write(ctypes.c_uint16(base_tile_height)) | ||
104 | output.write(ctypes.c_uint16(num_layers)) | ||
105 | |||
106 | tileset_path = None | ||
107 | 116 | ||
117 | with open(output_filepath, 'bw') as output: | ||
108 | for child in root: | 118 | for child in root: |
109 | if child.tag == "tileset": | 119 | if child.tag == "tileset": |
120 | assert (not tileset_path) # Only supporting one tile set per map right now. | ||
121 | |||
110 | tileset = child | 122 | tileset = child |
111 | tileset_path = tileset.attrib["source"] | 123 | tileset_path = tileset.attrib["source"] |
112 | 124 | ||
113 | print(f"Tile set: {tileset_path}") | 125 | print(f"Tile set: {tileset_path}") |
114 | 126 | ||
115 | tileset_path = tileset_path.replace("tsx", "ts") | 127 | tileset_path = tileset_path.replace("tsx", "ts") |
128 | |||
129 | # Write the header. | ||
130 | output.write(to_char_array(tileset_path, MAX_PATH_LENGTH)) | ||
131 | output.write(ctypes.c_uint16(map_width)) | ||
132 | output.write(ctypes.c_uint16(map_height)) | ||
133 | output.write(ctypes.c_uint16(base_tile_width)) | ||
134 | output.write(ctypes.c_uint16(base_tile_height)) | ||
135 | output.write(ctypes.c_uint16(num_layers)) | ||
116 | elif child.tag == "layer": | 136 | elif child.tag == "layer": |
117 | layer = child | 137 | layer = child |
118 | layer_id = int(layer.attrib["id"]) | 138 | layer_id = int(layer.attrib["id"]) |
@@ -123,9 +143,6 @@ def convert_tmx(input_filepath, output_filepath): | |||
123 | print(f"Width: {layer_width}") | 143 | print(f"Width: {layer_width}") |
124 | print(f"Height: {layer_height}") | 144 | print(f"Height: {layer_height}") |
125 | 145 | ||
126 | assert (tileset_path) | ||
127 | output.write(to_char_array(tileset_path, MAX_PATH_LENGTH)) | ||
128 | |||
129 | # Assume the layer's dimensions matches the map's. | 146 | # Assume the layer's dimensions matches the map's. |
130 | assert (layer_width == map_width) | 147 | assert (layer_width == map_width) |
131 | assert (layer_height == map_height) | 148 | assert (layer_height == map_height) |
@@ -139,7 +156,9 @@ def convert_tmx(input_filepath, output_filepath): | |||
139 | for row in rows: | 156 | for row in rows: |
140 | tile_ids = [x.strip() for x in row.split(',') if x] | 157 | tile_ids = [x.strip() for x in row.split(',') if x] |
141 | for tile_id in tile_ids: | 158 | for tile_id in tile_ids: |
142 | output.write(ctypes.c_uint16(int(tile_id))) | 159 | # TODO: We need to handle 'firsgid' in TMX files. |
160 | # For now, assume it's 1 and do -1 to make the tiles 0-based. | ||
161 | output.write(ctypes.c_uint16(int(tile_id) - 1)) | ||
143 | 162 | ||
144 | 163 | ||
145 | def get_num_cols(image, sprite_width): | 164 | def get_num_cols(image, sprite_width): |