tools: allow passing tilemap width and location to background_convert.py

This commit is contained in:
Pierre de La Morinerie 2021-01-09 09:27:31 +00:00
parent 029a1e04ef
commit ddb30d65ad
3 changed files with 13 additions and 14 deletions

View File

@ -12,10 +12,10 @@ To make change to a tilemap:
1. Decode the tilemap:
```
tools/convert_background.py --decode src/data/backgrounds/marin_beach.tilemap.encoded --output src/data/backgrounds/marin_beach.tilemap
tools/convert_background.py decode src/data/backgrounds/marin_beach.tilemap.encoded --output src/data/backgrounds/marin_beach.tilemap
```
2. Edit the tilemap, using your favorite editor.
3. Re-encode the modified tilemap:
```
tools/convert_background.py --encode src/data/backgrounds/marin_beach.tilemap --output src/data/backgrounds/marin_beach.tilemap.encoded
tools/convert_background.py encode src/data/backgrounds/marin_beach.tilemap --output src/data/backgrounds/marin_beach.tilemap.encoded
```

View File

@ -29,13 +29,16 @@ if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
options_parser = argparse.ArgumentParser(add_help=False)
options_parser.add_argument('--output', '-o', type=str, metavar='outfile', action='store', help='file to write the output to')
options_parser.add_argument('--wrap', '-w', type=int, metavar='char_count', default=40, action='store', help='wrap the stdout output to a number of characters (40 by default; 0 to disable)')
operations_subparser = arg_parser.add_subparsers(title='commands', dest='command', required=True)
decoding_parser = operations_subparser.add_parser('decode', parents=[options_parser], help='convert a tilemap encoded with the ZLADX format to a raw tilemap')
decoding_parser.add_argument('infile', type=str, help='encoded tilemap file to decode')
decoding_parser.add_argument('--wrap', '-w', type=int, metavar='char_count', default=40, action='store', help='wrap the stdout output to a number of characters (40 by default; 0 to disable)')
encoding_parser = operations_subparser.add_parser('encode', parents=[options_parser], help='convert a raw tilemap to the encoded ZLADX format')
encoding_parser.add_argument('infile', type=str, help='raw tilemap file to encode')
encoding_parser.add_argument('--width', type=int, metavar="tiles_count", default=20, action='store', help='number of tiles in the tilemap width')
encoding_parser.add_argument('--location', type=int, metavar="VRAM_address", default=0x9800, action='store', help='start address of the tilemap in VRAM')
args = arg_parser.parse_args()
@ -52,11 +55,8 @@ if __name__ == "__main__":
result.append(tilemap_bytes[address])
write_result(result, outfile, wrap_count=args.wrap)
elif args.encode:
# TODO: allow to specify the tilemap location and width from command-line argument
# (For now defaults of location=0x9800 and width=20 are assumed.)
result = BackgroundCoder.encode(data)
write_result(result, outfile, wrap_count=args.wrap)
elif args.command == 'encode':
result = BackgroundCoder.encode(data, args.location, args.width)
write_result(result, outfile)
outfile.close()

View File

@ -31,7 +31,7 @@ class BackgroundCoder:
return tilemap_bytes
@staticmethod
def encode(tilemap_bytes, tilemap_location=0x9800, tilemap_width=20):
def encode(bytes, tilemap_location=0x9800, tilemap_width=20):
"""
Encode a raw BG tilemap to the LADX commands format.
@ -47,16 +47,15 @@ class BackgroundCoder:
# Split the tilemap into chunks
chunk_size = tilemap_width
chunks = [tilemap_bytes[i:i + chunk_size] for i in range(0, len(tilemap_bytes), chunk_size)]
chunks = [bytes[i:i + chunk_size] for i in range(0, len(bytes), chunk_size)]
encoded_bytes = bytearray()
address = tilemap_location
for i, chunk in enumerate(chunks):
count = len(chunk)
for chunk in chunks:
encoded_bytes.append(address >> 8)
encoded_bytes.append(address & 0xFF)
encoded_bytes.append(count - 1)
encoded_bytes.append(len(chunk) - 1)
encoded_bytes.extend(chunk)
address += 0x20