RPG template — a Tiled-driven street of the city, its residents, a dialog box.
More...
|
| 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) |
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:
- 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.
- 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.
- 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
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).
| 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.