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

Kit's Rooftop Run — a side-scrolling platformer, title to game over. More...

#include <snes.h>
#include <snes/vramqueue.h>
#include <snes/anim.h>
#include <snes/text.h>
#include <snes/snesmod.h>
#include "soundbank.h"
#include "res/tiles.inc"
#include "res/skyline.inc"
#include "res/kit.inc"
#include "res/enemies.inc"
#include "res/shard.inc"
#include "res/level_objects.h"

Macros

#define DOWN_FRAMES   60
 Frames the world waits after a life lost.
#define EN_ALIVE   0
 A machine patrols; stomped, it lies flat for a while; then it is gone.
#define EN_FLAT   1
 See EN_ALIVE.
#define EN_GONE   2
 See EN_ALIVE.
#define ENEMY_DRONE   1
 A drone flies over the roofs.
#define ENEMY_INSECT   0
 Machine types of enemy_def[][0].
#define FRAME_DRONE   4
 First frame of the drone's hover.
#define FRAME_FALL   25
 Jump pose while coming down.
#define FRAME_IDLE   0
 Kit's sheet (res/kit.png, four 32x32 frames a row): idle 0-3, walk 4-11, run 12-19, jump 20-27 (crouch, rise, top, fall, land).
#define FRAME_INSECT   0
 The machines' sheet (res/enemies.png): the insect's walk 0-3, the drone's hover 4-7, the insect stomped 8, the drone stomped 9.
#define FRAME_RISE   23
 Jump pose while going up.
#define FRAME_SQUASH   8
 The stomped insect, then the stomped drone.
#define FRAME_WALK   4
 First frame of the walk cycle.
#define GRAVITY   48
 Gravity acceleration in 8.8 fixed-point units per frame.
#define GS_CLEAR   4
 The beacon reached.
#define GS_DOWN   2
 A life lost: Kit blinks, the world waits.
#define GS_OVER   3
 No life left.
#define GS_PLAY   1
 Running.
#define GS_TITLE   0
 The game's states.
#define HIDE_Y_32   224
 Where a 32x32 dynamic sprite is drawn to be unseen: below the 224 visible lines. OBJ_HIDE_Y (240) is for 8 and 16-px sprites: a 32-px one there wraps past line 255 and shows its bottom 16 rows at the top.
#define KIT_ACCEL   0x0038
 Horizontal acceleration per frame in 8.8 fixed-point (0.22 pixels/frame).
#define KIT_ACT_FALL   3
 Kit is falling (downward velocity or walked off an edge).
#define KIT_ACT_JUMP   2
 Kit is jumping (upward velocity, ascending phase).
#define KIT_ACT_STAND   0
 Kit is standing on solid ground, idle.
#define KIT_ACT_WALK   1
 Kit is walking (horizontal velocity nonzero, on ground).
#define KIT_BOUNCE   0x0300
 The bounce off a stomped machine.
#define KIT_DRAW_DX   (-8)
 Where the 32x32 sprite is drawn relative to the 16x16 collision box: Kit's feet are on row 30 of his frame, the box's bottom is row 16.
#define KIT_DRAW_DY   (-14)
 Vertical offset of the frame from the box (see KIT_DRAW_DX).
#define KIT_HIJUMPING   0x0594
 High jump initial upward velocity (when holding UP during jump).
#define KIT_JUMPING   0x0394
 Normal jump initial upward velocity in 8.8 fixed-point.
#define KIT_MAXACCEL   0x0140
 Maximum horizontal velocity in 8.8 fixed-point (1.25 pixels/frame).
#define OAM_SHARD   32
 The first OAM entry of the shards: the dynamic sprites take 0-6.
#define SAFE_FRAMES   90
 Frames Kit cannot be hurt after coming back.
#define SHARD_TILE   0xC0
 OBJ tile number of the shard's first frame.
#define START_LIVES   3
 Lives at the start.
#define T_EMPTY   0x0000
 Tile property: passable (air, background decoration).
#define T_SOLID   0xFF00
 Tile property: solid (ground, walls, platforms).
#define VRAM_BG2_MAP   0x7000
 VRAM word address for BG2 tilemap (SC_64x32).
#define VRAM_BG2_TILES   0x1000
 VRAM word address for BG2 (the skyline) tiles, at most 256.
#define VRAM_BG3_MAP   0x7800
 VRAM word address for the BG3 tilemap, the HUD (SC_32x32).
#define VRAM_BG3_TILES   0x6000
 VRAM word address for the BG3 font (2bpp, 96 glyphs).
#define VRAM_BG_MAP   0x6800
 VRAM word address for BG1 tilemap (SC_64x32).
#define VRAM_BG_TILES   0x2000
 VRAM word address for BG1 tile character data (831 tiles, to $53F0).
#define VRAM_SHARD   0x0C00
 The shard's frames: tile $C0 of the first OBJ table, where the engine's slots 12-15 would go (it never uses more than seven).
#define VRAM_SPR_LARGE   0x0000
 VRAM word address for large (32x32) sprite tiles.
#define VRAM_SPR_SMALL   0x1000
 VRAM word address for small sprite tiles: the engine's fixed second pool; this game draws no small dynamic sprite, so BG2 uses the room.

Functions

static s16 asr8 (s16 val)
 Arithmetic right shift by 8 bits (extract integer part of 8.8 fixed-point).
static void banner (const char *line1, u8 x1, const char *line2, u8 x2)
 Two centred lines over the level (title, game over, cleared), the HUD kept.
static u8 boxes_touch (s16 ax, s16 ay, s16 bx, s16 by, s16 inset)
 Do two 16x16 boxes, shrunk by inset on each side, overlap?
static void come_back (void)
 After a life lost: Kit at the last checkpoint he passed, safe for a while.
static void enemies_reset (void)
 Put the machines back where the level places them, alive.
static void enemies_update (u8 move, u8 meet)
 Move the machines (when move), meet Kit (when meet), and draw them.
static void hud_draw (void)
 The top line: score, shards, lives. The score is kept in tens.
static void kit_animate (void)
 Update Kit's sprite animation frame based on current action.
static void kit_apply_physics (void)
 Apply gravity and integrate velocity into position.
static u8 kit_clamp_and_transition (void)
 Clamp Kit to the world and handle action state transitions.
static void kit_collide_horizontal (void)
 Check horizontal tile collisions (wall sliding).
static void kit_collide_vertical (void)
 Check vertical tile collisions (ground landing and ceiling bonk).
static void kit_handle_input (void)
 Process D-pad and button input for Kit's movement.
static void kit_place (s16 x, s16 y)
 Put Kit at a point, at rest, and the camera on him.
static void kit_update_camera (void)
 Update camera to follow Kit and position the sprite on screen.
static void lose_life (void)
 Kit loses a life: the world waits, then he comes back or the game is over.
int main (void)
 Main entry point – initialization and the game loop.
static u16 map_get_tile_prop (s16 px, s16 py)
 Look up the collision property of the tile at pixel coordinates.
static void map_load (void)
 Load the map from ROM and populate the initial VRAM tilemap.
static void map_prepare_column (u16 map_col, u16 vram_col)
 Buffer a map column for deferred VRAM write (prepare phase).
static void map_redraw (void)
 Write the columns the camera shows, in forced blank, after a jump of the camera.
static void map_update (void)
 Check if the camera has scrolled to a new tile column and prepare it.
static void play_frame (void)
 One frame of the run: Kit, the checkpoints, the goal.
static void put_num (u8 x, u8 y, u16 value, u8 digits)
 Print value with digits digits, zeros in front, at (x, y).
static void run_reset (void)
 A new run: score, lives, shards, machines, Kit on the first roof.
static void shards_update (u8 take)
 Draw the shards on screen, and take the ones Kit touches.
static void to_title (void)
 Back to the title screen: a new run under the title's two lines.
static void write_vram_column (u16 map_col, u16 vram_col)
 Write a full tile column to VRAM via direct register writes.

Variables

static const s8 bob [16] = { 0, 1, 2, 3, 4, 3, 2, 1, 0, -1, -2, -3, -4, -3, -2, -1 }
 The drone's bob, 16 steps of a slow wave, in pixels.
static s16 cam_max_x
static s16 camera_x
static AnimClip clip_fall = { clip_fall_frames, 0, 1, 1, 0 , 0 }
static u16 clip_fall_frames [] = { 25 }
static AnimClip clip_jump = { clip_jump_frames, 0, 1, 1, 0 , 0 }
static u16 clip_jump_frames [] = { 23 }
static AnimClip clip_stand = { clip_stand_frames, 0, 4, 12, 0 , 0 }
static u16 clip_stand_frames [] = { 0 , 0 + 1, 0 + 2, 0 + 3 }
static AnimClip clip_walk = { clip_walk_frames, 0, 8, 5, 0 , 0 }
static u16 clip_walk_frames []
static u16 col_buffer [32]
 Column tile buffer for deferred VRAM streaming.
static u8 cp_index
 The last checkpoint Kit stood beyond (index into checkpoint_pos).
static s8 en_dir [ENEMY_COUNT]
static u8 en_frame [ENEMY_COUNT]
static u8 en_state [ENEMY_COUNT]
static u8 en_timer [ENEMY_COUNT]
static s16 en_x [ENEMY_COUNT]
 Each machine: position (its 16x16 box), direction, state, timer, frame.
static s16 en_y [ENEMY_COUNT]
u8 game_state
 Probe oracles: the state (GS_*), the score in tens, the lives, the shards taken.
static u8 kit_action
static AnimPlayer kit_anim = {0, 0, 0, 0, 0}
static u8 kit_safe
static s16 kit_x
static u8 kit_xfrac
static s16 kit_xvel
static s16 kit_y
static u8 kit_yfrac
static s16 kit_yvel
static s16 last_tile_x
u8 lives
static const u16 * map_data
static u16 map_height
static s16 map_max_x
static const u16 * map_row_ptrs [32]
 Precomputed row pointers into map_data for fast tile lookup.
static u16 map_width
const u8 maplevel []
 The level's tiles, the skyline of BG2 and Kit's frames (32x32, 4bpp, for the dynamic sprite engine).
u16 score
static u8 screen_off
 Set when the screen was turned off for a camera jump: the loop turns it on after the next VBlank, once the NMI has sent the sprites' tiles.
static u8 sfx_clear
static u8 sfx_hurt
static u8 sfx_jump
 SPC700 sound effect slots.
static u8 sfx_shard
static u8 sfx_stomp
static u8 shard_taken [SHARD_COUNT]
 1 for each shard picked up this run
u8 shards_got
static u16 state_timer
 Frames since the state began (blink, waits).
static const u16 * tile_props
const u8 tilesetatt []
 Tile attribute table (T_SOLID/T_EMPTY per tile index, b16 format).

Detailed Description

Kit's Rooftop Run — a side-scrolling platformer, title to game over.

A complete platformer on the rooftops of the city at night: Kit, the fox-kit courier, runs from roof to roof to the radio beacon at the end of the level, picking up data shards and stomping the security machines on the way. Three lives; a fall between two roofs or a machine touched from the side costs one, and Kit comes back at the start of the last roof he reached. The game goes title → play → roof cleared or game over → title.

It shows the core techniques of a scrolling SNES game: tile-based map streaming, subpixel physics, tile collision, camera tracking, animated sprites, a parallax layer, a text HUD in front of everything, and SPC700 music and sound effects through SNESMOD.

The map engine streams one tile column per frame using a prepare/flush pattern: during active display, the next column is read from ROM into a RAM buffer (map_prepare_column) and noted in the VRAM queue; during VBlank, vramQueueFlush() sends it with one 64-byte DMA. This keeps VRAM writes within the VBlank budget. The column at +32 tiles ahead is streamed to cover the 33rd partial tile at the screen edge.

Kit and the machines are dynamic sprites: the engine gives each one drawn a 32x32 slot of VRAM in the order they are drawn, so all of them are drawn every frame, in the same order (a machine off screen or stomped is drawn hidden), and each one keeps its slot. The shards are plain 16x16 sprites whose eight frames sit once in VRAM, in the tiles the engine's last slots would use (it uses seven).

This began as the PVSnesLib likemario example; the level, the roofs, the skyline, Kit, the machines and the shards are ours (devtools/art_assets/build.py writes the level and its objects from the pictures in assets/neo-lumen/).

SNES Concepts
  • Scroll streaming: 1 column/frame prepare-during-display, DMA-in-VBlank
  • SC_64x32 tilemap with 64-column wraparound for seamless scrolling
  • Subpixel 8.8 fixed-point physics (acceleration, gravity, max velocity)
  • Tile collision via property lookup table (tilesetatt)
  • Dynamic sprites in stable slots, beside static sprites in the same OAM
  • Box collision between sprites, from above (a stomp) or from the side
  • A BG3 text HUD in front of BG1 and BG2 (BG3_MODE1_PRIORITY_HIGH)
  • A second layer scrolled at a quarter of the camera's speed (parallax)
  • SNESMOD: SPC700 module playback and five sound effects
  • A game state machine: title, play, a life lost, game over, cleared
