Loading...
Searching...
No Matches
main.c File Reference

Semi-transparent darkened rectangle via HDMA windows and color math. More...

#include <snes.h>

Macros

#define RECT_HEIGHT   112
 Height of the darkened rectangle in scanlines.
 
#define RECT_WIDTH   176
 Width of the darkened rectangle in pixels.
 
#define RECT_X   40
 X coordinate of the darkened rectangle's left edge (pixels)
 
#define RECT_Y   96
 Y coordinate of the darkened rectangle's top edge (scanlines from top)
 

Functions

int main (void)
 Entry point: darkened rectangle overlay using HDMA windows and color math.
 

Variables

u8 hdma_left [128]
 HDMA table for the left window boundary (WH0, $2126).
 
u8 hdma_right [128]
 HDMA table for the right window boundary (WH1, $2127).
 
u8 palette []
 16-color palette for the background
 
u8 tilemap []
 Background tilemap data for the 32x32 tile grid.
 
u8 tilemap_end []
 
u8 tiles []
 4bpp background tile data (defined in data.asm, stored in ROM)
 
u8 tiles_end []
 

Detailed Description

Semi-transparent darkened rectangle via HDMA windows and color math.

Combines two SNES hardware features to render a darkened rectangle over a background image. HDMA drives the window position registers WH0 ($2126) and WH1 ($2127) every scanline to define the rectangle boundaries. The color math unit is configured to subtract a fixed color intensity inside the window region only (CGWSEL bit 5-4 = 01), producing a tinted overlay.

The HDMA tables use repeat mode (bit 7 set in the line count byte) because WH0/WH1 are write-only registers that must be refreshed every scanline, even when the window position is constant across the rectangle height.

Ported from PVSnesLib "TransparentWindow" example by Digifox. Uses bare-metal register writes to match PVSnesLib behavior exactly.

SNES Concepts
  • HDMA repeat mode for per-scanline window boundary updates
  • Window registers WH0/WH1 ($2126/$2127) defining a rectangular region
  • Color math subtraction inside the window region (CGWSEL/CGADSUB)
  • Fixed color source (COLDATA, $2132) for darkening intensity
What to Observe
  • A background image with a darkened rectangle overlaid in the center
  • The effect is static; no input is required
  • The rectangle edges are pixel-sharp (HDMA-defined per scanline)
Modules Used
console, sprite, dma, background, window, colormath, hdma, math
See also
hdma.h, colormath.h, video.h

Macro Definition Documentation

◆ RECT_HEIGHT

#define RECT_HEIGHT   112

Height of the darkened rectangle in scanlines.

◆ RECT_WIDTH

#define RECT_WIDTH   176

Width of the darkened rectangle in pixels.

◆ RECT_X

#define RECT_X   40

X coordinate of the darkened rectangle's left edge (pixels)

◆ RECT_Y

#define RECT_Y   96

Y coordinate of the darkened rectangle's top edge (scanlines from top)

Function Documentation

◆ main()

int main ( void  )

Entry point: darkened rectangle overlay using HDMA windows and color math.

Builds HDMA tables to define a rectangle region, loads a background image, then uses the PPU's color math unit to subtract a fixed color intensity inside the window — creating a semi-transparent darkened overlay. This technique is used in RPGs for dialogue boxes, inventory screens, etc.

Returns
Never returns (infinite loop).

Variable Documentation

◆ hdma_left

u8 hdma_left[128]

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

Built at startup to define where the darkened rectangle starts horizontally on each scanline. Before and after the rectangle, WH0 is set to 0xFF (disabling the window by placing the left edge beyond the right edge).

◆ hdma_right

u8 hdma_right[128]

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

Built at startup to define where the darkened rectangle ends horizontally on each scanline. Before the rectangle, WH1 is set to 0x00 (disabling the window by placing the right edge before the left edge).

◆ palette

u8 palette[]
extern

16-color palette for the background

◆ tilemap

u8 tilemap[]
extern

Background tilemap data for the 32x32 tile grid.

◆ tilemap_end

u8 tilemap_end[]

◆ tiles

u8 tiles[]
extern

4bpp background tile data (defined in data.asm, stored in ROM)

◆ tiles_end

u8 tiles_end[]