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

Scene stack demo — title → counter → pause overlay. More...

#include <snes.h>
#include <snes/text.h>
#include <snes/scene.h>
#include <snes/colormath.h>
#include "res/field.inc"
#include "res/logo.inc"

Macros

VRAM (word addresses), as in basics/game_skeleton
#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

Functions

static void counter_init (void)
static void counter_update (void)
int main (void)
 Hand control to the scene framework. Never returns.
static void pause_init (void)
static void pause_update (void)
static void title_draw (void)
static void title_init (void)
 The root scene's init: load everything once, then draw the title.
static void title_update (void)

Variables

static const Scene counter_scene
 Counter screen — increments a number every frame.
static u16 counter_value
 Counter value, lives across pause/resume because counter's init runs once on push, not on every resume.
static const Scene pause_scene
 Pause overlay — freezes the counter, waits for Select to resume.
u16 title_redraws
static u8 title_resume_pending
 Set by the counter before it pops: the title is resumed, not re-initialised (scene.h: init runs once per push), so it redraws itself here. The luna manifest counts the redraws.
static const Scene title_scene
 Title screen — waits for Start to begin, no game state.

Detailed Description

Scene stack demo — title → counter → pause overlay.

Demonstrates the opt-in scene framework: the running game is split into three Scene structs that push and pop without a hand-rolled state machine. The demo is deliberately small — three screens that swap when buttons are pressed — so you can read the dispatch shape. It uses the pictures of basics/game_skeleton (the logo, the play square), so the two ways of structuring a game can be compared side by side.

Flow:

  1. title scene: the logo and "PRESS START". Start pushes counter.
  2. counter scene: the square, and a number that increments every frame. Select pushes pause (counter is suspended; numbers stop). Start pops back to title.
  3. pause scene: shows "PAUSED" and darkens the square beneath it (colour math): an overlay, the scene below still drawn but frozen. Select pops back to counter (numbers resume from where they stopped, no init replay).
SNES Concepts
  • Scene stack: push/pop without enum + switch boilerplate
  • init runs once on first push; resume after pop does not re-init
  • Suspended scenes get no callbacks (pause overlay = counter frozen)
  • Text rendering through the auto-flush NMI hook, on BG3 in front
  • Each scene picks its layers (setMainScreen) when it is entered or resumed
  • A pause overlay by colour math: colorMathShadow() darkens BG1
What to Observe
  • Start on title: enters counter, numbers increment from 0
  • Select while counting: PAUSED appears, numbers freeze
  • Select in pause: counter resumes, numbers continue from where they stopped (no reset — init was not re-called)
  • Start while counting: pops back to title (counter discarded; pressing Start again restarts from 0 because counter's init re-runs on the next push)
Modules Used
console, sprite, dma, background, text, input, scene, colormath
See also
scene.h, gameloop.h

Macro Definition Documentation

◆ VRAM_BG1_GFX

#define VRAM_BG1_GFX   0x1000

◆ VRAM_BG1_MAP

#define VRAM_BG1_MAP   0x0000

the 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

◆ counter_init()

void counter_init ( void )
static

◆ counter_update()

void counter_update ( void )
static

◆ main()

int main ( void )

Hand control to the scene framework. Never returns.

◆ pause_init()

void pause_init ( void )
static

◆ pause_update()

void pause_update ( void )
static

◆ title_draw()

void title_draw ( void )
static

◆ title_init()

void title_init ( void )
static

The root scene's init: load everything once, then draw the title.

The title is pushed once, by sceneRun(), so its init is where the program sets up the hardware — the same layers as basics/game_skeleton: Mode 1, BG1 the square, BG2 the logo, BG3 the text in front.

< the square

< the logo

< the square

< the logo

< the text

< the text

◆ title_update()

void title_update ( void )
static

Variable Documentation

◆ counter_scene

const Scene counter_scene
static
Initial value:
= {
.init = counter_init,
.update = counter_update,
}
static void counter_init(void)
Definition main.c:161
static void counter_update(void)
Definition main.c:170

Counter screen — increments a number every frame.

◆ counter_value

u16 counter_value
static

Counter value, lives across pause/resume because counter's init runs once on push, not on every resume.

◆ pause_scene

const Scene pause_scene
static
Initial value:
= {
.init = pause_init,
.update = pause_update,
}
static void pause_init(void)
Definition main.c:190
static void pause_update(void)
Definition main.c:197

Pause overlay — freezes the counter, waits for Select to resume.

◆ title_redraws

u16 title_redraws

◆ title_resume_pending

u8 title_resume_pending
static

Set by the counter before it pops: the title is resumed, not re-initialised (scene.h: init runs once per push), so it redraws itself here. The luna manifest counts the redraws.

◆ title_scene

const Scene title_scene
static
Initial value:
= {
.init = title_init,
.update = title_update,
}
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

Title screen — waits for Start to begin, no game state.