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

Game math & UI, rung — panel HUD: a status box that survives a dialog. More...

#include <snes.h>
#include <snes/panel.h>
#include <snes/input.h>
#include "res/noodlebar.inc"
#include "res/boxes.inc"
#include "res/residents.inc"

Macros

#define BG1_CHR   0x0000
#define BG1_MAP   0x3800
#define BG3_CHR   0x4000
#define BG3_MAP   0x4400
#define BOX_PAL   4
#define DLG_H   7
#define DLG_W   24
#define DLG_X   4
#define DLG_Y   20
#define HUD_H   3
#define HUD_W   14
#define HUD_X   1
#define HUD_Y   1
#define MAX_HP   5
#define OBJ_CHR   0x6000
#define SHEET_BASE   1 /* top-left corner of the 3x3 border */
#define SHEET_STRIDE   3
#define TILE_BLANK   0
#define TILE_HEART   10 /* icons live past the 9-slice */
#define TILE_HEART_EMPTY   11
Where Kit and the raccoon stand (top-left of their 32x32 sprites)
#define KIT_X   112
#define KIT_Y   132
#define NPC_X   112
#define NPC_Y   60

Functions

static void draw_hearts (void)
 Redraw the heart row inside the HUD from hero_hp.
static void flush_hud_row (void)
 Push ONLY the HUD's heart row to VRAM — a small VBlank-safe DMA.
int main (void)

Variables

u8 bg3_high
 Probe oracle: 1 while BG3_MODE1_PRIORITY_HIGH is set.
u8 dialog_shown
 Probe oracle: 1 while the dialog box is open.
u8 hero_hp
 Probe oracle: current hearts shown (0..MAX_HP).
static u16 panel_map [32 *32]
 The panel tilemap — caller-owned (the module never allocates 2 KB).
static const Panel ui
 The panel: HUD + dialog share this one layer and one upload.

Detailed Description

Game math & UI, rung — panel HUD: a status box that survives a dialog.

Every game needs furniture: a status bar with hearts and a coin count, a dialog box that opens over the world and closes again. Both are the same primitive — a 9-slice panel, a bordered box stamped from a 3x3 tile sheet (four corners, four edges, one fill) onto a background layer. The panel module does the stamping; you own the tilemap.

Its whole point is on show here: the HUD and the dialog live in ONE tilemap on ONE layer. Opening the dialog (panelDraw + panelFlush) and closing it (panelClear + panelFlush) never disturbs the HUD.

panelFlush() uploads the whole 2 KB map under FORCED BLANK, which briefly blanks the screen — fine for a structural change like a dialog box, but far too heavy to run every time a heart changes. So the hearts, which change often, upload only their own 64-byte tilemap row with an ordinary VBlank DMA (flush_hud_row) — no forced blank, no flicker. The rule: panelFlush for structure, a small VBlank DMA for frequent HUD tweaks.

The scene is the noodle bar seen from above (BG1), with Kit at the counter and the raccoon who runs it behind it (sprites). The panel sheet is res/boxes.png, twelve 2bpp tiles in an order the code relies on: tile 0 is left transparent (that is what panelInit() stamps everywhere), so the bar shows through wherever no panel has been drawn; 1-9 are the 9-slice, 10 and 11 the hearts.

The panel layer is BG3, the 2bpp layer of Mode 1 — the usual home of a HUD, cheap in VRAM. In Mode 1, BG3 is drawn behind BG1 and BG2 unless the BGMODE bit BG3_MODE1_PRIORITY_HIGH is set and the BG3 tiles carry their priority bit; then it is in front of everything, sprites included. SELECT turns the bit off and on, and the HUD goes behind the scene and comes back. (That lesson was the mode1_bg3_priority example.)

ROM mode: LoROM (project default).

Controls
  • B: lose a heart · A: gain one (small VBlank DMA of the heart row)
  • START: toggle the dialog box — the raccoon talking (panelFlush; the HUD stays put)
  • SELECT: BG3_MODE1_PRIORITY_HIGH off / on (the panels behind / in front)
