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

Mixed scrolling with fixed and auto-scrolling background layers. More...

#include <snes.h>

Functions

int main (void)
 Entry point – mixed scrolling with auto-scrolling BG1 and fixed BG2.
 

Variables

u8 bg1_map
 BG1 tilemap (32x32 tile grid loaded to VRAM $1800).
 
u8 bg1_map_end
 
u8 bg1_pal
 BG1 palette (16 colors loaded to CGRAM slot 1, offset 16).
 
u8 bg1_pal_end
 
u8 bg1_tiles
 BG1 tile data – repeating shader pattern (4bpp, tiles at VRAM $4000).
 
u8 bg1_tiles_end
 
u8 bg2_map
 BG2 tilemap (32x32 tile grid loaded to VRAM $1400).
 
u8 bg2_map_end
 
u8 bg2_pal
 BG2 palette (16 colors loaded to CGRAM slot 0, offset 0).
 
u8 bg2_pal_end
 
u8 bg2_tiles
 BG2 tile data – static logo (4bpp, tiles at VRAM $5000).
 
u8 bg2_tiles_end
 
s16 scrX = 0
 BG1 horizontal scroll offset (auto-incremented each frame).
 
s16 scrY = 0
 BG1 vertical scroll offset (auto-incremented each frame).
 

Detailed Description

Mixed scrolling with fixed and auto-scrolling background layers.

Layers two Mode 1 backgrounds: BG1 displays a repeating shader pattern that auto-scrolls diagonally each frame, while BG2 shows a static logo that remains fixed. Each layer uses its own tile set and palette slot in VRAM and CGRAM (BG1 tiles at $4000 with palette slot 1, BG2 tiles at $5000 with palette slot 0). The tilemaps are placed at non-overlapping VRAM addresses ($1800 for BG1, $1400 for BG2). This demonstrates how the SNES PPU composites multiple BG layers with independent scroll offsets, making it straightforward to combine static UI elements with animated backgrounds.

SNES Concepts
  • Independent per-layer scroll registers (BG1 scrolls, BG2 stays fixed)
  • Multiple tile sets sharing VRAM without overlap
  • Separate palette slots per layer via gfx4snes -e flag
  • Mode 1 dual-layer compositing (BG1 behind BG2)
What to Observe
  • The shader pattern (BG1) scrolls diagonally and wraps seamlessly
  • The logo (BG2) remains stationary in the center
  • Both layers composite together with proper priority ordering
Modules Used
console, sprite, dma, background
See also
background.h, dma.h, video.h

Function Documentation

◆ main()

int main ( void  )

Entry point – mixed scrolling with auto-scrolling BG1 and fixed BG2.

Loads two Mode 1 background layers into non-overlapping VRAM regions:

  • BG1: shader pattern (tiles at $4000, tilemap at $1800, palette slot 1)
  • BG2: static logo (tiles at $5000, tilemap at $1400, palette slot 0)

Each frame, BG1's scroll position is incremented diagonally by 1 pixel in both X and Y, causing the shader pattern to scroll continuously. BG2 has no scroll updates, so it remains fixed on screen. The SNES PPU composites both layers together, with BG2 (the logo) appearing on top of BG1 due to default priority ordering.

Note
The extern declarations use scalar syntax (not array) because the data symbols are defined in assembly as labels. Address-of (&) is used to obtain their pointers for DMA operations.
Returns
Does not return (infinite loop).

Variable Documentation

◆ bg1_map

u8 bg1_map
extern

BG1 tilemap (32x32 tile grid loaded to VRAM $1800).

◆ bg1_map_end

u8 bg1_map_end

◆ bg1_pal

u8 bg1_pal
extern

BG1 palette (16 colors loaded to CGRAM slot 1, offset 16).

◆ bg1_pal_end

u8 bg1_pal_end

◆ bg1_tiles

u8 bg1_tiles
extern

BG1 tile data – repeating shader pattern (4bpp, tiles at VRAM $4000).

◆ bg1_tiles_end

u8 bg1_tiles_end

◆ bg2_map

u8 bg2_map
extern

BG2 tilemap (32x32 tile grid loaded to VRAM $1400).

◆ bg2_map_end

u8 bg2_map_end

◆ bg2_pal

u8 bg2_pal
extern

BG2 palette (16 colors loaded to CGRAM slot 0, offset 0).

◆ bg2_pal_end

u8 bg2_pal_end

◆ bg2_tiles

u8 bg2_tiles
extern

BG2 tile data – static logo (4bpp, tiles at VRAM $5000).

◆ bg2_tiles_end

u8 bg2_tiles_end

◆ scrX

s16 scrX = 0

BG1 horizontal scroll offset (auto-incremented each frame).

◆ scrY

s16 scrY = 0

BG1 vertical scroll offset (auto-incremented each frame).