OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

Dynamic sprite engine: per-frame VRAM tile streaming, single sprites and metasprites. More...

#include <snes.h>
#include "res/sprite16_grid.inc"
#include "res/spritehero32.inc"
#include "res/spritehero16.inc"
#include "res/spritehero8.inc"

Functions

static void animStep (void)
 One frame of the animated scene: a step every 8 frames, then the draws.
static void changeObjSize (void)
 Reinitialize sprite engine and upload all tiles during force blank.
static void drawSprites (void)
 Draw metasprites for the current OBJSEL configuration.
static void drawText (void)
 Draw the configuration menu text.
static void enterAnimScene (void)
 The four animated 16x16 sprites: the engine, the palette, the sprites' state and their first draw; the screen comes on at the end.
static void enterMetaScene (void)
 The metasprite scene: Mode 0, the text menu on BG1, the heroes' palette; changeObjSize() configures the engine and draws them.
int main (void)
 Entry point: the animated sprites at boot, SELECT to the metasprites and back.
static void metaStep (void)
 One frame of the metasprite scene: UP/DOWN pick the size pair.

Variables

static u16 frame0
 Animation frame index for sprite 0 (0-23, wraps at 24).
static u16 frame1
 Animation frame index for sprite 1 (0-23, wraps at 24).
static u16 frame2
 Animation frame index for sprite 2 (0-23, wraps at 24).
static u16 frame3
 Animation frame index for sprite 3 (0-23, wraps at 24).
static u8 frame_counter
 Global frame counter for animation timing.
static const MetaspriteItem hero16_frame0 []
 16x16 metasprite: Kit 32x48 (2x3 grid of 16x16)
static const MetaspriteItem hero32_frame0 []
 32x32 metasprite: Kit 64x64 (2x2 grid of 32x32, blocks 0-3 of the sheet)
static const MetaspriteItem hero8_frame0 []
 8x8 metasprite: Kit 16x16 (2x2 grid of 8x8, tiles 0-3 of the sheet)
u8 in_meta
 Entry point.
u16 pad0
u16 selectedItem
static const s16 xpos [4] = {64, 112, 160, 208}
 ROM source for the 16x16 sprite sheet tile data.

Detailed Description

Dynamic sprite engine: per-frame VRAM tile streaming, single sprites and metasprites.

Demonstrates the OpenSNES dynamic sprite engine, which uploads only the currently-needed sprite tiles to VRAM each frame instead of pre-loading the entire sprite sheet. This trades VBlank DMA bandwidth for VRAM space, allowing many unique animation frames without exhausting the 32KB OBJ VRAM region.

Four 16x16 sprites are animated simultaneously. Each sprite's tile data is streamed from ROM to VRAM via oamDynamicDraw() — the engine queues dirty tiles and the NMI handler auto-flushes the queue every VBlank within the DMA budget.

SELECT switches to the next step of the same engine: a metasprite — a character made of several OBJ — drawn with oamMetaDrawDyn(), its sub-sprites' tiles streamed the same way, under the three OBJSEL size pairs (8/16, 8/32, 16/32) chosen with UP/DOWN. SELECT again comes back. (The metasprite scene was the dynamic_metasprite example, ported from PVSnesLib's DynamicEngineMetaSprite.)

SNES Concepts
  • Dynamic VRAM tile streaming to conserve OBJ VRAM space
  • Batched VBlank DMA for sprite tile uploads
  • Multiple independently-animated sprites sharing a tile pool
  • oambuffer[] structure for sprite state management
  • MetaspriteItem arrays defining a character's sub-sprite layout, and the size class (OBJ_SMALL / OBJ_LARGE) of the OBJSEL pair it is drawn with
  • OBJSEL size pairs, switched at run time with oamDynamicInit()
What to Observe
  • Four 16x16 sprites displayed in a row, each animating through a 24-frame cycle with an 8-frame delay between advances
  • No input required; the animation runs continuously
  • SELECT: two hero characters; UP/DOWN switch between the 3 OBJSEL size pairs (the menu cursor ">" shows which); the characters change size
Modules Used
console, sprite, sprite_dynamic, sprite_dynamic_meta, sprite_lut, dma, background, text, input
See also
sprite.h, dma.h, video.h

Function Documentation

◆ animStep()

void animStep ( void )
static

One frame of the animated scene: a step every 8 frames, then the draws.

◆ changeObjSize()

void changeObjSize ( void )
static

Reinitialize sprite engine and upload all tiles during force blank.