What to Observe
  • START on the title screen starts the run
  • Kit walks left/right with acceleration and deceleration; A jumps (hold UP for a higher jump)
  • Shards spin; walking or jumping through one counts it, a chime plays
  • Landing on a robot insect or a drone flattens it and bounces Kit; touching one from the side, or falling, costs a life
  • Reaching the beacon clears the roof: the shards left give a bonus
Modules Used
console, sprite, sprite_dynamic, sprite_lut, dma, input, background, anim, vramqueue, text
See also
background.h, sprite.h, input.h, dma.h, snesmod.h, text.h

Macro Definition Documentation

◆ DOWN_FRAMES

#define DOWN_FRAMES   60

Frames the world waits after a life lost.

◆ EN_ALIVE

#define EN_ALIVE   0

A machine patrols; stomped, it lies flat for a while; then it is gone.

◆ EN_FLAT

#define EN_FLAT   1

See EN_ALIVE.

◆ EN_GONE

#define EN_GONE   2

See EN_ALIVE.

◆ ENEMY_DRONE

#define ENEMY_DRONE   1

A drone flies over the roofs.

◆ ENEMY_INSECT

#define ENEMY_INSECT   0

Machine types of enemy_def[][0].

◆ FRAME_DRONE

#define FRAME_DRONE   4

First frame of the drone's hover.

◆ FRAME_FALL

#define FRAME_FALL   25

Jump pose while coming down.

◆ FRAME_IDLE

#define FRAME_IDLE   0

Kit's sheet (res/kit.png, four 32x32 frames a row): idle 0-3, walk 4-11, run 12-19, jump 20-27 (crouch, rise, top, fall, land).

◆ FRAME_INSECT

#define FRAME_INSECT   0

The machines' sheet (res/enemies.png): the insect's walk 0-3, the drone's hover 4-7, the insect stomped 8, the drone stomped 9.

◆ FRAME_RISE

#define FRAME_RISE   23

Jump pose while going up.

◆ FRAME_SQUASH

#define FRAME_SQUASH   8

The stomped insect, then the stomped drone.

◆ FRAME_WALK

#define FRAME_WALK   4

First frame of the walk cycle.

◆ GRAVITY

#define GRAVITY   48

Gravity acceleration in 8.8 fixed-point units per frame.

Applied to kit_yvel every frame when airborne (jumping or falling). 48/256 = 0.1875 pixels/frame^2 – gives a natural parabolic arc.

◆ GS_CLEAR

#define GS_CLEAR   4

The beacon reached.

◆ GS_DOWN

#define GS_DOWN   2

A life lost: Kit blinks, the world waits.

◆ GS_OVER

#define GS_OVER   3

No life left.

◆ GS_PLAY

#define GS_PLAY   1

Running.

◆ GS_TITLE

#define GS_TITLE   0

The game's states.

◆ HIDE_Y_32

#define HIDE_Y_32   224

Where a 32x32 dynamic sprite is drawn to be unseen: below the 224 visible lines. OBJ_HIDE_Y (240) is for 8 and 16-px sprites: a 32-px one there wraps past line 255 and shows its bottom 16 rows at the top.

◆ KIT_ACCEL

#define KIT_ACCEL   0x0038

Horizontal acceleration per frame in 8.8 fixed-point (0.22 pixels/frame).

◆ KIT_ACT_FALL

#define KIT_ACT_FALL   3

Kit is falling (downward velocity or walked off an edge).

◆ KIT_ACT_JUMP

#define KIT_ACT_JUMP   2

Kit is jumping (upward velocity, ascending phase).

◆ KIT_ACT_STAND

#define KIT_ACT_STAND   0

Kit is standing on solid ground, idle.

◆ KIT_ACT_WALK

#define KIT_ACT_WALK   1

Kit is walking (horizontal velocity nonzero, on ground).

◆ KIT_BOUNCE

#define KIT_BOUNCE   0x0300

The bounce off a stomped machine.

◆ KIT_DRAW_DX

#define KIT_DRAW_DX   (-8)

