Aim, distance, and angle showcase for <snes/math.h>. More...
#include <snes.h>#include <snes/text.h>#include <snes/math.h>#include <snes/fixed32.h>#include <snes/gameloop.h>Macros | |
| #define | PLAYER_X 124 |
| Player position — fixed at screen centre. | |
| #define | PLAYER_Y 108 |
Functions | |
| int | main (void) |
| static void | on_init (void) |
| static void | on_update (void) |
Variables | |
| static s16 | prev_target_x |
| Previous target X — for change detection (skip text reformat when nothing moved). | |
| static s16 | prev_target_y |
| Previous target Y — see prev_target_x. | |
| static const u8 | sprite_pal [] |
| Sprite palette — single palette shared by player and target. | |
| static const u8 | sprite_tiles [64] |
| Two 8×8 sprite tiles in SNES 4bpp format (64 bytes total). | |
| static s16 | target_x |
| Target X coordinate (movable, 0–247). | |
| static s16 | target_y |
| Target Y coordinate (movable, 0–215). | |
Aim, distance, and angle showcase for <snes/math.h>.
Demonstrates the four math primitives a 2D action game leans on the most: sqrt16 (pixel distance), atan2_8 (8-bit angle to a target), and the fixCos / fixSin LUTs (project a velocity vector along that angle). Everything updates live as the user moves the target with the D-pad — the numbers on screen are real arithmetic, not pre-baked.
The player ("P") sits at screen center. The target ("X") starts upper-right and follows the D-pad. Each frame the example recomputes dx/dy from the two positions, calls sqrt16(dx² + dy²) for the pixel distance, calls atan2_8(dy, dx) for the 8-bit angle, then calls fixCos and fixSin on that angle to read back the unit direction vector. All five values are printed live in the text panel above the play area.
| #define PLAYER_X 124 |
Player position — fixed at screen centre.
Made into a #define rather than a runtime variable so the compiler folds the constant into the dx/dy expressions and we don't pay a load+sub for a quantity that never changes.
| #define PLAYER_Y 108 |
| int main | ( | void | ) |
|
static |
|
static |
|
static |
Previous target X — for change detection (skip text reformat when nothing moved).
|
static |
Previous target Y — see prev_target_x.
|
static |
Sprite palette — single palette shared by player and target.
Both sprites use palette 0; the visual difference between them is entirely the tile shape (diamond vs. X). Keeping the palette minimal trims four bytes off the CGRAM upload and avoids the second dmaCopyCGram call that an earlier draft used.
BGR555 layout: [R5G5B5] packed into two bytes, low byte first. Only colour 3 is rendered — the tile bitplanes above always resolve to index 3.
|
static |
Two 8×8 sprite tiles in SNES 4bpp format (64 bytes total).
Tile 0 ("P", player): a filled square edged with a dot — easy to spot at the screen centre.
Tile 1 ("X", target): a crosshair / plus sign — points at its own centre so the user knows exactly where the target's coordinate is.
Both tiles use bitplanes 0 and 1; planes 2 and 3 are zero so every drawn pixel is colour index 3 in the chosen palette.
|
static |
Target X coordinate (movable, 0–247).
|
static |
Target Y coordinate (movable, 0–215).