Two-layer parallax scrolling with player sprite. More...
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 | |
| GameState | game = {20, 100, 0, 32, 0, 32} |
| Global game state instance with initial values. | |
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.
| #define MAX_SCROLL_X 512 |
Maximum horizontal scroll (depends on tilemap size)
| #define SCROLL_THRESHOLD_LEFT 80 |
When player X < this, scroll left
| #define SCROLL_THRESHOLD_RIGHT 140 |
When player X > this, scroll right
| 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.
< 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
| GameState game = {20, 100, 0, 32, 0, 32} |
Global game state instance with initial values.