Where the 32x32 sprite is drawn relative to the 16x16 collision box: Kit's feet are on row 30 of his frame, the box's bottom is row 16.

◆ KIT_DRAW_DY

#define KIT_DRAW_DY   (-14)

Vertical offset of the frame from the box (see KIT_DRAW_DX).

◆ KIT_HIJUMPING

#define KIT_HIJUMPING   0x0594

High jump initial upward velocity (when holding UP during jump).

◆ KIT_JUMPING

#define KIT_JUMPING   0x0394

Normal jump initial upward velocity in 8.8 fixed-point.

◆ KIT_MAXACCEL

#define KIT_MAXACCEL   0x0140

Maximum horizontal velocity in 8.8 fixed-point (1.25 pixels/frame).

◆ OAM_SHARD

#define OAM_SHARD   32

The first OAM entry of the shards: the dynamic sprites take 0-6.

◆ SAFE_FRAMES

#define SAFE_FRAMES   90

Frames Kit cannot be hurt after coming back.

◆ SHARD_TILE

#define SHARD_TILE   0xC0

OBJ tile number of the shard's first frame.

◆ START_LIVES

#define START_LIVES   3

Lives at the start.

◆ T_EMPTY

#define T_EMPTY   0x0000

Tile property: passable (air, background decoration).

◆ T_SOLID

#define T_SOLID   0xFF00

Tile property: solid (ground, walls, platforms).

◆ VRAM_BG2_MAP

#define VRAM_BG2_MAP   0x7000

VRAM word address for BG2 tilemap (SC_64x32).

◆ VRAM_BG2_TILES

#define VRAM_BG2_TILES   0x1000

VRAM word address for BG2 (the skyline) tiles, at most 256.

◆ VRAM_BG3_MAP

#define VRAM_BG3_MAP   0x7800

VRAM word address for the BG3 tilemap, the HUD (SC_32x32).

◆ VRAM_BG3_TILES

#define VRAM_BG3_TILES   0x6000

VRAM word address for the BG3 font (2bpp, 96 glyphs).

◆ VRAM_BG_MAP

#define VRAM_BG_MAP   0x6800

VRAM word address for BG1 tilemap (SC_64x32).

◆ VRAM_BG_TILES

#define VRAM_BG_TILES   0x2000

VRAM word address for BG1 tile character data (831 tiles, to $53F0).

◆ VRAM_SHARD

#define VRAM_SHARD   0x0C00

The shard's frames: tile $C0 of the first OBJ table, where the engine's slots 12-15 would go (it never uses more than seven).

◆ VRAM_SPR_LARGE

#define VRAM_SPR_LARGE   0x0000

VRAM word address for large (32x32) sprite tiles.

◆ VRAM_SPR_SMALL

#define VRAM_SPR_SMALL   0x1000

VRAM word address for small sprite tiles: the engine's fixed second pool; this game draws no small dynamic sprite, so BG2 uses the room.

Function Documentation

◆ asr8()

s16 asr8 ( s16 val)
static

Arithmetic right shift by 8 bits (extract integer part of 8.8 fixed-point).

cc65816's >> on a signed value IS arithmetic (compiler check test_signed_ops.c), so val >> 8 would do; this helper spells the sign handling out:

  • Positive values: simple unsigned shift
  • Negative values: bitwise NOT, unsigned shift, NOT back – preserves the sign bit through the inversion trick.
Parameters
val8.8 fixed-point signed value
Returns
Integer part (effectively val / 256 with sign preservation)

◆ banner()

void banner ( const char * line1,
u8 x1,
const char * line2,
u8 x2 )
static

Two centred lines over the level (title, game over, cleared), the HUD kept.

◆ boxes_touch()

u8 boxes_touch ( s16 ax,
s16 ay,
s16 bx,
s16 by,
s16 inset )
static

Do two 16x16 boxes, shrunk by inset on each side, overlap?

◆ come_back()

void come_back ( void )
static

After a life lost: Kit at the last checkpoint he passed, safe for a while.

◆ enemies_reset()

void enemies_reset ( void )
static

Put the machines back where the level places them, alive.

◆ enemies_update()

void enemies_update ( u8 move,
u8 meet )
static

Move the machines (when move), meet Kit (when meet), and draw them.

