OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
tilering.h File Reference

Scrolling in eight directions over a map with more characters than VRAM holds. More...

#include <snes/types.h>

Go to the source code of this file.

Classes

struct  TileRing
 One ring: fill it with tileRingInit(); the fields are read-only. More...
struct  TileRingMap
 The map as opensnes-tilering wrote it: NAME_TILERING_MAP in the generated .inc fills it. More...

Macros

#define TILERING_CHAR_BYTES   32
 Bytes of one 4 bpp character.
#define TILERING_NONE   0xFFFF
 Value of slot_of[] for a character not in the ring, of char_of[] for a slot that never held one.
#define TILERING_STAGING_BYTES(w, h)
 Bytes of the staging buffer for a window of w x h characters: one row and one column of new characters at most.

Functions

u16 tileRingCost (const TileRing *r, u16 dx, u16 dy)
 The most characters a frame can bring at a speed.
void tileRingFill (TileRing *r, u16 camx, u16 camy)
 Load the whole window at a camera position: screen off or in force blank.
u16 tileRingInit (TileRing *r, const TileRingMap *map, u16 chr_vram, u16 first_char, u16 slots, u16 tilemap, u16 FAR *slot_of, u16 FAR *char_of, u8 FAR *refs, u8 FAR *staging)
 Set a ring up: no VRAM is written.
u16 tileRingUpdate (TileRing *r, u16 camx, u16 camy)
 Follow the camera: queue the characters and tilemap the move needs.

Detailed Description

Scrolling in eight directions over a map with more characters than VRAM holds.

A tilemap entry names one of 1,024 characters, and a 4 bpp background rarely gets more than 512 to 768 of them once sprites and a status bar have their share of VRAM. A large hand-drawn map easily has 1,200 to 1,600 distinct 8x8 characters. What the screen shows at once is much less (300 to 450 in the maps measured): the module keeps a ring of character slots in VRAM, and as the camera moves it loads the characters of the row and the column that come into view, and frees the slots of those that leave.

#include <snes/tilering.h> // and `tilering vramqueue` in LIB_MODULES
#include "res/world.inc" // opensnes-tilering convert res/world.png
static const TileRingMap world = world_TILERING_MAP;
TileRing ring; // plain RAM, 258 bytes
FAR u16 slot_of[world_CHARS]; // the work areas, FAR RAM
FAR u8 refs[640];
tileRingInit(&ring, &world, 0x0000, 0, 640, 0x6000, slot_of, char_of, refs, staging);
bgSetGfxPtr(0, 0x0000);
bgSetMapPtr(0, 0x6000, SC_64x32);
tileRingFill(&ring, camx, camy); // screen off: the whole first screen
while (1) {
// ... move camx, camy (at most 8 px a frame on each axis)
tileRingUpdate(&ring, camx, camy); // queues what changed
vramQueueFlush(); // sends it
}
void bgSetMapPtr
Definition background.h:140
void bgSetGfxPtr
Definition background.h:169
void bgSetScroll(u8 bg, u16 x, u16 y)
Set background scroll position.
void WaitForVBlank(void)
Wait for next VBlank period.
void setScreenOn(void)
Enable screen display.
#define SC_64x32
Definition background.h:37
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
#define FAR
Place a global in bank $7E WRAM, outside the 8 KB bank-0 band.
Definition types.h:130
FAR u8 refs[640]
Cells of the window showing each slot.
Definition main.c:77
FAR u8 staging[(((33)+(29)) *32)]
The frame's new characters, copied from ROM, sent by one or two DMAs.
Definition main.c:79
FAR u16 slot_of[archipelago_CHARS]
The slot of each map character.
Definition main.c:73
TileRing ring
The ring's state (plain RAM).
Definition main.c:71
u16 camx
Camera position in map pixels.
Definition main.c:105
u16 camy
Definition main.c:105
FAR u16 char_of[640]
The character of each slot.
Definition main.c:75
static const TileRingMap world
What the converter wrote, in one structure.
Definition main.c:69
The map as opensnes-tilering wrote it: NAME_TILERING_MAP in the generated .inc fills it.
Definition tilering.h:106
One ring: fill it with tileRingInit(); the fields are read-only.
Definition tilering.h:116
Scrolling in eight directions over a map with more characters than VRAM holds.
u16 tileRingUpdate(TileRing *r, u16 camx, u16 camy)
Follow the camera: queue the characters and tilemap the move needs.
u16 tileRingInit(TileRing *r, const TileRingMap *map, u16 chr_vram, u16 first_char, u16 slots, u16 tilemap, u16 FAR *slot_of, u16 FAR *char_of, u8 FAR *refs, u8 FAR *staging)
Set a ring up: no VRAM is written.
void tileRingFill(TileRing *r, u16 camx, u16 camy)
Load the whole window at a camera position: screen off or in force blank.
#define TILERING_STAGING_BYTES(w, h)
Bytes of the staging buffer for a window of w x h characters: one row and one column of new character...
Definition tilering.h:96
void vramQueueFlush(void)
Send every queued transfer to VRAM and empty the queue.

The map is what opensnes-tilering convert writes from an indexed PNG: each 8x8 character kept once, mirrored copies merged (the map entry carries the flip), numbered in order of first appearance row by row from the bottom of the map. The map entry is 16 bits: character 0-4095 in bits 0-11, priority in bit 13, the flips in bits 14 and 15; the palette is a property of the character.

