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

World-space sprites — a camera, culling, a depth sort and OAM in one call per batch. More...

#include <snes.h>
#include "res/plaza.inc"
#include "res/residents.inc"

Macros

#define BG_VRAM   0x1000
#define CAM_XMAX   (WORLD_W - 256)
 The camera may not show past the world's edge.
#define CAM_YMAX   (WORLD_H - 224)
#define MAP_VRAM   0x0000
 BG1's 64x64 map (4096 words, four 32x32 pages) and its tiles.
#define NRES   6
 Residents (the 32 x 32 batch, sorted by depth).
#define NSHARDS   17
 Shards in the world (the 16 x 16 batch).
#define RES_ID   NSHARDS
 The residents' OAM ids follow the shards'.
#define SHARD_TILE   192
 The shard's tile: the first 16 x 16 block of the sheet's fourth row.
#define SPR_VRAM   0x4000
 Sprite tile VRAM word address.
#define WORLD_H   448
#define WORLD_W   512
 World size in pixels.

Functions

static u8 frame_tile (u16 f)
 The OAM tile of frame f of res/residents.png.
int main (void)
static void sort_residents (void)
 Sort the residents by y, the lowest on screen first: an insertion sort of six bytes.

Variables

u16 cam_x
 Probe oracles: the camera, the shards placed this frame, the resident in the front slot.
u16 cam_y
static OamWorldBatch crowd
static u8 res_order [6]
 The residents' drawing order: slot i of the batch shows resident order[i].
static OamWorldBatch shards
 The two batches: the shards (16 x 16) and the residents (32 x 32, sorted).
static u8 wattr [17+6]
u16 world_front
u16 world_visible
static u8 wseen [17+6]
static u8 wtile [17+6]
static s8 wvx [17+6]
static s8 wvy [17+6]
static s16 wx [17+6]
static s16 wy [17+6]

Detailed Description

World-space sprites — a camera, culling, a depth sort and OAM in one call per batch.

A square of the Rue basse, seen from above: data shards drift over the paving and six residents pace up and down round the neon ring at its centre. A scrolling game keeps its actors in WORLD coordinates (the square is 512 x 448, BG1 a 64x64 map scrolled with the camera) and shows a 256 x 224 window of it. Every frame it has to subtract the camera from each actor, decide whether the result is on screen, write the OAM entry or hide it, and set the ninth x bit for a sprite half out on the left. oamPlaceWorld() does it in assembly for a whole batch, about 1,700 a sprite on the library bench (docs/PERF.md, row world).

The game keeps one array per property (structure of arrays: what the 65816 indexes cheaply) and fills an OamWorldBatch with their addresses once. Two batches, because a batch has one size: 17 shards of 16 x 16 (the SMALL size of OBJ_SIZE16_L32) and six residents of 32 x 32 (the LARGE size, set once with oamSetSize(): the call never touches the size bit).

The residents show order. Seen from above, the one lower on the screen is nearer and must be drawn in front — and OAM order is drawing order, the lower slot in front. So the residents are sorted by y every frame (an insertion sort of six bytes) and the batch's order says which resident each slot shows: the arrays stay indexed by the resident, only the six bytes of order move. Each one faces the way it walks: its frame (south going down, north going up) is its entry of tile.

Hold the D-pad to move the camera. visible[] comes back from the call: the example counts the shards in view with it (world_visible); a game reads it to stream the animation frames of the sprites that are shown and skip the others — a sprite off screen should cost no VRAM DMA.

ROM mode: LoROM (project default).

SNES Concepts
  • World coordinates minus a camera = screen coordinates; a sprite partly off the left edge needs the ninth x bit (x in 256..511), which the PPU reads as negative
  • OAM culling: a sprite off screen is hidden (x = 257, y = 240), not left wherever it was
  • OAM order is drawing order: a depth sort permutes slots, not data
  • Structure of arrays for per-sprite data, and the size bit set once
What to Observe
Six residents (Kit, a frog, an owl, a pangolin, a raccoon, a rat) pace up and down round the ring and pass in front of and behind one another as their y changes, turning their back when they walk up; the shards drift over the square; the D-pad scrolls the camera (and the paving with it) and stops at the kerb. world_visible (probe oracle) follows how many shards are in view, world_front which resident is in the front slot, cam_x / cam_y the camera.
Modules Used
console, dma, background, sprite, input
See also
lib/include/snes/sprite.h — OamWorldBatch, oamPlaceWorld, oamSetSize
examples/sprites/sprite_swarm — the same sprites moved by direct OAM writes, no camera

Macro Definition Documentation

◆ BG_VRAM

#define BG_VRAM   0x1000

◆ CAM_XMAX

#define CAM_XMAX   (WORLD_W - 256)

The camera may not show past the world's edge.

◆ CAM_YMAX

#define CAM_YMAX   (WORLD_H - 224)

◆ MAP_VRAM

#define MAP_VRAM   0x0000

BG1's 64x64 map (4096 words, four 32x32 pages) and its tiles.

◆ NRES

#define NRES   6

Residents (the 32 x 32 batch, sorted by depth).

◆ NSHARDS

#define NSHARDS   17

Shards in the world (the 16 x 16 batch).

◆ RES_ID

#define RES_ID   NSHARDS

The residents' OAM ids follow the shards'.

◆ SHARD_TILE

#define SHARD_TILE   192

The shard's tile: the first 16 x 16 block of the sheet's fourth row.

◆ SPR_VRAM

#define SPR_VRAM   0x4000

Sprite tile VRAM word address.

◆ WORLD_H

#define WORLD_H   448

◆ WORLD_W

#define WORLD_W   512

World size in pixels.

Function Documentation

◆ frame_tile()

u8 frame_tile ( u16 f)
static

The OAM tile of frame f of res/residents.png.

A 32x32 frame is 4x4 tiles and a row of OBJ tiles is 16 wide, so frame f (four a row of the sheet) starts at column (f % 4) * 4 of tile row (f / 4) * 4. Frame 2k is resident k facing south, 2k + 1 facing north.

◆ main()

int main ( void )

◆ sort_residents()

void sort_residents ( void )
static

Sort the residents by y, the lowest on screen first: an insertion sort of six bytes.

Variable Documentation

◆ cam_x

u16 cam_x

Probe oracles: the camera, the shards placed this frame, the resident in the front slot.

◆ cam_y

u16 cam_y

◆ crowd

OamWorldBatch crowd
static

the residents

◆ res_order

u8 res_order[6]
static

The residents' drawing order: slot i of the batch shows resident order[i].

◆ shards

OamWorldBatch shards
static

The two batches: the shards (16 x 16) and the residents (32 x 32, sorted).

◆ wattr

u8 wattr[17+6]
static

vhoopppc, as OAM_ATTR() builds it

◆ world_front

u16 world_front

◆ world_visible

u16 world_visible

◆ wseen

u8 wseen[17+6]
static

out: 1 placed, 0 hidden

◆ wtile

u8 wtile[17+6]
static

tile name, low byte

◆ wvx

s8 wvx[17+6]
static

drift

◆ wvy

s8 wvy[17+6]
static

◆ wx

s16 wx[17+6]
static

world x of the top-left corner

◆ wy

s16 wy[17+6]
static

world y