Skip to content

2D engine

Everything on a DS screen that is not 3D is drawn by a 2D engine: text boxes, menus, the battle’s health bars, the location banner, weather, the whole field of a tile-map game. There are two engines, one for each screen (see Two screens and the pen), and they work the same way.

A 2D engine can show a plain picture (a bitmap), but one full screen at 15 bits a pixel is 96 KB of video memory, and changing it means rewriting thousands of pixels. So games mostly let the engine build each line of the picture as it is sent to the screen, from small reusable pieces:

  • A tile is an 8 × 8 picture. With 16 colours it takes 32 bytes; with 256 colours, 64 bytes.
  • A tile map is a grid of 16-bit entries, one per 8 × 8 cell of a layer. Each entry names a tile, a palette, and whether to flip the tile left to right or upside down.
  • A palette is a list of colours. A tile’s pixels are numbers into its palette, not colours.

A screen of grass made of one repeated tile costs 32 bytes of picture and a map. A window frame is nine tiles. A tree drawn once can appear anywhere, flipped for its other half. That is why DS and GBA art is made of repeating 8 × 8 blocks.

Palettes give a second saving: changing one colour of a palette recolours every pixel that uses it, at once, for free. Menus show a selected button by switching its palette; the field’s time of day multiplies its palettes.

A DS colour is 15 bits: 5 bits each of red, green and blue, 32 levels each. The engines work inside with one more bit a channel, and the screen widens that again.

  • 16-colour tiles pick one of 16 palettes of 16 colours. Colour 0 of every palette is transparent: what is behind shows through.
  • 256-colour tiles use one palette of 256 colours, or, with extended palettes, one of 16 palettes of 256 colours for each layer. Extended palettes live in video memory banks set aside for them.

Each engine has room for 256 layer colours and 256 sprite colours in its own palette memory; extended palettes add more from video memory.

Each engine has four background layers. A layer is a tile map that can be scrolled by any number of pixels, and it wraps around at its edges. A layer’s map is 32 × 32, 64 × 32, 32 × 64 or 64 × 64 tiles, so 256 to 512 pixels each way: a little more than a screen, so new rows can be written just out of sight while the layer scrolls.

The engine’s mode decides what each layer can be:

Kind of layer What it is
Tile layer 16- or 256-colour tiles, scrolled, flipped per tile. The everyday kind.
Rotating layer A 256-colour tile map that can be scaled and rotated as a whole
Extended layer A rotating layer with full 16-bit map entries, or a bitmap with no tiles
3D layer On the main engine, layer 0 can show the 3D engine’s picture instead

Most DS menus and every GBA-style field use plain tile layers.

Sprites are the pictures that move freely: cursors, icons, a creature’s menu sprite, rain drops. Each engine has 128, described in a small table (the object attribute memory). Each entry says where on the screen, which tiles, which palette and:

  • Size and shape: twelve sizes from 8 × 8 to 64 × 64, square, wide or tall.
  • Flip left to right and upside down.
  • Scale and rotation by one of 32 shared settings, optionally in a box twice the size so the turned picture is not cut off.
  • Translucency: blended with what is behind it.
  • Priority: which layers it is in front of.

A sprite’s tiles live in sprite memory, separate from layer tiles. Sprites can use 16- or 256-colour tiles. There is also a limit on how many sprite pixels one line of the screen can draw, so a line crowded with very large sprites may lose some.

Priority settles who is in front. Each layer and each sprite has a priority from 0 (front) to 3 (back). Between layers of equal priority the lower-numbered layer is in front; a sprite is in front of a layer of the same priority; between sprites of equal priority the earlier entry wins. This is how a character walks behind a roof: the roof is on a layer with a higher priority than the character’s sprite.

Windows are rectangles (two of them, plus one shaped by sprites) inside which some layers are hidden or blending is turned off. Games use them for spotlights, wipes, and to cut a scrolling list to a box.

Blending mixes two pictures. Alpha blending adds a share of the top and a share of what is below, each counted out of 16. A fade brightens or darkens toward white or black. Each screen also has a master brightness for whole-screen fades.

Each frame the engine lists, per screen, the four layers (on or off, priority, scroll), up to 128 sprites, up to 32 scale-and-rotate settings, a fade and one blend setting. The tiles, maps and palettes themselves are loaded into video memory when a scene starts, not every frame. This is the DS’s own model, so on the DS the backend only copies it into the hardware’s registers at the vertical blank.

  • Mode. Both engines run four plain tile layers. On the top screen, layer 0 is the 3D picture whenever there is a 3D scene.
  • Tiles. Each screen has room for 1,024 tiles of 16 colours and four maps. Map entries use the DS’s 16-bit layout, so they go to video memory unchanged.
  • 256 colours. In the tile-map style (no 3D on top), the top screen switches to an extended layout: the 3D engine’s texture memory becomes layer memory, with 1,024 tiles of 256 colours and 16 extended palettes per layer. See GBA tile worlds and The tile-map style.
  • Sprites. 16 colours each, all twelve sizes, flips, scale and rotation, and translucency mixed by the screen’s one blend setting (8 parts sprite to 8 parts background unless a screen says otherwise). Sprite memory is 512 tiles on the top screen and 1,024 on the lower.
  • Fades are the master brightness: from black (-16) through unchanged (0) to white (16).
  • Text is drawn into tiles in main memory, a glyph at a time, and the changed tiles are sent to video memory. See How menus are built from tiles.

Not used: rotating and bitmap layers, the hardware windows and mosaic.

Planned (plan 37): in the tile-map style, 256-colour sprites with extended palettes for large set pieces that move or fade, in a bank the 3D engine leaves free.

On desktop the software renderer composes the same layers and sprites by the same rules: the same priority order, colour 0 transparent, the same blend arithmetic, and colours widened from 5 bits to 8 the way the DS’s screen does it. So the desktop’s 2D equals melonDS’s pixel for pixel, and one test picture covers both platforms.