Loading...
Searching...
No Matches
main.c File Reference

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

#include <snes.h>

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)
 
BG0 asset pointers (defined in data.asm via .incbin)
u8 bgm0 []
 
u8 bgm0_end []
 
u8 p0 []
 
u8 t0 []
 
u8 t0_end []
 
BG1 asset pointers (defined in data.asm via .incbin)
u8 bgm1 []
 
u8 bgm1_end []
 
u8 p1 []
 Player 1 state, initialized at left-center of screen.
 
u8 t1 []
 
u8 t1_end []
 
BG2 asset pointers (defined in data.asm via .incbin)
u8 bgm2 []
 
u8 bgm2_end []
 
u8 p2 []
 Player 2 state, initialized at right-center of screen.
 
u8 t2 []
 
u8 t2_end []
 
BG3 asset pointers (defined in data.asm via .incbin)
u8 bgm3 []
 
u8 bgm3_end []
 
u8 p3 []
 
u8 t3 []
 
u8 t3_end []
 

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

◆ bgm0

u8 bgm0[]
extern

◆ bgm0_end

u8 bgm0_end[]

BG0 tilemap

◆ bgm1

u8 bgm1[]
extern

◆ bgm1_end

u8 bgm1_end[]

BG1 tilemap

◆ bgm2

u8 bgm2[]
extern

◆ bgm2_end

u8 bgm2_end[]

BG2 tilemap

◆ bgm3

u8 bgm3[]
extern

◆ bgm3_end

u8 bgm3_end[]

BG3 tilemap

◆ flip

u16 flip = 0

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

◆ p0

u8 p0[]
extern

BG0 palette (4 colors, 8 bytes)

◆ p1

u8 p1[]
extern

Player 1 state, initialized at left-center of screen.

BG1 palette

◆ p2

u8 p2[]
extern

Player 2 state, initialized at right-center of screen.

BG2 palette

◆ p3

u8 p3[]
extern

BG3 palette

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

◆ t0

u8 t0[]
extern

◆ t0_end

u8 t0_end[]

BG0 tile data (2bpp)

◆ t1

u8 t1[]
extern

◆ t1_end

u8 t1_end[]

BG1 tile data (2bpp)

◆ t2

u8 t2[]
extern

◆ t2_end

u8 t2_end[]

BG2 tile data (2bpp)

◆ t3

u8 t3[]
extern

◆ t3_end

u8 t3_end[]

BG3 tile data (2bpp)