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

Scrolling map with animated sprite character. More...

#include <snes.h>
#include <snes/map.h>
#include "res/roofs.inc"
#include "res/kit.inc"
#include "res/rooftops.inc"

Macros

#define FRAME_WALK   4
 The roofs' 802 tiles, Kit's 32x32 frames.
#define ROOF_Y   (22 * 8)
 The roofs' surface: row 22 of the map.

Functions

static u16 kit_tile (u16 f)
 OAM tile number of frame f of Kit's sheet.
int main (void)
 Entry point — scrolling map with Kit walking on it.

Variables

u16 flip
 Animation counter (one walk frame every 4 frames of movement).
u16 flipx
 Horizontal flip flag (1 = facing left).
u16 frame
 Current sprite tile number.
u16 frameidx
 Current walk frame index (0-7).
u16 xloc
 Kit X position in map pixels.
u16 yloc
 Kit Y position in map pixels (the top of his 32x32 frame).

Detailed Description

Scrolling map with animated sprite character.

A large Tiled map scrolls as Kit, the fox-kit courier, walks left and right along a row of rooftops at night. The map engine streams only the visible tiles to VRAM, so the map can be much wider than the screen. The sprite cycles through 8 walk frames and flips horizontally based on direction.

This is the stepping stone between "static background" and "continuous_scroll" — it teaches the map engine basics with a simple player character.

This began as the PVSnesLib "mapscroll" example by alekmaul; the map, its tiles and Kit are ours (devtools/art_assets/build.py writes the Tiled map and its tile sheet from assets/neo-lumen/).

SNES Concepts
  • Map engine: mapLoad / mapUpdate / mapUpdateCamera / mapVblank
  • Camera-follow: sprite position drives the viewport
  • Sprite animation: 8-frame walk cycle with horizontal flip
  • oamSet for simple single-sprite rendering
  • mapGetCameraX() / mapGetCameraY() for screen-relative sprite positioning
What to Observe
  • Press LEFT/RIGHT to move Kit along the roofs
  • The background scrolls to follow the character
  • Kit's walk advances one frame every 4 frames of movement
  • Kit flips horizontally when changing direction
Modules Used
console, sprite, dma, input, background, map
See also
map.h, sprite.h, input.h

Macro Definition Documentation

◆ FRAME_WALK

#define FRAME_WALK   4

The roofs' 802 tiles, Kit's 32x32 frames.

The level's tilemap, tile definitions and objects (opensnes-level)

First walk frame in Kit's sheet (frames 4-11, four 32x32 frames a row)

◆ ROOF_Y

#define ROOF_Y   (22 * 8)

The roofs' surface: row 22 of the map.

Function Documentation

◆ kit_tile()

u16 kit_tile ( u16 f)
static

OAM tile number of frame f of Kit's sheet.

The converter lays the sheet's 32x32 blocks out four to a 128-pixel row of OBJ VRAM: block f starts at tile (f % 4) * 4 + (f / 4) * 64.

◆ main()

int main ( void )

Entry point — scrolling map with Kit walking on it.

Returns
Never returns (infinite game loop)

Variable Documentation

◆ flip

u16 flip

Animation counter (one walk frame every 4 frames of movement).

◆ flipx

u16 flipx

Horizontal flip flag (1 = facing left).

◆ frame

u16 frame

Current sprite tile number.

◆ frameidx

u16 frameidx

Current walk frame index (0-7).

◆ xloc

u16 xloc

Kit X position in map pixels.

◆ yloc

u16 yloc

Kit Y position in map pixels (the top of his 32x32 frame).