Two-layer parallax scrolling with player sprite.
Demonstrates continuous horizontal scrolling with two independent background layers moving at different speeds (parallax) and a player-controlled sprite. BG1 and BG2 are loaded with separate tile sets and palettes in different VRAM/CGRAM slots to avoid conflicts. The scroll position is updated each frame based on player movement: when the sprite crosses a screen threshold, the background auto-scrolls and the player is pushed back, creating a camera-follow effect common in side-scrolling games. Scroll registers are written via bgSetScroll(), which defers the actual PPU register write to the NMI handler via a dirty-flag mechanism.
- SNES Concepts
- Multi-layer parallax scrolling (BG1 + BG2 at independent speeds)
- Deferred scroll register writes via NMI dirty flags
- Multiple tile sets and palette slots in VRAM/CGRAM
- Sprite overlay on scrolling backgrounds
- Auto-scroll camera with player threshold tracking
- What to Observe
- Move the character with D-pad; background scrolls when near screen edges
- BG1 and BG2 scroll together (parallax could use different speeds)
- The sprite animates and moves freely within the scroll boundaries
- Modules Used
- console, sprite, input, background, dma
- See also
- background.h, sprite.h, input.h, dma.h
Entry point – two-layer parallax scrolling with player sprite.
Loads two independent background layers (BG1 and BG2) with separate tile sets and palette slots, plus a character sprite. Each frame, the D-pad moves the player sprite; when the sprite crosses the scroll threshold, the backgrounds auto-scroll and the sprite is pushed back, creating a camera-follow effect. Scroll registers are deferred to the NMI handler via bgSetScroll()'s dirty-flag mechanism for safe VBlank-only writes.
- Returns
- Does not return (infinite loop).
< Current joypad button state (read from hardware)
< When player X > this, scroll right
< Maximum horizontal scroll (depends on tilemap size)
< When player X < this, scroll left