The tilemap is 64x32 (SC_64x32, 4 KB of VRAM). The window, the cells in view, is 33x29 characters by default (the converter's --window): enough for a 256x224 screen at any scroll. Map cell (x, y) lives at tilemap cell (x mod 64, y mod 32).

The slots. Slot s is the character first_char + s of the background, at VRAM word chr_vram + 16 * s. Each slot counts the cells of the window that show it; a slot whose count falls to 0 keeps its character until it is needed again: the character comes back without a transfer if the camera returns first. New characters take the free slots in sequence, from a cursor that goes round the ring, so the characters of a new row usually land in consecutive slots and leave in one or two transfers.

What it costs, and who decides. tileRingUpdate() reads the map, updates the counts, and notes in the VRAM queue the characters to load and the tilemap row and column to write. It sends nothing itself: vramQueueFlush() does, in VBlank, and vramQueueCost() says beforehand what that will take. tileRingCost() says the most a frame can bring for a speed. No budget is imposed: a game that wants a cap tests vramQueueCost() before the flush, or moves the camera less.

Two ways to send the characters:

  • staging (a buffer given): the frame's new characters are copied to it, one DMA from ROM to WRAM per run of consecutive characters, outside VBlank; the queue then holds one entry per run of consecutive SLOTS, usually one or two;
  • direct (no buffer, staging NULL): one queue entry per run of characters consecutive both in ROM and in the ring. No copy and no RAM, but in VBlank each entry costs VRAM_QUEUE_ENTRY_COST more.
Note
Uses DMA channel 0 to fill the staging buffer, outside VBlank: a game that runs HDMA on channel 0 gives no staging buffer.
The camera moves at most one character (8 px) a frame on each axis: tileRingUpdate() moves the window one row and one column at most and returns how far it still is. A jump is a new tileRingFill() (screen off) or the game's own re-centring over several frames.

Macro Definition Documentation

◆ TILERING_CHAR_BYTES

#define TILERING_CHAR_BYTES   32

Bytes of one 4 bpp character.

◆ TILERING_NONE

#define TILERING_NONE   0xFFFF

Value of slot_of[] for a character not in the ring, of char_of[] for a slot that never held one.

◆ TILERING_STAGING_BYTES

#define TILERING_STAGING_BYTES ( w,
h )
Value:
(((w) + (h)) * TILERING_CHAR_BYTES)
#define TILERING_CHAR_BYTES
Bytes of one 4 bpp character.
Definition tilering.h:92

Bytes of the staging buffer for a window of w x h characters: one row and one column of new characters at most.

Function Documentation

◆ tileRingCost()

u16 tileRingCost ( const TileRing * r,
u16 dx,
u16 dy )

The most characters a frame can bring at a speed.

From the converter's figures for this map: a speed of dx pixels a frame crosses (dx + 7) / 8 column boundaries at most, each bringing at most the map's worst column; the same for dy and rows. In bytes for the VBlank: the characters times TILERING_CHAR_BYTES, plus the tilemap (a row 2 x win_w bytes in two entries, a column 2 x win_h in two), plus VRAM_QUEUE_ENTRY_COST for each entry.

Parameters
rThe ring
dxHorizontal speed, pixels a frame (absolute value)
dyVertical speed, pixels a frame (absolute value)
Returns
Characters, at most

◆ tileRingFill()

void tileRingFill ( TileRing * r,
u16 camx,
u16 camy )

Load the whole window at a camera position: screen off or in force blank.

Empties the ring, then writes every character of the window and the window's tilemap with direct DMA (not through the queue). For the first screen of a level, or after a jump. 448 characters, the worst of the maps measured, are 14,336 bytes: a VBlank holds about 5 KB.

Parameters
rThe ring
camxCamera x in map pixels (the screen's left edge)
camyCamera y in map pixels (the screen's top edge)

◆ tileRingInit()

u16 tileRingInit ( TileRing * r,
const TileRingMap * map,
u16 chr_vram,
u16 first_char,
u16 slots,
u16 tilemap,
u16 FAR * slot_of,
u16 FAR * char_of,
u8 FAR * refs,
u8 FAR * staging )

Set a ring up: no VRAM is written.

Parameters
rThe ring (plain RAM)
mapThe converted map (static const TileRingMap m = NAME_TILERING_MAP;)
chr_vramVRAM word address of slot 0's character
first_charIts character number for the background (from the BG's character base: (chr_vram - base) / 16)
slotsSlots in the ring; first_char + slots <= 1024. The window's worst (NAME_WORST_WINDOW) must fit, with room to spare for the characters kept for a return
tilemapVRAM word address of a 64x32 tilemap
slot_ofNAME_CHARS words (FAR RAM is fine)
char_ofslots words
refsslots bytes
stagingTILERING_STAGING_BYTES(window) bytes, or NULL (direct mode)
Returns
1, or 0 when the map is not a tilering map or the window is larger than 64x32

◆ tileRingUpdate()

u16 tileRingUpdate ( TileRing * r,
u16 camx,
u16 camy )

Follow the camera: queue the characters and tilemap the move needs.

Moves the window one column and one row at most toward the camera, then notes in the VRAM queue the new characters (see the two modes above) and the tilemap column (one or two entries) and row (two entries). Call it once a frame, before WaitForVBlank(), and vramQueueFlush() after: the entries point into the ring and the staging buffer, which the next call rewrites.

Parameters
rThe ring
camxCamera x in map pixels, at most width * 8 - 256
camyCamera y in map pixels, at most height * 8 - 224
Returns
Cells the window still lags behind the camera, the two axes added: 0 when it caught up