OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Dynamic Sprite -- VRAM Streaming for Animated Sprites
Screenshot

What This Example Shows

How to use the dynamic sprite system to stream sprite tile data into VRAM every frame. Unlike static sprites (where all frames live in VRAM permanently), dynamic sprites upload only the current animation frame, saving VRAM space when sprites have many frames.

This demo displays four data shards, 16x16 sprites, each turning through 24 frames of animation. SELECT switches to the next step of the same engine: metasprites — Kit, built from several OBJ at 16x16, 32x48 and 64x64 — streamed the same way under the three OBJSEL size pairs.

Prerequisites

Read sprites/simple_sprite first (OAM basics), then sprites/animated_sprite (frame-based animation).

Controls

Button Action
SELECT Switch between the four animated sprites and the metasprite scene
D-PAD Up/Down (metasprite scene) switch the OBJSEL size pair: 8/16, 8/32, 16/32

At boot the four sprites animate by themselves.

Build & Run

cd $OPENSNES_HOME
make -C examples/sprites/dynamic_sprite

Then open dynamic_sprite.sfc in luna (luna-gui dynamic_sprite.sfc) or any emulator.

How It Works

1. Initialize the dynamic sprite engine

static const OamDynamicConfig dyn = {
.vramLarge = 0x0000,
.vramSmall = 0x1000,
.slotLargeInit = 0,
.slotSmallInit = 0,
.sizeMode = OBJ_SIZE8_L16,
};
void oamDynamicInit(const OamDynamicConfig *cfg)
Initialize the dynamic sprite engine from a config struct.
#define OBJ_SIZE8_L16
Sprite size indices (for oamInit, oamInitGfxSet).
Definition sprite.h:52
Configuration for the dynamic sprite engine.
Definition sprite.h:748

This configures the engine:

  • VRAM base for large tiles: $0000
  • VRAM base for small tiles: $1000
  • Sprite size pair: 8x8 small / 16x16 large

2. Set up sprites via the oambuffer

oambuffer[0].oamx = 64;
oambuffer[0].oamy = 100;
oambuffer[0].oamframeid = 0;
oambuffer[0].oamattribute = OBJ_PRIO(3);
oambuffer[0].oamrefresh = 1;
OAM_SET_GFX(0, spr16_tiles);
#define OAM_SET_GFX(id, gfx)
Set sprite graphics address — any bank.
Definition sprite.h:182
t_sprites oambuffer[128]
Dynamic sprite buffer (128 entries, 2048 bytes).
#define OBJ_PRIO(prio)
Metasprite priority attribute macro.
Definition sprite.h:663

Each dynamic sprite has:

  • oamframeid: which animation frame to display (index into the sprite sheet)
  • oamrefresh: set to 1 when the frame changes – tells the engine to re-upload tiles
  • OAM_SET_GFX: points to the source tile data in ROM

3. Draw and upload every frame

oamDynamicDraw(0); /* Queue sprite 0; NMI auto-flushes the VRAM tile
* queue and hides last frame's leftovers. */
void oamDynamicDraw(u16 id)
Draw a dynamic sprite — engine picks the size routine.

The draw call runs every frame in the main loop. Only sprites with oamrefresh = 1 actually trigger a DMA transfer. The NMI handler finalises the frame automatically — it hides leftover sprites, resets the slot allocator, and DMAs the queued tiles to VRAM — so user code never needs to issue a manual flush.

4. Animate by changing frame IDs

if (frame0 >= 24) frame0 = 0;
oambuffer[0].oamframeid = frame0;
oambuffer[0].oamrefresh = 1;
static u16 frame0
Animation frame index for sprite 0 (0-23, wraps at 24).
Definition main.c:81

Every 8 frames, the animation advances. Setting oamrefresh = 1 tells the engine to upload the new frame's tiles to VRAM on the next VBlank.

The Metasprite Scene

A metasprite is a list of pieces, each an offset, a tile and attributes:

