OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
map.h File Reference

Scrolling tilemap engine for Mode 1 backgrounds. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define ACT_BURN   0x8000
#define ACT_CLIMB   0x2000
#define ACT_DIE   0x4000
#define ACT_FALL   0x0004
#define ACT_JUMP   0x0002
#define ACT_STAND   0x0000
#define ACT_WALK   0x0001
#define MAP_OPT_1WAY   0x01
 Only allow scrolling to the right (no left scroll).
#define MAP_OPT_BG2   0x02
 Scroll BG2 instead of BG1 (VRAM address is still $6800).
#define T_EMPTY   0x0000
 Empty tile (object will fall through).
#define T_FIRES   0x0002
 Fire tile (object burns).
#define T_LADDER   0x0001
 Ladder tile (object can climb).
#define T_PLATE   0x0008
 Platform tile (can jump through from below, land on top).
#define T_SLOPED1   0x0021
 Slope 1x1 descending right (floor drops left-to-right).
#define T_SLOPELD2   0x0023
 Slope 2x1 lower half descending right.
#define T_SLOPELU2   0x0022
 Slope 2x1 lower half ascending right.
#define T_SLOPEU1   0x0020
 Slope 1x1 ascending right (floor rises left-to-right).
#define T_SLOPEUD2   0x0025
 Slope 2x1 upper half descending right.
#define T_SLOPEUU2   0x0024
 Slope 2x1 upper half ascending right.
#define T_SOLID   0xFF00
 Solid tile (blocks movement).
#define T_SPIKE   0x0004
 Spike tile (object dies).

Functions

u16 mapGetCameraX (void)
 Camera X position in pixels.
u16 mapGetCameraY (void)
 Camera Y position in pixels.
u16 mapGetMetaTile (u16 xpos, u16 ypos)
 Get metatile index at map coordinates.
u16 mapGetMetaTilesProp (u16 xpos, u16 ypos)
 Get tile collision property at map coordinates.
void mapLoad (const u8 *layer1map, const u8 *layertiles, const u8 *tilesprop)
 Load map data into the engine and flush to VRAM.
void mapSetMapOptions (u8 optmap)
 Set map engine options.
void mapUpdate (void)
 Update map scroll buffers based on camera position.
void mapUpdateCamera (u16 xpos, u16 ypos)
 Update camera position to follow an object.
void mapVblank (void)
 Transfer map updates to VRAM.

Detailed Description

Scrolling tilemap engine for Mode 1 backgrounds.

Provides hardware-efficient tilemap streaming using metatiles (8x8 pixel blocks). Supports horizontal and vertical scrolling with incremental VRAM updates during VBlank.

Usage

#include <snes.h>
#include <snes/map.h>
// Background tileset must be loaded at VRAM $2000 (tiles) and $6800 (tilemap)
bgInitTileSet(0, &tileset, &tilesetpal, 0, size, 16*2, BG_16COLORS, 0x2000);
bgSetMapPtr(0, 0x6800, SC_64x32);
// Load map data (flushes VRAM internally — screen must be off)
setScreenOn(); // VRAM is ready
// In main loop:
while (1) {
mapUpdate(); // Update scroll buffers
mapUpdateCamera(playerx, playery);
mapVblank(); // DMA updates to VRAM
}
void bgInitTileSet
Definition background.h:209
void bgSetMapPtr
Definition background.h:140
void WaitForVBlank(void)
Wait for next VBlank period.
void setScreenOn(void)
Enable screen display.
u8 maplevel
Map data (tile indices for the scrolling level layout).
u8 tilesetdef
BG1 tileset tile data (4bpp) – start label.
u8 tilesetatt
Tile attribute table (collision flags per tile index).
#define BG_16COLORS
Definition background.h:47
#define SC_64x32
Definition background.h:37
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
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.
Note
Map engine uses VRAM address $6800 for the tilemap by default.

Attribution

Based on: PVSnesLib map engine by Alekmaul Original: undisbeliever's castle_platformer License: zlib (compatible with MIT)

Macro Definition Documentation

◆ ACT_BURN

#define ACT_BURN   0x8000

Burning (bit 15 — flag, combinable with state)

◆ ACT_CLIMB

#define ACT_CLIMB   0x2000

Climbing ladder (bit 13 — flag, combinable with state)

◆ ACT_DIE

#define ACT_DIE   0x4000

Dying (bit 14 — flag, combinable with state)

◆ ACT_FALL

#define ACT_FALL   0x0004

Falling

◆ ACT_JUMP

#define ACT_JUMP   0x0002

Jumping

◆ ACT_STAND

#define ACT_STAND   0x0000

Standing still

◆ ACT_WALK

#define ACT_WALK   0x0001

Walking

◆ MAP_OPT_1WAY

#define MAP_OPT_1WAY   0x01

Only allow scrolling to the right (no left scroll).

◆ MAP_OPT_BG2

#define MAP_OPT_BG2   0x02

Scroll BG2 instead of BG1 (VRAM address is still $6800).

◆ T_EMPTY

#define T_EMPTY   0x0000

Empty tile (object will fall through).

◆ T_FIRES

#define T_FIRES   0x0002

Fire tile (object burns).

◆ T_LADDER

#define T_LADDER   0x0001

Ladder tile (object can climb).

