An HDMA-shaped window: a torch beam in a dark alley, masking layers or lighting them. More...
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). | |
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.
| #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 BEAM_X 44 |
the beam's tip: the torch, at (27, 19) in his frame
| #define BEAM_Y 187 |
| #define KIT_X 16 |
Kit's sprite x
| #define KIT_Y 168 |
Kit's sprite y: his feet on the alley's ground
| #define VRAM_BG1_MAP 0x0000 |
the alley's map, 32x32
| #define VRAM_BG1_TILES 0x1000 |
the alley's 889 tiles, up to $478F
| #define VRAM_BG2_MAP 0x0400 |
the rain's map, 32x32
| #define VRAM_BG2_TILES 0x5000 |
the rain's tiles
| #define VRAM_OBJ 0x6000 |
Kit
| #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.
|
static |
Compute the two tables of the beam.
< the beam's tip: the torch, at (27, 19) in his frame
| 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.
< 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
|
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.
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.
| layers | Bitmask of BG layers for TMW (e.g., TM_BG1 | TM_BG2) |
| w12sel_val | W12SEL register value specifying which BGs use Window 1 |
|
static |
The rain's scroll: it falls a pixel left for four down.
|
static |
| 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.
| 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.