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

Fundamentals — how a glyph is drawn: hand-coded 2bpp font tiles. More...

#include <snes.h>

Functions

int main (void)
 Entry point – set up PPU, load font, build tilemap, display text.

Variables

static const u8 bg_palette []
 Background palette in BGR555 format (3 colors, 6 bytes total).
static const u8 font_tiles []
 Hand-coded 2bpp bitmap font tiles (9 glyphs, 16 bytes each).
static const u8 message []
 "HELLO WORLD!" encoded as tile indices into font_tiles[]

Detailed Description

Fundamentals — how a glyph is drawn: hand-coded 2bpp font tiles.

The under-the-hood companion to the Text family. Instead of calling the text module, it writes a hand-coded 2bpp bitmap font straight into VRAM, builds a tilemap row to spell "HELLO WORLD!", and shows it on BG1 in Mode 0, in the city's colours: cyan letters with a pink shadow on a terminal's glass. Every tile is raw bitplane data in a C array — no assets, no font library — so you see exactly how the SNES PPU turns bytes into pixels. Read examples/text/print_string first for the easy way; read this to understand what that module hides.

SNES Concepts
  • 2bpp tile format: each 8x8 tile is 16 bytes (two bitplanes interleaved); plane 0 draws colour 1, plane 1 colour 2
  • VRAM tilemap structure: 32x32 entries, each a 16-bit word (tile index + attributes)
  • CGRAM palette: three BGR555 colors loaded at palette index 0
  • Mode 0: four BG layers, each limited to 4 colors (2bpp)
  • DMA transfers for tile data and palette via dmaCopyVram / dmaCopyCGram
What to Observe
  • "HELLO WORLD!" in cyan, each letter with a pink shadow one pixel right and down, on the dark glass of a terminal
  • The text appears centered at row 14, column 10 of the 32x32 tilemap
  • The screen is otherwise blank (tilemap filled with tile 0, the space glyph)
Modules Used
console, sprite, dma, background
See also
background.h, dma.h, video.h

Function Documentation

◆ main()

int main ( void )

Entry point – set up PPU, load font, build tilemap, display text.

Initializes the SNES hardware via consoleInit(), loads a hand-coded 2bpp font into VRAM, fills the 32x32 tilemap with blank tiles, writes the "HELLO WORLD!" message at a specific tilemap row/column, and enters an infinite VBlank loop. All VRAM writes happen during forced blank (screen off) to satisfy the PPU timing constraint.

Returns
Never returns (infinite loop).

< VRAM word address for tilemap cursor position

< Current tile index read from message[]

< Loop counter for tilemap fill and message write

Variable Documentation

◆ bg_palette

const u8 bg_palette[]
static
Initial value:
= {
0x61, 0x10,
0x49, 0x73,
0xB4, 0x30,
}

Background palette in BGR555 format (3 colors, 6 bytes total).

SNES CGRAM stores colors as 15-bit BGR555: 0bbbbbgggggrrrrr in two bytes (little-endian). Color 0 is the background fill (a terminal's glass), color 1 the letters, color 2 their shadow: the neon of the Rue basse. Color 3 of the 4 Mode 0 palette entries is not used.

◆ font_tiles

const u8 font_tiles[]
static

Hand-coded 2bpp bitmap font tiles (9 glyphs, 16 bytes each).

Each 8x8 tile occupies 16 bytes in SNES 2bpp format: two interleaved bitplanes per pixel row, plane 0 first. A pixel's colour is its plane 1 bit times two plus its plane 0 bit. Plane 0 holds the letter, the glyph of the city's font (the text module's, lib/source/text.asm): colour 1, cyan. Plane 1 holds its shadow, the letter moved one pixel right and one down, where the letter is not: colour 2, pink. No pixel has both bits, so colour 3 is never drawn.

Tile index map: 0=Space, 1=H, 2=E, 3=L, 4=O, 5=W, 6=R, 7=D, 8=!

◆ message

const u8 message[]
static
Initial value:
= {
1, 2, 3, 3, 4,
0,
5, 4, 6, 3, 7,
8,
0xFF
}

"HELLO WORLD!" encoded as tile indices into font_tiles[]

Each byte is an index into the font tile array. The string is terminated by 0xFF, which acts as an end-of-message sentinel (not a valid tile index).