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

An HDMA-shaped window: a torch beam in a dark alley, masking layers or lighting them. More...

#include <snes.h>
#include "res/alley.inc"
#include "res/rain.inc"
#include "res/kit.inc"

Macros

#define BEAM_LINES   (224 - BEAM_TOP)
 Lines of the beam, BEAM_TOP to the last visible line (223).
#define BEAM_TOP   (BEAM_Y - 70)
 First line of the beam: it widens by one line every 3 pixels, so at x 255 it spans BEAM_Y - 70 to the bottom of the screen.
#define W12SEL_BG1   0x03 /* BG1: enable(0x02) + invert(0x01) */
 W12SEL value to enable Window 1 on BG1 with inversion (mask outside).
#define W12SEL_BG2   0x30 /* BG2: enable(0x20) + invert(0x10) */
 W12SEL value to enable Window 1 on BG2 with inversion.
#define W12SEL_BOTH   0x33 /* Both BGs */
 W12SEL value to enable Window 1 on both BG1 and BG2 with inversion.
VRAM layout (word addresses; a BG's tiles start on a $1000 boundary)
#define VRAM_BG1_MAP   0x0000
#define VRAM_BG1_TILES   0x1000
#define VRAM_BG2_MAP   0x0400
#define VRAM_BG2_TILES   0x5000
#define VRAM_OBJ   0x6000
Kit and his torch
#define BEAM_X   44
#define BEAM_Y   187
#define KIT_X   16
#define KIT_Y   168

Functions

static void buildBeam (void)
 Compute the two tables of the beam.
int main (void)
 Entry point: an HDMA beam window with selectable BG layers.
static void setup_light (void)
 Use the beam as the region of colour math instead of a mask.
static void setup_window (u8 layers, u8 w12sel_val)
 Configure HDMA-driven window masking on the specified BG layers.

Variables

static u16 rain_x
 The rain's scroll: it falls a pixel left for four down.
static u16 rain_y
u8 tablelefttriangle [2+1+(224 -(187 - 70))+1]
 HDMA table for the left window boundary (WH0, $2126).
u8 tablerighttriangle [2+1+(224 -(187 - 70))+1]
 HDMA table for the right window boundary (WH1, $2127).

Detailed Description

An HDMA-shaped window: a torch beam in a dark alley, masking layers or lighting them.

Kit stands in a dark back alley with a torch. The beam is a window: its left edge (WH0) changes on every scanline, driven by HDMA, so the window is a cone that leaves the torch and opens to the right. Two backgrounds are loaded — the alley (BG1) and the rain falling in front of it (BG2) — and the buttons choose what the beam does to them.

HDMA channels 4 and 5 drive WH0 ($2126) and WH1 ($2127) in repeat mode, updating the left and right window boundaries every scanline. The W12SEL register ($2123) controls which BG layers are affected by Window 1, with the invert bit set so pixels outside the beam are masked (clipped to black). Kit is a sprite, and the window is not enabled on sprites: he stays visible in the dark.

The same window has a second use: instead of hiding layers, it can mark where colour math happens. Press Y and the beam stops clipping and lights instead — colour math adds a warm fixed colour inside it only (CGWSEL "inside the colour window"), the way a game draws a light, a shadow or a dialogue box over the scene.

Ported from PVSnesLib's "Window" example; the colour-math mode is its "TransparentWindow" example (by Digifox), adding instead of subtracting.

SNES Concepts
  • Window masking via W12SEL ($2123) and TMW ($212E) registers
  • HDMA repeat mode for per-scanline WH0/WH1 updates, the table computed at boot from the shape it draws
  • Window inversion (mask outside vs inside the region)
  • Runtime window reconfiguration via register writes
  • The colour window (WOBJSEL) as the region of colour math (CGWSEL/CGADSUB)
  • Fixed colour source (COLDATA, $2132) added to brighten
What to Observe
  • At boot the alley and the rain are black but for the torch's beam
  • Press A: the beam on the alley only — the rain falls everywhere
  • Press X: the beam on the rain only — the alley is lit, the rain shows only where the torch catches it
  • Press B: the beam on both again
  • Press Y: nothing is clipped, and the beam brightens what it falls on
Modules Used
console, sprite, dma, input, background, window, hdma, colormath
See also
hdma.h, window.h, colormath.h, video.h, background.h

Macro Definition Documentation

◆ BEAM_LINES

#define BEAM_LINES   (224 - BEAM_TOP)

Lines of the beam, BEAM_TOP to the last visible line (223).

◆ BEAM_TOP

#define BEAM_TOP   (BEAM_Y - 70)

First line of the beam: it widens by one line every 3 pixels, so at x 255 it spans BEAM_Y - 70 to the bottom of the screen.

◆ BEAM_X

#define BEAM_X   44

the beam's tip: the torch, at (27, 19) in his frame

◆ BEAM_Y

#define BEAM_Y   187

◆ KIT_X

#define KIT_X   16

Kit's sprite x

◆ KIT_Y

#define KIT_Y   168

Kit's sprite y: his feet on the alley's ground

◆ VRAM_BG1_MAP

#define VRAM_BG1_MAP   0x0000

the alley's map, 32x32

◆ VRAM_BG1_TILES

#define VRAM_BG1_TILES   0x1000

the alley's 889 tiles, up to $478F

◆ VRAM_BG2_MAP

#define VRAM_BG2_MAP   0x0400

the rain's map, 32x32

◆ VRAM_BG2_TILES

#define VRAM_BG2_TILES   0x5000

the rain's tiles

◆ VRAM_OBJ

#define VRAM_OBJ   0x6000

Kit

◆ W12SEL_BG1

#define W12SEL_BG1   0x03 /* BG1: enable(0x02) + invert(0x01) */

W12SEL value to enable Window 1 on BG1 with inversion (mask outside).

◆ W12SEL_BG2

#define W12SEL_BG2   0x30 /* BG2: enable(0x20) + invert(0x10) */

W12SEL value to enable Window 1 on BG2 with inversion.

◆ W12SEL_BOTH

#define W12SEL_BOTH   0x33 /* Both BGs */

W12SEL value to enable Window 1 on both BG1 and BG2 with inversion.

Function Documentation

◆ buildBeam()

void buildBeam ( void )
static

Compute the two tables of the beam.

< the beam's tip: the torch, at (27, 19) in his frame

◆ main()

int main ( void )

Entry point: an HDMA beam window with selectable BG layers.

Loads the alley, the rain and Kit, computes the beam, and enters a loop where button presses reconfigure what the beam does.

Returns
Never returns (infinite loop).

< the alley's map, 32x32

< the alley's 889 tiles, up to $478F

< the alley's map, 32x32

< the rain's map, 32x32

< the rain's tiles

< the rain's map, 32x32

< Kit

< Kit's sprite x

< Kit's sprite y: his feet on the alley's ground

◆ setup_light()

void setup_light ( void )
static

Use the beam as the region of colour math instead of a mask.

The HDMA tables are the same; what changes is what the window is for. No layer is clipped (TMW = 0): the colour window, not inverted, is the beam, and colour math adds a warm fixed colour (6, 5, 2) to BG1 and BG2 inside it only (CGWSEL bits 5-4 = 01). PVSnesLib's TransparentWindow subtracted, to darken; adding lights.

◆ setup_window()

void setup_window ( u8 layers,
u8 w12sel_val )
static

Configure HDMA-driven window masking on the specified BG layers.

The window "inverts" masking (via the invert bits in W12SEL), so pixels OUTSIDE the beam are clipped to black, while pixels inside remain visible. This is the opposite of the default behavior (which masks inside).

HDMA channels 4 and 5 are used (not 7, which carries every DMA of the NMI handler). HDMA is disabled before reconfiguration and re-enabled after to avoid partial-table reads during the transition.

Parameters
layersBitmask of BG layers for TMW (e.g., TM_BG1 | TM_BG2)
w12sel_valW12SEL register value specifying which BGs use Window 1

Variable Documentation

◆ rain_x

u16 rain_x
static

The rain's scroll: it falls a pixel left for four down.

◆ rain_y

u16 rain_y
static

◆ tablelefttriangle

u8 tablelefttriangle[2+1+(224 -(187 - 70))+1]

HDMA table for the left window boundary (WH0, $2126).

BEAM_TOP lines with WH0 = 255 (left > right: no window), then one value a line for the beam — BEAM_X + 3 x |line - BEAM_Y|, the cone's edge, or 255 where that is past the screen — then the end of the table.

◆ tablerighttriangle

u8 tablerighttriangle[2+1+(224 -(187 - 70))+1]

HDMA table for the right window boundary (WH1, $2127).

The cone reaches the right edge of the screen on every line it covers: WH1 = 255 there, and 0 (no window) above it.