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

Backgrounds — offset-per-tile in Mode 2 and Mode 4: per-column scroll from BG3. More...

#include <snes.h>
#include <snes/math.h>
#include "res/street.inc"
#include "res/canal.inc"

Macros

#define BG1_MAP   0x7000
#define BG3_MAP   0x7400 /* BG3 = the OPT offset table, both modes */
#define OPT_BG1   0x2000
#define OPT_VERTICAL   0x8000
#define SCENE_CHR   0x0000 /* BG1's tiles, either scene: the street's 883, or the canal's two parts */

Functions

static void loadScene (u8 mode4)
 Load one scene in force blank: its tiles, map and palette, the mode, BG1's pointers.
int main (void)

Variables

u8 in_mode4
 1 while the Mode 4 scene is shown.
u8 opt_vertical
 Mode 4 only: 1 = the wave goes in as vertical words.
static u16 owords [32]
static u16 vwords [32]
u16 wave_phase
 Probe oracle: wave phase, advances each frame.
static u16 zeros [32]

Detailed Description

Backgrounds — offset-per-tile in Mode 2 and Mode 4: per-column scroll from BG3.

Modes 2, 4 and 6 have a trick no other mode does: offset-per-tile (OPT). BG3 stops being a drawable layer and becomes an offset table — for each 8-pixel column of BG1/BG2 the PPU reads a scroll offset out of BG3's tilemap. That lets each column scroll independently, which is how you get a flag rippling in the wind, heat-haze, or per-column parallax without a scanline of HDMA.

The example boots in Mode 2 on the Rue basse under the rain (4bpp, the picture of backgrounds/mode1): a sine table written into BG3 gives every column its own vertical offset, a few lines up or down, and the air shimmers over the street like the heat above a steam vent. B switches to Mode 4 and the canal at night (8bpp, 256 colours, the picture of backgrounds/mode3), with the same wave in Mode 4's single offset row — as vertical words, or as horizontal ones after A, when the columns slide sideways in 8-pixel steps. B again goes back to Mode 2. The two pictures do not fit in VRAM together (28 KB and 50 KB), so a switch reloads the scene in force blank: one dark frame.

ROM mode: LoROM (project default).

SNES Concepts
  • Mode 2 OPT: setMode(BG_MODE2, 0); BG3's tilemap is the offset source, not a picture
  • Mode 2: per column N≥1 the PPU reads BG3 tile-row 0 for the H offset word and tile-row 1 for the V offset word; column 0 cannot be offset
  • Mode 4: ONE row; bit 15 of a word says vertical (1) or horizontal (0)
  • Offset word: bit 0x2000 = apply to BG1 (0x4000 = BG2); V value in bits 0-9, H value in bits 3-9 (whole tiles)
  • An offset word replaces the layer's scroll register for its column, so it is the raw value: the -1 the lib adds to a vertical scroll is not there, and a word of 0x3FF (-1) lines a column up with column 0
  • Mode 4's BG1 is 8bpp (256 colours), Mode 2's is 4bpp
What to Observe
The street shimmers as a travelling wave, each 8-pixel column a few lines higher or lower, all from one small BG3 table; column 0, at the left edge, stays still. B: the canal in 256 colours ripples the same way; A there: its columns slide sideways instead, by whole tiles. wave_phase is the probe oracle.
Modules Used
console, dma, background, input, math
See also
lib/include/snes/video.h — setMode(BG_MODE2), setMode(BG_MODE4)

Macro Definition Documentation

◆ BG1_MAP

#define BG1_MAP   0x7000

◆ BG3_MAP

#define BG3_MAP   0x7400 /* BG3 = the OPT offset table, both modes */

◆ OPT_BG1

#define OPT_BG1   0x2000

apply this offset word to BG1

◆ OPT_VERTICAL

#define OPT_VERTICAL   0x8000

Mode 4: 1 = vertical offset, 0 = horizontal

◆ SCENE_CHR

#define SCENE_CHR   0x0000 /* BG1's tiles, either scene: the street's 883, or the canal's two parts */

Function Documentation

◆ loadScene()

void loadScene ( u8 mode4)
static

Load one scene in force blank: its tiles, map and palette, the mode, BG1's pointers.

◆ main()

int main ( void )

< apply this offset word to BG1

< Mode 4: 1 = vertical offset, 0 = horizontal

< apply this offset word to BG1

Variable Documentation

◆ in_mode4

u8 in_mode4

1 while the Mode 4 scene is shown.

◆ opt_vertical

u8 opt_vertical

Mode 4 only: 1 = the wave goes in as vertical words.

◆ owords

u16 owords[32]
static

◆ vwords

u16 vwords[32]
static

◆ wave_phase

u16 wave_phase

Probe oracle: wave phase, advances each frame.

◆ zeros

u16 zeros[32]
static