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

RPG template — a Tiled-driven street of the city, its residents, a dialog box. More...

#include <snes.h>
#include <snes/background.h>
#include <snes/sprite.h>
#include <snes/text.h>
#include <snes/input.h>
#include <snes/collision.h>
#include <snes/panel.h>
#include "res/town_entities.inc"
#include "res/house_entities.inc"
#include "res/palettes.inc"
#include "res/town.inc"
#include "res/house.inc"
#include "res/tileset.inc"
#include "res/interior.inc"
#include "res/uibox.inc"
#include "res/hero.inc"
#include "res/owl.inc"
#include "res/frog.inc"
#include "res/rat.inc"
#include "res/pangolin.inc"
#include "res/raccoon.inc"

Classes

struct  Look
 A resident's look: its 32x32 frame (converted alone) and its OBJ slot. More...
struct  Npc
 A resident of the street: where it stands, what it says, how it looks. The struct shape and the rows both come from the Entities layer of town.tmj, so adding one is a map edit. More...

Macros

#define BOX_B   9
#define BOX_BL   8
#define BOX_BR   10
#define BOX_C   5
#define BOX_L   4
#define BOX_PAL   2 /* BG2 palette 2 -> CGRAM 32-47 */
#define BOX_R   6
#define BOX_T   1
#define BOX_TL   0
#define BOX_TR   2
#define FACE_DOWN   0
#define FACE_LEFT   2
#define FACE_RIGHT   3
#define FACE_UP   1
#define FONT_TILE   2 /* the font's first 2bpp tile: words $5000-$500F are BG2's blank tile 0 */
#define HERO_FRAME(f)
#define HERO_MAX_HP   3
#define HUD_H   3
#define HUD_W   16
#define HUD_X   0
#define HUD_Y   0
#define ICON_CHIP   11
#define ICON_HEART   3
#define ICON_HEART_EMPTY   7
#define MAP_CELLS   64 /* the map, in 8x8 cells */
#define MT   2 /* a metatile, in cells */
#define PANEL_H   6
#define PANEL_W   28
#define PANEL_X   2
#define PANEL_Y   22
#define RESIDENT_TILE(k)
#define RESIDENTS   5
#define SCREEN_CX   120 /* where the hero's metatile sits on screen */
#define SCREEN_CY   104
#define UI_BASE_TILE   64 /* ($5400 - $5000) / 16 words a 4bpp tile */
#define VRAM_FONT   0x5000 /* BG3's and BG2's character base: tile 0 blank, the font from 2bpp tile 2 */
#define VRAM_OBJ   0x6000 /* Kit's 15 frames (tiles 0-255), the residents from tile 256 ($7000) */
#define VRAM_SCENE_MAP   0x4000 /* 64x64: four 32x32 pages */
#define VRAM_SCENE_TILES   0x0000 /* the scene's tiles, up to 1024 (the town has 822) */
#define VRAM_TEXT_MAP   0x5800
#define VRAM_UI_MAP   0x5C00
#define VRAM_UI_TILES   0x5400 /* BG2's tiles: tile 64 from the base at $5000 */
#define WORLD_H   (MAP_CELLS * 8)
#define WORLD_W   (MAP_CELLS * 8)

Enumerations

enum  { ST_FADEIN , ST_EXPLORE , ST_DIALOG }
enum  { SCENE_TOWN , SCENE_HOUSE }

Functions

static u8 at_locker (u16 ftx, u16 fty)
 Is the metatile (ftx, fty) one of the lockers' four?
static void begin_step (s8 dx, s8 dy)
static void build_panel (void)
 Stamp the HUD's frame and icons into the BG2 buffer.
static u8 cell_walkable (u16 cx, u16 cy)
static void dialog_close (void)
 Close the dialog box, the HUD untouched.
static void dialog_open (const char *line)
 Open the dialog box on a line of text.
static void draw_char (u8 oam_id, u16 wx, u16 wy, u16 cam_x, u16 cam_y, u16 tile, u8 palette, u8 flags)
 Draw a 32x32 character over its metatile, or hide it.
static void front_tile (u16 *tx, u16 *ty)
 The metatile in front of the hero (its top-left cell).
static u16 hero_tile (void)
 Kit's tile for the current facing and walk frame.
static u16 hero_tx (void)
static u16 hero_ty (void)
static void hud_icons (void)
 Draw the HUD icons: hearts for HP, a chip for the credits.
static void hud_text (void)
 The HUD's numeric half, on BG3. Re-drawn after every textClearRect, since clearing the dialog does not touch these rows.
static void interact (void)
 A press of A while exploring: talk to who stands in front, or open the locker.