SNES Concepts
  • 9-slice stamping: panelDraw() picks corner/edge/fill from the sheet by position; panelPut() drops a single sheet tile (an icon) into the box
  • Several panels, one layer, one upload — the module's reason to exist
  • panelFlush() uploads under real forced blank (INIDISP bit 7): safe for 2 KB but it blanks briefly, so use it for structural changes; push a frequently-updated HUD row with a small ordinary VBlank DMA instead
  • BG3 in Mode 1: 2bpp tiles (8 words each), 4-colour palettes taken from CGRAM 0-31; in front of BG1/BG2 only with BG3_MODE1_PRIORITY_HIGH and the tile priority bit (Panel.priority = 1)
What to Observe
A HUD box with hearts sits over the noodle bar. A/B change the hearts; START opens/closes a dialog box lower down without touching the HUD. SELECT sends both boxes behind the bar (and behind Kit and the raccoon), SELECT again brings them back in front of everything. hero_hp, dialog_shown and bg3_high are the probe oracles.
Modules Used
console, dma, background, panel, input, sprite
See also
lib/include/snes/panel.h — panelInit/Draw/Put/Clear/Flush

Macro Definition Documentation

◆ BG1_CHR

#define BG1_CHR   0x0000

BG1 scene tiles: the bar, 823 tiles up to $3370

◆ BG1_MAP

#define BG1_MAP   0x3800

BG1 tilemap

◆ BG3_CHR

#define BG3_CHR   0x4000

BG3 panel sheet tiles (2bpp)

◆ BG3_MAP

#define BG3_MAP   0x4400

BG3 panel tilemap (uploaded by panelFlush)

◆ BOX_PAL

#define BOX_PAL   4

◆ DLG_H

#define DLG_H   7

◆ DLG_W

#define DLG_W   24

◆ DLG_X

#define DLG_X   4

◆ DLG_Y

#define DLG_Y   20

◆ HUD_H

#define HUD_H   3

◆ HUD_W

#define HUD_W   14

◆ HUD_X

#define HUD_X   1

◆ HUD_Y

#define HUD_Y   1

◆ KIT_X

#define KIT_X   112

◆ KIT_Y

#define KIT_Y   132

◆ MAX_HP

#define MAX_HP   5

◆ NPC_X

#define NPC_X   112

◆ NPC_Y

#define NPC_Y   60

◆ OBJ_CHR

#define OBJ_CHR   0x6000

Kit and the raccoon

◆ SHEET_BASE

#define SHEET_BASE   1 /* top-left corner of the 3x3 border */

◆ SHEET_STRIDE

#define SHEET_STRIDE   3

◆ TILE_BLANK

#define TILE_BLANK   0

◆ TILE_HEART

#define TILE_HEART   10 /* icons live past the 9-slice */

◆ TILE_HEART_EMPTY

#define TILE_HEART_EMPTY   11

Function Documentation

◆ draw_hearts()

void draw_hearts ( void )
static

Redraw the heart row inside the HUD from hero_hp.

◆ flush_hud_row()

void flush_hud_row ( void )
static

Push ONLY the HUD's heart row to VRAM — a small VBlank-safe DMA.

The hearts change often (every hit), so they must not pay panelFlush()'s 2 KB forced-blank cost — that would blank the top of the screen for a frame on every change. One tilemap row is 64 bytes, which fits the VBlank budget with room to spare, so we upload just that row with no forced blank. panelFlush() stays for structural changes (opening the dialog).

< BG3 panel tilemap (uploaded by panelFlush)

◆ main()

int main ( void )

< BG1 scene tiles: the bar, 823 tiles up to $3370

< BG1 tilemap

< BG1 tilemap

< BG3 panel sheet tiles (2bpp)

< BG3 panel sheet tiles (2bpp)

< BG3 panel tilemap (uploaded by panelFlush)

< Kit and the raccoon

Variable Documentation

◆ bg3_high

u8 bg3_high

Probe oracle: 1 while BG3_MODE1_PRIORITY_HIGH is set.

◆ dialog_shown

u8 dialog_shown

Probe oracle: 1 while the dialog box is open.

◆ hero_hp

u8 hero_hp

Probe oracle: current hearts shown (0..MAX_HP).

◆ panel_map

u16 panel_map[32 *32]
static

The panel tilemap — caller-owned (the module never allocates 2 KB).

◆ ui

const Panel ui
static
Initial value:
= {
0x4400 ,
1 ,
3 ,
4 ,
1,
}
static u16 panel_map[32 *32]
The panel tilemap — caller-owned (the module never allocates 2 KB).
Definition main.c:128

The panel: HUD + dialog share this one layer and one upload.