XMLGameEngine

An engine for describing games built around Xerces, exprtk, and SFML.

View the Project on GitHub beefviper/XMLGameEngine

42. Bitmap sprites and animation

Status: built (games/spaceinvaders.xml, tests/test_bitmap_sprites.cpp); new tags: <bitmap> (with <row>, <scale>), <animation> (with <interval>, <frame>), a name on <sprite>; <fire> now centers the projectile on the shooter

Why

Every picture so far was a plain shape, a text, an image file, or a drawing made of straight lines. A chunky arcade sprite (an alien, a ship) is none of those comfortably: lines have no fill, and an image file is not in the XML, so the picture cannot be read, edited or diffed with the rest of the game. Arcade sprites of that era were small grids of on and off pixels, and a text file shows such a grid directly. The second gap was movement without motion: nothing changed an object’s picture over time, which the invaders’ two-pose flap needs.

The language

A <bitmap> is a sprite written as rows of text. A period is a clear pixel, an asterisk a solid one, and <scale> says how many real pixels each character is across, so a handful of characters is a chunky sprite whatever the window size. It sits where <line>s sit, as the one thing in a <sprite>, and it can also be the shape a <grid> repeats.

An object that changes picture has several <sprite>s, each with a name, and an <animation> after them. The animation holds an <interval> (seconds per picture) and <frame sprite="name" /> elements that refer back to the sprites by name, in the order shown. Referring by name, rather than nesting the sprites inside the animation, keeps a sprite an ordinary sprite and lets one be used in more than one place in the sequence.

Decisions

Space Invaders as the test

The aliens are a group of three members (a row of squids, two of crabs, two of octopuses), each a grid of two bitmaps animated on a one-second interval; the ship is one bitmap; the bullet is a thin tall rectangle. Making the bullet thin showed that <fire> put the projectile’s left edge, not its middle, at the middle of the shooter, so it left from beside the barrel. It is now centered on the shooter’s top edge, as the docs said, which also moved the shots of Depth Charge and Astrosmash by half their width.

Moving the game from one grid of identical rectangles to three kinds of alien broke several tests that were only borrowing the game as a fixture for grid naming, swept hits, lockstep spacing and the win. They now load a frozen copy of the first game (tests/invaders_fixture.h), so they check the engine and do not change when the game does.

Options considered

Not done

Per-frame intervals, an animation that runs once, animations driven by a variable or by a collision, a color per pixel, and a tool to turn an image into rows of text.