Skip to content

Instantly share code, notes, and snippets.

@ISSOtm
Last active May 22, 2021 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ISSOtm/cfb3927a660303d2f063c194fdeb3e7f to your computer and use it in GitHub Desktop.
Save ISSOtm/cfb3927a660303d2f063c194fdeb3e7f to your computer and use it in GitHub Desktop.
CSV → Binary tilemap converter
#!/usr/bin/env python3
import sys
def main(argv):
if len(argv) != 3:
print(f"Usage: {argv[0} -(0|1) file.csv", file=sys.stderr)
return 1
if argv[1] == "-0":
bounds = (-128, 127)
elif argv[1] == "-1":
bounds = (0, 255)
else:
print(f'Expected "-0" or "-1" as first option (LCDC.4 setting), got "{argv[1]}"', file=sys.stderr)
return 2
try:
with open(argv[2]) as f:
for (y,line) in enumerate(f):
for (x,entry) in enumerate(map(int,line.split(','))):
if entry < bounds[0] or entry > bounds[1]:
print(f"Entry at x={x},y={y} ({entry}) not in [{bounds[0]}; {bounds[1]}]", file=sys.stderr)
return 1
sys.stdout.buffer.write(int(entry).to_bytes(1, 'little'))
except FileNotFoundError:
print("File not found: ", argv[2], file=sys.stderr)
return 3
if __name__ == "__main__":
# execute only if run as a script
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment