This tutorial covers game state management on the SNES, including state machines, screen transitions, and common game flow patterns (title screen, gameplay, pause, game over).
What Are Game States?
A game state represents what the game is currently doing. Most SNES games cycle through a small set of states:
| State | Purpose |
| Title | Show title screen, wait for START |
| Playing | Run game logic, handle input, render |
| Paused | Freeze game logic, show overlay message |
| Game Over | Display result, wait for restart |
Each state has its own update logic, and transitions between states involve screen fades, VRAM reloads, or both.
The scene stack (recommended) — scene module
Before hand-rolling a state variable, reach for the opt-in scene module (LIB_MODULES += scene). It gives you a stack of Scene structs — each a pair of callbacks — that push and pop, so "title → play → pause → back to
play" is a data structure instead of an enum and a switch:
static void play_init(void); static void play_update(void);
static const Scene play = { play_init, play_update };
static void play(u8 sample, u8 vol, u8 pan, u16 pitch, u16 tint)
Play + tint the backdrop + update the probe oracles.
Definition main.c:71
static void title_init(void)
The root scene's init: load everything once, then draw the title.
Definition main.c:119
static void title_update(void)
Definition main.c:145
static void pause_update(void)
Definition main.c:197
#define NULL
Null pointer constant.
Definition types.h:217
Opt-in scene/state stack — push/pop game scenes without rolling your own state machine.
A single scene's callbacks.
Definition scene.h:148
- init runs once when a scene is first pushed — load its tiles / palettes here (it happens before the first WaitForVBlank). It is not re-run when a scene is resumed after a pop, so a pause overlay that pops back to play leaves the game exactly as it was.
- update runs every VBlank while the scene is on top. A suspended scene (one below the top) gets no callbacks — that is your pause-freeze for free.
- scenePush/scenePop record the change; the new top's init/update dispatch on the next VBlank; both return 1 on success and 0 when refused (full stack, or a pop at depth 1). To replace rather than stack, call sceneReplace(&next) — it works for the bottom scene too, which scenePop(); scenePush(&next); does not (the pop is refused at depth 1 and the push then stacks, leaking a slot per swap).
Pair it with gameLoopRun (gameloop module) so you don't even write the while (1) WaitForVBlank() loop:
void gameLoopRun(const GameLoopConfig *cfg)
Run the OpenSNES game loop. Never returns.
Scene GameLoopConfig
Game loop callbacks. Alias for Scene from <snes/scene.h>.
Definition gameloop.h:106
examples/basics/scene_stack is the worked example: title → counter → pause overlay, three Scene structs, no hand-rolled dispatch. Prefer this for new games — it is the approach the manual pattern below exists to replace.
The manual state machine (under the hood)
The classic SNES pattern uses a state variable and a switch in the main loop. It is what scene is built on top of, and it is still the right tool when you need a custom rhythm (half-rate updates, a bespoke transition) or are reading the shipped games blocks/breakout, which use it directly. Define states as constants and dispatch each frame:
#define STATE_TITLE 0
#define STATE_PLAYING 1
#define STATE_PAUSED 2
#define STATE_GAME_OVER 3
while (1) {
break;
break;
case STATE_PAUSED:
statePaused();
break;
break;
}
}
return 0;
}
int main(void)
Definition main.c:73
u8 game_state
Probe oracle: current state (0 title, 1 play, 2 over).
Definition main.c:96
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
#define STATE_GAME_OVER
Game over: board frozen, "GAME OVER" rainbow cycle, await restart.
Definition main.c:84
#define STATE_PLAYING
Active gameplay: piece falling, player input, gravity.
Definition main.c:80
static void stateGameOver(void)
Game over state: display frozen board with rainbow "GAME OVER" text.
Definition main.c:570
static void stateTitle(void)
Title screen state: rainbow "PRESS START" text until player begins.
Definition main.c:612
static void statePlaying(void)
Main gameplay state: handle input, apply gravity, lock pieces.
Definition main.c:430
#define STATE_TITLE
Title screen: displaying "PRESS START" with rainbow cycle.
Definition main.c:78
#define BG_MODE1
Definition video.h:39
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
OpenSNES common-case master header.
void setMode(u8 mode, u8 flags)
Set background mode.
examples/games/blocks/main.c is built the same way, with its own states (STATE_TITLE, STATE_PLAYING, STATE_LINE_CLEAR, STATE_GAME_OVER) and a paused flag rather than a pause state. Each state function runs once per frame and can change game_state to trigger a transition.
State Transitions
Transitioning between states often requires reloading VRAM, resetting variables, or fading the screen. The SNES PPU silently ignores VRAM writes during active display, so transitions must happen during VBlank or forced blank.
Fade Out / Fade In
The INIDISP register ($2100) controls screen brightness (0-15). Stepping through brightness levels over multiple frames creates a smooth fade:
static void fade_out(
u8 speed) {
for (brightness = 15; brightness >= 0; brightness--) {
for (
i = 0;
i < speed;
i++) {
}
}
}
static void fade_in(
u8 speed) {
for (brightness = 0; brightness <= 15; brightness++) {
for (
i = 0;
i < speed;
i++) {
}
}
}
void setBrightness(u8 brightness)
Set screen brightness.
static u8 i
Definition main.c:144
signed char s8
8-bit signed integer (-128 to 127)
Definition types.h:44
A speed of 1 gives a fast 16-frame fade (~267ms). A speed of 3 gives a slower cinematic fade. See examples/transitions/fading/ for a complete working example.
VRAM Reload Between States
When switching from title screen to gameplay (or vice versa), you typically need to load different tiles, tilemaps, and palettes. Use forced blank to guarantee VRAM writes succeed:
static void transition_to_gameplay(void) {
fade_out(2);
dmaCopyVram(gameplay_tiles, 0x1000, gameplay_tiles_size);
fade_in(2);
}
u16 score
Probe oracle: shards collected.
Definition main.c:98
void setScreenOff(void)
Disable screen display (blank).
void setScreenOn(void)
Enable screen display.
void dmaCopyVram(const u8 *source, u16 vramAddr, u16 size)
Copy data to VRAM (PVSnesLib compatible).
void dmaCopyCGram(const u8 *source, u16 startColor, u16 size)
Copy palette data to CGRAM (PVSnesLib compatible).
static u16 lives
Definition main.c:153
The key rule: call setScreenOff() before bulk VRAM/CGRAM writes, and setScreenOn() after. This is the forced blank pattern used throughout the OpenSNES examples.
Registers the previous screen left
VRAM is not the only thing a screen leaves behind. The PPU registers are write-only and keep their value until something writes them, and consoleInit() rewrites only a few (BG mode, BG1's map and tiles, mosaic, the palettes). A game-over screen set up after the game inherits the game's sprite size, the maps and scrolls of the layers it does not use, the main and sub screen, the windows, color math, Mode 7's matrix, a running HDMA.
ppuReset() (module ppu_reset) puts every PPU register from $2100 to $2133 back to the value it had at power-on, stops HDMA, resets the library's shadows (scrolls, mosaic, color math, windows, Mode 7, brightness, a running fade) and hides every sprite. It leaves VRAM, CGRAM, the audio and the NMI settings alone. Call it between two screens, before the setup of the next one, which then starts like a first screen:
static void transition_to_game_over(void) {
}
void ppuReset(void)
Put every PPU register, and the library's PPU shadows, back to their power-on state.
examples/basics/ppu_reset shows the difference: built without the call, its second screen keeps the first one's scroll and tint.
Title Screen Implementation
A title screen waits for the player to press START, then transitions to gameplay. A minimal form of the title state of examples/games/blocks/main.c:
return;
}
do {
}
static void startGame(void)
Initialize a new game: clear board, reset state, start music.
Definition main.c:381
The release-wait loop (do { WaitForVBlank(); } while (padHeld(0) & KEY_START)) prevents the START press from triggering an immediate pause in the gameplay state. This is a standard debounce pattern on the SNES.
Complete Title Flow
Breakout (examples/games/breakout/main.c) shows the full sequence: display "READY" text, wait for START, clear the message, then enter the game loop:
FAR u16 blockmap[]
BG1 tilemap RAM copy (0x400 entries = 2KB), far RAM.
#define ST_READY
Definition main.c:94
static void writestring(const char *st, u16 FAR *tilemap, u16 pos, u16 offset)
Write a null-terminated string to a tilemap buffer.
Definition main.c:200
#define ST_BLANK
Definition main.c:97
Pause Screen
Pausing overlays a message on the current gameplay, freezes all game logic, and resumes when START is pressed again. The key is a three-phase wait: release START, wait for START press, wait for release again.
From examples/games/breakout/main.c:
}
#define ST_PAUSED
Definition main.c:96
static void handle_pause(void)
Handle pause functionality.
Definition main.c:594
u16 pad0
Definition kit.c:31
The three-phase wait prevents the unpause START press from being read as a new pause request on the next frame. Blocks (examples/games/blocks/main.c) uses the same three-phase wait, and detects the first press with padPressed(), which reports only the buttons that went down this frame:
}
static u8 paused
Definition main.c:145
const char str_paused[]
Global frame counter incremented by the NMI handler.
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
void hudShowMessage(const char *str)
Definition hud.c:108
Pause Design Rules
- Do not reload VRAM for a simple pause overlay. Write to a tilemap buffer and DMA it.
- Freeze game logic by returning early from the update function while paused.
- The NMI handler keeps running during pause. Input is still read, OAM is still transferred. Only your game logic stops.
- Redraw sprites before entering the pause loop if you use delta rendering, so the screen looks correct while frozen.
Game Over
Game over detection happens in gameplay logic. When the end condition is met, display a message and wait for player input before restarting.
Game Over inside the game logic (Breakout)
Breakout detects game over when lives reach zero. It shows a message, waits for START, and starts a new game:
return;
}
}
static void start_game(void)
Enter the play state: reset score/timer, centre the player.
Definition main.c:181
static s16 pos_x
Definition main.c:175
static s16 pos_y
Definition main.c:176
#define ST_GAMEOVER
Definition main.c:95
static void die(void)
Handle player losing a life.
Definition main.c:524
static void waitStartPressRelease(void)
Wait until START is pressed, then until it is released.
Definition main.c:163
Game Over as a state (blocks)
Blocks restarts from a state of its own. When a new piece cannot spawn (board full), the game transitions to STATE_GAME_OVER, which shows a message, waits for START, then calls startGame() to reinitialize everything:
}
}
const char str_gameover[]
"GAME OVER" overlay message string
void hudClearMessage(void)
Definition hud.c:139
void renderFlush(void)
Definition render.c:397
void renderBoard(void)
Definition render.c:276
Game Over with Fade
Combining game over with a fade transition creates a polished feel:
writestring(
"GAME OVER", tilemap_buf, msg_pos, offset);
}
fade_out(2);
initGame();
fade_in(2);
}
#define TILEMAP_ADDR
VRAM word address for the BG1 tilemap (32x32 = 2KB).
Definition main.c:119
State Machine Summary
| Concern | Pattern |
| State storage | static u8 game_state; with #define constants |
| Dispatch | switch (game_state) in main loop, one call per frame |
| Transition | Set game_state = NEW_STATE; then return; |
| VRAM reload | setScreenOff() before DMA, setScreenOn() after |
| Fade | Step setBrightness(0..15) across frames |
| Button debounce | Three-phase: release, press, release |
| Pause | Overlay text, freeze logic, same VRAM |
| Game over | Message, wait for input, reinitialize or halt |
Next Steps