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 (2 colors, 4 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

  1. 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)
  • VRAM tilemap structure: 32x32 entries, each a 16-bit word (tile index + attributes)
  • CGRAM palette: two 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!" rendered in white text on a dark blue background
  • 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:
= {
0x00, 0x28,
0xFF, 0x7F,
}

Background palette in BGR555 format (2 colors, 4 bytes total).

SNES CGRAM stores colors as 15-bit BGR555: bbbbbgggggrrrrr in two bytes (little-endian). Color 0 is the background fill, color 1 is the text. Only 2 of the 4 available Mode 0 palette entries are 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. Bitplane 0 selects palette color 1, bitplane 1 selects color 2, and both together select color 3. All bitplane-1 bytes are zero here, so every lit pixel maps to palette color 1 (white).

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).