OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

Mode 0 parallax scrolling with four independent 2bpp backgrounds. More...

#include <snes.h>
#include "vram_map.h"
#include "res/bg0.inc"
#include "res/bg1.inc"
#include "res/bg2.inc"
#include "res/bg3.inc"

Functions

int main (void)
 Entry point – load 4 BG layers and animate parallax scrolling.

Variables

u16 flip = 0
 Frame counter for scroll speed throttle (scrolls every 3 frames).
s16 sxbg1 = 0
 Horizontal scroll position for BG1 (fastest layer, 3px per tick).
s16 sxbg2 = 0
 Horizontal scroll position for BG2 (medium layer, 2px per tick).
s16 sxbg3 = 0
 Horizontal scroll position for BG3 (slowest layer, 1px per tick).

Detailed Description

Mode 0 parallax scrolling with four independent 2bpp backgrounds.

Showcases BG Mode 0, the only SNES video mode that provides four simultaneous background layers. Each layer is limited to 4 colors (2bpp), giving a total of 16 on-screen palette entries across four independent palette banks. The four layers are loaded from separate BMP assets and scroll horizontally at different speeds every 3 frames, creating a parallax depth effect.

This is a direct port of the PVSnesLib "Mode0" example.

SNES Concepts
  • Mode 0: four BG layers, each 2bpp (4 colors), each with its own palette bank
  • Palette banking: BG0 uses palette 0-3, BG1 uses 4-7, BG2 uses 8-11, BG3 uses 12-15
  • bgInitTileSet() loads tiles, palette, and configures the tile base address in one call
  • Independent per-layer scrolling via bgSetScroll() for parallax effects
  • VRAM layout: four separate tilemap regions ($0000/$0400/$0800/$0C00) and tile regions
What to Observe
  • Four layered backgrounds visible simultaneously
  • BG1-BG3 scroll horizontally at different speeds (3x, 2x, 1x) for parallax
  • BG0 (frontmost) remains stationary
  • Each layer uses a distinct 4-color palette
Modules Used
console, dma, background
See also
background.h, dma.h, video.h

Function Documentation

◆ main()

int main ( void )

Entry point – load 4 BG layers and animate parallax scrolling.

Loads four independent 2bpp tilesets and tilemaps into separate VRAM regions, one per Mode 0 background layer. Each layer gets its own palette bank (Mode 0 provides 4 banks of 4 colors). After setup, the main loop increments scroll positions at different speeds every 3 frames to create a parallax depth illusion: BG1 (foreground) moves fastest, BG3 (farthest) moves slowest, and BG0 stays stationary.

Returns
Never returns (infinite loop).

Variable Documentation

◆ flip

u16 flip = 0

Frame counter for scroll speed throttle (scrolls every 3 frames).

◆ sxbg1

s16 sxbg1 = 0

Horizontal scroll position for BG1 (fastest layer, 3px per tick).

◆ sxbg2

s16 sxbg2 = 0

Horizontal scroll position for BG2 (medium layer, 2px per tick).

◆ sxbg3

s16 sxbg3 = 0

Horizontal scroll position for BG3 (slowest layer, 1px per tick).