◆ T_PLATE

#define T_PLATE   0x0008

Platform tile (can jump through from below, land on top).

◆ T_SLOPED1

#define T_SLOPED1   0x0021

Slope 1x1 descending right (floor drops left-to-right).

◆ T_SLOPELD2

#define T_SLOPELD2   0x0023

Slope 2x1 lower half descending right.

◆ T_SLOPELU2

#define T_SLOPELU2   0x0022

Slope 2x1 lower half ascending right.

◆ T_SLOPEU1

#define T_SLOPEU1   0x0020

Slope 1x1 ascending right (floor rises left-to-right).

◆ T_SLOPEUD2

#define T_SLOPEUD2   0x0025

Slope 2x1 upper half descending right.

◆ T_SLOPEUU2

#define T_SLOPEUU2   0x0024

Slope 2x1 upper half ascending right.

◆ T_SOLID

#define T_SOLID   0xFF00

Solid tile (blocks movement).

◆ T_SPIKE

#define T_SPIKE   0x0004

Spike tile (object dies).

Function Documentation

◆ mapGetCameraX()

u16 mapGetCameraX ( void )

Camera X position in pixels.

The left edge of the visible map, as mapUpdateCamera() last computed it. Subtract it from a world X to get a screen X.

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 mapGetCameraY(void)
Camera Y position in pixels.
u16 mapGetCameraX(void)
Camera X position in pixels.
void oamSet(u16 id, u16 x, u16 y, u16 tile, u16 palette, u16 priority, u16 flags)
Set sprite properties.
Returns
Camera X, 0 to the map width minus 256

◆ mapGetCameraY()

u16 mapGetCameraY ( void )

Camera Y position in pixels.

Returns
Camera Y, 0 to the map height minus 224
See also
mapGetCameraX

◆ mapGetMetaTile()

u16 mapGetMetaTile ( u16 xpos,
u16 ypos )

Get metatile index at map coordinates.

Parameters
xposX coordinate in map pixels
yposY coordinate in map pixels
Returns
Tile id: the low 10 bits of the map entry, 0 to 1023.

◆ mapGetMetaTilesProp()

u16 mapGetMetaTilesProp ( u16 xpos,
u16 ypos )

Get tile collision property at map coordinates.

Parameters
xposX coordinate in map pixels
yposY coordinate in map pixels
Returns
Tile property value (T_EMPTY, T_SOLID, T_LADDER, etc.)

◆ mapLoad()

void mapLoad ( const u8 * layer1map,
const u8 * layertiles,
const u8 * tilesprop )

Load map data into the engine and flush to VRAM.

Initializes metatile definitions, tile properties, and the row lookup table. Performs a full-screen tilemap refresh and DMAs it directly to VRAM. After this call, the tilemap is ready — no separate mapVblank() needed before setScreenOn().

Parameters
layer1mapAddress of map data (format: u16 width, u16 height, u16 pad, then tile indices)
layertilesAddress of the tile definitions: one 16-bit tilemap entry per tile id of the map (opensnes-level's .t16), up to 1024. The engine copies 4096 bytes from this address whatever the file's size — a 126-byte .t16 is followed in RAM by the ROM bytes that follow it. Entries past the last defined tile are therefore garbage; a map must not reference them (opensnes-level never does).
tilespropAddress of tile property data (collision types)
Note
Must be called during forced blank (screen off). The function writes directly to VRAM via DMA.
Data placement: the three blobs may live in any bank — mapLoad and the scroll runtime honour each far pointer's bank byte. The blobs opensnes-level writes are declared with ASSET_SECTION, which keeps them out of bank $00; a map written by hand in assembly is declared the same way. C may read the blobs through a const pointer, which is compiled as a far read; casting the const away is the one bank-blind read (KNOWN_LIMITATIONS.md). To read the map at a position in pixels, use mapGetMetaTile() and mapGetMetaTilesProp().

◆ mapSetMapOptions()

void mapSetMapOptions ( u8 optmap)

Set map engine options.

Warning
Call it AFTER mapLoad(): mapLoad() clears the options (and zeroes the camera), so options set before it are lost without a trace.
Parameters
optmapOptions bitmask (MAP_OPT_1WAY, MAP_OPT_BG2)

◆ mapUpdate()

void mapUpdate ( void )

Update map scroll buffers based on camera position.

Call once per frame in the main loop. Builds horizontal/vertical update buffers when the camera crosses tile boundaries.

◆ mapUpdateCamera()

void mapUpdateCamera ( u16 xpos,
u16 ypos )

Update camera position to follow an object.

Centers the camera on the given coordinates with boundary clamping. Scroll trigger distances: X=128px from edges, Y=80px from edges.

Parameters
xposX coordinate to focus on (in map pixels)
yposY coordinate to focus on (in map pixels)
Note
Pass the position of the object to FOLLOW (typically the player, as in examples/maps/mapandobjects) — NOT a scroll/view origin. The view centers itself on the point with edge clamping; feeding it a view origin shifts the level by half a screen from where it was painted.

◆ mapVblank()

void mapVblank ( void )

Transfer map updates to VRAM.

Performs DMA transfers of scroll update buffers during VBlank. Also updates BG scroll registers.

Note
Must be called during VBlank (after WaitForVBlank).