143 lines
4.8 KiB
Python
143 lines
4.8 KiB
Python
from __future__ import annotations
|
|
|
|
import pygame
|
|
|
|
from typing import Final
|
|
|
|
from data.constants import Color
|
|
from data.sprites import SPRITES, TRANSPARENT
|
|
|
|
_cache: dict[tuple, pygame.Surface] = {}
|
|
|
|
|
|
def _rasterize(
|
|
names: tuple[str, ...],
|
|
px: int,
|
|
tint: Color | None,
|
|
flip_x: bool,
|
|
) -> pygame.Surface:
|
|
sprites = [SPRITES[name] for name in names]
|
|
cells_w = max(len(sprite.grid[0]) for sprite in sprites)
|
|
cells_h = max(len(sprite.grid) for sprite in sprites)
|
|
surface = pygame.Surface((cells_w * px, cells_h * px), pygame.SRCALPHA)
|
|
for sprite in sprites:
|
|
rows = sprite.grid
|
|
off_x = (cells_w - len(rows[0])) * px // 2
|
|
off_y = (cells_h - len(rows)) * px // 2
|
|
for row, line in enumerate(rows):
|
|
for col, key in enumerate(line):
|
|
if key == TRANSPARENT:
|
|
continue
|
|
color = tint if tint is not None else sprite.palette[key]
|
|
surface.fill(color, (off_x + col * px, off_y + row * px, px, px))
|
|
if flip_x:
|
|
surface = pygame.transform.flip(surface, True, False)
|
|
return surface
|
|
|
|
|
|
def sprite_surface(
|
|
names: str | tuple[str, ...],
|
|
px: int = 1,
|
|
tint: Color | None = None,
|
|
flip_x: bool = False,
|
|
) -> pygame.Surface:
|
|
"""A cached rasterization of one sprite or a stack of overlay layers."""
|
|
if isinstance(names, str):
|
|
names = (names,)
|
|
key = (names, px, tint, flip_x)
|
|
cached = _cache.get(key)
|
|
if cached is None:
|
|
cached = _rasterize(names, px, tint, flip_x)
|
|
_cache[key] = cached
|
|
return cached
|
|
|
|
|
|
def clear_cache() -> None:
|
|
_cache.clear()
|
|
|
|
|
|
def cache_size() -> int:
|
|
return len(_cache)
|
|
|
|
|
|
def distinct_color_count(surface: pygame.Surface) -> int:
|
|
"""How many opaque colors a sprite actually shows (flat-art detector)."""
|
|
seen: set[tuple[int, int, int, int]] = set()
|
|
for x in range(surface.get_width()):
|
|
for y in range(surface.get_height()):
|
|
pixel = tuple(surface.get_at((x, y)))
|
|
if pixel[3] > 0:
|
|
seen.add(pixel)
|
|
return len(seen)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Run from the repo root: python -m render.sprites
|
|
import os
|
|
|
|
os.environ.setdefault("SDL_VIDEODRIVER", "dummy")
|
|
os.environ.setdefault("SDL_AUDIODRIVER", "dummy")
|
|
pygame.init()
|
|
pygame.display.set_mode((1, 1))
|
|
|
|
from data.enemies import ROSTER
|
|
from data.items import CATALOG
|
|
from data.sprites import player_layers
|
|
|
|
|
|
|
|
for name in SPRITES:
|
|
flat = sprite_surface(name)
|
|
assert flat.get_size() == (
|
|
len(SPRITES[name].grid[0]) * 1,
|
|
len(SPRITES[name].grid) * 1,
|
|
)
|
|
assert distinct_color_count(flat) >= 3, f"{name} rasterizes too flat"
|
|
doubled = sprite_surface("rat", px=2)
|
|
assert doubled.get_size() == (32, 32)
|
|
|
|
# every enemy and item is visually distinct: no two share a pixel signature
|
|
signatures: dict[tuple, str] = {}
|
|
for template in ROSTER.values():
|
|
surface = sprite_surface(template.sprite_key)
|
|
signature = tuple(
|
|
tuple(surface.get_at((x, y))) for x in range(16) for y in range(16)
|
|
)
|
|
assert signature not in signatures or signatures[signature] == template.sprite_key
|
|
signatures[signature] = template.sprite_key
|
|
item_signatures: set[tuple] = set()
|
|
for key in CATALOG:
|
|
surface = sprite_surface(f"item_{key}")
|
|
signature = tuple(
|
|
tuple(surface.get_at((x, y))) for x in range(16) for y in range(16)
|
|
)
|
|
assert signature not in item_signatures, f"item art collision: {key}"
|
|
item_signatures.add(signature)
|
|
|
|
# equipment visibly changes the player sprite, layer by layer
|
|
bare = sprite_surface(player_layers(None, None, None))
|
|
with_axe = sprite_surface(player_layers("battle_axe", None, None))
|
|
with_armor = sprite_surface(player_layers(None, "plate_armor", None))
|
|
with_boots = sprite_surface(player_layers(None, None, "shoes_of_hermes"))
|
|
full = sprite_surface(
|
|
player_layers("hunters_bow", "chain_mail", "shoes_of_hermes")
|
|
)
|
|
flipped = sprite_surface(player_layers("short_sword", None, None), flip_x=True)
|
|
assert distinct_color_count(full) > distinct_color_count(bare)
|
|
assert tuple(bare.get_at((13, 6)))[3] == 0 # weapon column empty when bare
|
|
assert tuple(with_axe.get_at((13, 6)))[3] > 0 # axe pixels appear there
|
|
assert tuple(flipped.get_at((2, 6)))[3] > 0 # sword mirrored to the left
|
|
|
|
# hit-flash tint keeps silhouette, becomes white
|
|
flash = sprite_surface("goblin", tint=(255, 255, 255))
|
|
colors = {
|
|
tuple(flash.get_at((x, y)))
|
|
for x in range(16)
|
|
for y in range(16)
|
|
if flash.get_at((x, y))[3] > 0
|
|
}
|
|
assert colors == {(255, 255, 255, 255)}
|
|
|
|
assert cache_size() > 40 # everything above was served through the cache
|
|
print(f"render.sprites self-check: PASS ({cache_size()} cached surfaces)")
|