A machine patrols between its two bounds at half a pixel a frame; a drone also bobs. Kit falling onto one from above flattens it and bounces; any other touch hurts him. Every machine is drawn every frame, hidden when gone or off screen, so each keeps its slot of the dynamic engine; its frame is changed (and uploaded) only while it is seen.

◆ hud_draw()

void hud_draw ( void )
static

The top line: score, shards, lives. The score is kept in tens.

◆ kit_animate()

void kit_animate ( void )
static

Update Kit's sprite animation frame based on current action.

Pure state->clip mapping: select the clip for the current action and tick. The anim module handles cadence, continue-if-same (walking keeps its phase), and the oamrefresh flag (tile re-upload only when the frame actually changes).

◆ kit_apply_physics()

void kit_apply_physics ( void )
static

Apply gravity and integrate velocity into position.

Adds GRAVITY to Y velocity when airborne (capped at terminal velocity). Then integrates both axes: adds velocity to the fractional accumulator, extracts the integer pixel displacement via asr8(), and stores the remaining fraction for sub-pixel precision next frame.

◆ kit_clamp_and_transition()

u8 kit_clamp_and_transition ( void )
static

Clamp Kit to the world and handle action state transitions.

Prevents Kit from leaving the map horizontally or going above the top, and moves WALK->STAND when velocity reaches zero, JUMP->FALL at the apex.

Returns
1 if Kit fell below the screen (between two roofs)

◆ kit_collide_horizontal()

void kit_collide_horizontal ( void )
static

Check horizontal tile collisions (wall sliding).

Probes one point at Kit's vertical midpoint in the direction of movement. If a solid tile is hit, snaps X to the tile boundary and zeroes horizontal velocity. Only checks the direction Kit is actually moving to avoid false positives.

◆ kit_collide_vertical()

void kit_collide_vertical ( void )
static

Check vertical tile collisions (ground landing and ceiling bonk).

Ground check: probes two points at Kit's feet (left+2, right+13). If either hits a solid tile, snaps Y to the tile boundary and resets vertical velocity. Ceiling check: probes one point above Kit's head. Collision detection uses the tile property table, not per-pixel checks.

◆ kit_handle_input()

void kit_handle_input ( void )
static

Process D-pad and button input for Kit's movement.

LEFT/RIGHT apply acceleration (with max velocity clamping and deceleration when released). A triggers a jump if grounded; holding UP during jump gives a higher arc. The sprite's H-flip attribute is set to match the facing direction.

◆ kit_place()

void kit_place ( s16 x,
s16 y )
static

Put Kit at a point, at rest, and the camera on him.

The sprite is the dynamic engine's slot 0; its tile data may sit in any ROM bank, OAM_SET_GFX() records the bank byte of the pointer it is given.

◆ kit_update_camera()

void kit_update_camera ( void )
static

Update camera to follow Kit and position the sprite on screen.

Centers the camera on Kit (offset by half-screen width = 128 pixels), clamped to the map boundaries, and draws Kit through the dynamic sprite engine. While he cannot be hurt, or the world waits after a life lost, he blinks: drawn hidden one frame in four.

◆ lose_life()

void lose_life ( void )
static

Kit loses a life: the world waits, then he comes back or the game is over.

◆ main()

int main ( void )

Main entry point – initialization and the game loop.

Initializes all subsystems during force blank: Mode 1 video with BG3 in front, tiles and palettes by DMA, the dynamic sprite engine, the shard frames, the map, the text HUD and SNESMOD (music + five effects). Then the loop, a strict pipeline:

Active display (CPU-only, no VRAM):

  1. the state's work: the title waits for START, the run moves Kit and meets the shards and the machines, a life lost waits, the end waits for START
  2. Kit's animation and camera, the shards and the machines drawn
  3. map_update() - prepare next column in RAM buffer

VBlank (VRAM writes allowed):

  1. vramQueueFlush() - DMA the column noted (64 bytes)
  2. bgSetScroll() - update hardware scroll registers
  3. the NMI sends the OAM, the HUD when it changed, and the sprite tiles
  4. snesmodProcess() - SPC700 communication (no VBlank restriction)
Returns
0 (never reached – infinite game loop)

◆ map_get_tile_prop()

u16 map_get_tile_prop ( s16 px,
s16 py )
static

Look up the collision property of the tile at pixel coordinates.

Converts pixel coordinates to tile coordinates, reads the tile index from the map via precomputed row pointers, then looks up the tile's property (T_SOLID or T_EMPTY) in the attribute table. Out-of-bounds coordinates above or left return T_SOLID (wall); below or right return T_EMPTY (open air, allowing fall-off-screen respawn).

