Part 2: Backgrounds, dynamic sprites, and sound
This is Part 2 of the build-it-yourself Breakout tutorial. It starts from the game you finished in Part 1 and changes it one feature at a time. Build and run each checkpoint before moving on.
Try the completed Part 2 game!
Download the Part 2 ROMBy the end of this part your game will:
- paint a starfield backdrop on a second background layer;
- draw the walls, HUD frame, and bricks as background tiles instead of
#characters and sprites, erasing a brick's tiles when it breaks; - use PVSnesLib's dynamic sprite engine for the paddle and ball, with a short glimmer animation when they hit something;
- play music and sound effects through the SDK's sound driver.

Before you start
Part 2 builds on Part 1. Start with a finished, working Part 1 project.
Get the new artwork
Download these four files into your breakout project folder, next to sprites.png:
| File | Size | Contents |
|---|---|---|
| backdrop.png | 256 x 256 | Four-color diamond pattern with a few stars |
| playfield.png | 256 x 256 | HUD frame, side walls, and a gapless 14 x 5 wall of bricks |
| paddle.png | 128 x 48 | Left, middle, and right paddle pieces, each with 6 animation frames |
| ball.png | 128 x 8 | Sixteen 8 x 8 ball frames (6 used) |
Keep sprites.png for now; section 4 stops using it.
Check the audio files
The music and sound effects come from the SDK's own sound example, so they are not included in this repository. Check that these two files exist:
<SDK root>\snes-examples\audio\effectsandmusic\res\effectssfx.it
<SDK root>\snes-examples\audio\effectsandmusic\res\pollen8.itYou do not need to copy them. In section 6, your Makefile copies them into your project automatically.
Contents
- Paint a backdrop on background 3
- Draw the walls, HUD, and bricks on background 2
- Erase and restore bricks in the tile map
- Switch the paddle and ball to dynamic sprites
- Make the paddle and ball glimmer
- Play music
- Add sound effects
See also troubleshooting and official references.
1. Paint a backdrop on background 3
How the Mode 1 layers fit together
Part 1 called setMode(BG_MODE1, 0) but used only background 1 (BG1) for text and disabled the other two layers. Mode 1 gives you three background layers:
| Layer | Colors per tile | Front-to-back order |
|---|---|---|
| BG1 | 16 (4 bits per pixel) | In front |
| BG2 | 16 (4 bits per pixel) | Middle |
| BG3 | 4 (2 bits per pixel) | Behind |
Sprites with priority 3, like ours, appear in front of all three. In PVSnesLib, the layers are numbered from zero: 0 is BG1, 1 is BG2, and 2 is BG3.
A background is made of two pieces of data in video memory (VRAM):
- tiles (character data): 8 x 8 pixel images, with no position;
- a map: a 32 x 32 grid of 16-bit entries, each choosing a tile number and a palette for one 8 x 8 cell of the screen.
By the end of this part, VRAM will hold this data:
| Data | VRAM address (16-bit words) |
|---|---|
| 16 x 16 sprite frames | 0x0000 |
| 8 x 8 sprite frames | 0x1000 |
| Font tiles (BG1) | 0x3000 |
| Playfield tiles (BG2) | 0x4000 |
| Backdrop tiles (BG3) | 0x5000 |
| Text map (BG1) | 0x6800 |
| Playfield map (BG2) | 0x6C00 |
| Backdrop map (BG3) | 0x7000 |
Tile addresses must be multiples of 0x1000. Each 32 x 32 map takes 0x400 words, so the three maps sit side by side without overlapping.
Colors live in CGRAM (Color Generator RAM), a 256-entry (512 bytes) section of memory used for storing color palettes. In Mode 1, BG3's 4-color palette 0 uses colors 0 to 3, which are also the first colors of BG1's palette 0. Part 1's font used palette 0. If it kept doing so, the backdrop and the font would overwrite each other's colors. So the font moves to palette 1 (colors 16 to 31). Our layers will use these palettes:
| User | Palette | CGRAM colors |
|---|---|---|
| Backdrop (BG3) | 0 | 0-3 |
| Font (BG1) | 1 | 16-31 |
| Playfield (BG2) | 2 | 32-47 |
| Sprites | sprite palette 0 | 128-143 |
Convert the backdrop
Open Makefile
Add a conversion rule for the backdrop to your Makefile, after the sprites.pic rule:
Goal: Convert the backdrop into four-color BG3 tiles and a map.
Makefile — in the file, apply these changes:
sprites.pic: sprites.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
src/data.obj: sprites.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfxsprites.pic: sprites.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
backdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<
src/data.obj: sprites.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfxThe options mean:
-s 8: cut the picture into 8 x 8 tiles;-u 4and-o 4: use four colors (2 bits per pixel) and write four palette colors;-e 0: store palette number 0 in every map entry;-pand-m: also write a palette (backdrop.pal) and a map (backdrop.map).
Unlike sprites.pic, there is no -R, so gfx4snes removes duplicate tiles. The 256 x 256 picture has 1,024 cells but only 18 different tiles. The map records which tile goes in each cell, so backdrop.pic stays tiny (288 bytes).
Open src\data.asm
Add the converted files to the end of src\data.asm:
Goal: Store the backdrop tiles, map, and palette in the ROM.
src\data.asm — in the file, apply these changes:
sprite_palette:
.incbin "sprites.pal"
.endssprite_palette:
.incbin "sprites.pal"
backdrop_tiles:
.incbin "backdrop.pic"
backdrop_tiles_end:
backdrop_map:
.incbin "backdrop.map"
backdrop_palette:
.incbin "backdrop.pal"
.endsThe backdrop_tiles_end label marks the end of the tile data. C can subtract the two addresses to get the size, so you do not have to hard-code a number that changes whenever the artwork does.
Load the backdrop
Open src\main.c
In src\main.c, name the backdrop map address and declare the new labels:
Goal: Make the backdrop assets and map address available to video initialization.
src\main.c — in file-scope declarations before draw_bricks, apply these changes:
#include <snes.h>
#include "game.h"
extern u8 sprite_tiles[];
extern u8 sprite_palette[];
Game game;#include <snes.h>
#include "game.h"
#define BACKDROP_MAP 0x7000
extern u8 sprite_tiles[];
extern u8 sprite_palette[];
extern u8 backdrop_tiles[], backdrop_tiles_end[];
extern u8 backdrop_map[];
extern u8 backdrop_palette[];
Game game;The declarations refer to assembly labels; BACKDROP_MAP reserves space apart from the text map.
Then, in init_video, move the font to palette 1, load the backdrop, and stop disabling BG3:
Goal: Keep the font palette from overwriting BG3's colors.
src\main.c — in init_video, apply these changes:
int i;
setScreenOff();
consoleInitDefaultText(0);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
oamInitGfxSet(sprite_tiles, 1024, sprite_palette, 32, 0, 0x0000, int i;
setScreenOff();
consoleInitDefaultText(1);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
oamInitGfxSet(sprite_tiles, 1024, sprite_palette, 32, 0, 0x0000,Font palette 1 starts at CGRAM color 16, leaving colors 0–3 for BG3.
Goal: Upload the backdrop's tiles, colors, and map while the screen is off.
src\main.c — in init_video, apply these changes:
consoleInitDefaultText(1);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
oamInitGfxSet(sprite_tiles, 1024, sprite_palette, 32, 0, 0x0000,
OBJ_SIZE8_L16); consoleInitDefaultText(1);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
bgInitTileSet(2, backdrop_tiles, backdrop_palette, 0,
backdrop_tiles_end - backdrop_tiles, 4 * 2, BG_4COLORS,
0x5000);
bgInitMapSet(2, backdrop_map, 32 * 32 * 2, SC_32x32, BACKDROP_MAP);
oamInitGfxSet(sprite_tiles, 1024, sprite_palette, 32, 0, 0x0000,
OBJ_SIZE8_L16);The initialization calls load BG3's four-color tiles at 0x5000 and its map at BACKDROP_MAP.
Goal: Allow BG3 to appear behind the existing text and sprites.
src\main.c — in init_video, apply these changes:
setMode(BG_MODE1, 0);
bgSetDisable(1);
bgSetDisable(2);
oamClear(0, 0);
for (i = 0; i < BRICK_COUNT; ++i)
{
setMode(BG_MODE1, 0);
bgSetDisable(1);
oamClear(0, 0);
for (i = 0; i < BRICK_COUNT; ++i)
{bgInitTileSet copies the tiles to VRAM 0x5000 and the palette to CGRAM. Its arguments are:
- the layer (
2, which is BG3); - the tiles;
- the palette;
- the palette number (
0); - the tile data size;
- the palette size in bytes (4 colors x 2 bytes each);
- the color mode;
- the VRAM address.
bgInitMapSet copies the 2,048-byte map (32 x 32 entries x 2 bytes each) to BACKDROP_MAP and points BG3 at it. Both functions write to VRAM, which is safe here because the screen is off.
Checkpoint: Build and run. A dark blue diamond pattern with a few stars now fills the screen behind everything else. The text is still white and readable, and the # walls, bricks, paddle, and ball look and play exactly as before.

2. Draw the walls, HUD, and bricks on background 2
Why move bricks to a background
In Part 1, every brick was a 16 x 16 sprite. The SNES can show 128 sprites in total and only 32 on one scanline, so a wall of sprites cannot grow much. A background has neither limit: every 8 x 8 cell of the screen is a map entry. A 16 x 8 brick is just two map entries, and erasing it means writing two "empty" entries.
playfield.png is a full 256 x 224 screen (plus unused rows) drawn on an 8 x 8 grid:
- a HUD frame on tile rows 0 to 3, around the score and lives text;
- side walls on tile columns 1 and 30;
- 70 bricks in 5 rows of 14, with no gaps, starting at pixel (16, 48).
Gameplay already treats pixels 16 to 239 as the field, and 14 bricks x 16 pixels fills exactly that width.
Convert the playfield
Open Makefile
Add another rule to the Makefile:
Goal: Convert the playfield into sixteen-color BG2 tiles and a map.
Makefile — in the file, apply these changes:
sprites.pic: sprites.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
backdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<sprites.pic: sprites.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
playfield.pic: playfield.png
$(GFXCONV) -s 8 -o 16 -u 16 -e 2 -p -m -t png -i $<
backdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<The converter stores palette 2 in map entries and removes duplicate tiles.
Goal: Generate the playfield data before its assembly object is built.
Makefile — in the file, apply these changes:
backdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<
src/data.obj: sprites.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfxbackdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<
src/data.obj: sprites.pic playfield.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfxOpen src\data.asm
This time the tiles use 16 colors, and -e 2 stores palette number 2 in each map entry. Add the labels to the end of src\data.asm:
Goal: Include the playfield tiles, map, and palette in the ROM.
src\data.asm — in the file, apply these changes:
backdrop_palette:
.incbin "backdrop.pal"
.endsbackdrop_palette:
.incbin "backdrop.pal"
playfield_tiles:
.incbin "playfield.pic"
playfield_tiles_end:
playfield_map:
.incbin "playfield.map"
playfield_palette:
.incbin "playfield.pal"
.endsThe labels expose each binary asset to C; the end label supplies the tile-data size.
Close the gaps in the brick grid
Open src\game.h
Change the brick layout in src\game.h. The pitch macros go away, because bricks now touch: the distance from one brick to the next is just its width or height.
Goal: Align collision geometry with the artwork's gapless fourteen-column wall.
src\game.h — in the file, apply these changes:
#define PADDLE_Y 200
#define PADDLE_SPEED 3
#define BRICK_COLS 10
#define BRICK_ROWS 5
#define BRICK_COUNT (BRICK_COLS * BRICK_ROWS)
#define BRICK_X 30
#define BRICK_Y 48
#define BRICK_WIDTH 16
#define BRICK_HEIGHT 8
#define BRICK_PITCH_X 20
#define BRICK_PITCH_Y 16
enum GameState
{#define PADDLE_Y 200
#define PADDLE_SPEED 3
#define BRICK_COLS 14
#define BRICK_ROWS 5
#define BRICK_COUNT (BRICK_COLS * BRICK_ROWS)
#define BRICK_X 16
#define BRICK_Y 48
#define BRICK_WIDTH 16
#define BRICK_HEIGHT 8
enum GameState
{BRICK_X and BRICK_Y must match where the bricks are painted in playfield.png. Both are multiples of 8, so every brick lines up with the tile grid.
Find bricks with arithmetic instead of a search
Part 1's hit_brick checked every live brick in the touched rows. With 14 columns that loop gets expensive. The 65816 CPU has no instruction to multiply or divide 16-bit numbers, so the compiler turns col * BRICK_PITCH_X into a call to a slow library routine. hit_brick runs up to six times per frame (three substeps, for both X and Y), and when a frame's work takes too long, the game visibly slows down.
Open src\game.c
With no gaps, there is a shortcut: the ball (8 x 8) can only touch the bricks under its corners. Divide its position by the brick size to get at most two rows and two columns. In src\game.c, apply these focused edits to hit_brick:
Goal: Track the small row and column ranges touched by the ball.
src\game.c — in hit_brick, apply these changes:
static int hit_brick(Game *game, int x, int y)
{
int row;
int col;
int brick_x;
int brick_y;
int index;
if (y + BALL_SIZE <= BRICK_Y ||static int hit_brick(Game *game, int x, int y)
{
int first_row;
int last_row;
int first_col;
int last_col;
int row;
int col;
int index;
if (y + BALL_SIZE <= BRICK_Y ||Four range endpoints replace the individual brick coordinates used by the old scan.
Goal: Reject ball rectangles entirely outside any edge of the brick wall.
src\game.c — in hit_brick, apply these changes:
int col;
int index;
if (y + BALL_SIZE <= BRICK_Y ||
y >= BRICK_Y + (BRICK_ROWS - 1) * BRICK_PITCH_Y + BRICK_HEIGHT)
{
return 0;
} int col;
int index;
if (y + BALL_SIZE <= BRICK_Y || y >= BRICK_Y + BRICK_ROWS * BRICK_HEIGHT ||
x + BALL_SIZE <= BRICK_X || x >= BRICK_X + BRICK_COLS * BRICK_WIDTH)
{
return 0;
}The early return ensures the subsequent arithmetic only handles overlapping ranges.
Goal: Test only the one to four brick cells underneath the ball's corners.
src\game.c — in hit_brick, apply these changes:
return 0;
}
for (row = 0; row < BRICK_ROWS; ++row)
{
brick_y = BRICK_Y + row * BRICK_PITCH_Y;
if (y + BALL_SIZE <= brick_y || y >= brick_y + BRICK_HEIGHT)
{
continue;
}
for (col = 0; col < BRICK_COLS; ++col)
{
index = row * BRICK_COLS + col;
if (!game->bricks[index])
{
continue;
}
brick_x = BRICK_X + col * BRICK_PITCH_X;
if (x + BALL_SIZE > brick_x && x < brick_x + BRICK_WIDTH)
{
game->bricks[index] = 0;
game->score += 10;
--game->bricks_left;
if (game->bricks_left == 0)
{
game->state = STATE_WON;
}
return 1;
}
}
} return 0;
}
/* The grid has no gaps, so only the bricks under the ball's corners can // [!code ++]
be hit. Working them out directly is much cheaper than a full scan. */ // [!code ++]
first_row = y < BRICK_Y ? 0 : (unsigned int)(y - BRICK_Y) / BRICK_HEIGHT;
last_row = (unsigned int)(y + BALL_SIZE - 1 - BRICK_Y) / BRICK_HEIGHT;
if (last_row >= BRICK_ROWS)
{
last_row = BRICK_ROWS - 1;
}
first_col = x < BRICK_X ? 0 : (unsigned int)(x - BRICK_X) / BRICK_WIDTH;
last_col = (unsigned int)(x + BALL_SIZE - 1 - BRICK_X) / BRICK_WIDTH;
if (last_col >= BRICK_COLS)
{
last_col = BRICK_COLS - 1;
}
for (row = first_row; row <= last_row; ++row)
{
for (col = first_col; col <= last_col; ++col)
{
index = row * BRICK_COLS + col;
if (game->bricks[index])
{
game->bricks[index] = 0;
game->score += 10;
--game->bricks_left;
if (game->bricks_left == 0)
{
game->state = STATE_WON;
}
return 1;
}
}
}The early return 0 handles a ball that is entirely outside the wall. After that, at least one row and column overlap, so the clamped ranges are never empty. The (unsigned int) casts matter: dividing an unsigned value by 8 or 16 compiles to quick bit shifts, while signed division has to round negative numbers toward zero and needs the slow routine. Only one multiplication, row * BRICK_COLS, remains, and it runs only for the one to four candidate bricks. Checking rows top to bottom and columns left to right keeps the same "first hit wins" behavior as Part 1.
Show the playfield
Open src\main.c
In src\main.c, add the playfield's map address and labels. playfield_map is declared as u16, because each map entry is a 16-bit value; section 3 reads individual entries from it.
Goal: Reserve a separate VRAM map address for BG2.
src\main.c — in file-scope declarations before draw_bricks, apply these changes:
#include <snes.h>
#include "game.h"
#define BACKDROP_MAP 0x7000
extern u8 sprite_tiles[];
extern u8 sprite_palette[];#include <snes.h>
#include "game.h"
#define PLAYFIELD_MAP 0x6C00
#define BACKDROP_MAP 0x7000
extern u8 sprite_tiles[];
extern u8 sprite_palette[];The two map constants keep BG2 and BG3 from sharing the same VRAM region.
Goal: Expose the playfield assets, including individually readable map entries.
src\main.c — in file-scope declarations before draw_bricks, apply these changes:
extern u8 backdrop_tiles[], backdrop_tiles_end[];
extern u8 backdrop_map[];
extern u8 backdrop_palette[];
Game game;extern u8 backdrop_tiles[], backdrop_tiles_end[];
extern u8 backdrop_map[];
extern u8 backdrop_palette[];
extern u8 playfield_tiles[], playfield_tiles_end[];
extern u16 playfield_map[];
extern u8 playfield_palette[];
Game game;The map uses u16 entries, unlike the byte arrays used for tiles and palettes.
The bricks are now part of the background, so delete the whole draw_bricks function, from static void draw_bricks(void) to its closing brace. Leave the shown_alive and shown_bricks variables above it; section 3 uses them again. Then remove the call to it:
Goal: Stop drawing sprite bricks now that BG2 supplies their artwork.
src\main.c — remove the entire draw_bricks function shown below:
static void draw_bricks(void)
{
int index;
int id;
if (shown_bricks == game.bricks_left)
{
return;
}
for (index = 0; index < BRICK_COUNT; ++index)
{
if (shown_alive[index] == game.bricks[index])
{
continue;
}
id = (index + 4) * 4;
if (game.bricks[index])
{
oamSet(id, BRICK_X + (index % BRICK_COLS) * BRICK_PITCH_X,
BRICK_Y + (index / BRICK_COLS) * BRICK_PITCH_Y, 3, 0, 0,
(index / BRICK_COLS) * 2, 0);
oamSetEx(id, OBJ_LARGE, OBJ_SHOW);
}
else
{
oamSetVisible(id, OBJ_HIDE);
}
shown_alive[index] = game.bricks[index];
}
shown_bricks = game.bricks_left;
}
Removing this renderer releases the brick sprite slots; its state cache will be reused for tile-map updates.
Goal: Remove the call to the deleted sprite-brick renderer.
src\main.c — in draw_game, apply these changes:
for (i = 0; i < 3; ++i)
{
oamSetXY((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y);
}
draw_bricks();
draw_text();
} for (i = 0; i < 3; ++i)
{
oamSetXY((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y);
}
draw_text();
}draw_game now handles only the paddle, ball, and text.
Finally, update init_video. Load BG2, stop disabling it, and delete the old brick reset loop and the # walls:
Goal: Remove the row variable used only to draw the old text border.
src\main.c — in init_video, apply these changes:
static void init_video(void)
{
int row;
int i;
setScreenOff();static void init_video(void)
{
int i;
setScreenOff();The upcoming BG2 artwork replaces that border loop.
Goal: Upload BG2's playfield tiles, palette, and map.
src\main.c — in init_video, apply these changes:
consoleInitDefaultText(1);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
bgInitTileSet(2, backdrop_tiles, backdrop_palette, 0,
backdrop_tiles_end - backdrop_tiles, 4 * 2, BG_4COLORS,
0x5000); consoleInitDefaultText(1);
bgSetGfxPtr(0, 0x3000);
bgSetMapPtr(0, 0x6800, SC_32x32);
bgInitTileSet(1, playfield_tiles, playfield_palette, 2,
playfield_tiles_end - playfield_tiles, 16 * 2, BG_16COLORS,
0x4000);
bgInitMapSet(1, (u8 *)playfield_map, 32 * 32 * 2, SC_32x32, PLAYFIELD_MAP);
bgInitTileSet(2, backdrop_tiles, backdrop_palette, 0,
backdrop_tiles_end - backdrop_tiles, 4 * 2, BG_4COLORS,
0x5000);BG2 uses tile address 0x4000, its reserved map address, and palette 2.
Goal: Leave BG2 enabled and remove obsolete sprite-brick initialization.
src\main.c — in init_video, apply these changes:
OBJ_SIZE8_L16);
setMode(BG_MODE1, 0);
bgSetDisable(1);
oamClear(0, 0);
for (i = 0; i < BRICK_COUNT; ++i)
{
shown_alive[i] = 0;
}
oamSet(0, game.ball_x, game.ball_y, 3, 0, 0, 12, 0);
oamSetEx(0, OBJ_SMALL, OBJ_SHOW); OBJ_SIZE8_L16);
setMode(BG_MODE1, 0);
oamClear(0, 0);
oamSet(0, game.ball_x, game.ball_y, 3, 0, 0, 12, 0);
oamSetEx(0, OBJ_SMALL, OBJ_SHOW);BG2 now displays the bricks directly from its map, without per-brick OAM setup.
Goal: Replace the text-character border with the playfield's drawn walls.
src\main.c — in init_video, apply these changes:
}
consoleDrawText(2, 1, "SCORE LIVES");
consoleDrawText(1, 3, "##############################");
for (row = 4; row < 28; ++row)
{
consoleDrawText(1, row, "#");
consoleDrawText(30, row, "#");
}
draw_game();
setScreenOn();
} }
consoleDrawText(2, 1, "SCORE LIVES");
draw_game();
setScreenOn();
}The (u8 *) cast is needed because bgInitMapSet takes a byte pointer. Palette number 2 matches the -e 2 used in conversion, and 16 * 2 is the palette size in bytes.
Checkpoint: Build and run. Metal walls frame the field and the HUD, the score and lives sit inside the HUD frame, and a gapless wall of 70 bricks in five colors fills the top. The ball and paddle still use Part 1's sprites. Play for a moment: the ball bounces off bricks and the score goes up, but the bricks do not disappear yet. Section 3 fixes that.

3. Erase and restore bricks in the tile map
Change VRAM only during vertical blank
The SNES video chip reads VRAM constantly while it draws the screen. Your code may write to VRAM only during forced blank (screen off, as in init_video) or vertical blank (vblank), the short pause after each frame is drawn. WaitForVBlank() returns at the start of vblank, so VRAM writes belong right after it.
Vblank is short: only a few kilobytes can be copied by DMA in one NTSC vblank, and the library already uses some of that time. The text console copies its whole 2 KB map to VRAM in any frame where text changed. A copy that runs past the end of vblank does not crash. It silently drops data, so some bricks just stay on screen.
So the plan is:
- keep a RAM copy of the five brick rows of the map (
brick_map); - when bricks change, update that copy and flag the changed rows;
- after
WaitForVBlank(), copy at most one flagged row to VRAM.
One row is 14 bricks x 2 tiles x 2 bytes = 56 bytes, which easily fits. A restart redraws all five rows over five frames, about a twelfth of a second.
Track and upload changed rows
Open src\main.c
Add these definitions to src\main.c:
Goal: Name the blank map entry and the grid's tile-map origin.
src\main.c — in file-scope declarations before draw_text, apply these changes:
#define PLAYFIELD_MAP 0x6C00
#define BACKDROP_MAP 0x7000
extern u8 sprite_tiles[];
extern u8 sprite_palette[];
#define PLAYFIELD_MAP 0x6C00
#define BACKDROP_MAP 0x7000
#define EMPTY_TILE 0x0800
#define BRICK_TILE_X (BRICK_X / 8)
#define BRICK_TILE_Y (BRICK_Y / 8)
extern u8 sprite_tiles[];
extern u8 sprite_palette[];BRICK_TILE_X and BRICK_TILE_Y are the bricks' map column (2) and row (6). EMPTY_TILE is the map entry gfx4snes wrote for empty areas of playfield.png: tile 0, which is fully transparent, with palette 2 in bits 10 to 12 (2 << 10 = 0x0800).
Add the RAM copy and the row flags, then the two new functions, between the variables and draw_text:
Goal: Stage changed brick tiles in RAM and track rows awaiting upload.
src\main.c — in file-scope declarations before draw_text, apply these changes:
Game game;
static unsigned char shown_alive[BRICK_COUNT];
static char score_text[] = "000";
static char lives_text[] = "3";
Game game;
static u16 brick_map[BRICK_ROWS][BRICK_COLS * 2];
static unsigned char row_changed[BRICK_ROWS];
static unsigned char shown_alive[BRICK_COUNT];
static char score_text[] = "000";
static char lives_text[] = "3";Each brick occupies two u16 entries; one dirty flag covers a whole brick row.
Goal: Update the RAM map when bricks break or return.
src\main.c — add this complete prepare_bricks function immediately before draw_text:
static void prepare_bricks(void)
{
int index;
int row;
int col;
int tile;
if (shown_bricks == game.bricks_left)
{
return;
}
index = 0;
for (row = 0; row < BRICK_ROWS; ++row)
{
tile = (BRICK_TILE_Y + row) * 32 + BRICK_TILE_X;
for (col = 0; col < BRICK_COLS * 2; col += 2)
{
if (shown_alive[index] != game.bricks[index])
{
shown_alive[index] = game.bricks[index];
if (game.bricks[index])
{
brick_map[row][col] = playfield_map[tile + col];
brick_map[row][col + 1] = playfield_map[tile + col + 1];
}
else
{
brick_map[row][col] = EMPTY_TILE;
brick_map[row][col + 1] = EMPTY_TILE;
}
row_changed[row] = 1;
}
++index;
}
}
shown_bricks = game.bricks_left;
}Changed cells receive either their original ROM entries or two transparent entries. The containing row is marked for transfer.
Goal: Upload at most one changed row per VBlank.
src\main.c — add this complete upload_bricks function immediately before draw_text:
static void upload_bricks(void)
{
int row;
/* One row per frame keeps the copy inside vblank. */ // [!code ++]
for (row = 0; row < BRICK_ROWS; ++row)
{
if (row_changed[row])
{
dmaCopyVram((u8 *)brick_map[row],
PLAYFIELD_MAP + (BRICK_TILE_Y + row) * 32 +
BRICK_TILE_X,
BRICK_COLS * 4);
row_changed[row] = 0;
return;
}
}
}prepare_bricks works like Part 1's draw_bricks. It does nothing unless the brick count changed, then compares each brick with what is on screen. Here, col counts map entries, two per brick. A broken brick gets two EMPTY_TILE entries. A restored brick gets its original two entries back, read from playfield_map in ROM, so the colors always match the artwork.
dmaCopyVram(source, address, size) copies size bytes to a VRAM word address. A row's address is the map's base address, plus 32 entries per map row, plus the bricks' starting column.
Initialize everything explicitly
init_video must fill brick_map from the artwork and clear row_changed. Do not rely on C's rule that global variables start at zero: the PVSnesLib toolchain does not clear uninitialized globals and statics. On real hardware (and in accurate emulators) they start with leftover values, so a "zero" flag might start as 1. Variables with an initializer, like shown_score = -1, are set correctly.
Goal: Prepare brick-map changes alongside the other frame's drawing work.
src\main.c — in draw_game, apply these changes:
for (i = 0; i < 3; ++i)
{
oamSetXY((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y);
}
draw_text();
} for (i = 0; i < 3; ++i)
{
oamSetXY((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y);
}
prepare_bricks();
draw_text();
}Preparation modifies RAM only; upload_bricks will perform the later VRAM transfer.
Goal: Iterate through brick rows and map columns during initialization.
src\main.c — in init_video, apply these changes:
static void init_video(void)
{
int i;
setScreenOff();static void init_video(void)
{
int row;
int col;
int i;
setScreenOff();The new loop variables address rows and their two entries per brick.
Goal: Initialize the RAM map and cache to match the full wall already in VRAM.
src\main.c — in init_video, apply these changes:
oamSetEx((i + 1) * 4, OBJ_LARGE, OBJ_SHOW);
}
consoleDrawText(2, 1, "SCORE LIVES");
draw_game();
setScreenOn(); oamSetEx((i + 1) * 4, OBJ_LARGE, OBJ_SHOW);
}
for (row = 0; row < BRICK_ROWS; ++row)
{
row_changed[row] = 0;
for (col = 0; col < BRICK_COLS * 2; ++col)
{
brick_map[row][col] =
playfield_map[(BRICK_TILE_Y + row) * 32 + BRICK_TILE_X + col];
}
}
for (i = 0; i < BRICK_COUNT; ++i)
{
shown_alive[i] = 1;
}
shown_bricks = BRICK_COUNT;
consoleDrawText(2, 1, "SCORE LIVES");
draw_game();
setScreenOn();The map loaded by bgInitMapSet already shows all 70 bricks, so the startup state says exactly that: every brick shown, nothing to upload.
Finally, upload a row right after waiting for vblank in main:
Goal: Transfer pending brick changes during the next VBlank.
src\main.c — in main, apply these changes:
while (1)
{
WaitForVBlank();
held = padsCurrent(0);
pressed = padsDown(0);
movement = 0; while (1)
{
WaitForVBlank();
upload_bricks();
held = padsCurrent(0);
pressed = padsDown(0);
movement = 0;The call follows WaitForVBlank, keeping the row DMA within the permitted VRAM-write window.
Checkpoint: Build and run. Hit bricks: each one vanishes, showing the backdrop behind it, and adds ten points. Lose all three lives and restart: all 70 bricks return. Clear the wall (it takes a while) and check for YOU WIN! and a score of 700, then restart. Nothing should flicker, and no brick should be left behind.

4. Switch the paddle and ball to dynamic sprites
What dynamic sprites do
With oamInitGfxSet, Part 1 copied the whole sprite sheet into VRAM once and picked tiles by number. That works for a small sheet. But a game with many animation frames quickly runs out of sprite VRAM, which holds only 16 KB.
PVSnesLib's dynamic sprite engine keeps all frames in ROM. Each frame it copies only the frames currently on screen into VRAM. You describe each sprite in the oambuffer array: position, frame number, attributes, and a pointer to its graphics in ROM. Then each frame you:
- set positions and frames, and call
oamDynamic16Draw(id)oroamDynamic8Draw(id)for each visible sprite; - call
oamInitDynamicSpriteEndFrame()to finish the frame's sprite list; - call
WaitForVBlank(); - call
oamVramQueueUpdate()to copy the queued frames into VRAM, during vblank.
Steps 1 and 2 must happen before the vblank wait and step 4 right after it, so the game loop changes shape. All game logic now runs first, and the loop ends by waiting for vblank and then doing the VRAM copies.
Convert the new sprite sheets
The sprite sheets are laid out for the engine. Frames are numbered left to right, then top to bottom, 128 pixels per row:
paddle.pnghas one row of eight 16 x 16 frames for each paddle piece (left, middle, right). The pieces use only the top 8 pixel rows of each frame. So frame0is the left piece,8the middle, and16the right; the next five frames of each row are its glimmer animation.ball.pnghas sixteen 8 x 8 frames; frame0is the normal ball.
Open Makefile
Replace the sprites.pic rule in the Makefile:
Goal: Convert separate paddle and ball frame sheets for dynamic sprites.
Makefile — in the file, apply these changes:
all: $(ROMNAME).sfc
sprites.pic: sprites.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
playfield.pic: playfield.png
all: $(ROMNAME).sfc
paddle.pic: paddle.png
$(GFXCONV) -s 16 -o 16 -u 16 -p -t png -i $<
ball.pic: ball.png
$(GFXCONV) -s 8 -R -o 16 -u 16 -p -t png -i $<
playfield.pic: playfield.pngThe paddle uses 16-by-16 blocks; the ball keeps 8-by-8 tiles in sheet order.
Goal: Build the new sheets instead of the old combined sprite sheet.
Makefile — in the file, apply these changes:
backdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<
src/data.obj: sprites.pic playfield.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfxbackdrop.pic: backdrop.png
$(GFXCONV) -s 8 -o 4 -u 4 -e 0 -p -m -t png -i $<
src/data.obj: paddle.pic ball.pic playfield.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfx-s 16 cuts the paddle sheet into 16 x 16 blocks. Without -m, gfx4snes keeps every block in order, so paddle.pic is 24 frames x 128 bytes. The ball keeps Part 1's -R option. Both sheets share one 16-color palette, so we load only paddle.pal.
Open src\data.asm
Update the start of src\data.asm:
Goal: Store the paddle and ball frame data under separate ROM labels.
src\data.asm — in the file, apply these changes:
.section ".breakout_assets" superfree
sprite_tiles:
.incbin "sprites.pic"
sprite_palette:
.incbin "sprites.pal"
.section ".breakout_assets" superfree
paddle_tiles:
.incbin "paddle.pic"
ball_tiles:
.incbin "ball.pic"
sprite_palette:
.incbin "sprites.pal"Separate labels let each dynamic sprite point at its own animation sheet.
Goal: Use the shared palette supplied with the new paddle artwork.
src\data.asm — in the file, apply these changes:
.incbin "ball.pic"
sprite_palette:
.incbin "sprites.pal"
backdrop_tiles:
.incbin "backdrop.pic".incbin "ball.pic"
sprite_palette:
.incbin "paddle.pal"
backdrop_tiles:
.incbin "backdrop.pic"Both sheets use the paddle palette, so no separate ball-palette upload is needed.
You can run make clean and then delete sprites.png from your project; nothing uses it now. (make clean removes the generated sprites.pic, sprites.pal, and sprites_data.as files.)
Set up and draw dynamic sprites
Open src\main.c
In src\main.c, replace the old sprite label:
Goal: Point C's sprite declarations at the new frame data.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
#define BRICK_TILE_X (BRICK_X / 8)
#define BRICK_TILE_Y (BRICK_Y / 8)
extern u8 sprite_tiles[];
extern u8 sprite_palette[];
extern u8 backdrop_tiles[], backdrop_tiles_end[];
extern u8 backdrop_map[];#define BRICK_TILE_X (BRICK_X / 8)
#define BRICK_TILE_Y (BRICK_Y / 8)
extern u8 paddle_tiles[];
extern u8 ball_tiles[];
extern u8 sprite_palette[];
extern u8 backdrop_tiles[], backdrop_tiles_end[];
extern u8 backdrop_map[];The declarations match the new assembly labels rather than the removed combined sheet.
Replace the whole draw_game function with draw_sprites and a small helper, init_sprite. prepare_bricks and draw_text move into the main loop.
Goal: Submit the ball and paddle positions to the dynamic sprite engine.
src\main.c — add this complete draw_sprites function immediately before init_video:
static void draw_sprites(void)
{
int i;
oambuffer[0].oamx = game.ball_x;
oambuffer[0].oamy = game.ball_y;
oamDynamic8Draw(0);
for (i = 0; i < 3; ++i)
{
oambuffer[i + 1].oamx = game.paddle_x + i * 16;
oambuffer[i + 1].oamy = PADDLE_Y;
oamDynamic16Draw(i + 1);
}
}The ball uses slot 0; the paddle uses slots 1–3 with sixteen-pixel X offsets.
Goal: Initialize each sprite's frame, attributes, graphics pointer, and refresh flag.
src\main.c — add this complete init_sprite function immediately before init_video:
static void init_sprite(int id, u8 *graphics, int frame, int size)
{
oambuffer[id].oamframeid = frame;
oambuffer[id].oamattribute = OBJ_PAL(0) | OBJ_PRIO(3) | size;
oambuffer[id].oamgraphics = graphics;
oambuffer[id].oamrefresh = 1;
}The initial refresh flag queues a graphics upload before the sprite's first display.
Goal: Remove the fixed-OAM renderer now replaced by dynamic drawing.
src\main.c — remove the entire draw_game function shown below:
static void draw_game(void)
{
int i;
oamSetXY(0, game.ball_x, game.ball_y);
for (i = 0; i < 3; ++i)
{
oamSetXY((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y);
}
prepare_bricks();
draw_text();
}
Sprite 0 is the ball and sprites 1 to 3 are the paddle pieces. Note the plain sprite numbers: dynamic sprites index oambuffer directly, unlike Part 1's oamSet, which used multiples of 4.
init_sprite fills in what never changes:
oamgraphicspoints at the sheet in ROM, andoamframeidselects a frame in it;oamattributeholds the sprite palette, priority, and the size bit;oamrefresh = 1tells the engine to copy the frame's graphics into VRAM.
The size bit matters. OBJ_SIZES marks a small (8 x 8) sprite and OBJ_SIZEL a large (16 x 16) one. The engine stores small frames at the second VRAM address given to oamInitDynamicSprite, and this bit makes the hardware look there. If the ball uses OBJ_SIZEL, it shows part of the paddle instead.
In init_video, replace the oamInitGfxSet, oamClear, oamSet, and oamSetEx calls:
Goal: Allocate dynamic frame regions and initialize the ball and paddle slots.
src\main.c — in init_video, apply these changes:
backdrop_tiles_end - backdrop_tiles, 4 * 2, BG_4COLORS,
0x5000);
bgInitMapSet(2, backdrop_map, 32 * 32 * 2, SC_32x32, BACKDROP_MAP);
oamInitGfxSet(sprite_tiles, 1024, sprite_palette, 32, 0, 0x0000,
OBJ_SIZE8_L16);
setMode(BG_MODE1, 0);
oamClear(0, 0); backdrop_tiles_end - backdrop_tiles, 4 * 2, BG_4COLORS,
0x5000);
bgInitMapSet(2, backdrop_map, 32 * 32 * 2, SC_32x32, BACKDROP_MAP);
oamInitDynamicSprite(0x0000, 0x1000, 0, 0, OBJ_SIZE8_L16);
setPalette(sprite_palette, 128, 16 * 2);
init_sprite(0, ball_tiles, 0, OBJ_SIZES);
for (i = 0; i < 3; ++i)
{
init_sprite(i + 1, paddle_tiles, i * 8, OBJ_SIZEL);
}
setMode(BG_MODE1, 0);
oamClear(0, 0);The engine separates large and small frames at 0x0000 and 0x1000; sprite palette 0 remains at CGRAM color 128.
Goal: Remove the old fixed-sprite setup that would overwrite dynamic state.
src\main.c — in init_video, apply these changes:
}
setMode(BG_MODE1, 0);
oamClear(0, 0);
oamSet(0, game.ball_x, game.ball_y, 3, 0, 0, 12, 0);
oamSetEx(0, OBJ_SMALL, OBJ_SHOW);
for (i = 0; i < 3; ++i)
{
oamSet((i + 1) * 4, game.paddle_x + i * 16, PADDLE_Y, 3, 0, 0, 10, 0);
oamSetEx((i + 1) * 4, OBJ_LARGE, OBJ_SHOW);
}
for (row = 0; row < BRICK_ROWS; ++row)
{ }
setMode(BG_MODE1, 0);
for (row = 0; row < BRICK_ROWS; ++row)
{oamInitDynamicSprite(0x0000, 0x1000, 0, 0, OBJ_SIZE8_L16) puts large frames at VRAM 0x0000 and small frames at 0x1000, starts both sprite lists at entry 0, and keeps Part 1's 8 x 8 small / 16 x 16 large sizes. It also resets the sprite table, so oamClear is no longer needed. setPalette copies the 16 sprite colors to CGRAM 128, where sprite palette 0 begins.
At the end of init_video, draw_game no longer exists. Draw the text instead; the sprites appear on the first pass through the loop:
Goal: Draw the initial HUD without calling the deleted fixed-sprite renderer.
src\main.c — in init_video, apply these changes:
shown_bricks = BRICK_COUNT;
consoleDrawText(2, 1, "SCORE LIVES");
draw_game();
setScreenOn();
} shown_bricks = BRICK_COUNT;
consoleDrawText(2, 1, "SCORE LIVES");
draw_text();
setScreenOn();
}The HUD appears before the screen turns on; dynamic sprites are submitted by the first frame of the main loop.
Now reshape the main loop:
Goal: Move the frame wait out of the beginning of the loop.
src\main.c — in main, apply these changes:
while (1)
{
WaitForVBlank();
upload_bricks();
held = padsCurrent(0);
pressed = padsDown(0);
movement = 0;
while (1)
{
held = padsCurrent(0);
pressed = padsDown(0);
movement = 0;Dynamic sprites must be prepared before the wait, then uploaded during VBlank.
Goal: Finish RAM preparation before waiting, then upload sprites and one brick row.
src\main.c — in main, apply these changes:
++movement;
}
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
draw_game();
}
return 0;
} ++movement;
}
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
draw_sprites();
prepare_bricks();
draw_text();
oamInitDynamicSpriteEndFrame();
WaitForVBlank();
oamVramQueueUpdate();
upload_bricks();
}
return 0;
}Everything that only prepares data in RAM happens first. The vblank wait comes next, and both VRAM copies (sprite frames and brick row) come right after it, while vblank is still going.
Checkpoint: Build and run. The paddle is now a cyan bar with rounded ends, and the ball is a small silver ball. Both move and collide exactly as before, and bricks still disappear and come back on restart. If the ball looks like a piece of the paddle, check its OBJ_SIZES attribute.
![]()
5. Make the paddle and ball glimmer
Report hits from the game logic
The glimmer should start when the ball hits something. Only game.c knows when that happens, but game.c must not know about sprites. So the game records what was hit during the current tick in a set of bit flags, and main.c decides how to show it.
Open src\game.h
In src\game.h, add the flags and a field. Put hits before the bricks array:
Goal: Describe collision events with independent bit flags.
src\game.h — in the file, apply these changes:
#define BRICK_WIDTH 16
#define BRICK_HEIGHT 8
enum GameState
{
STATE_READY,#define BRICK_WIDTH 16
#define BRICK_HEIGHT 8
#define HIT_WALL 1
#define HIT_PADDLE 2
#define HIT_BRICK 4
enum GameState
{
STATE_READY,Values 1, 2, and 4 use different bits, allowing several events in one tick.
Goal: Carry the current tick's collision events in the gameplay state.
src\game.h — in the file, apply these changes:
int lives;
int bricks_left;
int state;
unsigned char bricks[BRICK_COUNT];
} Game; int lives;
int bricks_left;
int state;
int hits;
unsigned char bricks[BRICK_COUNT];
} Game;hits reports events to the renderer without introducing SNES APIs into game.c.
Open src\game.c
In src\game.c, clear the flags in game_reset and at the start of every tick, and set one at each kind of bounce:
Goal: Begin a new game without pending collision events.
src\game.c — in game_reset, apply these changes:
game->lives = 3;
game->bricks_left = BRICK_COUNT;
game->state = STATE_READY;
for (i = 0; i < BRICK_COUNT; ++i)
{
game->bricks[i] = 1; game->lives = 3;
game->bricks_left = BRICK_COUNT;
game->state = STATE_READY;
game->hits = 0;
for (i = 0; i < BRICK_COUNT; ++i)
{
game->bricks[i] = 1;Clearing hits prevents stale events from triggering effects after a restart.
Goal: Report brick impacts when the collision helper removes a brick.
src\game.c — in hit_brick, apply these changes:
index = row * BRICK_COLS + col;
if (game->bricks[index])
{
game->bricks[index] = 0;
game->score += 10;
--game->bricks_left; index = row * BRICK_COLS + col;
if (game->bricks[index])
{
game->hits |= HIT_BRICK;
game->bricks[index] = 0;
game->score += 10;
--game->bricks_left;Bitwise OR preserves any other collisions already recorded during the tick.
Goal: Report impacts against either side wall.
src\game.c — in move_ball_x, apply these changes:
next_x = game->ball_x + (game->vx > 0 ? 1 : -1);
if (next_x < FIELD_LEFT || next_x + BALL_SIZE > FIELD_RIGHT)
{
game->vx = -game->vx;
}
else if (hit_brick(game, next_x, game->ball_y)) next_x = game->ball_x + (game->vx > 0 ? 1 : -1);
if (next_x < FIELD_LEFT || next_x + BALL_SIZE > FIELD_RIGHT)
{
game->hits |= HIT_WALL;
game->vx = -game->vx;
}
else if (hit_brick(game, next_x, game->ball_y))The wall event accompanies the existing horizontal velocity reversal.
Goal: Report ceiling impacts as wall events.
src\game.c — in move_ball_y, apply these changes:
next_y = game->ball_y + (game->vy > 0 ? 1 : -1);
if (next_y < FIELD_TOP)
{
game->vy = -game->vy;
}
else if (game->vy > 0 && game->ball_y + BALL_SIZE <= PADDLE_Y && next_y = game->ball_y + (game->vy > 0 ? 1 : -1);
if (next_y < FIELD_TOP)
{
game->hits |= HIT_WALL;
game->vy = -game->vy;
}
else if (game->vy > 0 && game->ball_y + BALL_SIZE <= PADDLE_Y &&The same flag represents horizontal and vertical field boundaries.
Goal: Distinguish paddle impacts from walls and bricks.
src\game.c — in move_ball_y, apply these changes:
game->ball_x + BALL_SIZE > game->paddle_x &&
game->ball_x < game->paddle_x + PADDLE_WIDTH)
{
game->ball_y = PADDLE_Y - BALL_SIZE;
paddle_bounce(game);
} game->ball_x + BALL_SIZE > game->paddle_x &&
game->ball_x < game->paddle_x + PADDLE_WIDTH)
{
game->hits |= HIT_PADDLE;
game->ball_y = PADDLE_Y - BALL_SIZE;
paddle_bounce(game);
}The paddle flag is recorded when the existing contact test selects a rebound.
Goal: Restrict collision flags to events from the current tick.
src\game.c — in game_tick, apply these changes:
{
int step;
if (game->state == STATE_GAME_OVER || game->state == STATE_WON)
{
if (action_pressed)
{
game_reset(game);
}
return;
}{
int step;
game->hits = 0;
if (game->state == STATE_GAME_OVER || game->state == STATE_WON)
{
if (action_pressed)
{
game_reset(game);
}
return;
}|= sets a bit without clearing the others, so one tick can report a wall and a brick.
Play the animation
Open src\main.c
Each glimmer is five frames (1 to 5) shown for three video frames each, then back to frame 0. In src\main.c, add the timing constants and two timers:
Goal: Set a five-frame glimmer animation with three ticks per frame.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
#define BRICK_TILE_X (BRICK_X / 8)
#define BRICK_TILE_Y (BRICK_Y / 8)
extern u8 paddle_tiles[];
extern u8 ball_tiles[];
extern u8 sprite_palette[];#define BRICK_TILE_X (BRICK_X / 8)
#define BRICK_TILE_Y (BRICK_Y / 8)
#define GLIMMER_FRAMES 5
#define GLIMMER_TICKS 3
extern u8 paddle_tiles[];
extern u8 ball_tiles[];
extern u8 sprite_palette[];These constants make each complete glimmer last fifteen video frames.
Goal: Track the paddle and ball animations independently.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
static int shown_score = -1;
static int shown_lives = -1;
static int shown_state = -1;
static int shown_score = -1;
static int shown_lives = -1;
static int shown_state = -1;
static int paddle_glimmer;
static int ball_glimmer;Each timer holds the remaining animation ticks for one object.
Add two helpers above draw_sprites, and make draw_sprites start the timers and pick frames:
Goal: Convert a remaining glimmer duration into its sprite-frame number.
src\main.c — add this complete glimmer_frame function immediately before draw_sprites:
static int glimmer_frame(int *timer)
{
if (*timer == 0)
{
return 0;
}
--*timer;
return GLIMMER_FRAMES - *timer / GLIMMER_TICKS;
}A zero timer selects normal frame 0; otherwise the countdown selects frames 1–5.
Goal: Upload new sprite artwork only when the animation frame changes.
src\main.c — add this complete show_frame function immediately before draw_sprites:
static void show_frame(int id, int frame)
{
if (oambuffer[id].oamframeid != frame)
{
oambuffer[id].oamframeid = frame;
oambuffer[id].oamrefresh = 1;
}
}oamrefresh is set only when the requested frame differs from the current one.
Goal: Restart glimmers on impacts and choose the current frame for each object.
src\main.c — in draw_sprites, apply these changes:
static void draw_sprites(void)
{
int i;
oambuffer[0].oamx = game.ball_x;
oambuffer[0].oamy = game.ball_y;static void draw_sprites(void)
{
int i;
int paddle_frame;
int ball_frame;
if (game.hits & HIT_PADDLE)
{
paddle_glimmer = GLIMMER_FRAMES * GLIMMER_TICKS;
}
if (game.hits)
{
ball_glimmer = GLIMMER_FRAMES * GLIMMER_TICKS;
}
paddle_frame = glimmer_frame(&paddle_glimmer);
ball_frame = glimmer_frame(&ball_glimmer);
oambuffer[0].oamx = game.ball_x;
oambuffer[0].oamy = game.ball_y;Every collision restarts the ball timer; only paddle impacts restart the paddle timer.
Goal: Apply the selected glimmer frame to the ball.
src\main.c — in draw_sprites, apply these changes:
oambuffer[0].oamx = game.ball_x;
oambuffer[0].oamy = game.ball_y;
oamDynamic8Draw(0);
for (i = 0; i < 3; ++i)
oambuffer[0].oamx = game.ball_x;
oambuffer[0].oamy = game.ball_y;
show_frame(0, ball_frame);
oamDynamic8Draw(0);
for (i = 0; i < 3; ++i)The frame is set before the dynamic draw call queues the ball's graphics.
Goal: Animate all three paddle pieces in the same glimmer phase.
src\main.c — in draw_sprites, apply these changes:
{
oambuffer[i + 1].oamx = game.paddle_x + i * 16;
oambuffer[i + 1].oamy = PADDLE_Y;
oamDynamic16Draw(i + 1);
}
} {
oambuffer[i + 1].oamx = game.paddle_x + i * 16;
oambuffer[i + 1].oamy = PADDLE_Y;
show_frame(i + 1, i * 8 + paddle_frame);
oamDynamic16Draw(i + 1);
}
}A hit sets a timer to 15. glimmer_frame counts it down and turns 14...0 into frames 1, 1, 1, 2, 2, 2, ... 5, 5, 5; at zero it returns frame 0. Another hit restarts the animation. Every hit makes the ball glimmer; only paddle hits make the paddle glimmer. Each paddle piece adds its row's first frame (i * 8).
show_frame sets oamrefresh only when the frame actually changes, so the engine copies new graphics to VRAM only when needed.
Remember the uninitialized-globals rule from section 3. Set both timers in init_video:
Goal: Start both animation timers at zero rather than relying on startup RAM.
src\main.c — in init_video, apply these changes:
oamInitDynamicSprite(0x0000, 0x1000, 0, 0, OBJ_SIZE8_L16);
setPalette(sprite_palette, 128, 16 * 2);
init_sprite(0, ball_tiles, 0, OBJ_SIZES);
for (i = 0; i < 3; ++i)
{
oamInitDynamicSprite(0x0000, 0x1000, 0, 0, OBJ_SIZE8_L16);
setPalette(sprite_palette, 128, 16 * 2);
paddle_glimmer = 0;
ball_glimmer = 0;
init_sprite(0, ball_tiles, 0, OBJ_SIZES);
for (i = 0; i < 3; ++i)
{The explicit assignments prevent random glimmers at power-on.
Checkpoint: Build and run. Launch the ball. A white and yellow flash sweeps across the ball whenever it touches a wall, brick, or the paddle, and a shine runs along the paddle from left to right when the ball lands on it. Between hits, both look normal. The glimmer never starts on its own when the game boots.

6. Play music
How SNES sound works
The SNES has a separate sound CPU, the SPC700, with its own 64 KB of RAM. PVSnesLib includes snesmod, a sound driver for that CPU:
spcBoot()uploads the driver;spcSetBank()tells it where the sound data is in ROM;spcLoad()copies a song into the sound RAM;spcPlay()starts the song, andspcStop()stops it.
From then on, the main CPU must call spcProcess() once per frame to keep the driver fed.
Songs and sound effects start as Impulse Tracker (.it) modules. The SDK's smconv tool converts a list of modules into a soundbank: an assembly file and a header with an ID for each module. The PVSnesLib build rules already know how to run it; you just list the files.
Build a soundbank
Open Makefile
Update your Makefile. The new lines go in two places: the audio variables before the include line, and everything else after it.
Goal: Tell the SDK which modules belong in the soundbank and where to generate it.
Makefile — in the file, apply these changes:
$(error PVSnesLib not found at $(PVSNESLIB_HOME). Place this project in snes-examples/breakout or set PVSNESLIB_HOME)
endif
export ROMNAME := breakout
export ROMTITLE := BREAKOUT TUTORIAL$(error PVSnesLib not found at $(PVSNESLIB_HOME). Place this project in snes-examples/breakout or set PVSNESLIB_HOME)
endif
AUDIOFILES := res/effectssfx.it res/pollen8.it
export SOUNDBANK := res/soundbank
export ROMNAME := breakout
export ROMTITLE := BREAKOUT TUTORIALThe effects module is first because snesmod reads effects from that bank entry.
Goal: Configure soundbank conversion in ROM bank 5.
Makefile — in the file, apply these changes:
include $(PVSNESLIB_HOME)/devkitsnes/snes_rules
.PHONY: all clean
all: $(ROMNAME).sfc
include $(PVSNESLIB_HOME)/devkitsnes/snes_rules
SMCONVFLAGS := -s -o $(SOUNDBANK) -V -b 5 -f
all: $(ROMNAME).sfcSMCONVFLAGS requests a bank, fixes its ROM location, and enables size reporting.
Goal: Copy the SDK modules on demand and build audio before linking the ROM.
Makefile — in the file, apply these changes:
SMCONVFLAGS := -s -o $(SOUNDBANK) -V -b 5 -f
all: $(ROMNAME).sfc
paddle.pic: paddle.png
$(GFXCONV) -s 16 -o 16 -u 16 -p -t png -i $<
SMCONVFLAGS := -s -o $(SOUNDBANK) -V -b 5 -f
.PHONY: all clean musics
all: musics $(ROMNAME).sfc
musics: $(SOUNDBANK).obj
res/%.it: $(PVSNESLIB_HOME)/snes-examples/audio/effectsandmusic/res/%.it
mkdir -p res # [!code ++]
cp $< $@
paddle.pic: paddle.png
$(GFXCONV) -s 16 -o 16 -u 16 -p -t png -i $<The pattern rule fills res; the musics prerequisite builds the soundbank object.
Goal: Remove generated soundbank files during a clean rebuild.
Makefile — in the file, apply these changes:
src/data.obj: paddle.pic ball.pic playfield.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfx src/data.obj: paddle.pic ball.pic playfield.pic backdrop.pic
src/main.obj src/game.obj: src/game.h
clean: cleanBuildRes cleanRom cleanGfx cleanAudio What each part does:
AUDIOFILESlists the modules. The effects module must be first: snesmod takes sound effects from the first module in the bank.SOUNDBANKis where the generated files go:res/soundbank.asm,res/soundbank.h, andres/soundbank.bnk.SMCONVFLAGSmakes a soundbank (-s) in ROM bank 5 (-b 5), checks module sizes against the effects module (-f), and prints memory use (-V). Part 1'shdr.asmhas 8 banks of 32 KB, so bank 5 exists.musicsbuilds the soundbank before the ROM.- The
res/%.itrule copies each module from the SDK's sound example the first time it is needed.$<is the SDK file and$@is the file in yourresfolder. Themkdir -pandcplines start with a tab. cleanAudiodeletes the generated soundbank onmake clean.
Start the music
Open src\main.c
At the top of src\main.c, include the generated header and declare the soundbank label. smconv creates SOUNDBANK__ in soundbank.asm:
Goal: Use the module IDs generated by the audio converter.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
#include <snes.h>
#include "game.h"
#define PLAYFIELD_MAP 0x6C00
#define BACKDROP_MAP 0x7000#include <snes.h>
#include "game.h"
#include "res/soundbank.h"
#define PLAYFIELD_MAP 0x6C00
#define BACKDROP_MAP 0x7000The generated header names each module's index in the soundbank.
Goal: Give the sound driver access to the soundbank's ROM address.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
#define GLIMMER_FRAMES 5
#define GLIMMER_TICKS 3
extern u8 paddle_tiles[];
extern u8 ball_tiles[];
extern u8 sprite_palette[];#define GLIMMER_FRAMES 5
#define GLIMMER_TICKS 3
extern char SOUNDBANK__;
extern u8 paddle_tiles[];
extern u8 ball_tiles[];
extern u8 sprite_palette[];SOUNDBANK__ is an assembly label whose address is passed to spcSetBank.
Add start_music above init_sprite:
Goal: Load the song and start it from its first pattern.
src\main.c — add this complete start_music function immediately before init_sprite:
static void start_music(void)
{
spcStop();
spcLoad(MOD_POLLEN8);
spcPlay(0);
}MOD_POLLEN8 comes from res/soundbank.h; MOD_EFFECTSSFX is the effects module. spcPlay(0) starts from the song's first pattern.
In main, boot the driver before anything else, start the music after the screen is set up, and call spcProcess every frame before waiting for vblank:
Goal: Boot the sound CPU, select the soundbank, and start music after video setup.
src\main.c — in main, apply these changes:
u16 pressed;
int movement;
game_reset(&game);
init_video();
while (1)
{ u16 pressed;
int movement;
spcBoot();
spcSetBank(&SOUNDBANK__);
game_reset(&game);
init_video();
start_music();
while (1)
{The driver must be booted and given its bank before loading any module.
Goal: Service the sound driver once per frame without blocking graphics updates.
src\main.c — in main, apply these changes:
prepare_bricks();
draw_text();
oamInitDynamicSpriteEndFrame();
WaitForVBlank();
oamVramQueueUpdate(); prepare_bricks();
draw_text();
spcProcess();
oamInitDynamicSpriteEndFrame();
WaitForVBlank();
oamVramQueueUpdate();Uploading the song takes most of a second, so the game appears and then responds to the controller a moment later. This happens once, at power-on.
Checkpoint: Run make. The build now prints Compiling Soundbank ... and smconv's memory report, and your project has a res folder with the two .it files and the generated soundbank. Run the ROM: music starts shortly after the game appears and keeps looping while you play. The game, glimmer, and bricks still work as in section 5.

7. Add sound effects
Load and trigger effects
The effects module contains five samples:
| Index | Sound |
|---|---|
| 0 | Tada |
| 1 | Hall strings |
| 2 | Honky-tonk piano |
| 3 | Marimba |
| 4 | Cowbell |
spcLoadEffect(index) copies one into sound RAM. Loading a song with spcLoad replaces the loaded effects, so they must be loaded again after every spcLoad. spcEffect(pitch, index, volpan) plays one:
pitch: 4 is the sample's natural pitch (16 kHz), 2 is an octave lower, and higher numbers are higher;volpan: the volume (0 to 15) times 16, plus the pan position (0 left, 8 center, 15 right).
The game uses eight sounds:
| Event | Sound |
|---|---|
| Launch | Piano |
| Ball hits a brick | Cowbell |
| Ball hits the paddle | Marimba |
| Ball hits a wall | Marimba, higher |
| Life lost | Strings |
| Game over | Music stops; low strings |
| Win | Music stops; tada |
| New game after game over or a win | Music restarts |
Name the effects and load them
Open src\main.c
Add names for the indexes in src\main.c:
Goal: Name the effects module's five sample indices.
src\main.c — in file-scope declarations before prepare_bricks, apply these changes:
#define GLIMMER_FRAMES 5
#define GLIMMER_TICKS 3
extern char SOUNDBANK__;
extern u8 paddle_tiles[];#define GLIMMER_FRAMES 5
#define GLIMMER_TICKS 3
#define SFX_TADA 0
#define SFX_STRINGS 1
#define SFX_PIANO 2
#define SFX_MARIMBA 3
#define SFX_COWBELL 4
extern char SOUNDBANK__;
extern u8 paddle_tiles[];The constants match the sample order in the effects module.
Add play_effect above start_music, and load the effects after the song:
Goal: Play any named effect at a chosen pitch with consistent volume and pan.
src\main.c — add this complete play_effect function immediately before start_music:
static void play_effect(int sound, int pitch)
{
spcEffect(pitch, sound, 15 * 16 + 8);
}The packed volume/pan value selects full volume and the center position.
Goal: Reload all effects after the song replaces the SPC's loaded data.
src\main.c — in start_music, apply these changes:
static void start_music(void)
{
spcStop();
spcLoad(MOD_POLLEN8);
spcPlay(0);
}static void start_music(void)
{
int i;
spcStop();
spcLoad(MOD_POLLEN8);
for (i = 0; i < 5; ++i)
{
spcLoadEffect(i);
}
spcPlay(0);
}Every effect plays at full volume (15), centered (8).
Choose a sound for each event
State changes are the most important events, so play_sounds checks them first. It compares the state before and after the tick, then falls back to the hit flags from section 5. Add it after start_music:
Goal: Choose sounds from state transitions first, then from collision events.
src\main.c — add this complete play_sounds function immediately before init_sprite:
static void play_sounds(int old_state)
{
if (game.state != old_state)
{
if (game.state == STATE_PLAYING)
{
play_effect(SFX_PIANO, 4);
}
else if (game.state == STATE_GAME_OVER)
{
spcStop();
play_effect(SFX_STRINGS, 2);
}
else if (game.state == STATE_WON)
{
spcStop();
play_effect(SFX_TADA, 4);
}
else if (old_state == STATE_PLAYING)
{
play_effect(SFX_STRINGS, 3);
}
else
{
start_music();
}
}
else if (game.hits & HIT_BRICK)
{
play_effect(SFX_COWBELL, 4);
}
else if (game.hits & HIT_PADDLE)
{
play_effect(SFX_MARIMBA, 4);
}
else if (game.hits & HIT_WALL)
{
play_effect(SFX_MARIMBA, 6);
}
}The game enters STATE_READY in two ways. Coming from STATE_PLAYING means a life was lost. Coming from game over or a win means a new game, so the music starts again. spcStop only stops the song; an effect started right after it still plays. Only one hit sound plays per tick, chosen in the order brick, paddle, wall.
Finally, remember the state before each tick in main and play the sounds after it:
Goal: Retain the previous game state for detecting audio events.
src\main.c — in main, apply these changes:
u16 held;
u16 pressed;
int movement;
spcBoot();
spcSetBank(&SOUNDBANK__); u16 held;
u16 pressed;
int movement;
int old_state;
spcBoot();
spcSetBank(&SOUNDBANK__);The local variable holds the state from immediately before a tick.
Goal: Capture the state before gameplay can change it.
src\main.c — in main, apply these changes:
if (held & KEY_RIGHT)
{
++movement;
}
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
draw_sprites(); if (held & KEY_RIGHT)
{
++movement;
}
old_state = game.state;
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
draw_sprites();Comparing this snapshot with the post-tick state distinguishes launch, loss, win, and restart events.
Goal: Trigger the appropriate sound after the tick's state and hit flags are known.
src\main.c — in main, apply these changes:
old_state = game.state;
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
draw_sprites();
prepare_bricks();
draw_text(); old_state = game.state;
game_tick(&game, movement, pressed & (KEY_B | KEY_START));
play_sounds(old_state);
draw_sprites();
prepare_bricks();
draw_text();Calling play_sounds here uses the completed tick before drawing and servicing audio.
Checkpoint: Build and run. Launching plays a piano note. The ball makes a marimba sound on the paddle, a higher marimba on walls, and a cowbell on bricks, all over the music. Losing a life plays strings. Losing the last life stops the music with low strings. A new game brings the music back.

Troubleshooting
| Symptom | Check |
|---|---|
No rule to make target 'backdrop.png' (or another PNG) | Copy the four PNG files from the repository's part2 folder into your project root. |
No rule to make target 'res/effectssfx.it' | Check that both .it files exist in <SDK root>\snes-examples\audio\effectsandmusic\res, and that the res/%.it rule is spelled exactly as shown. |
missing separator | Makefile command lines need a real tab. |
res/soundbank.h: No such file | Add musics before $(ROMNAME).sfc in all, and put AUDIOFILES/SOUNDBANK before the include line. |
| Font text invisible or wrong colors after section 1 | Use consoleInitDefaultText(1), not 0. |
| Backdrop or playfield missing | Remove the matching bgSetDisable call, and check the layer numbers (1 is BG2, 2 is BG3). |
| Garbage tiles on a background | Check that the VRAM addresses in the table in section 1 do not overlap. |
| Bricks bounce the ball but stay visible | Expected after section 2; section 3 erases them. Afterwards, check that upload_bricks runs right after WaitForVBlank. |
| Some bricks stay visible or do not come back | upload_bricks must copy only one row and then return. Check row_changed is cleared in init_video. |
| The game slows down near bricks | Use the arithmetic hit_brick from section 2, with the unsigned int casts. |
| Ball looks like part of the paddle | The ball's init_sprite call needs OBJ_SIZES; the paddle uses OBJ_SIZEL. |
| Paddle or ball missing | Call oamInitDynamicSpriteEndFrame() before WaitForVBlank() and oamVramQueueUpdate() after it. |
| Wrong paddle pieces | Each piece's base frame is i * 8: 0, 8, and 16. |
| Glimmer at power-on, or a random first sound | Set paddle_glimmer, ball_glimmer, and row_changed to 0 in init_video; uninitialized globals are not zeroed. |
| No sound at all | Call spcBoot and spcSetBank before start_music, and spcProcess every frame. |
| Music plays but no effects | Load the effects with spcLoadEffect after spcLoad, and keep effectssfx.it first in AUDIOFILES. |
| Other problems | See the Part 1 troubleshooting table. |
Official references
- Dynamic sprites (wiki)
- Backgrounds (wiki)
- Sounds and musics (wiki)
- Sprite and OAM API (
sprite.h) - Background API (
background.h) - DMA API (
dma.h) - Sound API (
sound.h) - gfx4snes options, pinned to 4.6.0
- Effects and music example, pinned to 4.6.0
The header links are pinned to PVSnesLib 4.6.0; the same headers are in pvsneslib\include\snes inside your SDK root. The wiki describes the latest release, which may differ slightly from 4.6.0.
Previous: Part 1 — The Core Mechanics · Next: Part 3 — screens, effects, saving, and the mouse