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

Mode 7 EXTBG: one plane split in two layers, a sprite between them. More...

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

Macros

#define PILLAR_ROW   12
 The pillar row and the ball's height on screen.
#define VRAM_OBJ   0x4000
 Sprite tiles in VRAM (words): name base slot 2, past Mode 7's 32 KB.

Functions

static void applyExtBg (void)
 EXTBG on: BG2 (split by bit 7) on screen; off: BG1.
static void buildBall (void)
 Draw a 16 x 16 ball and upload it as sprite tiles 0, 1, 16, 17.
static void buildPlane (void)
 Fill the plane: checkered floor, 2 x 2-tile pillars on one row.
int main (void)

Variables

static const u16 ball_pal [3] = { 0, RGB(31, 28, 4), RGB(31, 16, 2) }
 OBJ palette 1 (CGRAM 144-159): transparent, yellow, orange.
u16 ball_x
static u8 ballpx [256]
 The ball, 16 x 16 pixels, and one encoded 8 x 8 tile.
static const u16 bg_pal [5]
 CGRAM 0-4 (BG2 and BG1), 131-132 (the pillar as BG1 reads it).
u8 extbg_on
 Probe oracles: EXTBG on, the ball's x.
static FAR u8 m7map [128 *128]
 The 128 x 128 Mode 7 tilemap, built at run time (16 KB: FAR).
static u8 m7tiles [3 *64]
 Three 8bpp Mode 7 tiles: floor A, floor B, pillar.
static u8 px [64]
static u8 tilebuf [32]

Detailed Description

Mode 7 EXTBG: one plane split in two layers, a sprite between them.

Mode 7 has a single layer — unless SETINI bit 6 (EXTBG) is set. Then BG2 shows the same plane as BG1 (same tilemap, same pixels, same transform), but reads each pixel's bit 7 as a priority bit and bits 0-6 as its colour. Front to back: sprites 3, sprites 2, BG2 pixels with bit 7 set, sprites 1, BG1, sprites 0, BG2 pixels with bit 7 clear. A priority-1 sprite therefore slides over the floor (bit 7 clear) and under the pillars (bit 7 set) of one and the same Mode 7 plane.

The ball crosses a row of pillars on its own; press A to switch EXTBG off (BG1 shown instead: one layer, the ball in front of everything) and on.

SNES Concepts
  • SETINI ($2133) bit 6 through mode7SetExtBg()
  • Mode 7 EXTBG priority: BG2's high pixels above priority-1 sprites, its low pixels below every sprite and below BG1 (hence BG2 alone on screen)
  • One pixel, two colours: 0x83 is colour 3 on BG2 (7-bit) and colour 131 on BG1 (8-bit)
  • Mode 7 VRAM interleave (dmaCopyVramMode7), a plane and its tiles built at run time in a FAR buffer
What to Observe
  • The yellow ball passes in front of the green floor and behind the grey pillars — the same Mode 7 plane, split by bit 7 of its pixels.
  • Press A: EXTBG off, BG1 on screen, the ball now covers the pillars too.
Modules Used
console, dma, sprite, input, mode7, tile
See also
mode7.h (mode7SetExtBg), docs/tutorials/mode7.md

Macro Definition Documentation

◆ PILLAR_ROW

#define PILLAR_ROW   12

The pillar row and the ball's height on screen.

◆ VRAM_OBJ

#define VRAM_OBJ   0x4000

Sprite tiles in VRAM (words): name base slot 2, past Mode 7's 32 KB.

Function Documentation

◆ applyExtBg()

void applyExtBg ( void )
static

EXTBG on: BG2 (split by bit 7) on screen; off: BG1.

◆ buildBall()

void buildBall ( void )
static

Draw a 16 x 16 ball and upload it as sprite tiles 0, 1, 16, 17.

◆ buildPlane()

void buildPlane ( void )
static

Fill the plane: checkered floor, 2 x 2-tile pillars on one row.

◆ main()

int main ( void )

Variable Documentation

◆ ball_pal

const u16 ball_pal[3] = { 0, RGB(31, 28, 4), RGB(31, 16, 2) }
static

OBJ palette 1 (CGRAM 144-159): transparent, yellow, orange.

◆ ball_x

u16 ball_x

◆ ballpx

u8 ballpx[256]
static

The ball, 16 x 16 pixels, and one encoded 8 x 8 tile.

◆ bg_pal

const u16 bg_pal[5]
static
Initial value:
= {
RGB(2, 2, 6), RGB(6, 20, 6), RGB(3, 12, 3), RGB(26, 26, 28), RGB(14, 14, 16),
}
#define RGB(r, g, b)
Create RGB color value.
Definition video.h:88

CGRAM 0-4 (BG2 and BG1), 131-132 (the pillar as BG1 reads it).

◆ extbg_on

u8 extbg_on

Probe oracles: EXTBG on, the ball's x.

◆ m7map

FAR u8 m7map[128 *128]
static

The 128 x 128 Mode 7 tilemap, built at run time (16 KB: FAR).

◆ m7tiles

u8 m7tiles[3 *64]
static

Three 8bpp Mode 7 tiles: floor A, floor B, pillar.

◆ px

u8 px[64]
static

◆ tilebuf

u8 tilebuf[32]
static