|
@ -16,7 +16,9 @@ def abort(msg: str) -> NoReturn: |
|
|
sys.exit(1) |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_coll_map(in_path: pathlib.Path, width: int, height: int) -> str: |
|
|
|
|
|
|
|
|
def generate_coll_map( |
|
|
|
|
|
in_path: pathlib.Path, width: int, height: int, compress: bool = False |
|
|
|
|
|
) -> str: |
|
|
png = Image.open(in_path).convert("RGB") |
|
|
png = Image.open(in_path).convert("RGB") |
|
|
if png.width % 8 != 0 or png.height % 8 != 0: |
|
|
if png.width % 8 != 0 or png.height % 8 != 0: |
|
|
abort(f"file '{in_path}' has invalid dimensions (should be multiple of 8)") |
|
|
abort(f"file '{in_path}' has invalid dimensions (should be multiple of 8)") |
|
@ -33,37 +35,35 @@ def generate_coll_map(in_path: pathlib.Path, width: int, height: int) -> str: |
|
|
|
|
|
|
|
|
bit = None |
|
|
bit = None |
|
|
if pixel == RED: |
|
|
if pixel == RED: |
|
|
bit = 1 |
|
|
|
|
|
elif pixel == GREEN: |
|
|
|
|
|
bit = 0 |
|
|
bit = 0 |
|
|
|
|
|
elif pixel == GREEN: |
|
|
|
|
|
bit = 1 |
|
|
else: |
|
|
else: |
|
|
abort(f"unsupported pixel in collision map: {pixel}") |
|
|
abort(f"unsupported pixel in collision map: {pixel}") |
|
|
|
|
|
|
|
|
bits.append(bit) |
|
|
|
|
|
if len(bits) == 8: |
|
|
|
|
|
byte = sum([bit << i for i, bit in enumerate(bits)]) |
|
|
|
|
|
out_bytes.append(byte) |
|
|
|
|
|
bits = [] |
|
|
|
|
|
|
|
|
if compress: |
|
|
|
|
|
bits.append(bit) |
|
|
|
|
|
if len(bits) == 8: |
|
|
|
|
|
byte = sum([bit << i for i, bit in enumerate(bits)]) |
|
|
|
|
|
out_bytes.append(byte) |
|
|
|
|
|
bits = [] |
|
|
|
|
|
else: |
|
|
|
|
|
out_bytes.append(bit) |
|
|
|
|
|
|
|
|
png.close() |
|
|
png.close() |
|
|
|
|
|
|
|
|
lines = [] |
|
|
|
|
|
for line_no in range(0, len(out_bytes), 16): |
|
|
|
|
|
line = out_bytes[line_no : line_no + 16] |
|
|
|
|
|
lines.append(" DB " + ", ".join(["$%02X" % b for b in line])) |
|
|
|
|
|
|
|
|
|
|
|
return "\n".join(lines) |
|
|
|
|
|
|
|
|
return format_bytes(out_bytes, width=width) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_bytes(data: bytes) -> str: |
|
|
|
|
|
|
|
|
def format_bytes(data: bytes, width: int = 16) -> str: |
|
|
|
|
|
print(f"formatting with width={width}") |
|
|
lines = [] |
|
|
lines = [] |
|
|
for line_no in range(0, len(data), 16): |
|
|
|
|
|
line = data[line_no : line_no + 16] |
|
|
|
|
|
|
|
|
for line_no in range(0, len(data), width): |
|
|
|
|
|
line = data[line_no : line_no + width] |
|
|
lines.append(" DB " + ", ".join(["$%02X" % b for b in line])) |
|
|
lines.append(" DB " + ", ".join(["$%02X" % b for b in line])) |
|
|
return "\n".join(lines) |
|
|
return "\n".join(lines) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_map(pngfile: str) -> None: |
|
|
|
|
|
|
|
|
def generate_map(pngfile: str, compress: bool = False) -> None: |
|
|
pngpath = pathlib.Path(pngfile).resolve() |
|
|
pngpath = pathlib.Path(pngfile).resolve() |
|
|
incpath = pngpath.parent / pngpath.name.replace(".png", ".inc") |
|
|
incpath = pngpath.parent / pngpath.name.replace(".png", ".inc") |
|
|
spath = pngpath.parent / pngpath.name.replace(".png", ".s") |
|
|
spath = pngpath.parent / pngpath.name.replace(".png", ".s") |
|
@ -90,13 +90,13 @@ def generate_map(pngfile: str) -> None: |
|
|
] |
|
|
] |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
map_data = format_bytes(tilef.read()) |
|
|
|
|
|
|
|
|
map_data = format_bytes(tilef.read(), width=width) |
|
|
tile_data = format_bytes(mapf.read()) |
|
|
tile_data = format_bytes(mapf.read()) |
|
|
|
|
|
|
|
|
section = pngpath.name.replace(".png", "") |
|
|
section = pngpath.name.replace(".png", "") |
|
|
|
|
|
|
|
|
collpath = pngpath.parent / pngpath.name.replace(".png", "_coll.png") |
|
|
collpath = pngpath.parent / pngpath.name.replace(".png", "_coll.png") |
|
|
coll_map = generate_coll_map(collpath, width, height) |
|
|
|
|
|
|
|
|
coll_map = generate_coll_map(collpath, width, height, compress=compress) |
|
|
|
|
|
|
|
|
with open(incpath, "w") as outf: |
|
|
with open(incpath, "w") as outf: |
|
|
outf.write( |
|
|
outf.write( |
|
@ -131,9 +131,10 @@ ASSERT {section}_MAP_SIZE == {section}_WIDTH * {section}_HEIGHT |
|
|
|
|
|
|
|
|
def main() -> None: |
|
|
def main() -> None: |
|
|
parser = argparse.ArgumentParser("generate_map") |
|
|
parser = argparse.ArgumentParser("generate_map") |
|
|
|
|
|
parser.add_argument("-c", "--compress", default=False) |
|
|
parser.add_argument("png") |
|
|
parser.add_argument("png") |
|
|
args = parser.parse_args() |
|
|
args = parser.parse_args() |
|
|
generate_map(args.png) |
|
|
|
|
|
|
|
|
generate_map(args.png, compress=(not not args.compress)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|