Process tileset.

This commit is contained in:
2025-09-12 12:43:56 -05:00
parent 46a94ecacd
commit 9b98181d28
18 changed files with 205 additions and 47 deletions

View File

@@ -3,6 +3,7 @@ import sys
from processimage import processImage
from processpalette import processPalette
from processconfig import processConfig
from processtileset import processTileset
processedAssets = []
@@ -20,8 +21,8 @@ def processAsset(asset):
return processImage(asset)
elif t == 'config':
return processConfig(asset)
# elif t == 'tileset':
# return processTileset(asset)
elif t == 'tileset':
return processTileset(asset)
else:
print(f"Error: Unknown asset type '{asset['type']}' for path '{asset['path']}'")
sys.exit(1)

View File

@@ -64,7 +64,10 @@ def processPalettizedImage(asset):
f.write(data)
outImage = {
"imagePath": outputFileRelative,
"files": [ outputFilePath ],
'width': image.width,
'height': image.height,
}
return outImage
@@ -90,6 +93,9 @@ def processAlphaImage(asset):
f.write(data)
outImage = {
"files": [ outputFilePath ]
"imagePath": outputFileRelative,
"files": [ outputFilePath ],
'width': image.width,
'height': image.height,
}
return outImage

View File

@@ -0,0 +1,84 @@
import json
from processimage import processImage
import sys
from assethelpers import getAssetRelativePath
import os
import datetime
from args import args
def processTileset(asset):
print(f"Processing tileset: {asset['path']}")
# We need to determine how big each tile is. This can either be provided as
# an arg of tileWidth/tileHeight or as a count of rows/columns.
# Additionally, if the image has been factored, then the user can provide both
# tile sizes AND cols/rows to indicate the original size of the image.
image = processImage(asset)
tileWidth, tileHeight = None, None
columns, rows = None, None
originalWidth, originalHeight = image['width'], image['height']
if 'tileWidth' in asset['options'] and 'columns' in asset['options']:
tileWidth = int(asset['options']['tileWidth'])
columns = int(asset['options']['columns'])
originalWidth = tileWidth * columns
elif 'tileWidth' in asset['options']:
tileWidth = int(asset['options']['tileWidth'])
columns = image['width'] // tileWidth
elif 'columns' in asset['options']:
columns = int(asset['options']['columns'])
tileWidth = image['width'] // columns
else:
print(f"Error: Tileset {asset['path']} must specify either tileWidth or columns")
sys.exit(1)
if 'tileHeight' in asset['options'] and 'rows' in asset['options']:
tileHeight = int(asset['options']['tileHeight'])
rows = int(asset['options']['rows'])
originalHeight = tileHeight * rows
elif 'tileHeight' in asset['options']:
tileHeight = int(asset['options']['tileHeight'])
rows = image['height'] // tileHeight
elif 'rows' in asset['options']:
rows = int(asset['options']['rows'])
tileHeight = image['height'] // rows
else:
print(f"Error: Tileset {asset['path']} must specify either tileHeight or rows")
sys.exit(1)
fileNameWithoutExtension = os.path.splitext(os.path.basename(asset['path']))[0]
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
tilesetName = fileNameWithoutExtension
tilesetNameUpper = tilesetName.upper()
widthScale = originalWidth / image['width']
heightScale = originalHeight / image['height']
# Create header
data = f"// Tileset Generated for {asset['path']} at {now}\n"
data += f"#pragma once\n"
data += f"#include \"display/tileset.h\"\n\n"
data += f"static const tileset_t TILESET_{tilesetNameUpper} = {{\n"
data += f" .tileWidth = {tileWidth},\n"
data += f" .tileHeight = {tileHeight},\n"
data += f" .tileCount = {columns * rows},\n"
data += f" .columns = {columns},\n"
data += f" .rows = {rows},\n"
data += f" .uv = {{ {widthScale / columns}f, {heightScale / rows}f }},\n"
data += f" .image = {json.dumps(image['imagePath'])},\n"
data += f"}};\n"
# Write Header
outputFile = os.path.join(args.headers_dir, "display", "tileset", f"tileset_{tilesetName}.h")
os.makedirs(os.path.dirname(outputFile), exist_ok=True)
with open(outputFile, 'w') as f:
f.write(data)
outTileset = {
"image": image,
"headerFile": outputFile,
"files": image['files'],
}
return outTileset