Loading...
Searching...
No Matches
main.c File Reference

Dynamic sprite engine with per-frame VRAM tile streaming. More...

#include <snes.h>

Functions

int main (void)
 Entry point: dynamic sprite engine demo with four animated sprites.
 

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.
 
u8 spr16_properpal []
 16-color palette for the 16x16 sprites
 
u8 spr16_tiles []
 ROM source for the 16x16 sprite sheet tile data.
 
static const s16 xpos [4] = {64, 112, 160, 208}
 X positions for the four sprites displayed in a horizontal row.
 

Detailed Description

Dynamic sprite engine with per-frame VRAM tile streaming.

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.

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
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
Modules Used
console, sprite, sprite_dynamic, sprite_lut, dma, background, input
See also
sprite.h, dma.h, video.h

Function Documentation

◆ main()

int main ( void  )

Entry point: dynamic sprite engine demo with four animated sprites.

Initializes the dynamic sprite system, which streams sprite tiles from ROM to VRAM on demand each frame. Four 16x16 sprites are placed in a row, each cycling through 24 animation frames with an 8-frame delay.

The dynamic sprite pipeline per frame:

  1. oamDynamicDraw() — main thread marks which tiles each sprite needs
  2. NMI VBlank handler auto-flushes: DMAs queued tiles to VRAM and resets the per-frame tile allocator for next frame

This approach trades VBlank DMA time for VRAM conservation: instead of pre-loading all 24 frames (24 x 4 tiles x 32 bytes = 3KB per sprite), only the 4 active tiles per sprite are in VRAM at any time.

Returns
Never returns (infinite loop).

< Priority 3 = in front of all BGs

< Force initial tile upload

< Point to ROM tile source

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.

◆ spr16_properpal

u8 spr16_properpal[]
extern

16-color palette for the 16x16 sprites

◆ spr16_tiles

u8 spr16_tiles[]
extern

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.

◆ xpos

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

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.