OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Scrolling & Parallax Tutorial

This tutorial covers SNES background scrolling techniques, from basic offset control to continuous tile streaming.

How BG Scrolling Works

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.

Wrap Behavior

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.

Basic BG Scrolling

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.).

#include <snes.h>
u16 scroll_x = 0;
u16 scroll_y = 0;
int main(void) {
/* ... load tiles, palettes, tilemap ... */
bgSetMapPtr(0, 0x0000, SC_32x32);
while (1) {
/* Auto-scroll: 1 pixel per frame horizontally */
scroll_x++;
bgSetScroll(0, scroll_x, scroll_y);
}
return 0;
}
int main(void)
Definition main.c:73
void bgSetMapPtr
Definition background.h:140
void bgSetScroll(u8 bg, u16 x, u16 y)
Set background scroll position.
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
void setScreenOn(void)
Enable screen display.
#define BG_MODE1
Definition video.h:39
#define SC_32x32
Definition background.h:36
#define REG_TM
Main screen designation (W).
Definition registers.h:187
#define TM_BG1
Definition registers.h:443
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
OpenSNES common-case master header.
void setMode(u8 mode, u8 flags)
Set background mode.

You can also set horizontal and vertical scroll independently:

bgSetScrollX(0, scroll_x); /* Horizontal only */
bgSetScrollY(0, scroll_y); /* Vertical only */
void bgSetScrollY(u8 bg, u16 y)
Set vertical scroll only.
void bgSetScrollX(u8 bg, u16 x)
Set horizontal scroll only.

To read back the current scroll position (from the shadow variables):

u16 cur_x = bgGetScrollX(0);
u16 cur_y = bgGetScrollY(0);
u16 bgGetScrollY(u8 bg)
Get current vertical scroll position (from shadow).
u16 bgGetScrollX(u8 bg)
Get current horizontal scroll position (from shadow).

D-PAD Controlled Scrolling

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.

#include <snes.h>
s16 scroll_x = 0;
s16 scroll_y = 0;
int main(void) {
/* ... load tiles, palettes, tilemap ... */
bgSetMapPtr(0, 0x0000, SC_64x64);
while (1) {
u16 pad = padHeld(0);
if (pad & KEY_LEFT) scroll_x -= 2;
if (pad & KEY_RIGHT) scroll_x += 2;
if (pad & KEY_UP) scroll_y -= 2;
if (pad & KEY_DOWN) scroll_y += 2;
bgSetScroll(0, scroll_x, scroll_y);
}
return 0;
}
#define SC_64x64
Definition background.h:39
#define KEY_RIGHT
Definition input.h:81
#define KEY_DOWN
Definition input.h:79
#define KEY_LEFT
Definition input.h:80
#define KEY_UP
Definition input.h:78
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:50
#define padHeld(pad)
Definition input.h:158

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.

Mixed Scroll (Static + Moving Layers)

A common technique is scrolling one background while keeping another fixed. This works well for a moving pattern behind a static logo or HUD.

s16 scrX = 0, scrY = 0;
while (1) {
/* BG1 scrolls diagonally */
scrX++;
scrY++;
/* BG2 stays fixed at (0, 0) - no bgSetScroll call needed */
}
s16 scrY
BG1 vertical scroll offset (auto-incremented each frame).
Definition main.c:62
s16 scrX
BG1 tile data – the rain, a repeating picture (4bpp, tiles at VRAM $4000).
Definition main.c:60

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 Scrolling

Parallax creates a depth illusion by scrolling background layers at different speeds. Distant layers move slowly; near layers move quickly.

Simple Parallax (Multiple BGs)

The easiest approach uses separate BG layers, each scrolled at a different rate:

s16 scroll_x = 0;
while (1) {
scroll_x++;
/* BG1 (foreground): full speed */
bgSetScroll(0, scroll_x, 0);
/* BG2 (background): half speed */
bgSetScroll(1, scroll_x >> 1, 0);
}

This is limited to the number of available BG layers (Mode 1 has 3 layers).

HDMA Parallax (Per-Scanline Scroll)

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.