Uses force blank for a glitch-free OBJSEL switch. The current configuration can enqueue >7 entries (config 2 hits 10), and the NMI auto-flush hook drains 7 per VBlank, so oamDynamicDrainQueue loops WaitForVBlank() until the queue is empty before we release force blank. NMI suppresses the end-frame "hide stale sprites" path during the drain so the just-queued sprites are not pushed off-screen.

◆ drawSprites()

void drawSprites ( void )
static

Draw metasprites for the current OBJSEL configuration.

◆ drawText()

void drawText ( void )
static

Draw the configuration menu text.

◆ enterAnimScene()

void enterAnimScene ( void )
static

The four animated 16x16 sprites: the engine, the palette, the sprites' state and their first draw; the screen comes on at the end.

◆ enterMetaScene()

void enterMetaScene ( void )
static

The metasprite scene: Mode 0, the text menu on BG1, the heroes' palette; changeObjSize() configures the engine and draws them.

VRAM (words): $0000 large tiles, $1000 small tiles (the engine), $3000 the 2bpp font, $3800 BG1's tilemap.

◆ main()

int main ( void )

Entry point: the animated sprites at boot, SELECT to the metasprites and back.

Returns
Never returns

◆ metaStep()

void metaStep ( void )
static

One frame of the metasprite scene: UP/DOWN pick the size pair.

Variable Documentation

◆ frame0

u16 frame0
static

Animation frame index for sprite 0 (0-23, wraps at 24).

◆ frame1

u16 frame1
static

Animation frame index for sprite 1 (0-23, wraps at 24).

◆ frame2

u16 frame2
static

Animation frame index for sprite 2 (0-23, wraps at 24).

◆ frame3

u16 frame3
static

Animation frame index for sprite 3 (0-23, wraps at 24).

◆ frame_counter

u8 frame_counter
static

Global frame counter for animation timing.

Counts from 0 to 7, advancing sprite animation every 8th frame (8/60 = 133ms). Using a shared counter keeps all four sprites synchronized.

◆ hero16_frame0

const MetaspriteItem hero16_frame0[]
static
Initial value:
= {
METASPR_ITEM(0, 0, 0, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(16, 0, 1, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(0, 16, 2, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(16, 16, 3, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(0, 32, 4, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(16, 32, 5, OBJ_PAL(0) | OBJ_PRIO(3)),
}
#define OBJ_PRIO(prio)
Metasprite priority attribute macro.
Definition sprite.h:663
#define METASPR_ITEM(dx, dy, tile, attr)
Metasprite item macro (PVSnesLib compatible).
Definition sprite.h:651
#define METASPR_TERM
End marker for metasprite data (dx = -128).
Definition sprite.h:654
#define OBJ_PAL(pal)
Metasprite palette attribute macro.
Definition sprite.h:660

16x16 metasprite: Kit 32x48 (2x3 grid of 16x16)

◆ hero32_frame0

const MetaspriteItem hero32_frame0[]
static
Initial value:
= {
METASPR_ITEM(0, 0, 0, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(32, 0, 1, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(0, 32, 2, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(32, 32, 3, OBJ_PAL(0) | OBJ_PRIO(3)),
}

32x32 metasprite: Kit 64x64 (2x2 grid of 32x32, blocks 0-3 of the sheet)

◆ hero8_frame0

const MetaspriteItem hero8_frame0[]
static
Initial value:
= {
METASPR_ITEM(0, 0, 0, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(8, 0, 1, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(0, 8, 2, OBJ_PAL(0) | OBJ_PRIO(3)),
METASPR_ITEM(8, 8, 3, OBJ_PAL(0) | OBJ_PRIO(3)),
}

8x8 metasprite: Kit 16x16 (2x2 grid of 8x8, tiles 0-3 of the sheet)

◆ in_meta

u8 in_meta

Entry point.

1 while the metasprite scene is shown

◆ pad0

u16 pad0

◆ selectedItem

u16 selectedItem

◆ xpos

const s16 xpos[4] = {64, 112, 160, 208}
static

ROM source for the 16x16 sprite sheet tile data.

Contains all 24 animation frames laid out sequentially. The dynamic sprite engine reads individual frames from this ROM data and DMAs only the currently-needed tiles to VRAM each frame, conserving OBJ VRAM space.

16-color palette for the 16x16 sprites

X positions for the four sprites displayed in a horizontal row.

Static const arrays are placed in ROM (SUPERFREE section) by the compiler. The sprites are evenly spaced across the 256-pixel screen width.