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

Two-layer parallax scrolling with player sprite. More...

#include <snes.h>

Classes

struct  GameState
 Centralized game state structure. More...
 

Macros

Scroll Configuration

Constants controlling the auto-scroll behavior

#define MAX_SCROLL_X   512
 
#define SCROLL_THRESHOLD_LEFT   80
 
#define SCROLL_THRESHOLD_RIGHT   140
 

Functions

int main (void)
 Entry point – two-layer parallax scrolling with player sprite.
 

Variables

u8 bg1_map []
 
u8 bg1_map_end []
 
u8 bg1_pal []
 
u8 bg1_pal_end []
 
u8 bg1_tiles []
 
u8 bg1_tiles_end []
 
u8 bg2_map []
 
u8 bg2_map_end []
 
u8 bg2_pal []
 
u8 bg2_pal_end []
 
u8 bg2_tiles []
 
u8 bg2_tiles_end []
 
u8 char_pal []
 
u8 char_pal_end []
 
u8 char_tiles []
 
u8 char_tiles_end []
 
GameState game = {20, 100, 0, 32, 0, 32}
 Global game state instance with initial values.
 

Detailed Description

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

Macro Definition Documentation

◆ MAX_SCROLL_X

#define MAX_SCROLL_X   512

Maximum horizontal scroll (depends on tilemap size)

◆ SCROLL_THRESHOLD_LEFT

#define SCROLL_THRESHOLD_LEFT   80

When player X < this, scroll left

◆ SCROLL_THRESHOLD_RIGHT

#define SCROLL_THRESHOLD_RIGHT   140

When player X > this, scroll right

Function Documentation

◆ main()

int main ( void  )

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

Variable Documentation

◆ bg1_map

u8 bg1_map[]
extern

◆ bg1_map_end

u8 bg1_map_end[]

◆ bg1_pal

u8 bg1_pal[]
extern

◆ bg1_pal_end

u8 bg1_pal_end[]

◆ bg1_tiles

u8 bg1_tiles[]
extern

◆ bg1_tiles_end

u8 bg1_tiles_end[]

◆ bg2_map

u8 bg2_map[]
extern

◆ bg2_map_end

u8 bg2_map_end[]

◆ bg2_pal

u8 bg2_pal[]
extern

◆ bg2_pal_end

u8 bg2_pal_end[]

◆ bg2_tiles

u8 bg2_tiles[]
extern

◆ bg2_tiles_end

u8 bg2_tiles_end[]

◆ char_pal

u8 char_pal[]
extern

◆ char_pal_end

u8 char_pal_end[]

◆ char_tiles

u8 char_tiles[]
extern

◆ char_tiles_end

u8 char_tiles_end[]

◆ game

GameState game = {20, 100, 0, 32, 0, 32}

Global game state instance with initial values.