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

Mode 7 racing — hover-bikes on a rooftop circuit, F-Zero style. More...

#include <snes.h>
#include <snes/mode7.h>
#include <snes/math.h>
#include <snes/input.h>
#include "res/track.inc"
#include "res/bikes.inc"

Macros

#define ACCEL   6
 Physics tuning (positions in 12.4 fixed, speeds in 8.8).
#define BOOST_SPEED   0x0500 /* a pad: 5.0 px/frame, then back to MAX_SPEED */
#define BRAKE   12
#define CAR_SCREEN_X   128
 Screen position of the bike's centre (the rotation pivot).
#define CAR_SCREEN_Y   168
#define DRAG   2
#define FRAME_TILE(f)
 Tiles of the bike sheet: 32x32 frames four to a row, 16 tiles a row.
#define KIT_FRAME   0
 Kit's frame, then each rival's five turned frames (0 to 90 degrees).
#define MAX_GRASS   0x00C0 /* gravel cap: 0.75 px/frame */
#define MAX_SPEED   0x0300 /* 3.0 px/frame */
#define RIVAL_FRAME(r)
#define STEER_STEP   2

Functions

static void draw_rival (u8 r)
 Draw rival r and its shadow where its lane puts it.
int main (void)
static u8 to_screen (s16 wx, s16 wy, s16 *sx, s16 *sy)
 Put a world point (pixels) on the screen, through the camera.

Variables

u8 car_heading
u16 car_speed
u8 car_surface
u16 car_x
 Probe oracles / game state (12.4 fixed-point pixels).
u16 car_y
const u8 lanes_heading []
 The heading at each point of the two lanes (64 = east).
const u16 lanes_xy []
 The rivals' lanes (data.asm): 2 lanes x 256 points of (x, y).
u8 race_on
 Set once Kit moves: the rivals wait for him on the line.
u16 rival_pos [2]
 Where each rival is on its lane: point number in 8.8 (256 points a lap).
static const u16 rival_speed [2] = { 0x0050, 0x0056 }
 Each rival's speed along its lane, points per frame in 8.8.
const u8 track_class []
 Class of each track tile, 128x128 (data.asm, res/track_class.bin): 0 road, 1 gravel, 2 water or a prop, 3 boost pad.

Detailed Description

Mode 7 racing — hover-bikes on a rooftop circuit, F-Zero style.

A drivable race circuit on the Mode 7 plane. Kit's hover-bike stays fixed near the bottom of the screen and the WORLD rotates and scrolls under it — the classic SNES racing camera: per frame, mode7SetAngle(heading) + mode7SetScroll/SetCenter follow the bike's fixed-point position, through the mode7 module.

  • D-pad left/right: steer — A: accelerate — B: brake
  • The gravel slows the bike down; the lagoon, the quay and the rooftop props stop it; the chevron pads throw it forward.
  • Two rivals leave the line when Kit does, and ride their lanes.

The circuit, its 128x128 class map (one byte per tile), the rivals' lanes and the bike sheet are written by devtools/art_assets/build.py (racing.py) from the PixelLab art of assets/neo-lumen/; the build converts the circuit with opensnes-tileset (mode = 7) and the bikes with opensnes-sprite.

ROM mode: LoROM (project default).

SNES Concepts
  • The F-Zero camera: SetCenter(bike) makes the bike the rotation pivot, SetScroll places it low on screen, SetAngle spins the world opposite to the bike's heading
  • Sprites standing on a turning Mode 7 plane: a rival's world position goes through the inverse of the plane's matrix to reach the screen, and its picture turns with the camera (five frames and the flips)
  • Fixed-point driving physics from the math module: velocity = fixMul(speed, fixSin/fixCos(heading)) — same 0-255 angle unit as mode7SetAngle, so heading feeds both directly
  • Mode 7 VRAM interleave loaded with dmaCopyVramMode7(); OBJ tiles coexist at name base 3 (word $6000)