#include <snes.h>
/* HDMA table in RAM: [line_count] [scroll_lo] [scroll_hi] per zone */
int main(void) {
/* ... load tiles, palette, tilemap ... */
bgSetMapPtr(0, 0x0000, SC_64x32);
/* Define 3 scroll zones */
scroll_table[0] = 72; /* Top: 72 scanlines */
*(u16 *)&scroll_table[1] = 0; /* Initial offset */
scroll_table[3] = 88; /* Middle: 88 scanlines */
*(u16 *)&scroll_table[4] = 0;
scroll_table[6] = 64; /* Bottom: 64 scanlines */
*(u16 *)&scroll_table[7] = 0;
scroll_table[9] = 0x00; /* End of table */
/* Start HDMA on channel 6, targeting BG1 horizontal scroll */
while (1) {
/* Each zone scrolls at a different speed */
*(u16 *)&scroll_table[1] += 1; /* Slow (sky) */
*(u16 *)&scroll_table[4] += 2; /* Medium (midground) */
*(u16 *)&scroll_table[7] += 4; /* Fast (foreground) */
}
return 0;
}
#define SC_64x32
Definition background.h:37
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
void hdmaEnableMask(u8 channelMask)
Enable HDMA channel(s), given as a bit mask.
#define HDMA_CHANNEL_6
HDMA channel 6 (recommended for HDMA — never used by the runtime).
Definition hdma.h:135
void hdmaParallax(u8 channel, u8 bg, const void *scrollTable)
Set up a background parallax scroll effect.
u8 scroll_table[10]
4bpp background tile data (converted by the build from res/)
Definition main.c:64

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().

Continuous / Infinite Scrolling

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 Streaming Pattern

  1. Scroll the BG offset as normal
  2. When the scroll crosses an 8-pixel tile boundary, DMA a new column (or row) of tilemap entries into VRAM
  3. The PPU wraps the tilemap, so the newly written column appears seamlessly at the leading edge

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:

typedef struct {
s16 bg1_scroll_x;
s16 bg1_scroll_y;
s16 bg2_scroll_x;
s16 bg2_scroll_y;
static s16 player_y
Kit's hitbox, top-left corner, Y.
Definition main.c:78
static s16 player_x
Kit's hitbox, top-left corner, in screen coordinates.
Definition main.c:76
Centralized game state structure.
Definition main.c:102

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:

if (game.player_x > SCROLL_THRESHOLD_RIGHT && game.bg1_scroll_x < MAX_SCROLL_X) {
d = game.player_x - SCROLL_THRESHOLD_RIGHT;
if (d > MAX_SCROLL_X - game.bg1_scroll_x) d = MAX_SCROLL_X - game.bg1_scroll_x;
game.bg1_scroll_x += d;
game.player_x -= d; /* back on the threshold */
}
game.bg2_scroll_x = game.bg1_scroll_x >> 1; /* parallax: the far layer at half speed */
/* Update hardware scroll */
bgSetScroll(0, game.bg1_scroll_x, game.bg1_scroll_y);
bgSetScroll(1, game.bg2_scroll_x, game.bg2_scroll_y);
static GameState game
Definition main.c:113
#define MAX_SCROLL_X
Definition main.c:53
#define SCROLL_THRESHOLD_RIGHT
Definition main.c:54

See examples/scrolling/continuous_scroll/ for the full implementation including the sprite, the two layers and their 64x32 maps.

Streaming Tips

  • DMA budget: VBlank allows roughly 4KB of DMA. A single column of a 32-tile-high tilemap is 64 bytes (32 entries x 2 bytes), well within budget.
  • Stream +32 columns, not +31: the 33rd partial tile at the right edge is visible, so always stream one column ahead.
  • Use SC_64x32 or SC_64x64: wider tilemaps give you a 64-column circular buffer, reducing the streaming frequency.

Maps With More Characters Than VRAM

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.

  1. opensnes-tilering convert (opensnes-tilering — maps with more characters than VRAM holds) reads an indexed PNG and keeps each character once, a mirrored copy merged with it (the map entry carries the flip), numbered in order of first appearance from the bottom of the map; it also measures the worst screen, row and column.
  2. tileRingInit() takes the converted map, where the slots live in VRAM, how many there are, and the work areas (FAR RAM is fine); tileRingFill() loads the first screen in force blank.
  3. Each frame, tileRingUpdate(&ring, camx, camy) moves the window one column and one row at most: the cells that leave release their slots (a count per slot; a slot at 0 keeps its character, which comes back without a transfer if the camera returns), the cells that come in take theirs, new characters take free slots in sequence. It notes the characters and the tilemap row and column in the VRAM queue; vramQueueFlush() sends them right after WaitForVBlank().
static const TileRingMap world = archipelago_TILERING_MAP;
FAR u16 slot_of[archipelago_CHARS];
FAR u8 refs[640];
tileRingInit(&ring, &world, 0x0000, 0, 640, 0x3000, slot_of, char_of, refs, staging);
while (1) {
/* move camx, camy */
}
#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
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.

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).

VBlank Timing

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:

while (1) {
/* 1. Update game logic and scroll positions */
scroll_x += 2;
bgSetScroll(0, scroll_x, 0);
/* 2. WaitForVBlank triggers NMI, which writes scroll to hardware */
}

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:

bgSetScroll(0, initial_x, initial_y);
setScreenOn(); /* First visible frame already has correct scroll */

Next Steps