Parameters
pxWorld X position in pixels
pyWorld Y position in pixels
Returns
T_SOLID (0xFF00) or T_EMPTY (0x0000)

◆ map_load()

void map_load ( void )
static

Load the map from ROM and populate the initial VRAM tilemap.

Parses the map header (width, height in pixels), builds the row pointer table for O(1) tile lookups, and writes all 64 columns to VRAM. Must be called during force blank since it performs direct VRAM writes.

◆ map_prepare_column()

void map_prepare_column ( u16 map_col,
u16 vram_col )
static

Buffer a map column for deferred VRAM write (prepare phase).

Called during active display (not VBlank). Reads tile indices from ROM map data into the col_buffer[] RAM array and notes the transfer in the VRAM queue (a column: the address steps by 32 words). No VRAM write occurs here – vramQueueFlush() sends it during the next VBlank.

Parameters
map_colSource column index in the map data
vram_colTarget column in the SC_64x32 tilemap (0-63)

◆ map_redraw()

void map_redraw ( void )
static

Write the columns the camera shows, in forced blank, after a jump of the camera.

The streaming of map_update() follows a camera that moves a few pixels a frame; a camera that jumps (Kit back at a checkpoint, the run started again) needs the 33 columns it now shows written at once. The caller turns the screen off around it.

◆ map_update()

void map_update ( void )
static

Check if the camera has scrolled to a new tile column and prepare it.

Compares the current camera tile position to last_tile_x. If the camera moved right, prepares the column at +32 (one tile ahead of the visible right edge, covering the 33rd partial tile). If moved left, prepares the newly revealed left column. At most one column is prepared per frame.

◆ play_frame()

void play_frame ( void )
static

One frame of the run: Kit, the checkpoints, the goal.

◆ put_num()

void put_num ( u8 x,
u8 y,
u16 value,
u8 digits )
static

Print value with digits digits, zeros in front, at (x, y).

◆ run_reset()

void run_reset ( void )
static

A new run: score, lives, shards, machines, Kit on the first roof.

Called in forced blank: the camera jumps back to the start of the map.

◆ shards_update()

void shards_update ( u8 take)
static

Draw the shards on screen, and take the ones Kit touches.

Each shard is its own OAM entry (OAM_SHARD + i), a plain 16x16 sprite on the frame tiles loaded once; one off screen or taken is hidden.

◆ to_title()

void to_title ( void )
static

Back to the title screen: a new run under the title's two lines.

◆ write_vram_column()

void write_vram_column ( u16 map_col,
u16 vram_col )
static

Write a full tile column to VRAM via direct register writes.

Used during force blank (initial map load) when DMA is not needed because there is no time constraint. Sets VMAIN to vertical increment mode (bit 0 set = increment address by 32 after each write), which writes one tile per row down a column.

Parameters
map_colSource column index in the map data
vram_colDestination column in the SC_64x32 tilemap (0-63)

Variable Documentation

◆ bob

const s8 bob[16] = { 0, 1, 2, 3, 4, 3, 2, 1, 0, -1, -2, -3, -4, -3, -2, -1 }
static

The drone's bob, 16 steps of a slow wave, in pixels.

◆ cam_max_x

s16 cam_max_x
static

Maximum camera X offset (map_width*8 - 256)

◆ camera_x

s16 camera_x
static

Current camera X scroll offset in pixels

◆ clip_fall

AnimClip clip_fall = { clip_fall_frames, 0, 1, 1, 0 , 0 }
static

◆ clip_fall_frames

u16 clip_fall_frames[] = { 25 }
static

◆ clip_jump

AnimClip clip_jump = { clip_jump_frames, 0, 1, 1, 0 , 0 }
static

◆ clip_jump_frames

u16 clip_jump_frames[] = { 23 }
static

◆ clip_stand

AnimClip clip_stand = { clip_stand_frames, 0, 4, 12, 0 , 0 }
static

◆ clip_stand_frames

u16 clip_stand_frames[] = { 0 , 0 + 1, 0 + 2, 0 + 3 }
static

◆ clip_walk

AnimClip clip_walk = { clip_walk_frames, 0, 8, 5, 0 , 0 }
static

◆ clip_walk_frames

u16 clip_walk_frames[]
static
Initial value:
= { 4 , 4 + 1, 4 + 2, 4 + 3,
4 + 4, 4 + 5, 4 + 6, 4 + 7 }

◆ col_buffer

u16 col_buffer[32]
static

Column tile buffer for deferred VRAM streaming.

During active display, map_prepare_column() fills this 32-entry buffer from ROM map data (RAM-only writes) and notes it in the VRAM queue; vramQueueFlush() sends the 64 bytes in VBlank.

◆ cp_index

u8 cp_index
static

The last checkpoint Kit stood beyond (index into checkpoint_pos).

◆ en_dir

s8 en_dir[ENEMY_COUNT]
static

◆ en_frame

u8 en_frame[ENEMY_COUNT]
static

◆ en_state

u8 en_state[ENEMY_COUNT]
static

◆ en_timer

u8 en_timer[ENEMY_COUNT]
static

◆ en_x

s16 en_x[ENEMY_COUNT]
static

Each machine: position (its 16x16 box), direction, state, timer, frame.

◆ en_y

s16 en_y[ENEMY_COUNT]
static

◆ game_state

u8 game_state

Probe oracles: the state (GS_*), the score in tens, the lives, the shards taken.

◆ kit_action

u8 kit_action
static

Current action state (KIT_ACT_STAND/WALK/JUMP/FALL)

◆ kit_anim

AnimPlayer kit_anim = {0, 0, 0, 0, 0}
static

Kit's animation state

◆ kit_safe

u8 kit_safe
static

Frames left during which Kit cannot be hurt

◆ kit_x

s16 kit_x
static

Kit world X position in pixels

◆ kit_xfrac

u8 kit_xfrac
static

Kit X sub-pixel fraction (8.8 fixed-point low byte)

◆ kit_xvel

s16 kit_xvel
static

Kit X velocity in 8.8 fixed-point

◆ kit_y

s16 kit_y
static

Kit world Y position in pixels

◆ kit_yfrac

u8 kit_yfrac
static

Kit Y sub-pixel fraction (8.8 fixed-point low byte)

◆ kit_yvel

s16 kit_yvel
static

Kit Y velocity in 8.8 fixed-point (negative = upward)

◆ last_tile_x

s16 last_tile_x
static

Last tile column for which streaming was performed

◆ lives

u8 lives

◆ map_data

const u16* map_data
static

Pointer to the tile index array within maplevel

◆ map_height

u16 map_height
static

Map height in tiles (parsed from map header)

◆ map_max_x

s16 map_max_x
static

Rightmost valid Kit X position (map_width*8 - 16)

◆ map_row_ptrs

const u16* map_row_ptrs[32]
static

Precomputed row pointers into map_data for fast tile lookup.

Avoids an expensive multiply (row * map_width) on every tile access. map_row_ptrs[row] points directly to the start of that row's tile data. Maximum 32 rows for SC_64x32 tilemap height.

◆ map_width

u16 map_width
static

Map width in tiles (parsed from map header)

◆ maplevel

const u8 maplevel[]
extern

The level's tiles, the skyline of BG2 and Kit's frames (32x32, 4bpp, for the dynamic sprite engine).

The machines' frames (32x32) and the shard's eight frames (16x16)

Where the shards, the machines and the checkpoints are, and the goal

Map data (tile indices + header with width/height)

◆ score

u16 score

◆ screen_off

u8 screen_off
static

Set when the screen was turned off for a camera jump: the loop turns it on after the next VBlank, once the NMI has sent the sprites' tiles.

◆ sfx_clear

u8 sfx_clear
static

◆ sfx_hurt

u8 sfx_hurt
static

◆ sfx_jump

u8 sfx_jump
static

SPC700 sound effect slots.

◆ sfx_shard

u8 sfx_shard
static

◆ sfx_stomp

u8 sfx_stomp
static

◆ shard_taken

u8 shard_taken[SHARD_COUNT]
static

1 for each shard picked up this run

◆ shards_got

u8 shards_got

◆ state_timer

u16 state_timer
static

Frames since the state began (blink, waits).

◆ tile_props

const u16* tile_props
static

Pointer to the tile property table (T_SOLID/T_EMPTY)

◆ tilesetatt

const u8 tilesetatt[]
extern

Tile attribute table (T_SOLID/T_EMPTY per tile index, b16 format).