Skip to content

Commit bd535eb

Browse files
fix: placing sprites
1 parent e3ebcc2 commit bd535eb

File tree

5 files changed

+16
-31
lines changed

5 files changed

+16
-31
lines changed

system/lib/features/cut_sprites.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def render_objects(swf: SupercellSWF, output_folder: Path):
6060
for region_index in range(regions_count):
6161
region = shape.regions[region_index]
6262

63-
rendered_region = region.render(use_original_size=True)
63+
rendered_region = region.get_image()
6464
rendered_region.save(f"{output_folder}/shape_{shape.id}_{region_index}.png")
6565

6666
for shape_index in range(shapes_count):
@@ -78,5 +78,3 @@ def render_objects(swf: SupercellSWF, output_folder: Path):
7878
for i in range(region.get_point_count()):
7979
swf.xcod_writer.write_uint16(int(region.get_u(i)))
8080
swf.xcod_writer.write_uint16(int(region.get_v(i)))
81-
swf.xcod_writer.write_ubyte(1 if region.is_mirrored else 0)
82-
swf.xcod_writer.write_byte(region.rotation // 90)

system/lib/features/place_sprites.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,40 @@ def place_sprites(
5252
"L", texture_width, texture_height, region_info.points, MASK_COLOR
5353
)
5454

55-
if rect.width + rect.height <= 2:
55+
if rect.width == 0 or rect.height == 0:
5656
if rect.height != 0:
5757
for _y in range(int(rect.height)):
5858
img_mask.putpixel(
5959
(int(rect.right - 1), int(rect.top + _y - 1)), MASK_COLOR
6060
)
61+
rect.right += 1
6162
elif rect.width != 0:
6263
for _x in range(int(rect.width)):
6364
img_mask.putpixel(
6465
(int(rect.left + _x - 1), int(rect.bottom - 1)), MASK_COLOR
6566
)
67+
rect.bottom += 1
6668
else:
6769
img_mask.putpixel(
6870
(int(rect.right - 1), int(rect.bottom - 1)), MASK_COLOR
6971
)
72+
rect.right += 1
73+
rect.bottom += 1
7074

7175
x = int(rect.left)
7276
y = int(rect.top)
7377
width = int(rect.width)
7478
height = int(rect.height)
7579
bbox = int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)
7680

77-
tmp_region = Image.open(
81+
region_image = Image.open(
7882
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
7983
).convert("RGBA")
80-
if region_info.is_mirrored:
81-
tmp_region = tmp_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
82-
tmp_region = tmp_region.rotate(region_info.rotation, expand=True)
83-
tmp_region = tmp_region.resize((width, height), Image.Resampling.LANCZOS)
8484

8585
sheets[region_info.texture_id].paste(
8686
Image.new("RGBA", (width, height)), (x, y), img_mask.crop(bbox)
8787
)
88-
sheets[region_info.texture_id].paste(tmp_region, (x, y), tmp_region)
88+
sheets[region_info.texture_id].paste(region_image, (x, y), region_image)
8989
print()
9090

9191
return sheets

system/lib/math/polygon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def is_clockwise(polygon: Polygon) -> bool:
2525
def compare_polygons(
2626
polygon1: Polygon,
2727
polygon2: Polygon,
28-
) -> tuple[int, bool]:
28+
) -> tuple[float, bool]:
2929
"""Calculates rotation and if polygon is mirrored.
3030
3131
:param polygon1: shape polygon
@@ -48,7 +48,7 @@ def compare_polygons(
4848

4949
angle = (angle_xy - angle_uv) % 360
5050

51-
return int(angle), mirroring
51+
return angle, mirroring
5252

5353

5454
def get_rect(polygon: list[Point]) -> Rect:

system/lib/objects/shape/region.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
class Region:
1919
def __init__(self):
20-
self.texture_index = 0
21-
self.rotation = 0
22-
self.is_mirrored = False
20+
self.texture_index: int = 0
2321

2422
self._point_count = 0
2523
self._xy_points: list[Point] = []
@@ -52,17 +50,13 @@ def load(self, swf: SupercellSWF, tag: int):
5250

5351
self._uv_points[i] = Point(u, v)
5452

55-
def render(
56-
self, matrix: Matrix2x3 | None = None, use_original_size: bool = False
57-
) -> Image.Image:
53+
def render(self, matrix: Matrix2x3 | None = None) -> Image.Image:
5854
transformed_points = self.apply_matrix(matrix)
5955

6056
rect = get_rect(transformed_points)
6157
width, height = max(int(rect.width), 1), max(int(rect.height), 1)
6258

63-
self.rotation, self.is_mirrored = compare_polygons(
64-
transformed_points, self._uv_points
65-
)
59+
rotation, is_mirrored = compare_polygons(transformed_points, self._uv_points)
6660

6761
rendered_region = self.get_image()
6862
if rendered_region.width + rendered_region.height <= 2:
@@ -72,12 +66,9 @@ def render(
7266
rendered_region.mode, width, height, transformed_points, fill_color
7367
)
7468

75-
if self.is_mirrored:
69+
if is_mirrored:
7670
rendered_region = rendered_region.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
77-
rendered_region = rendered_region.rotate(-self.rotation, expand=True)
78-
79-
if use_original_size:
80-
return rendered_region
71+
rendered_region = rendered_region.rotate(-rotation, expand=True)
8172

8273
return rendered_region.resize((width, height), Image.Resampling.LANCZOS)
8374

system/lib/xcod.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ def height(self) -> int:
3030
class RegionInfo:
3131
texture_id: int
3232
points: list[Point]
33-
is_mirrored: bool
34-
rotation: int
3533

3634

3735
@dataclass
@@ -95,9 +93,7 @@ def parse_detailed_info(file_info: FileInfo, reader: Reader) -> None:
9593
for _ in range(points_count)
9694
]
9795

98-
is_mirrored, rotation = reader.read_uchar() == 1, reader.read_char() * 90
99-
100-
regions.append(RegionInfo(texture_id, points, is_mirrored, rotation))
96+
regions.append(RegionInfo(texture_id, points))
10197

10298
file_info.shapes.append(ShapeInfo(shape_id, regions))
10399

0 commit comments

Comments
 (0)