Console::Sprites
console 0.2.0 · app/console/sprites.rb · 298 lines · view source
Console::Sprites -- name -> texture resolution, plus procedural generation.
Two jobs:
1. RESOLUTION. Carts refer to sprites by short name (:star, :'misc/star') instead of by path. The index is built once at boot by walking the sprites tree of the booted cart, then the console's own (DR.list_files is not recursive). The cart's files win, so two carts can both have a sprites/hero.png without colliding.
2. ZERO-ART PROTOTYPING. auto will synthesise a placeholder texture for any name that has no file, so a cart can be written and played before any art exists. Generated textures are deterministic, so they double as test fixtures.
indexing
def index!-
Build the name -> path index. Cheap enough to call once at boot.
The cart's own tree is walked first, so a cart's
hero.pngshadows the console's; the console root is then walked as starter art.||=in walk is what makes "first one wins" true. def indexed?- line 48
def walk(dir, prefix)- line 52
def register(name, path)-
Register an explicit alias. Use this for names that should be short. A path is resolved against the booted cart, so
register :ship, 'sprites/hero/idle/0.png'inside a cart means that cart's file.
resolution
def path(name)-
Resolve a sprite reference to something renderable.
Order: explicit registry -> index (with and without extension) -> already-generated procedural texture -> a visible warning + :solid.
def note_missing(key)- line 109
def missing_names- line 113
def exists?(name)- line 117
def generated?(name)- line 123
def size(name, args = nil)-
Natural [w, h] of a sprite reference.
get_sprite_rect only understands file paths, so generated textures are measured from the pixel array itself.
animation groups
def frames(group)-
Collect a numbered directory of frames into an ordered Array of paths.
sprites/hero/run/0.png .. 3.png-> frames('hero/run')Falls back to a single frame holding the base sprite. The directory is resolved like every other asset, so this reads the booted cart's frames.
def animation?(group)- line 170
def numeric_prefix(filename)-
True if every entry in
namelooks like an indexed frame.
procedural generation
def auto(args, name, w = 16, h = 16, color = :accent, pattern = :solid)-
Create (or fetch) a procedural texture.
autois the forgiving version: it never overwrites a real file on disk. def generate(args, name, w, h, color = :accent, pattern = :solid)-
Force-generate a texture, even if a file exists.
8-bit helpers
def rgba(rgb, a = 255)- line 223
def put(args, key, x, y, value)-
Pixel arrays index from the top-left, while the console talks in bottom-left screen coordinates. These helpers take bottom-left coords.
def checker(args, key, w, h, rgb)-
NOTE: Numeric#zmod? is a boolean divisibility predicate ("is this evenly divisible?"), NOT a modulo. Use % for modulo maths.
def stripes(args, key, w, h, rgb)- line 249
def frame_pixels(args, key, w, h, rgb)- line 258
def circle(args, key, w, h, rgb)- line 268
def ring(args, key, w, h, rgb)- line 282