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. | |
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.
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:
| #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.
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.
| r | The ring |
| dx | Horizontal speed, pixels a frame (absolute value) |
| dy | Vertical speed, pixels a frame (absolute value) |
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.
| 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.
| r | The ring (plain RAM) |
| map | The converted map (static const TileRingMap m = NAME_TILERING_MAP;) |
| chr_vram | VRAM word address of slot 0's character |
| first_char | Its character number for the background (from the BG's character base: (chr_vram - base) / 16) |
| slots | Slots 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 |
| tilemap | VRAM word address of a 64x32 tilemap |
| slot_of | NAME_CHARS words (FAR RAM is fine) |
| char_of | slots words |
| refs | slots bytes |
| staging | TILERING_STAGING_BYTES(window) bytes, or NULL (direct mode) |
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.
| r | The ring |
| camx | Camera x in map pixels, at most width * 8 - 256 |
| camy | Camera y in map pixels, at most height * 8 - 224 |