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

Two ways to mix two layers: colour addition, and pseudo-hires. More...

#include <snes.h>
#include <snes/console.h>
#include <snes/video.h>
#include <snes/background.h>
#include <snes/colormath.h>
#include "res/street.inc"
#include "res/mist.inc"

Macros

#define BG1_MAP   0x5000
#define BG1_TILES   0x0000
 The street and the mist, converted by the build from res/.
#define BG3_MAP   0x5400
#define BG3_TILES   0x4000

Functions

int main (void)
 Entry point: color math transparency with drifting mist over a street.

Variables

u8 ph_on
 1 while the layers are mixed by pseudo-hires instead of addition

Detailed Description

Two ways to mix two layers: colour addition, and pseudo-hires.

Demonstrates the SNES color math unit by blending two background layers. BG1 (main screen) shows a 4bpp rainy street at night, while BG3 (sub screen) holds 2bpp mist that drifts horizontally. The PPU adds the sub screen pixel colors to the main screen via registers CGWSEL ($2130) and CGADSUB ($2131), producing a translucent overlay effect.

Mode 1 is used with BG3 priority high so the 2bpp mist layer renders above BG1. Color addition is applied to BG1 and the backdrop, meaning the mist colour is numerically added to whatever is underneath.

Press A for the other way the PPU mixes two layers: pseudo-hires (SETINI bit 3). The colour math is switched off, and the PPU interleaves the sub screen (even columns) with the main screen (odd columns) into a 512-pixel line: a 50 % blend that costs no colour math at all, at the price of the horizontal resolution of each layer. A again goes back to addition.

This began as the PVSnesLib Transparency example by Alekmaul; the pseudo-hires mode was the pseudo_hires example, merged here. The street and the mist are ours: the street generated, the mist procedural (wisps of a periodic noise, so they wrap at 256 pixels).

SNES Concepts
  • Color math: addition mode via CGWSEL/CGADSUB registers
  • Main screen vs sub screen layer assignment
  • Mode 1 with BG3 priority high for overlay effects
  • 4bpp (BG1) and 2bpp (BG3) tile formats in the same mode
  • Pseudo-hires: SETINI ($2133) bit 3, videoSetPseudoHires(); main and sub screens alternate pixel by pixel on a 512-pixel line
What to Observe
  • A rainy street with semi-transparent mist drifting over it
  • The mist blends additively, lightening the street beneath it
  • The mist drifts by itself
  • Press A: the mist is no longer added but interleaved column by column with the street (pseudo-hires); A again: back to addition
Modules Used
console, sprite, dma, background, colormath, input
See also
colormath.h, background.h, video.h

Macro Definition Documentation

◆ BG1_MAP

#define BG1_MAP   0x5000

◆ BG1_TILES

#define BG1_TILES   0x0000

The street and the mist, converted by the build from res/.

A C pointer carries its bank byte, so dmaCopyVram / dmaCopyCGram take the data wherever the linker placed it.

VRAM word addresses: the street's 883 tiles fill $0000-$372F

◆ BG3_MAP

#define BG3_MAP   0x5400

◆ BG3_TILES

#define BG3_TILES   0x4000

Function Documentation

◆ main()

int main ( void )

Entry point: color math transparency with drifting mist over a street.

Sets up Mode 1 with two background layers:

  • BG1 (main screen): 4bpp street, static
  • BG3 (sub screen): 2bpp semi-transparent mist, scrolling horizontally

The SNES color math unit adds the sub screen pixel colors to the main screen, producing a translucent mist overlay. BG3 is given priority high so its tiles render above BG1 in Mode 1's layer ordering.

Returns
Never returns (infinite loop).

Horizontal scroll offset for the mist layer (BG3), incremented each frame

Variable Documentation

◆ ph_on

u8 ph_on

1 while the layers are mixed by pseudo-hires instead of addition