What to Observe
Boot on the start line, a rival on each side. Hold A: the checker line recedes, the rivals pull away and the track streams under the bike; steer through the chicane. A rival seen side-on turns its bike with the camera. Cutting the gravel slows you; a chevron pad gives a burst of speed; the quay is solid.
Modules Used
console, dma, background, sprite, math, input, mode7
See also
devtools/art_assets/racing.py — how the circuit and the bikes are made
lib/source/mode7.c — the C Mode 7 module this game exercises

Macro Definition Documentation

◆ ACCEL

#define ACCEL   6

Physics tuning (positions in 12.4 fixed, speeds in 8.8).

◆ BOOST_SPEED

#define BOOST_SPEED   0x0500 /* a pad: 5.0 px/frame, then back to MAX_SPEED */

◆ BRAKE

#define BRAKE   12

◆ CAR_SCREEN_X

#define CAR_SCREEN_X   128

Screen position of the bike's centre (the rotation pivot).

◆ CAR_SCREEN_Y

#define CAR_SCREEN_Y   168

◆ DRAG

#define DRAG   2

◆ FRAME_TILE

#define FRAME_TILE ( f)
Value:
(((f) >> 2) * 64 + ((f) & 3) * 4)

Tiles of the bike sheet: 32x32 frames four to a row, 16 tiles a row.

◆ KIT_FRAME

#define KIT_FRAME   0

Kit's frame, then each rival's five turned frames (0 to 90 degrees).

◆ MAX_GRASS

#define MAX_GRASS   0x00C0 /* gravel cap: 0.75 px/frame */

◆ MAX_SPEED

#define MAX_SPEED   0x0300 /* 3.0 px/frame */

◆ RIVAL_FRAME

#define RIVAL_FRAME ( r)
Value:
(1 + (r) * 5)

◆ STEER_STEP

#define STEER_STEP   2

Function Documentation

◆ draw_rival()

void draw_rival ( u8 r)
static

Draw rival r and its shadow where its lane puts it.

Its picture turns with the camera: the direction it faces on screen is its heading minus Kit's, in 16 steps of 22.5 degrees. Five frames turn the bike from up (0) to right (4); the flips give the rest — V turns a direction d into 8 - d, H into 16 - d, both into d + 8.

◆ main()

int main ( void )

◆ to_screen()

u8 to_screen ( s16 wx,
s16 wy,
s16 * sx,
s16 * sy )
static

Put a world point (pixels) on the screen, through the camera.

The plane shows world = car + M * (screen - pivot), M = the matrix mode7SetAngle() writes at scale 0x0080 (cos, -sin / sin, cos, halved); this is the inverse, M^-1 = 2 * (cos, sin / -sin, cos).

Returns
0 if the point is too far from the bike to be on screen

Variable Documentation

◆ car_heading

u8 car_heading

◆ car_speed

u16 car_speed

◆ car_surface

u8 car_surface

◆ car_x

u16 car_x

Probe oracles / game state (12.4 fixed-point pixels).

◆ car_y

u16 car_y

◆ lanes_heading

const u8 lanes_heading[]
extern

The heading at each point of the two lanes (64 = east).

◆ lanes_xy

const u16 lanes_xy[]
extern

The rivals' lanes (data.asm): 2 lanes x 256 points of (x, y).

◆ race_on

u8 race_on

Set once Kit moves: the rivals wait for him on the line.

◆ rival_pos

u16 rival_pos[2]

Where each rival is on its lane: point number in 8.8 (256 points a lap).

◆ rival_speed

const u16 rival_speed[2] = { 0x0050, 0x0056 }
static

Each rival's speed along its lane, points per frame in 8.8.

◆ track_class

const u8 track_class[]
extern

Class of each track tile, 128x128 (data.asm, res/track_class.bin): 0 road, 1 gravel, 2 water or a prop, 3 boost pad.