static const MetaspriteItem hero16_frame0[] = {
METASPR_ITEM(0, 0, 0, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(16, 0, 1, OBJ_PAL(0) | OBJ_PRIO(3)),
...
};
oamMetaDrawDyn(10, 192, 140, hero16_frame0, spritehero16_tiles, OBJ_SMALL);
#define METASPR_ITEM(dx, dy, tile, attr)
Metasprite item macro (PVSnesLib compatible).
Definition sprite.h:651
#define OBJ_SMALL
Sprite size selection.
Definition sprite.h:102
#define METASPR_TERM
End marker for metasprite data (dx = -128).
Definition sprite.h:654
void oamMetaDrawDyn(u16 id, s16 x, s16 y, const MetaspriteItem *meta, const u8 *gfxptr, u8 size_class)
Draw a dynamic metasprite — engine picks the size routine.
#define OBJ_PAL(pal)
Metasprite palette attribute macro.
Definition sprite.h:660
static const MetaspriteItem hero16_frame0[]
16x16 metasprite: Kit 32x48 (2x3 grid of 16x16)
Definition main.c:103
Metasprite item structure (PVSnesLib compatible).
Definition sprite.h:639

oamMetaDrawDyn(id, x, y, meta, gfx, size_class) streams each piece's tiles with the dynamic engine; OBJ_SMALL / OBJ_LARGE say which half of the OBJSEL pair the pieces use. Changing the pair (UP/DOWN) re-runs oamDynamicInit() with a new sizeMode under force blank, and oamDynamicDrainQueue() uploads both heroes before the screen comes back on. The menu is text on BG1 (Mode 0, font at VRAM $3000, map at $3800); the engine's tiles stay at $0000 (large) and $1000 (small). The scene was the dynamic_metasprite example, a port of PVSnesLib's DynamicEngineMetaSprite.

SNES Concepts

VRAM Budget During VBlank

The SNES can only write to VRAM during VBlank. With DMA, you get roughly 4-5 KB per frame. Each 16x16 sprite frame is 128 bytes (4 tiles x 32 bytes). With 4 sprites updating simultaneously, that is 512 bytes – well within budget.

Static vs Dynamic Sprites

  • Static: All frames pre-loaded in VRAM. Frame selection is instant (just change the tile number). Downside: a 24-frame sprite uses 3 KB of VRAM permanently.
  • Dynamic: Only the current frame lives in VRAM. Supports hundreds of frames with minimal VRAM usage. Downside: each frame change costs DMA time during VBlank.

Dynamic sprites are the right choice when sprites have many animation frames (RPG characters with walk cycles, attack animations, etc.) or when multiple unique characters share limited VRAM space.

Force Blank for Initial Setup

This demo uses setScreenOff() (which sets REG_INIDISP = 0x80) during setup to allow unlimited VRAM writes. During gameplay, writes happen via DMA in VBlank only.

Project Structure

File Purpose
main.c Dynamic sprite initialization, animation loop
res/*.png.toml the import settings of the four sheets (opensnes-sprite, the three Kit sheets with metasprite); the build converts them and links the result, res/<name>.inc declares the symbols
res/sprite16_grid.png the data shard turning, 24 16x16 frames, 8 a row
res/spritehero8.png, spritehero16.png, spritehero32.png Kit at 16x16, 32x48 and 64x64, one frame each, on one palette (the metasprite scene loads one)

The shard and Kit are the Néo-Lumen art (PixelLab, CC0), turned and indexed by devtools/art_assets/build.py (assets/neo-lumen/README.md). | Makefile | LIB_MODULES := console sprite sprite_dynamic sprite_lut dma background input |

Going Further

  • Move sprites with input: Add padHeld() and update oambuffer[0].oamx/oamy based on D-pad direction.
  • Mix static and dynamic: Use static sprites for simple objects (bullets, UI elements) and dynamic sprites for complex animated characters.
  • Explore related examples:
    • games/breakout – Dynamic sprites in a real game context
    • games/rooftop_run – Dynamic sprites with scrolling backgrounds

Modules Used

console, sprite, sprite_dynamic, sprite_dynamic_meta, sprite_lut, dma, background, text, input (LIB_MODULES in the Makefile).