Super FX game skeleton — a game loop at 60 fps while the GSU renders. More...
#include <snes.h>#include <snes/superfx.h>#include <snes/snesmod.h>#include "soundbank.h"#include "res/square.inc"#include "res/sight.inc"Macros | |
| #define | COL_BEAM 12 |
| #define | COL_INNER 14 |
| #define | COL_OUTER 15 |
| Colour of each group of edges, an index in BG palette 0. | |
| #define | COL_STRUT 13 |
| #define | EDGE_TABLE ((u8 FAR *)0x708040) |
| The edge table: 34 x (point, point, colour). | |
| #define | HOLO_X 128 |
| Where the hologram turns, in the 256x128 bitmap: above the projector of the square, whose top is at bitmap line 96 (screen line 144: the bitmap starts at screen line 48). | |
| #define | HOLO_Y 48 |
| #define | LETTERBOX_BOTTOM 40 |
| Bottom band: at least one line keeps force blank across line 0. | |
| #define | LETTERBOX_TOP 40 |
| Letterbox bands (lines): the NMI moves frames in VBlank + top band. | |
| #define | NUM_EDGES 34 |
| Number of edges the GSU draws: 32 of the hologram, 2 beams. | |
| #define | NUM_TURNED 8 |
| Number of vertices that turn (the outer cube); the inner cube's are points 8-15. | |
| #define | POINT_X ((u8 FAR *)0x708000) |
| Game Pak RAM past the two framebuffers, as the CPU sees it (bank $70): the points' x, their y, the edge table. The renderer reads the same three at $8000, $8020 and $8040 (gsu_cube.sfx). The CPU writes them only while it owns the RAM: before the first job, or after gsuWait(). The pointers are FAR: a plain pointer reads and writes bank $00, and an indexed store through ((u8 *)0x708040)[i] would land in ROM at $00:8040. | |
| #define | POINT_Y ((u8 FAR *)0x708020) |
| The points' y. | |
| #define | PT_CENTRE 18 |
| Point 18: the hologram's centre, where the beams meet. | |
| #define | PT_RIM_L 16 |
| Points 16 and 17: the projector's rim, where the beams start. | |
| #define | PT_RIM_R 17 |
| #define | RIM_Y 96 |
| The projector's rim, where the two beams start: x 108 and 148. | |
| #define | SIGHT_TILE 256 |
| The sight's first tile: the second name table (256 + 0). | |
| #define | VRAM_BG1_A 0x0000 |
| VRAM word addresses. BG1's two char blocks take $0000-$3FFF (16 KB each), the square's 541 tiles $4000-$61CF. BG2's tiles run into the OBJ name base ($6000), so the sight lives in the second name table: tile 256, at $7000. The two 32x32 maps sit above it, clear of the sight's tiles. | |
| #define | VRAM_BG1_B 0x2000 |
| #define | VRAM_BG1_MAP 0x7800 |
| #define | VRAM_BG2 0x4000 |
| #define | VRAM_BG2_MAP 0x7C00 |
| #define | VRAM_OBJ 0x6000 |
| #define | VRAM_SIGHT 0x7000 |
Functions | |
| int | main (void) |
| static void | prepareFrame (u8 angle_y, u8 angle_x, u8 bob) |
| Turn the hologram to the given angles, bob it, write its points. | |
| void | rotateVertex (u16 idx) |
| Rotate one outer vertex, write its projection and the inner cube's matching point (idx + 8) to Game Pak RAM, clamped to the bitmap. | |
| static void | startJob (void) |
| Start the renderer from the code cache; returns at once. | |
| static void | writeEdgeTable (void) |
| Write the edge table and the projector's rim to Game Pak RAM, once: they never move. | |
Variables | |
| static const s16 | cvx [] = {-24, 24, 24,-24,-24, 24, 24,-24} |
| The data cube's vertices, x. The cube inside it is the same at half the size, so only these 8 are turned: the inner vertex i + 8 is the outer vertex i's offset halved, a shift instead of four multiplies. | |
| static const s16 | cvy [] = {-24,-24, 24, 24,-24,-24, 24, 24} |
| The data cube's vertices, y. | |
| static const s16 | cvz [] = {-24,-24,-24,-24, 24, 24, 24, 24} |
| The data cube's vertices, z. | |
| static const u8 | edges [34 *3] |
| The edge table the GSU walks: point, point, colour. Written to Game Pak RAM once; each job then only moves the points. | |
| s16 | g_cax |
| s16 | g_cay |
| s16 | g_holo_y |
| The hologram's centre line this frame (it bobs). | |
| s16 | g_sax |
| s16 | g_say |
| Sines and cosines of the two rotation angles. | |
| const u8 | gsu_cube [] |
| const u8 | gsu_cube_end [] |
| static const u16 | holo_pal [16] |
| BG palette 0: the GSU bitmap colours. 0 is the backdrop, the square's darkest ink (#080810), seen wherever no BG has a pixel. | |
| u16 | r_game_frames |
| Game-loop frames (one per WaitForVBlank). | |
| u16 | r_jobs |
| GSU jobs started. | |
| u16 | r_music_pos |
| The module's pattern position, sampled every frame. | |
| u16 | r_ship_x |
| Sight position (the game's state): its top-left corner. | |
| u16 | r_ship_y |
| static const s8 | sin_tab [256] |
| Sine table, 256 steps per turn, amplitude 127. | |
Super FX game skeleton — a game loop at 60 fps while the GSU renders.
The shape of a Super FX game: the CPU runs the game every frame (here, a sight steered with the D-pad, and music), the GSU renders the 3D view on its own clock (here, a hologram turning above a square of the city at night: a data cube, a smaller cube inside it, the struts between them and the projector's beams), and the NMI moves each finished frame to VRAM. Nothing waits for anything else.
| #define COL_BEAM 12 |
| #define COL_INNER 14 |
| #define COL_OUTER 15 |
Colour of each group of edges, an index in BG palette 0.
| #define COL_STRUT 13 |
| #define HOLO_X 128 |
Where the hologram turns, in the 256x128 bitmap: above the projector of the square, whose top is at bitmap line 96 (screen line 144: the bitmap starts at screen line 48).
| #define HOLO_Y 48 |
| #define LETTERBOX_BOTTOM 40 |
Bottom band: at least one line keeps force blank across line 0.
| #define LETTERBOX_TOP 40 |
Letterbox bands (lines): the NMI moves frames in VBlank + top band.
| #define NUM_EDGES 34 |
Number of edges the GSU draws: 32 of the hologram, 2 beams.
| #define NUM_TURNED 8 |
Number of vertices that turn (the outer cube); the inner cube's are points 8-15.
Game Pak RAM past the two framebuffers, as the CPU sees it (bank $70): the points' x, their y, the edge table. The renderer reads the same three at $8000, $8020 and $8040 (gsu_cube.sfx). The CPU writes them only while it owns the RAM: before the first job, or after gsuWait(). The pointers are FAR: a plain pointer reads and writes bank $00, and an indexed store through ((u8 *)0x708040)[i] would land in ROM at $00:8040.
| #define PT_CENTRE 18 |
Point 18: the hologram's centre, where the beams meet.
| #define PT_RIM_L 16 |
Points 16 and 17: the projector's rim, where the beams start.
| #define PT_RIM_R 17 |
| #define RIM_Y 96 |
The projector's rim, where the two beams start: x 108 and 148.
| #define SIGHT_TILE 256 |
The sight's first tile: the second name table (256 + 0).
| #define VRAM_BG1_A 0x0000 |
VRAM word addresses. BG1's two char blocks take $0000-$3FFF (16 KB each), the square's 541 tiles $4000-$61CF. BG2's tiles run into the OBJ name base ($6000), so the sight lives in the second name table: tile 256, at $7000. The two 32x32 maps sit above it, clear of the sight's tiles.
| #define VRAM_BG1_B 0x2000 |
| #define VRAM_BG1_MAP 0x7800 |
| #define VRAM_BG2 0x4000 |
| #define VRAM_BG2_MAP 0x7C00 |
| #define VRAM_OBJ 0x6000 |
| #define VRAM_SIGHT 0x7000 |
| int main | ( | void | ) |
Turn the hologram to the given angles, bob it, write its points.
| void rotateVertex | ( | u16 | idx | ) |
Rotate one outer vertex, write its projection and the inner cube's matching point (idx + 8) to Game Pak RAM, clamped to the bitmap.
|
static |
Start the renderer from the code cache; returns at once.
|
static |
Write the edge table and the projector's rim to Game Pak RAM, once: they never move.
|
static |
The data cube's vertices, x. The cube inside it is the same at half the size, so only these 8 are turned: the inner vertex i + 8 is the outer vertex i's offset halved, a shift instead of four multiplies.
|
static |
The data cube's vertices, y.
|
static |
The data cube's vertices, z.
|
static |
The edge table the GSU walks: point, point, colour. Written to Game Pak RAM once; each job then only moves the points.
| s16 g_cax |
| s16 g_cay |
| s16 g_holo_y |
The hologram's centre line this frame (it bobs).
| s16 g_sax |
| s16 g_say |
Sines and cosines of the two rotation angles.
|
extern |
| const u8 gsu_cube_end[] |
the cache-resident renderer
|
static |
| u16 r_game_frames |
Game-loop frames (one per WaitForVBlank).
| u16 r_jobs |
GSU jobs started.
| u16 r_music_pos |
The module's pattern position, sampled every frame.
| u16 r_ship_x |
Sight position (the game's state): its top-left corner.
| u16 r_ship_y |
|
static |
Sine table, 256 steps per turn, amplitude 127.