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

Backgrounds — Mode 4: a 256-colour layer with offset-per-tile. More...

#include <snes.h>
#include <snes/tile.h>
#include <snes/math.h>

Macros

#define BG1_CHR   0x0000
 VRAM word addresses: BG1 8bpp chars, BG1 map, BG3 offset map.
#define BG1_MAP   0x2000
#define BG3_MAP   0x3000
#define OPT_BG1   0x2000
#define OPT_VERTICAL   0x8000
 Mode 4 offset word bits.

Functions

static u16 hue (u16 i)
 Colour i of a 256-step hue ramp, 32 steps per sextant pair.
int main (void)

Variables

static u16 maprow [32]
 One tilemap row being built.
u8 opt_vertical
static u16 owords [32]
 The single offset row, rebuilt every frame.
static u16 pal [256]
 The 256-colour palette, built at boot.
static u8 px [64]
 One 8bpp tile's pixels and its encoding.
static u8 tilebuf [64]
u16 wave_phase
 Probe oracles: wave phase, 1 while the words are vertical.

Detailed Description

Backgrounds — Mode 4: a 256-colour layer with offset-per-tile.

Mode 4 is the cartridge-game mode of offset-per-tile: an 8bpp (256 colour) BG1, a 2bpp BG2, and BG3 as an offset table like Mode 2. The difference is in that table: Mode 4 reads only one row of it, so each column gets one offset word that is either horizontal or vertical, chosen by bit 15 of the word.

Here BG1 is a diagonal 256-colour gradient. A sine wave is written into the single offset row each frame: as vertical words, the columns ripple up and down; press A and the same wave is written as horizontal words, and the columns slide sideways instead (8 pixels at a time — the low 3 bits of a horizontal offset are ignored).

SNES Concepts
  • Mode 4: setMode(BG_MODE4, 0), BG1 8bpp + BG2 2bpp, BG3 = offset map
  • The Mode 4 offset word: bit 15 = 1 vertical / 0 horizontal, bit 13 = apply to BG1 (bit 14 = BG2), value in bits 0-9; one row of 32 words
  • Column 0 is never offset: word 0 applies to the second visible column
  • 8bpp tiles built at run time (tileEncode8bpp), a 256-entry palette
What to Observe
  • The gradient's columns ripple vertically as a travelling wave; the leftmost column stays put.
  • Press A: the columns now shift horizontally, in 8-pixel steps.
Modules Used
console, dma, background, input, math, tile
See also
backgrounds/mode2 (the two-row table), docs/tutorials/graphics.md

Macro Definition Documentation

◆ BG1_CHR

#define BG1_CHR   0x0000

VRAM word addresses: BG1 8bpp chars, BG1 map, BG3 offset map.

◆ BG1_MAP

#define BG1_MAP   0x2000

◆ BG3_MAP

#define BG3_MAP   0x3000

◆ OPT_BG1

#define OPT_BG1   0x2000

apply this word to BG1

◆ OPT_VERTICAL

#define OPT_VERTICAL   0x8000

Mode 4 offset word bits.

1 = vertical offset, 0 = horizontal

Function Documentation

◆ hue()

u16 hue ( u16 i)
static

Colour i of a 256-step hue ramp, 32 steps per sextant pair.

◆ main()

int main ( void )

< 1 = vertical offset, 0 = horizontal

< apply this word to BG1

< apply this word to BG1

Variable Documentation

◆ maprow

u16 maprow[32]
static

One tilemap row being built.

◆ opt_vertical

u8 opt_vertical

◆ owords

u16 owords[32]
static

The single offset row, rebuilt every frame.

◆ pal

u16 pal[256]
static

The 256-colour palette, built at boot.

◆ px

u8 px[64]
static

One 8bpp tile's pixels and its encoding.

◆ tilebuf

u8 tilebuf[64]
static

◆ wave_phase

u16 wave_phase

Probe oracles: wave phase, 1 while the words are vertical.