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

Tiled map editor integration with the map scrolling engine. More...

#include <snes.h>
#include <snes/map.h>

Functions

int main (void)
 Entry point — initialize hardware, load map, scroll with D-pad.

Variables

s16 focusx
 Focus-point X in map pixels — the point the camera FOLLOWS (your player's position), NOT a scroll origin. mapUpdateCamera() centers the view on it with boundary clamping; passing a view origin here shifts the whole level by half a screen (see map.h).
u8 mapdata []
 Full level tilemap — 224x30 tiles, 16-bit entries (from BG1.m16).
u8 tileset []
 4bpp tileset graphics (converted from tileslevel1.png)
u8 tileset_end []
u8 tilesetatt []
 Tile collision attributes — one property byte per metatile (from .b16).
u8 tilesetdef []
 Metatile definitions — 4 tile indices per metatile (from .t16).
u8 tilesetpal []
 Tileset palette (3 palette banks x 16 colors = 96 bytes).
u8 tilesetpal_end []

Detailed Description

Tiled map editor integration with the map scrolling engine.

Demonstrates how to use maps created in the Tiled editor (https://www.mapeditor.org/) with the OpenSNES map engine. The map is a large 224x30 tile level (1792x240 pixels) stored in ROM. The map engine streams the visible 32-tile window into VRAM as the camera scrolls, so only the on-screen portion uses VRAM at any time.

The Tiled workflow:

  1. Design your level in Tiled (paint tiles, set collision properties)
  2. Export as .tmj (JSON) or .tmx (XML)
  3. Convert with tmxconv to .m16 (tilemap) + .t16 (tile defs) + .b16 (attributes)
  4. Convert tileset PNG with gfx4snes to .pic + .pal
  5. Include all binaries in data.asm

This example uses pre-converted binaries. The original Tiled source file (maplevel01.tmj) is included in res/ for reference.

Ported from PVSnesLib "tiled" example by alekmaul.

SNES Concepts
  • Map engine: mapLoad() / mapUpdate() / mapUpdateCamera() / mapVblank()
  • Streaming tilemap: only 32 visible columns are in VRAM at any time
  • SC_64x32 tilemap layout at VRAM $6800 (required by map engine)
  • Tile data at VRAM $2000 (4bpp, 16-color)
  • Horizontal camera scrolling with pixel-level precision
  • VBlank DMA for tilemap updates (mapVblank)
What to Observe
  • Press LEFT/RIGHT on D-pad to scroll through the level
  • The background scrolls smoothly at 1 pixel per frame
  • Notice the level is much wider than the screen (1792px vs 256px)
  • The tileset uses platformer-style tiles (platforms, ladders, walls)
Modules Used
console, sprite, dma, input, background, map
See also
map.h, background.h, input.h, dma.h

Function Documentation

◆ main()

int main ( void )

Entry point — initialize hardware, load map, scroll with D-pad.

Returns
Never returns (infinite game loop)

Variable Documentation

◆ focusx

s16 focusx

Focus-point X in map pixels — the point the camera FOLLOWS (your player's position), NOT a scroll origin. mapUpdateCamera() centers the view on it with boundary clamping; passing a view origin here shifts the whole level by half a screen (see map.h).

◆ mapdata

u8 mapdata[]
extern

Full level tilemap — 224x30 tiles, 16-bit entries (from BG1.m16).

◆ tileset

u8 tileset[]
extern

4bpp tileset graphics (converted from tileslevel1.png)

◆ tileset_end

u8 tileset_end[]

◆ tilesetatt

u8 tilesetatt[]
extern

Tile collision attributes — one property byte per metatile (from .b16).

◆ tilesetdef

u8 tilesetdef[]
extern

Metatile definitions — 4 tile indices per metatile (from .t16).

◆ tilesetpal

u8 tilesetpal[]
extern

Tileset palette (3 palette banks x 16 colors = 96 bytes).

◆ tilesetpal_end

u8 tilesetpal_end[]