OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
video.h File Reference

SNES Video / PPU Functions. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define BG3_MODE1_PRIORITY_HIGH   0x08
#define BG_MODE0   0
#define BG_MODE1   1
#define BG_MODE2   2
#define BG_MODE3   3
#define BG_MODE4   4
#define BG_MODE5   5
#define BG_MODE6   6
#define BG_MODE7   7
#define LAYER_BG1   0x01
#define LAYER_BG2   0x02
#define LAYER_BG3   0x04
#define LAYER_BG4   0x08
#define LAYER_OBJ   0x10
#define RGB(r, g, b)
 Create RGB color value.
#define RGB24(r, g, b)
 Convert 24-bit RGB to SNES format.
#define SCREEN_HEIGHT   224
 Visible screen height in pixels (NTSC, 224 lines).
#define SCREEN_WIDTH   256
 Visible screen width in pixels.
#define setColor(index, color)
 Set a single CGRAM color.
#define setMainScreen(layers)
 Enable layers on the main screen.
#define setSubScreen(layers)
 Enable layers on the sub screen.

Functions

void setMode (u8 mode, u8 flags)
 Set background mode.
void videoSetInterlace (u8 on)
 Enable/disable screen interlace (SETINI bit 0).
void videoSetObjInterlace (u8 on)
 Enable/disable sprite interlace (SETINI bit 1).
void videoSetOverscan (u8 on)
 Enable/disable overscan (SETINI bit 2).
void videoSetPseudoHires (u8 on)
 Enable/disable pseudo-hires (SETINI bit 3).

Detailed Description

SNES Video / PPU Functions.

Low-level video functions for PPU control, palette management, and screen modes.

Author
OpenSNES Team

Macro Definition Documentation

◆ RGB

#define RGB ( r,
g,
b )
Value:
((((b) & 31) << 10) | (((g) & 31) << 5) | ((r) & 31))
static u16 b
Definition main.c:157

Create RGB color value.

Parameters
rRed (0-31; masked to 5 bits)
gGreen (0-31; masked to 5 bits)
bBlue (0-31; masked to 5 bits)
Returns
15-bit BGR color
Note
Operands are masked to 5 bits so an out-of-range component can't bleed into the neighbouring channel. With constant arguments (every call site in the SDK) the masks fold away at compile time — zero cost.

◆ RGB24

#define RGB24 ( r,
g,
b )
Value:
RGB((r) >> 3, (g) >> 3, (b) >> 3)
#define RGB(r, g, b)
Create RGB color value.
Definition video.h:87

Convert 24-bit RGB to SNES format.

Parameters
rRed (0-255)
gGreen (0-255)
bBlue (0-255)
Returns
15-bit BGR color

◆ SCREEN_HEIGHT

#define SCREEN_HEIGHT   224

Visible screen height in pixels (NTSC, 224 lines).

◆ SCREEN_WIDTH

#define SCREEN_WIDTH   256

Visible screen width in pixels.

◆ setColor

#define setColor ( index,
color )
Value:
do { \
REG_CGADD = (u8)(index); \
REG_CGDATA = (u8)((color) & 0xFF); \
REG_CGDATA = (u8)(((color) >> 8) & 0xFF); \
} while(0)
static u16 color
Definition main.c:163
#define REG_CGADD
CGRAM address (W).
Definition registers.h:154
#define REG_CGDATA
CGRAM data write (W).
Definition registers.h:157
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47

Set a single CGRAM color.

Writes a 15-bit BGR color to the specified palette index. Works during VBlank or force blank only.

Parameters
indexColor index (0-255)
color15-bit BGR color (use RGB() or RGB24() macros)
setColor(0, RGB(0, 0, 0)); // Color 0 = black
setColor(1, RGB(31, 31, 31)); // Color 1 = white
setColor(128, RGB(31, 0, 0)); // Sprite palette 0, color 0 = red
#define setColor(index, color)
Set a single CGRAM color.
Definition video.h:167

◆ setMainScreen

#define setMainScreen ( layers)
Value:
(REG_TM = (u8)(layers))
#define REG_TM
Main screen designation (W).
Definition registers.h:187

Enable layers on the main screen.

Sets which layers are visible on the main screen (REG_TM $212C). Forgetting to call this (or set REG_TM directly) is the #1 cause of blank screens after consoleInit().

Parameters
layersOR'd combination of LAYER_BG1..LAYER_OBJ
setMainScreen(LAYER_BG1 | LAYER_OBJ); // Show BG1 + sprites
setMainScreen(LAYER_BG1 | LAYER_BG2); // Show BG1 + BG2
#define LAYER_BG1
Definition video.h:107
#define LAYER_BG2
Definition video.h:108
#define LAYER_OBJ
Definition video.h:111
#define setMainScreen(layers)
Enable layers on the main screen.
Definition video.h:132

◆ setSubScreen

#define setSubScreen ( layers)
Value:
(REG_TS = (u8)(layers))
#define REG_TS
Sub screen designation (W).
Definition registers.h:190

Enable layers on the sub screen.

Sets which layers are visible on the sub screen (REG_TS $212D). The sub screen is used as the second operand for color math.

Parameters
layersOR'd combination of LAYER_BG1..LAYER_OBJ
setSubScreen(LAYER_BG2); // BG2 as color math source
#define setSubScreen(layers)
Enable layers on the sub screen.
Definition video.h:146

Function Documentation

◆ setMode()

void setMode ( u8 mode,
u8 flags )

Set background mode.

Parameters
modeBackground mode (BG_MODE0-BG_MODE7), optionally OR'd with priority flags

Mode overview:

  • 0: 4 BG layers, 4 colors each
  • 1: 2 BG 16-color, 1 BG 4-color (most common)
  • 7: Mode 7 rotation/scaling
Note
consoleInit() sets BG1 tilemap at VRAM $0400 and tile data at $0000. Use bgSetMapPtr() and bgSetGfxPtr() to customize after setMode().
// Mode 1 with BG3 having high priority (for HUD overlay)
#define BG_MODE1
Definition video.h:35
#define BG3_MODE1_PRIORITY_HIGH
Definition video.h:48
void setMode(u8 mode, u8 flags)
Set background mode.

◆ videoSetInterlace()

void videoSetInterlace ( u8 on)

Enable/disable screen interlace (SETINI bit 0).

With BG Mode 5/6 this doubles the vertical resolution to 448 visible lines (the PPU alternates odd/even fields). Modes 5/6 also render 512 pixels horizontally and use 16x8 tiles.

Warning
Hi-res/interlace content displays through BOTH screens: enable the layer on the MAIN and SUB screen (setMainScreen(LAYER_BG1); setSubScreen(LAYER_BG1);) or the odd columns/fields stay blank — the classic Mode 5 trap.
Parameters
on1 to enable, 0 to disable

◆ videoSetObjInterlace()

void videoSetObjInterlace ( u8 on)

Enable/disable sprite interlace (SETINI bit 1).

In interlace mode, sprites are halved vertically and rendered per field — doubles apparent sprite vertical resolution.

Parameters
on1 to enable, 0 to disable

◆ videoSetOverscan()

void videoSetOverscan ( u8 on)

Enable/disable overscan (SETINI bit 2).

Extends the visible frame from 224 to 239 lines (PAL-style height on NTSC). VBlank starts at line 240 instead of 225 — the VBlank budget for DMA shrinks by ~15 lines.

Warning
The SDK's NMI handler assumes the standard 224-line frame for its DMA budget guidance; with overscan on, heavy per-frame VRAM transfers may no longer fit VBlank. The lib does not adjust anything automatically.
Parameters
on1 to enable, 0 to disable

◆ videoSetPseudoHires()

void videoSetPseudoHires ( u8 on)

Enable/disable pseudo-hires (SETINI bit 3).

Renders 512 horizontal pixels in ANY BG mode by alternating main and sub screen pixels — the poor man's Mode 5 (used for cross-fade blend effects). Configure the sub screen layers before enabling.

Parameters
on1 to enable, 0 to disable