This tutorial covers SNES background scrolling techniques, from basic offset control to continuous tile streaming.
The SNES PPU has two scroll registers per background layer: one horizontal (BGnHOFS) and one vertical (BGnVOFS). These registers define which pixel of the tilemap appears at the top-left corner of the screen. The PPU reads these registers once per frame during rendering.
OpenSNES provides bgSetScroll() to set these values. The function writes to shadow variables and marks the background as dirty; the NMI handler then commits the values to hardware during VBlank.
One hardware quirk is hidden for you: the PPU never outputs scanline 0 (the OBJ data of every line is fetched during the previous one), so a raw BGnVOFS of 0 shows tilemap lines 1 to 224 and the bottom picture line is the first line of the 29th tile row — which most programs never write, so real hardware shows a strip of garbage there. The lib therefore writes y - 1 to the register ("many games set their vertical scroll values to -1 rather than 0", anomie's register doc): bgSetScroll(bg, x, 0) really puts tilemap row 0 on the first line. If you drive BGnVOFS yourself, from an HDMA table for instance, apply the -1 in your data.
Scroll offsets are 10 bits wide (0-1023). The tilemap wraps seamlessly:
| Tilemap Size | Pixel Dimensions | Wrap Point |
|---|---|---|
| SC_32x32 | 256 x 256 | Wraps at 256 |
| SC_64x32 | 512 x 256 | Wraps at 512 horizontally, 256 vertically |
| SC_32x64 | 256 x 512 | Wraps at 256 horizontally, 512 vertically |
| SC_64x64 | 512 x 512 | Wraps at 512 |
When the scroll offset exceeds the tilemap dimensions, the PPU wraps back to the beginning. This means a 32x32 tilemap scrolled to X=260 displays the same content as X=4.
Use bgSetScroll(bg, x, y) to set the scroll offset for a background layer. The bg parameter is 0-indexed (0 = BG1, 1 = BG2, etc.).
You can also set horizontal and vertical scroll independently:
To read back the current scroll position (from the shadow variables):
Read the joypad state with padHeld() and update the scroll position based on which directions are held. Use s16 for scroll variables so negative values wrap correctly.
The tilemap wraps automatically, so the player can scroll in any direction indefinitely on a SC_64x64 map (512x512 pixels). For a larger world, see Continuous Scrolling below.
A common technique is scrolling one background while keeping another fixed. This works well for a moving pattern behind a static logo or HUD.
See examples/scrolling/mixed_scroll/ for a complete example, a title screen: the rain auto-scrolls behind a static logo, whose tiles carry the priority bit to stay in front of BG1.
Parallax creates a depth illusion by scrolling background layers at different speeds. Distant layers move slowly; near layers move quickly.
The easiest approach uses separate BG layers, each scrolled at a different rate:
This is limited to the number of available BG layers (Mode 1 has 3 layers).
For more zones than available BG layers, use HDMA to rewrite the scroll register at different scanline positions. This splits a single BG into multiple horizontal bands, each scrolling at its own speed.
See examples/scrolling/parallax_scroll/ for the complete implementation.
Important: The scroll table must live in RAM (not const) because the main loop updates it every frame. HDMA reads from the table during rendering, so always update the values before WaitForVBlank().
For worlds larger than the tilemap (e.g., a side-scroller level), you must stream new tile columns or rows into VRAM as the player scrolls. The tilemap wraps in hardware, so you overwrite the column that just scrolled off-screen with the next column from your map data.
The continuous_scroll example shows the camera half of this — a threshold camera that follows a character sprite — over two 64x32 maps (512 px of world) loaded once, with the scroll capped at MAX_SCROLL_X; it streams no columns. For a level wider than the tilemap, with the column streaming done for you, use the map module (Scrolling maps, examples/maps/map_scroll). Key elements from the example:
The main loop reads input, moves the player, and scrolls the world by what the player went past a screen threshold, putting him back on it:
See examples/scrolling/continuous_scroll/ for the full implementation including the sprite, the two layers and their 64x32 maps.
A tilemap entry names one of 1,024 characters, and once sprites and a status bar have their share a 4 bpp background gets 512 to 768 of them. A hand-drawn map easily has 1,200 to 1,600 distinct 8x8 characters, so no single character set fits. What the screen shows at once is far less (300 to 450 in the maps measured), and that is what the tilering module keeps in VRAM: a ring of character slots, refilled as the camera moves.
What a frame costs is yours to decide. tileRingCost(&ring, dx, dy) gives the most characters a frame can bring at a speed, from the converter's figures for that map; vramQueueCost() gives what is queued before the flush. Measured on scrolling/tile_ring (1,433 characters, a ring of 640, legs at 3 to 7 px a frame and diagonals at 5 + 5): at most 21 new characters in a frame, flushed in 7,994 master cycles (staging: the characters are first copied to WRAM, one transfer per run of slots) or 19,270 (direct: one transfer per run of characters, from ROM), against a vertical blank of 50,468 on NTSC and 118,668 on PAL, the NMI handler's 6,216 included in neither. The module's own work outside the blank is about 100,000 master cycles for a frame that crosses a column, up to 280,000 for a diagonal into new characters (an NTSC frame is 357,366).
The PPU reads the scroll registers (BG1HOFS, BG1VOFS, etc.) as it renders, so a value written during HBlank is in force for the next line — that is what the HDMA parallax pattern above relies on (the references we hold do not pin the exact point inside a scanline where a write starts to count). An unsynchronised CPU write lands partway down the picture: the top of the frame shows the old scroll, the bottom the new one, visible as a one-frame tear.
OpenSNES handles this automatically: bgSetScroll() writes to shadow variables and sets a dirty flag. The NMI handler checks the dirty flag during VBlank and commits only the changed values to hardware. You do not need to manually time your scroll writes.
The typical frame loop is:
Important: Always set scroll values before WaitForVBlank(). If you set them after, the update is delayed by one frame.
Also set initial scroll values before setScreenOn() to avoid a single-frame glitch where the tilemap appears at (0, 0) before your intended position takes effect: