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

A threshold camera over two layers in parallax: Kit runs on the roofs. More...

#include <snes.h>
#include "res/roofs.inc"
#include "res/skyline.inc"
#include "res/kit.inc"

Classes

struct  GameState
 Centralized game state structure. More...

Macros

#define FRAME_IDLE   0
 The frames in res/kit.png, 32x32, four a row: idle 0-3, run 4-11.
#define FRAME_RUN   4
#define KIT_Y   105
 Kit's sprite y: his feet (row 30 of the frame) on the roofs (picture row 144, scrolled by 8).
Scroll Configuration

Constants controlling the camera

#define MAX_SCROLL_X   256
#define SCROLL_THRESHOLD_LEFT   80
#define SCROLL_THRESHOLD_RIGHT   140

Functions

static u16 frame_tile (u16 f)
 The OAM tile number of frame f of the sheet.
int main (void)
 Entry point – a threshold camera over two layers in parallax.

Variables

static u8 facing_left
 Kit faces left (his frames face right; the sprite is mirrored).
GameState game = {20, 105 , 0, 8, 0, 240}
 Global game state instance with initial values.

Detailed Description

A threshold camera over two layers in parallax: Kit runs on the roofs.

Kit runs along a stretch of roofs 512 px wide (BG1, a 64x32 map) in front of the far towers (BG2). The camera follows him without a map engine: while he is between two thresholds of the screen he moves and the world stays; what he goes past one, the world scrolls instead and he is put back on it, so he keeps his place on screen while the roofs go by. BG2 scrolls at half BG1's speed, which is what makes the towers look far away. Scroll registers are written via bgSetScroll(), which defers the actual PPU register write to the NMI handler via a dirty flag.

SNES Concepts
  • Multi-layer parallax scrolling (BG1 + BG2 at independent speeds)
  • A 64x32 background map: two 32x32 pages side by side, 512 px of world
  • Deferred scroll register writes via NMI dirty flags
  • Multiple tile sets and palette slots in VRAM/CGRAM
  • A 32x32 sprite, animated and mirrored by its attribute's H-flip bit
What to Observe
  • Hold RIGHT: Kit runs to x 140 and stays there; the roofs scroll by under him and the towers behind them follow at half the speed
  • At the end of the roofs the camera stops and Kit runs on to the edge
  • Release: Kit stands and breathes; LEFT runs back
Modules Used
console, sprite, input, background, dma
See also
background.h, sprite.h, input.h, dma.h

Macro Definition Documentation

◆ FRAME_IDLE

#define FRAME_IDLE   0

The frames in res/kit.png, 32x32, four a row: idle 0-3, run 4-11.

◆ FRAME_RUN

#define FRAME_RUN   4

◆ KIT_Y

#define KIT_Y   105

Kit's sprite y: his feet (row 30 of the frame) on the roofs (picture row 144, scrolled by 8).

◆ MAX_SCROLL_X

#define MAX_SCROLL_X   256

BG1's last scroll: 512 px of roofs minus one 256 px screen

◆ SCROLL_THRESHOLD_LEFT

#define SCROLL_THRESHOLD_LEFT   80

When Kit's x < this, scroll left

◆ SCROLL_THRESHOLD_RIGHT

#define SCROLL_THRESHOLD_RIGHT   140

When Kit's x > this, scroll right

Function Documentation

◆ frame_tile()

u16 frame_tile ( u16 f)
static

The OAM tile number of frame f of the sheet.

A 32x32 frame is 4x4 tiles. VRAM rows of sprite tiles are 16 tiles wide, so a frame row of four is one VRAM row of tiles: frame f starts at column (f % 4) * 4 of tile row (f / 4) * 4.

◆ main()

int main ( void )

Entry point – a threshold camera over two layers in parallax.

Loads the roofs (BG1, 64x32), the far towers (BG2, 64x32) and Kit's frames, each in its own VRAM and CGRAM slot. Each frame, the D-pad moves Kit; past a threshold the camera scrolls instead, BG2 at half speed.

Returns
Does not return (infinite loop).

< Current joypad button state (read from hardware)

< Kit's current frame

< Frames since the animation started

< How far past a threshold Kit went this frame

< When Kit's x > this, scroll right

< BG1's last scroll: 512 px of roofs minus one 256 px screen

< When Kit's x > this, scroll right

< BG1's last scroll: 512 px of roofs minus one 256 px screen

< BG1's last scroll: 512 px of roofs minus one 256 px screen

< When Kit's x < this, scroll left

< When Kit's x < this, scroll left

Variable Documentation

◆ facing_left

u8 facing_left
static

Kit faces left (his frames face right; the sprite is mirrored).

◆ game

GameState game = {20, 105 , 0, 8, 0, 240}

Global game state instance with initial values.