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

Game skeleton — the smallest complete game: title -> play -> over. More...

#include <snes.h>
#include <snes/input.h>
#include <snes/text.h>
#include "res/field.inc"
#include "res/logo.inc"
#include "res/cast.inc"

Macros

#define PLAYER_SPEED   2
#define ST_OVER   2
#define ST_PLAY   1
#define ST_TITLE   0
 Game states — the whole game is a switch over these.
#define START_TIME   30
VRAM (word addresses): three maps, then each layer's tiles on a $1000 boundary
#define SPR_VRAM   0x4000
#define VRAM_BG1_GFX   0x1000
#define VRAM_BG1_MAP   0x0000
#define VRAM_BG2_GFX   0x2000
#define VRAM_BG2_MAP   0x0400
#define VRAM_BG3_FONT   0x3000
#define VRAM_BG3_MAP   0x0800
Frames of res/cast.png: Kit 32x32 four a row, then the shard 16x16
#define FRAME_DOWN   0
#define FRAME_RIGHT   10
#define FRAME_UP   5
#define TILE_SHARD   256
The field Kit's pickup box may cover (inside the kerb, under the HUD)
#define PX_MAX   232
#define PX_MIN   16
#define PY_MAX   200
#define PY_MIN   32

Functions

static void draw_hud_labels (void)
 Print the fixed HUD labels once (numbers refreshed as they change).
static void draw_score (void)
 Refresh the SCORE number (clear the field first so it never smears).
static void draw_sprites (void)
 Draw the two gameplay sprites into the OAM buffer.
static void draw_time (void)
 Refresh the TIME number.
static u16 kit_tile (u16 f)
 The OAM tile of Kit's frame f: 32x32 frames are 4x4 tiles, and a row of OBJ tiles is 16 wide, so frame f starts at column (f % 4) * 4 of tile row (f / 4) * 4.
static void logo_screen (void)
 Show the logo screen: BG2 and the text, no square, no sprites.
int main (void)
static void show_over (void)
 Game-over screen.
static void show_title (void)
 Title screen: the logo, the game's name and the prompt.
static void spawn_shard (void)
 Move the shard to a fresh pseudo-random spot.
static void start_game (void)
 Enter the play state: reset score/timer, centre the player.
static void update_play (u16 held)
 One frame of gameplay.

Variables

static u8 anim
static u8 cx
static u8 cy
static u8 facing
static u8 facing_left
u8 game_state
 Probe oracle: current state (0 title, 1 play, 2 over).
static u8 moving
static u8 px
static u8 py
u16 score
 Probe oracle: shards collected.
static u16 seed_ctr
static u8 tick
u8 time_left
 Probe oracle: seconds remaining in a round.

Detailed Description

Game skeleton — the smallest complete game: title -> play -> over.

The capstone the curriculum was missing: not a feature demo but the shape of a whole game, small enough to read in one sitting and fork for your own. It recombines the rungs you've climbed — input drives a sprite, a second sprite is a goal, a bounding-box test scores it, text is the HUD, and a hand-rolled state machine ties title, play and game-over together.

The one structural idea: a game is a state machine around one loop. Every frame you WaitForVBlank(), read the pad, then switch (game_state) to the code for the screen you're on. Transitions are just assignments to game_state plus a one-time setup for the new screen. That is the skeleton under every SNES game; basics/scene_stack shows the same idea lifted into the opt-in scene framework once the enum + switch starts to sprawl.

The game: walk Kit across a square of the Rue basse with the D-pad and pick up the data shard before the timer runs out; each one picked up respawns elsewhere. The title and the game-over screens show the logo.

ROM mode: LoROM (project default).

Controls
  • Title / Game over: START to begin / play again
  • Play: D-pad to walk; reach the shard to score
SNES Concepts
  • The frame loop: WaitForVBlank -> read input -> update -> the NMI flushes
  • A plain enum + switch state machine (title / play / over)
  • Mode 1: BG1 the play square, BG2 the logo, BG3 the text in front; each screen picks its layers with setMainScreen()
  • Sprites (Kit + the shard) via the OAM buffer; text HUD via the auto-flush
  • Bounding-box collision on a pickup box smaller than the sprites; rngNext()/rngSeed() seeded for varied shard spawns
What to Observe
Title shows the logo and "PRESS START". In play, Kit faces the way he walks; reaching the turning shard bumps SCORE and respawns it; TIME counts down; at zero the game-over screen shows the final score. game_state, score, time_left are the probe oracles.
Modules Used
console, dma, background, sprite, text, input
See also
examples/basics/scene_stack — the same idea via the scene Framework

Macro Definition Documentation

◆ FRAME_DOWN

#define FRAME_DOWN   0

Kit walking down: frames 0-4

◆ FRAME_RIGHT

#define FRAME_RIGHT   10

walking right: 10-14; left mirrored

◆ FRAME_UP

#define FRAME_UP   5

walking up: 5-9

◆ PLAYER_SPEED

#define PLAYER_SPEED   2

◆ PX_MAX

#define PX_MAX   232

◆ PX_MIN

#define PX_MIN   16

◆ PY_MAX

#define PY_MAX   200

◆ PY_MIN

#define PY_MIN   32

◆ SPR_VRAM

#define SPR_VRAM   0x4000

sprite tiles

◆ ST_OVER

#define ST_OVER   2

◆ ST_PLAY

#define ST_PLAY   1

◆ ST_TITLE

#define ST_TITLE   0

Game states — the whole game is a switch over these.

◆ START_TIME

#define START_TIME   30

seconds on the clock

◆ TILE_SHARD

#define TILE_SHARD   256

the shard's frame k is tile 256 + 2k

◆ VRAM_BG1_GFX

#define VRAM_BG1_GFX   0x1000

◆ VRAM_BG1_MAP

#define VRAM_BG1_MAP   0x0000

the play square

◆ VRAM_BG2_GFX

#define VRAM_BG2_GFX   0x2000

◆ VRAM_BG2_MAP

#define VRAM_BG2_MAP   0x0400

the logo

◆ VRAM_BG3_FONT

#define VRAM_BG3_FONT   0x3000

◆ VRAM_BG3_MAP

#define VRAM_BG3_MAP   0x0800

the text

Function Documentation

◆ draw_hud_labels()

void draw_hud_labels ( void )
static

Print the fixed HUD labels once (numbers refreshed as they change).

◆ draw_score()

void draw_score ( void )
static

Refresh the SCORE number (clear the field first so it never smears).

◆ draw_sprites()

void draw_sprites ( void )
static

Draw the two gameplay sprites into the OAM buffer.

Kit's 32x32 frame is drawn 12 px left of and 20 px above his box, so the box sits at his feet; the shard's 16x16 frame is centred on its box. Kit walks through five frames while he moves (six vblanks each); the shard turns through eight (four vblanks each).

< the shard's frame k is tile 256 + 2k

◆ draw_time()

void draw_time ( void )
static

Refresh the TIME number.

◆ kit_tile()

u16 kit_tile ( u16 f)
static

The OAM tile of Kit's frame f: 32x32 frames are 4x4 tiles, and a row of OBJ tiles is 16 wide, so frame f starts at column (f % 4) * 4 of tile row (f / 4) * 4.

◆ logo_screen()

void logo_screen ( void )
static

Show the logo screen: BG2 and the text, no square, no sprites.

◆ main()

int main ( void )

< the play square

< the logo

< the play square

< the logo

< the text

< the text

< sprite tiles

◆ show_over()

void show_over ( void )
static

Game-over screen.

◆ show_title()

void show_title ( void )
static

Title screen: the logo, the game's name and the prompt.

◆ spawn_shard()

void spawn_shard ( void )
static

Move the shard to a fresh pseudo-random spot.

◆ start_game()

void start_game ( void )
static

Enter the play state: reset score/timer, centre the player.

< seconds on the clock

< Kit walking down: frames 0-4

◆ update_play()

void update_play ( u16 held)
static

One frame of gameplay.

< walking right: 10-14; left mirrored

< walking right: 10-14; left mirrored

< walking up: 5-9

< Kit walking down: frames 0-4

Variable Documentation

◆ anim

u8 anim
static

◆ cx

u8 cx
static

◆ cy

u8 cy
static

◆ facing

u8 facing
static

◆ facing_left

u8 facing_left
static

◆ game_state

u8 game_state

Probe oracle: current state (0 title, 1 play, 2 over).

◆ moving

u8 moving
static

◆ px

u8 px
static

◆ py

u8 py
static

◆ score

u16 score

Probe oracle: shards collected.

◆ seed_ctr

u16 seed_ctr
static

◆ tick

u8 tick
static

◆ time_left

u8 time_left

Probe oracle: seconds remaining in a round.