OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Scrolling maps

This tutorial covers the map module — the scrolling tilemap engine that streams a world larger than VRAM, and the Tiled content pipeline that feeds it. Add map to LIB_MODULES.

What the map module is

A SNES background tilemap is a fixed 32×32 (or 64×64) grid in VRAM. A game world is bigger than that. The map module holds the whole map in ROM and streams the strip of tiles the camera is about to reveal into VRAM one column/row at a time, during VBlank — so scrolling costs a few tiles per frame instead of a full-screen upload.

It works in metatiles: 8×8 blocks indexed through a small tile definition, which is how a large map fits in ROM. You give it three things at load time and then just move the camera each frame.

The Tiled pipeline

You do not hand-author the binary formats. Draw the map in Tiled against a tileset picture, export JSON (.tmj), and put two settings files in res/: one converts the tileset picture with opensnes-tileset, the other the level with opensnes-level — Tiled levels to SNES map data, against the .map the first one writes (examples/maps/map_scroll):

# res/roofs.png.toml
tool = "opensnes-tileset"
[convert]
colors = 16
# res/rooftops.tmj.toml
tool = "opensnes-level"
[convert]
tileset = "roofs.map"

The build converts the level after the pictures, so the tileset's .map is fresh when the level reads it; the Makefile names neither. By hand it is opensnes-level convert res/rooftops.tmj --tileset res/roofs.map. It emits four files the modules consume:

File Content Consumed by
<layer>.m16 the streaming tilemap (tile indices + flip bits) mapLoad() arg 1
<map>.t16 per-tile palette + priority mapLoad() arg 2 (tileset def)
<map>.b16 per-tile collision attribute mapLoad() arg 3
<map>.o16 the Entities object layer the object module

and <map>.inc, which declares them for C: rooftops_BG1_map, rooftops_tiledef, rooftops_tileattr, rooftops_objects (each with _end).

Three more keys of [convert] serve games that do not use the map module and want the same content in a different shape:

Key Output For
entities = true <map>_entities.inc — entities as C defines (struct + rows, custom properties) a game that keeps its own entity structs
quadrant = true <layer>.q16 — a quadrant-ordered 64×64 tilemap scrolling a fixed 64×64 area with bgSetScroll (the background module, not map)
collision = true <layer>.c16 — one collision byte per map cell collideTile() (a flattened grid, indexed directly)

examples/demos/rpg scrolls a fixed 64×64 town with background, not map (its res/town.tmj.toml asks for quadrant, collision and entities); examples/maps/map_scroll uses the default map pipeline. Pick the module that matches the map size — map for worlds larger than 64×64, background for a fixed screen or two.

cute_tiled needs no special export. opensnes-level minifies the JSON itself, so a pretty-printed .tmj from a generator script parses fine.

The minimum viable scroller

#include <snes.h>
#include <snes/map.h>
#include "res/tileslevel1.inc" // the tileset: tileslevel1_tiles[], tileslevel1_pal[]
#include "res/maplevel01.inc" // the level: the .m16/.t16/.b16
int main(void) {
// The tileset and the map's VRAM home; the map engine streams to $6800.
bgInitTileSet(0, tileslevel1_tiles, tileslevel1_pal, 0,
tileslevel1_tiles_end - tileslevel1_tiles,
tileslevel1_pal_end - tileslevel1_pal,
BG_16COLORS, 0x2000);
bgSetMapPtr(0, 0x6800, SC_64x32);
// mapLoad flushes the first screen of tilemap to VRAM.
// It must run BEFORE setScreenOn().
mapLoad(maplevel01_BG1_map, maplevel01_tiledef, maplevel01_tileattr);
u16 cam_x = 0, cam_y = 0;
while (1) {
u16 keys = padHeld(0);
if (keys & KEY_RIGHT) cam_x += 1;
if (keys & KEY_LEFT) cam_x -= 1;
if (keys & KEY_DOWN) cam_y += 1;
if (keys & KEY_UP) cam_y -= 1;
mapUpdateCamera(cam_x, cam_y); // where the camera wants to be
mapUpdate(); // stage the reveal strip
mapVblank(); // DMA the staged strip to VRAM
}
}
int main(void)
Definition main.c:73
void bgInitTileSet
Definition background.h:209
void bgSetMapPtr
Definition background.h:140
void consoleInit(void)
Initialize SNES hardware.
void WaitForVBlank(void)
Wait for next VBlank period.
void setScreenOn(void)
Enable screen display.
#define BG_16COLORS
Definition background.h:47
#define BG_MODE1
Definition video.h:39
#define SC_64x32
Definition background.h:37
#define KEY_RIGHT
Definition input.h:81
#define KEY_DOWN
Definition input.h:79
#define KEY_LEFT
Definition input.h:80
#define KEY_UP
Definition input.h:78
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
#define padHeld(pad)
Definition input.h:158
Scrolling tilemap engine for Mode 1 backgrounds.
void mapUpdateCamera(u16 xpos, u16 ypos)
Update camera position to follow an object.
void mapLoad(const u8 *layer1map, const u8 *layertiles, const u8 *tilesprop)
Load map data into the engine and flush to VRAM.
void mapVblank(void)
Transfer map updates to VRAM.
void mapUpdate(void)
Update map scroll buffers based on camera position.
OpenSNES common-case master header.
u16 cam_y
Definition main.c:91
u16 cam_x
Probe oracles: the camera, the shards placed this frame, the resident in the front slot.
Definition main.c:90
void setMode(u8 mode, u8 flags)
Set background mode.

The three-call rhythm each frame is the whole contract:

  • mapUpdateCamera(x, y) — set the camera's world position. The module gives the live scroll through mapGetCameraX() / mapGetCameraY().
  • mapUpdate() — compute which column/row just came into view and stage it. Cheap; runs in the active frame.
  • mapVblank() — do the actual VRAM DMA of the staged strip. Must run in VBlank (call it right after WaitForVBlank()).

Collision against the map

The map already knows which tiles are solid (the .b16 attributes). Query it by world pixel:

// walkable — the tile's attribute is 0
}
u16 tile = mapGetMetaTile(player_x, player_y); // the raw metatile id
static s16 player_y
Kit's hitbox, top-left corner, Y.
Definition main.c:78
static s16 player_x
Kit's hitbox, top-left corner, in screen coordinates.
Definition main.c:76
u16 mapGetMetaTilesProp(u16 xpos, u16 ypos)
Get tile collision property at map coordinates.
u16 mapGetMetaTile(u16 xpos, u16 ypos)
Get metatile index at map coordinates.

mapGetMetaTilesProp is the map-engine equivalent of collideTile(); use it when the map lives in the map module. (A game on the background module with a .c16 grid (collision = true) uses collideTile() instead — see the collision tutorial.)

One-way and second-layer options

mapSetMapOptions(MAP_OPT_1WAY); // horizontal-only — skips the vertical
// buffer, cheaper for a side-scroller
mapSetMapOptions(MAP_OPT_BG2); // stream a second map onto BG2
#define MAP_OPT_BG2
Scroll BG2 instead of BG1 (VRAM address is still $6800).
Definition map.h:109
void mapSetMapOptions(u8 optmap)
Set map engine options.
#define MAP_OPT_1WAY
Only allow scrolling to the right (no left scroll).
Definition map.h:106

Worked patterns (the shipped examples)

  • examples/maps/map_scroll — the full Tiled pipeline (a .tmj drawn in Tiled, converted with opensnes-level, loaded with mapLoad) and the minimum scroller above, a large map followed by a sprite.
  • examples/demos/platform_objects — per-tile collision attributes driving slope physics (the .b16 values mean more than solid/empty).

Gotchas

🔴 mapLoad() must run before setScreenOn()

mapLoad flushes the first screen of tilemap to VRAM internally. Call it after setScreenOn() and that flush lands during active display — garbage tiles on the first frame. Load during force blank, like every other VRAM setup.

🟠 mapVblank() belongs in VBlank, mapUpdate() does not

mapUpdate stages (cheap, active frame); mapVblank writes VRAM (must be in VBlank). Swapping them, or calling mapVblank outside VBlank, drops the reveal strip and the leading edge of the scroll shows stale tiles.

🟡 map streams .m16; background wants .q16

These are different formats for different modules. A 64×64 fixed area scrolled with bgSetScroll needs the quadrant layout (quadrant = true), not the streaming .m16. Feeding one module the other's format is silent garbage. Match the format to the module.

See also