-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (86 loc) · 3.3 KB
/
main.py
File metadata and controls
100 lines (86 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import sys
import os
import argparse
from pathlib import Path
from PIL import Image
# Add src to path if needed
sys.path.append(str(Path(__file__).parent))
from pr32_sprite_compiler.gui.main_window import MainWindow
from pr32_sprite_compiler.core.exporter import Exporter
from pr32_sprite_compiler.core.models import SpriteDefinition, CompilationOptions
def run_cli(args):
"""Executes the compilation from command line arguments."""
try:
input_path = Path(args.input)
if not input_path.exists():
print(f"ERROR: Input file not found: {input_path}")
return 1
img = Image.open(input_path).convert("RGBA")
# Parse grid
try:
gw, gh = map(int, args.grid.lower().split('x'))
except:
print(f"ERROR: Invalid grid format '{args.grid}'. Use WxH (e.g. 16x16)")
return 1
# Parse offset
ox, oy = 0, 0
if args.offset:
try:
ox, oy = map(int, args.offset.split(','))
except:
print(f"ERROR: Invalid offset format '{args.offset}'. Use X,Y (e.g. 0,10)")
return 1
# Parse sprites
sprites = []
if not args.sprite:
print("ERROR: No sprites defined. Use --sprite gx,gy,gw,gh at least once.")
return 1
for i, s_str in enumerate(args.sprite):
try:
gx, gy, sw, sh = map(int, s_str.split(','))
sprites.append(SpriteDefinition(gx, gy, sw, sh, i))
except:
print(f"ERROR: Invalid sprite format '{s_str}'. Use gx,gy,gw,gh")
return 1
options = CompilationOptions(
output_path=args.out,
grid_w=gw,
grid_h=gh,
offset_x=ox,
offset_y=oy,
mode=args.mode,
name_prefix=args.prefix or ""
)
print(f"Compiling {len(sprites)} sprites...")
if Exporter.export(img, sprites, options):
print(f"OK: Generated {args.out}")
return 0
else:
print("ERROR: Export failed.")
return 1
except Exception as e:
print(f"CRITICAL ERROR: {e}")
return 1
def main():
if len(sys.argv) > 1:
# CLI Mode
parser = argparse.ArgumentParser(description="PixelRoot32 Sprite Compiler CLI")
parser.add_argument("input", help="Input PNG file")
parser.add_argument("--grid", required=True, help="Grid size (WxH, e.g. 16x16)")
parser.add_argument("--offset", help="Initial offset (X,Y, e.g. 0,0)")
parser.add_argument("--sprite", action="append", help="Sprite definition (gx,gy,gw,gh). Can be used multiple times.")
parser.add_argument("--out", default="sprites.h", help="Output header file (.h)")
parser.add_argument("--mode", choices=["layered", "2bpp", "4bpp"], default="layered", help="Export mode")
parser.add_argument("--prefix", help="Prefix for the generated sprite names")
args = parser.parse_args()
sys.exit(run_cli(args))
else:
# GUI Mode
try:
app = MainWindow()
app.mainloop()
except Exception as e:
print(f"Critical error: {e}")
input("Press Enter to exit...")
if __name__ == "__main__":
main()