static void load_resident (const u8 *tiles, u16 first_tile)
 Copy a resident's 32x32 frame into its block of the OBJ name table.
int main (void)
static u8 metatile_walkable (u16 cx, u16 cy)
 A metatile (its top-left cell) is walkable if its four cells are, and no one stands on it.
static void scene_load (u8 which, u16 tx, u16 ty, u8 facing)

Variables

static u8 anim_tick
u8 chest_opened
static u8 fade_level
u8 game_state
u16 gold
u8 hero_facing
u8 hero_hp
u16 hero_x
 Probe oracles / state. hero_x/hero_y are the PIXEL position of the metatile the hero occupies (a multiple of 16 when idle).
u16 hero_y
static const Look looks [5]
 The look property of an entity indexes this table: owl, frog, rat, pangolin, raccoon.
static const Npc npcs [NPC_COUNT] = NPC_TABLE
 The residents. const, so the table lives in ROM and is read with bank-honouring far addressing.
static u16 panel_map [32 *32]
u8 scene
static u8 step_count
static s8 step_dx
static s8 step_dy
static const Panel ui
static u8 walk_frame

Detailed Description

RPG template — a Tiled-driven street of the city, its residents, a dialog box.

The SDK's modules composed into a playable RPG skeleton: Kit the courier walks the Rue basse at night, between a noodle bar, a garage, a junk shop and a canal, talks to the residents, opens a locker, and steps into the raccoon's den. Three things make it a template rather than a demo:

  1. The map is a real Tiled map (res/town.tmj): terrain, per-tile collision (the attribute property), entity positions AND each resident's line and look (text and look properties on the object) all live in the map, not in the code. The build converts it with opensnes-level (res/town.tmj.toml): the VRAM map, the collision bytes and the entities as C defines. Adding a resident is a map edit.
  2. A real bordered dialog box and a HUD, both 9-slice panels on BG2 with their text on BG3 above — the classic SNES RPG window.
  3. Two scenes: the street, and the inside of the apartment with the teal door. A scene is a tileset + a palette + a tilemap + a collision map + entities; switching one is three DMAs under force blank. The den is its own Tiled map (res/house.tmj).

Layer roles (Mode 1):

  • BG1: the scene (4bpp, 64x64 scrolling)
  • BG2: the HUD (always) and the dialog panel (while talking)
  • BG3: the dialog text (2bpp, high priority)
  • OBJ: Kit and the residents, 32x32, one palette each

ROM mode: LoROM (project default).

SNES Concepts
  • Tiled (.tmj) as the content pipeline: collision and entities are data, not hardcoded, and the build converts them (opensnes-level)
  • Metatile movement: the map is 8x8 cells, the world is walked on a grid of 16x16 metatiles (two cells a side). A character occupies one metatile and its 32x32 sprite is drawn over it, feet on it, head overhanging upward: what you see is what collides
  • A 32x32 sprite is a 4x4 block of the OBJ name table, which is 16 tiles wide: a frame converted alone is copied in four rows of 128 bytes
  • The sprite-palette plan: res/palettes.toml gives every character its OBJ slot, and palettes.inc names it (PAL_OWL_SLOT, PAL_OWL_CGRAM)
  • A 9-slice dialog box DMA'd to BG2 on open, with layer priorities stacking town < box < text
  • Forced blank (setScreenOff, INIDISP bit 7) around a multi-KB VRAM upload — setBrightness(0) only blacks the screen, it does NOT open the VRAM write window, and the tail of the transfer is dropped
  • A fixed 16-colour palette per scene, imposed on the converter rather than quantised: adding one tile to a quantised sheet re-derives the whole palette and every existing tile shifts hue
  • Off-camera entities MUST be hidden, not just drawn: OAM coordinates wrap, so an entity two screens away reappears somewhere plausible on screen (a resident standing inside a wall)
  • Collision through the SDK's collideTile() over the converted map — its const tilemap parameter means the read is bank-honouring, so a 4 KB map needs neither bank $00 nor RAM
What to Observe
The street fades in, with hearts and credits in the HUD. Walk with the D-pad, a metatile a step: buildings, the canal and the railing block you where they look. Face a resident and press A (the owl at the noodle bar, the frog at the garage, the rat at the junk shop, the pangolin over the bridge: each says its line, from the map), or face the lockers south of the street and press A: the credits go up by 10. Walk into the teal door of the apartment, south of the street, and you step inside — the raccoon greets you with no button press. The mat by the door takes you back out.
Modules Used
console, dma, background, sprite, text, input, collision, panel
See also
res/town.tmj, res/house.tmj — the Tiled content pipeline; devtools/art_assets/rpg.py, which composes them

Macro Definition Documentation

◆ BOX_B

#define BOX_B   9

◆ BOX_BL

#define BOX_BL   8

◆ BOX_BR

#define BOX_BR   10

◆ BOX_C

#define BOX_C   5

◆ BOX_L

#define BOX_L   4

◆ BOX_PAL

#define BOX_PAL   2 /* BG2 palette 2 -> CGRAM 32-47 */

◆ BOX_R

#define BOX_R   6

◆ BOX_T

#define BOX_T   1

◆ BOX_TL

#define BOX_TL   0

◆ BOX_TR

#define BOX_TR   2

◆ FACE_DOWN

#define FACE_DOWN   0

◆ FACE_LEFT

#define FACE_LEFT   2

◆ FACE_RIGHT

#define FACE_RIGHT   3

◆ FACE_UP

#define FACE_UP   1

◆ FONT_TILE

#define FONT_TILE   2 /* the font's first 2bpp tile: words $5000-$500F are BG2's blank tile 0 */

◆ HERO_FRAME

#define HERO_FRAME ( f)
Value:
((u16)(((f) & 3) * 4 + ((f) >> 2) * 64))
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53

◆ HERO_MAX_HP

#define HERO_MAX_HP   3

◆ HUD_H

#define HUD_H   3

◆ HUD_W

#define HUD_W   16

◆ HUD_X

#define HUD_X   0

◆ HUD_Y

#define HUD_Y   0

◆ ICON_CHIP

#define ICON_CHIP   11

◆ ICON_HEART

#define ICON_HEART   3

◆ ICON_HEART_EMPTY

#define ICON_HEART_EMPTY   7

◆ MAP_CELLS

#define MAP_CELLS   64 /* the map, in 8x8 cells */

◆ MT

#define MT   2 /* a metatile, in cells */

◆ PANEL_H

#define PANEL_H   6

◆ PANEL_W

#define PANEL_W   28

◆ PANEL_X

#define PANEL_X   2

◆ PANEL_Y

#define PANEL_Y   22

◆ RESIDENT_TILE

#define RESIDENT_TILE ( k)
Value:
((u16)(256 + ((k) & 3) * 4 + ((k) >> 2) * 64))
static u8 k
Definition main.c:144

◆ RESIDENTS

#define RESIDENTS   5

◆ SCREEN_CX

#define SCREEN_CX   120 /* where the hero's metatile sits on screen */

◆ SCREEN_CY

#define SCREEN_CY   104

◆ UI_BASE_TILE

#define UI_BASE_TILE   64 /* ($5400 - $5000) / 16 words a 4bpp tile */

◆ VRAM_FONT

#define VRAM_FONT   0x5000 /* BG3's and BG2's character base: tile 0 blank, the font from 2bpp tile 2 */

◆ VRAM_OBJ

#define VRAM_OBJ   0x6000 /* Kit's 15 frames (tiles 0-255), the residents from tile 256 ($7000) */

◆ VRAM_SCENE_MAP

#define VRAM_SCENE_MAP   0x4000 /* 64x64: four 32x32 pages */

◆ VRAM_SCENE_TILES

#define VRAM_SCENE_TILES   0x0000 /* the scene's tiles, up to 1024 (the town has 822) */

◆ VRAM_TEXT_MAP

#define VRAM_TEXT_MAP   0x5800

◆ VRAM_UI_MAP

#define VRAM_UI_MAP   0x5C00

◆ VRAM_UI_TILES

#define VRAM_UI_TILES   0x5400 /* BG2's tiles: tile 64 from the base at $5000 */

◆ WORLD_H

#define WORLD_H   (MAP_CELLS * 8)

◆ WORLD_W

#define WORLD_W   (MAP_CELLS * 8)

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ST_FADEIN 
ST_EXPLORE 
ST_DIALOG 

◆ anonymous enum

anonymous enum
Enumerator
SCENE_TOWN 
SCENE_HOUSE 

Function Documentation

◆ at_locker()

u8 at_locker ( u16 ftx,
u16 fty )
static

Is the metatile (ftx, fty) one of the lockers' four?

◆ begin_step()

void begin_step ( s8 dx,
s8 dy )
static

◆ build_panel()

void build_panel ( void )
static

Stamp the HUD's frame and icons into the BG2 buffer.

◆ cell_walkable()

u8 cell_walkable ( u16 cx,
u16 cy )
static

◆ dialog_close()

void dialog_close ( void )
static

Close the dialog box, the HUD untouched.

◆ dialog_open()

void dialog_open ( const char * line)
static

Open the dialog box on a line of text.

◆ draw_char()

void draw_char ( u8 oam_id,
u16 wx,
u16 wy,
u16 cam_x,
u16 cam_y,
u16 tile,
u8 palette,
u8 flags )
static

Draw a 32x32 character over its metatile, or hide it.

Two things happen here, both of them load-bearing.

The overhang is what makes collision feel exact: the character's logical position is one 16x16 metatile; the sprite is drawn 8 px left and 16 px up of it, so its feet stand on that metatile and its head overhangs upward — the standard top-down RPG convention. Drawing the sprite at the metatile's corner instead (the naive version) puts the visible body half a metatile away from what collides.

The cull is not an optimisation, it is correctness. OAM X is 9 bits and Y is 8, so an entity standing outside the camera does not quietly vanish — its coordinates WRAP and it reappears somewhere plausible-looking on screen. Anything off-camera goes to oamHide(), which parks it properly (Y AND the X high bit — Y alone still wraps for sprites over 16 px tall).

◆ front_tile()

void front_tile ( u16 * tx,
u16 * ty )
static

The metatile in front of the hero (its top-left cell).

◆ hero_tile()

u16 hero_tile ( void )
static

Kit's tile for the current facing and walk frame.

◆ hero_tx()

u16 hero_tx ( void )
static

◆ hero_ty()

u16 hero_ty ( void )
static

◆ hud_icons()

void hud_icons ( void )
static

Draw the HUD icons: hearts for HP, a chip for the credits.

The icons are tiles on BG2 (they came out of the same 4x3 sheet as the dialog border), the number is text on BG3. Splitting it that way costs nothing: both layers are already up for the dialog box.

◆ hud_text()

void hud_text ( void )
static

The HUD's numeric half, on BG3. Re-drawn after every textClearRect, since clearing the dialog does not touch these rows.

◆ interact()

void interact ( void )
static

A press of A while exploring: talk to who stands in front, or open the locker.

◆ load_resident()

void load_resident ( const u8 * tiles,
u16 first_tile )
static

Copy a resident's 32x32 frame into its block of the OBJ name table.

The converter writes a sheet in 128-px rows of tiles, the way the name table is laid out: a 32x32 frame converted on its own becomes four rows of 16 tiles, the frame in the first four of each. The block of a 32x32 sprite is four tiles wide and four rows deep, so the frame goes in four copies of 128 bytes, one name-table row (256 words) apart.

◆ main()

int main ( void )

◆ metatile_walkable()

u8 metatile_walkable ( u16 cx,
u16 cy )
static

A metatile (its top-left cell) is walkable if its four cells are, and no one stands on it.

◆ scene_load()

void scene_load ( u8 which,
u16 tx,
u16 ty,
u8 facing )
static

Variable Documentation

◆ anim_tick

u8 anim_tick
static

◆ chest_opened

u8 chest_opened

the locker

◆ fade_level

u8 fade_level
static

◆ game_state

u8 game_state

◆ gold

u16 gold

HUD: credits, +10 from the locker

◆ hero_facing

u8 hero_facing

◆ hero_hp

u8 hero_hp

HUD: static in this template

◆ hero_x

u16 hero_x

Probe oracles / state. hero_x/hero_y are the PIXEL position of the metatile the hero occupies (a multiple of 16 when idle).

◆ hero_y

u16 hero_y

◆ looks

const Look looks[5]
static
Initial value:
= {
{ owl_tiles, owl_pal, PAL_OWL_CGRAM, PAL_OWL_SLOT },
{ frog_tiles, frog_pal, PAL_FROG_CGRAM, PAL_FROG_SLOT },
{ rat_tiles, rat_pal, PAL_RAT_CGRAM, PAL_RAT_SLOT },
{ pangolin_tiles, pangolin_pal, PAL_PANGOLIN_CGRAM, PAL_PANGOLIN_SLOT },
{ raccoon_tiles, raccoon_pal, PAL_RACCOON_CGRAM, PAL_RACCOON_SLOT },
}

The look property of an entity indexes this table: owl, frog, rat, pangolin, raccoon.

◆ npcs

const Npc npcs[NPC_COUNT] = NPC_TABLE
static

The residents. const, so the table lives in ROM and is read with bank-honouring far addressing.

◆ panel_map

u16 panel_map[32 *32]
static

◆ scene

u8 scene

SCENE_TOWN or SCENE_HOUSE

◆ step_count

u8 step_count
static

◆ step_dx

s8 step_dx
static

◆ step_dy

s8 step_dy
static

◆ ui

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

◆ walk_frame

u8 walk_frame
static