OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Breakout

A fully playable Breakout clone — ball, paddle, bricks, levels, score, and shadows. This is the "put it all together" example. If you can follow this one, you can make a game. Dressed as Neon Breaker, the arcade cabinet of a bar in Daveo, the city of the examples.

Controls

Button Action
D-Pad Left/Right Move paddle
A (hold) Move faster
Start Begin; pause during play; after GAME OVER, a new game
Any button Continue after losing a ball

What You'll Learn

  • How to pack tilemaps in VRAM when space is tight (yes, they can overlap on purpose)
  • The "RAM buffer" pattern: copy ROM to RAM so you can modify tilemaps at runtime
  • Why bricks should be background tiles, not sprites (and how to destroy them live)
  • The drop shadow trick that makes your sprites look 10x better with zero effort
  • Atomic DMA: what happens when you don't update overlapping regions together (spoiler: pink flash)
  • Grid-based collision without testing 100 bricks individually

Build & Run

cd $OPENSNES_HOME
make -C examples/games/breakout

Then open breakout.sfc in luna (or any SNES emulator).

Walkthrough

1. Lights Off, Load Everything

The very first thing the game does is kill the display:

setScreenOff(); // Force blank — screen goes black, VRAM is ours
dmaCopyVram(tiles_tiles, 0x1000, tiles_tiles_end - tiles_tiles); // 4096 bytes: BG1 and sprite tiles
dmaCopyVram(back_tiles, 0x2000, back_tiles_end - back_tiles); // BG3's 2bpp tiles
void setScreenOff(void)
Disable screen display (blank).
void dmaCopyVram(const u8 *source, u16 vramAddr, u16 size)
Copy data to VRAM (PVSnesLib compatible).

Why force blank? Outside of VBlank, the PPU owns the VRAM bus for rendering. Writes during active display are silently ignored — not queued, not buffered, just gone. Setting bit 7 of INIDISP shuts off the renderer entirely. Now you can DMA as much as you want without racing the clock.

2. The VRAM Map — A Deliberate Overlap

Here's where it gets interesting. This game's VRAM layout has an intentional overlap:

$0000─$07FF BG1 tilemap (playfield, HUD, bricks)
$0400─$0BFF BG3 tilemap (background pattern) ← overlaps BG1!
$1000─$17FF BG1 tiles (cabinet, bricks, font) — and the sprites' (ball, paddle)
$2000─$2067 BG3 tiles (the circuit board)

BG1's tilemap is 32x32 entries but the game only uses the top 16 rows. Rows 16-31 (addresses $0400-$07FF) sit there unused — so we slide BG3's tilemap right into that gap. Free 1 KB of VRAM, no catch.

Well... one catch:

The pink flash incident. During the port, level transitions showed a single frame of garbled colors. The culprit? BG1 and BG3 tilemaps were uploaded in separate VBlanks. The frame between them had BG3 half-overwritten by BG1. Fix: both transfers in the same VBlank, always.

dmaCopyVram((u8 *)blockmap, 0x0000, 0x800); // BG1: 2 KB
dmaCopyVram((u8 *)backmap, 0x0400, 0x800); // BG3: 2 KB
void WaitForVBlank(void)
Wait for next VBlank period.
FAR u16 blockmap[]
BG1 tilemap RAM copy (0x400 entries = 2KB), far RAM.
FAR u16 backmap[]
BG3 tilemap RAM copy (0x400 entries = 2KB), far RAM.
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47

That's ~4 KB in one VBlank. Tight, but PVSnesLib routinely does 4.5 KB+. It works.

3. ROM is Read-Only (The RAM Buffer Pattern)

Bricks get destroyed. Score changes. Colors cycle each level. You can't modify ROM, so you copy everything to RAM and work on the copies:

extern FAR u16 blockmap[]; // Writable copy of BG1 tilemap — 2 KB
extern FAR u16 backmap[]; // Writable copy of BG3 tilemap — 2 KB
extern FAR u16 pal[]; // Writable copy of the palette — 512 bytes
mycopy((u8 FAR *)blockmap, bg1map, 0x800);
mycopy((u8 FAR *)backmap, bg2map, 0x800);
mycopy((u8 FAR *)pal, tiles_pal, 0x200);
static const u16 pal[]
Definition main.c:46
static void mycopy(u8 FAR *dest, const u8 *src, u16 len)
Simple byte-by-byte memory copy.
Definition main.c:249
u8 bg2map[]
BG3 tilemap: the circuit board under the playfield (2KB, data.asm).
u8 bg1map[]
BG1 tilemap: the cabinet and the HUD labels (2KB, data.asm).
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
#define FAR
Place a global in bank $7E WRAM, outside the 8 KB bank-0 band.
Definition types.h:130

Why FAR? They're big — 4.6 KB with the brick state. Plain C RAM lives in the first 8 KB of bank $00, which they would crowd; FAR puts them in bank $7E above it, and every access carries its bank (docs/tutorials/far_ram.md). data.asm reserves them in a RAMSECTION of bank $7E.

4. Bricks Are Not Sprites

The SNES gives you 128 sprites. A full brick wall has 100 bricks. You could use sprites, but you'd blow most of your budget on rectangles that don't even move.

The much better approach: bricks are background tiles. Each brick is 2 tiles wide (16 pixels), and the palette number encodes its color:

// Tilemap entry: bits 0-9 = tile number, bits 10-12 = palette
blockmap[offset] = 13 + (color << 10); // Left half
blockmap[offset + 1] = 14 + (color << 10); // Right half
static u16 color
Definition main.c:151

Destroying a brick? Write zeroes (transparent tile), DMA the tilemap, done:

blockmap[0x42 + idx] = 0;
blockmap[0x43 + idx] = 0;

One frame later the brick is gone. No sprite management, no flicker, no limits.

5. Grid Collision (Don't Loop Over 100 Bricks)

Instead of checking every brick against the ball, convert the ball's pixel position to grid coordinates and look up the single cell it's in:

bx = (pos_x - 14) >> 4; // ÷16 (brick width)
by = (pos_y - 14) >> 3; // ÷8 (brick height)
b = bx + (by << 3) + (by << 1) - 10; // index in 10×10 array
static s16 pos_x
Definition main.c:175
static u16 b
Definition main.c:145
static s16 pos_y
Definition main.c:176
static u16 bx
Definition main.c:147
static u16 by
Definition main.c:147

65816 trick: by * 10 is written as (by << 3) + (by << 1) — that's by*8 + by*2. A shift is 2 cycles. A software multiply is ~50. On this CPU, you learn to love bit shifts real fast.

If blocks[b] != 8, there's a brick there. One array lookup instead of 100 comparisons.

6. Paddle Bounce Physics

Where the ball hits the paddle determines the bounce angle. Four zones, 7 pixels each:

k = (pos_x - px) / 7;
switch (k) {
case 0: vel_x = -2; vel_y = -1; break; // Left edge — sharp angle
case 1: vel_x = -1; vel_y = -2; break; // Left-center — shallow
case 2: vel_x = 1; vel_y = -2; break; // Right-center — shallow
default: vel_x = 2; vel_y = -1; break; // Right edge — sharp angle
}
static u8 px
Definition main.c:104
static s16 vel_y
Definition main.c:174
static u8 k
Definition main.c:144
static s16 vel_x
Ball X velocity (-2 to +2 pixels/frame).
Definition main.c:173

This is what makes Breakout feel like Breakout. Without it, the ball just bounces at the same angle every time and the game plays itself.

7. Drop Shadows (Free Depth)

Every visible sprite has a shadow twin: same shape, darker palette, offset a few pixels down-right, drawn at a lower priority. Sprites are positioned via direct oamMemory[] writes — the escape hatch next to oamSet(), shown here on purpose (either is fine at 10 sprites):

// Ball — priority 3 (front), tile 20, attr 0x31
oamMemory[6] = 20;
oamMemory[7] = 0x31;
// Ball shadow — priority 1 (behind), tile 21, attr 0x11, offset +3/+3
oamMemory[24] = (u8)(pos_x + 3);
oamMemory[25] = (u8)(pos_y + 3);
oamMemory[26] = 21;
oamMemory[27] = 0x11;
u8 oamMemory[]
Hardware OAM buffer (544 bytes at $7E:0300).

It costs 5 extra sprites (one per visible piece), but the result looks surprisingly polished for something so simple.

What's tile | 256? With OBJSEL's name base at $0000 and a gap of 0, the sprites' second name table starts at word $1000 — exactly where BG1's tiles are. So the paddle and the ball are tiles 15-21 of the same sheet as the bricks, and bit 8 of the tile number (the attribute's low bit) picks that table. Without it, the PPU looks for tiles at $0000 — which is your tilemap. You'll get garbage sprites and spend an hour debugging before you remember this bit.

8. Color Cycling Between Levels

Each new level swaps CGRAM colors 16-23 — BG3's palette 4 (the board) and 5 (its shadow) — with one of 7 pre-made color sets:

if (color < 6) color++;
else color = 0;
mycopy((u8 FAR *)pal + 32, backpal + color * 16, 0x10);
dmaCopyCGram((u8 *)pal, 0, 256 * 2);
void dmaCopyCGram(const u8 *source, u16 startColor, u16 size)
Copy palette data to CGRAM (PVSnesLib compatible).
u8 backpal[]
7 level colour sets for BG3 (7 x 16 bytes = 112 bytes, data.asm)

Same tiles, same tilemap, completely different look. This is why the SNES palette system is so powerful — recoloring is essentially free.

9. The Game Loop

One iteration per frame, clean and predictable:

static void run_frame(void) {
pad0 = pad_keys[0]; // Read joypad (filled by NMI handler every frame)
if (pos_y > 195)
check_paddle(); // Bottom zone — paddle collision
else
check_bricks(); // Upper zone — brick collision
draw_screen(); // Update sprite positions
oamUpdate(); // Push OAM buffer to hardware
}
static void move_ball(void)
Update ball position and handle wall collision.
Definition main.c:636
static void run_frame(void)
Run one frame of gameplay.
Definition main.c:792
static void check_bricks(void)
Check collision with bricks.
Definition main.c:765
static void move_paddle(void)
Handle paddle movement from joypad input.
Definition main.c:616
static void check_paddle(void)
Check collision with paddle.
Definition main.c:674
static void handle_pause(void)
Handle pause functionality.
Definition main.c:594
static void draw_screen(void)
Update all sprite positions.
Definition main.c:294
u16 pad_keys[5]
u16 pad0
Definition kit.c:31
void oamUpdate(void)
Copy OAM buffer to hardware.

Note that we read pad_keys[0] directly — not through padPressed(). This is the raw buffer that the NMI handler fills every frame. Sometimes the simple way is the right way.


Tips & Tricks

  • Getting a pink flash on level change? Your overlapping tilemap DMAs are split across two VBlanks. Put them both after the same WaitForVBlank() call.
  • Why mycopy() instead of memcpy()? Either works: mycopy() takes a const (far) source and a FAR destination, and memcpy() of the string module follows the 24-bit pointer on both sides too. The example keeps its three-line loop so that it links one module less.
  • Why are loop variables global? static u8 i, j, k; at file scope reduces stack pressure. On the 65816, stack-relative addressing is slower than direct/zero-page access. In a game loop running 60 times per second, this matters.

Go Further

  • Add power-ups: When a brick breaks, spawn a falling sprite. Test collision with the paddle to activate bonuses (wide paddle, slow ball, multiball).
  • Add sound: Play a BRR sample on each bounce. Check out SNESMOD music and effects for the sound effect pattern.
  • Design your own levels: Edit brick_map in data.asm. Values 0-7 are colors, 8 is empty. The grid is 10 columns x 10 rows — go wild.
  • Next example: rooftop_run — scrolling camera, platformer physics, and tile streaming.

Under the Hood: The Build

The Makefile

TARGET := breakout.sfc
CSRC := main.c
ASMSRC := data.asm
USE_LIB := 1
LIB_MODULES := console sprite dma background input

Why ASMSRC for a Game?

data.asm serves two purposes in Breakout:

1. ROM data the converters do not make — the two tilemaps and the level colours are binaries, included via .INCBIN in an asset section (the linker places it in any bank but $00):

ASSET_SECTION ".rodata1"
bg1map:
.incbin "res/bg1map.bin"
bg1map_end:
.ENDS

2. The RAM buffers — the writable tilemap copies, the palette and the brick state (4.6 KB) in a RAMSECTION of bank $7E. C declares them FAR, so they sit above the 8 KB plain C band and every access carries its bank (docs/tutorials/far_ram.md).

The Assets

res/tiles.png and res/back.png are converted by the build, from the settings file beside each:

# res/tiles.png.toml
tool = "opensnes-tileset"
[convert]
bpp = 4
colors = 256
no-reduce = true

no-reduce keeps every tile where it is in the sheet: the game addresses its tiles by number (a brick is tiles 13-14, the font is ASCII − 10, the sprites are 15-21). The sheet's PNG palette is the whole CGRAM image, written as tiles.pal: one BG palette per brick colour (colours 13-15), the font in 10-12, the sprites' palette at 128. BG3's sheet takes bpp = 2 and no palette, since its colours come from backpal.bin.

All of res/ — the two sheets, the two maps and the level colours — is drawn by devtools/art_assets/breakout.py (make art-assets), in the game's tile numbering.

Why These Modules?

Module Why it's here
console PPU init, NMI handler, WaitForVBlank()
sprite OAM buffer for ball, paddle, and drop shadows (10 sprites total)
dma dmaCopyVram() for tilemap uploads, dmaCopyCGram() for palette, OAM DMA
background bgSetMapPtr() for configuring BG1/BG3 tilemap addresses
input Joypad buffers — though Breakout reads pad_keys[0] directly

Modules Used

console, sprite, dma, background, input (LIB_MODULES in the Makefile).

Technical Reference

Register Address Role in this example
INIDISP $2100 Force blank during initial load
BGMODE $2105 Mode 1 (BG1 4bpp, BG3 2bpp)
BG1SC $2107 BG1 tilemap at $0000
BG3SC $2109 BG3 tilemap at $0400
BG12NBA $210B BG1 tile data at $1000
BG34NBA $210C BG3 tile data at $2000
TM $212C Enable OBJ + BG3 + BG1 (0x15)
OBJSEL $2101 8x8 sprites, name base $0000, second table at word $1000 (BG1's tiles)

Files

File What's in it
main.c All game logic
data.asm The far RAM buffers, string constants, the map and colour binaries
res/tiles.png BG1 and sprite tiles (cabinet, bricks, paddle, ball, shadows, font, lives icon); its palette is the whole CGRAM. All of it is drawn by devtools/art_assets/breakout.py
res/back.png BG3's circuit board (2bpp)
res/bg1map.bin The cabinet and the HUD labels
res/bg2map.bin The board under the playfield
res/backpal.bin 7 color sets for level cycling
Makefile LIB_MODULES := console sprite dma background input

Credits

  • Original game: Ulrich Hecht (SNES SDK)
  • PVSnesLib port: alekmaul
  • OpenSNES port: OpenSNES team
  • Art (Neon Breaker): drawn by devtools/art_assets/breakout.py, CC0, with the library's own font