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

shard_dash: a small game in several files, with a test for each screen More...

#include <snes.h>
#include <snes/text.h>
#include <snes/sprite.h>
#include "res/field.inc"
#include "res/logo.inc"
#include "res/cast.inc"
#include "game.h"
#include "screen.h"
#include "save.h"

Functions

static void boot (void)
 Load what every screen shares, once, with the display off. The maps come per screen.
static void gamePump (void)
 The pump: what the game does on every image, in every screen, fades and loads included.
int main (void)

Variables

u8 game_screen
 Which screen runs (the tests read it by name).
u16 pump_frames
 Frames the pump ran since power-on: it runs in fades, waits and loads too.
u8 run_new_best
 The last round: 1 if it beat the saved best.
u16 run_time
 The last round: frames from the start to the last shard (or TIME_LIMIT).
u8 run_won
 The last round: 1 if the goal was reached before the clock.

Detailed Description

shard_dash: a small game in several files, with a test for each screen

The other examples are one file because they teach one thing. This one shows how a game is put together so that it stays readable and testable:

File Role
main.c boot, the pump, and the sequence: title, round, result, again
screen.c/h the screen layer: VBlank, queue flush, fades, pad edges, pump
title.c the title screen, one function
play.c the round, one function
result.c the result screen, one function
save.c/h the save: a versioned, checksummed record, defaults when it is off
ui.c/h text helpers the screens share

The game: grab five shards in thirty seconds. A win is saved if it is the best time; the result screen shows it and the title returns.

Design rules shown here (each is commented where it is applied):

  • nothing freezes the pump: fades use fadeStart()/fadeStep(), never fadeIn()/fadeOut();
  • a screen is a function that sets up, shows, loops and closes, and undoes what it set up in a close hook;
  • VRAM uploads go through the queue, in pieces that keep to a budget the game chose;
  • the save has a magic, a version and a checksum; absent or corrupt means defaults;
  • state lives in named globals, so a test can read it by name;
  • a key held over a screen change is not a press on the next screen.
Controls
  • Title / result: START (either pad)
  • Round: D-pad
SNES Concepts
  • A game as functions in files, not one switch in one loop
  • The VRAM queue and its VBlank budget
  • Non-blocking fades, battery-backed SRAM with a checked record
  • luna test manifests that play each screen, the real fade and a save round trip
What to Observe
Title with the best time (-- at first), a fade to the round, five shards in a fixed order, then the result and the title again. Win once, power cycle, and the best time is still there.
Modules Used
console, dma, background, sprite, text, input, vramqueue, debug, sram
See also
examples/basics/game_skeleton, examples/memory/save_game, examples/transitions/fading

Function Documentation

◆ boot()

void boot ( void )
static

Load what every screen shares, once, with the display off. The maps come per screen.

< the play square

< the logo

< the text

< the text

< sprite tiles

◆ gamePump()

void gamePump ( void )
static

The pump: what the game does on every image, in every screen, fades and loads included.

◆ main()

int main ( void )

Variable Documentation

◆ game_screen

u8 game_screen

Which screen runs (the tests read it by name).

◆ pump_frames

u16 pump_frames

Frames the pump ran since power-on: it runs in fades, waits and loads too.

◆ run_new_best

u8 run_new_best

The last round: 1 if it beat the saved best.

◆ run_time

u16 run_time

The last round: frames from the start to the last shard (or TIME_LIMIT).

◆ run_won

u8 run_won

The last round: 1 if the goal was reached before the clock.