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

Mode 7 rotation and scaling, flown over: a glider, its shadow and the altitude. More...

#include <snes.h>
#include "res/mode7bg.inc"
#include "res/glider.inc"

Macros

#define GLIDER_X   112
 Mode 7 tile pixel data (256 tiles x 64 bytes, 8bpp), converted by the build from res/.
#define GLIDER_Y   96

Functions

int main (void)
 Entry point – interactive Mode 7 rotation and scaling demo.
static void place_shadow (u16 zscale)
 Draw the shadow below the glider, further away the higher it flies.

Variables

u16 shadow_dy
 Probe oracle: the shadow's offset below the glider, in pixels.

Detailed Description

Mode 7 rotation and scaling, flown over: a glider, its shadow and the altitude.

Demonstrates SNES Mode 7, the hardware affine transformation mode that applies a 2x2 matrix (rotation + scaling) to a single 128x128 tile background layer. Mode 7 uses a unique interleaved VRAM format where tilemap bytes occupy the low bytes and 8bpp tile pixel data occupies the high bytes of each VRAM word. The transformation matrix is controlled via registers M7A-M7D, with the library's mode7SetAngle() and mode7SetScale() computing the sin/cos matrix entries from an 8-bit angle and 8.8 fixed-point scale factors.

A glider flies over the plane at the rotation centre, and the zoom is its altitude: zooming out widens the view as if it climbed, and its shadow — the same sprite in a dark palette, behind it — slides further away. The two together are how a flat Mode 7 plane reads as height. (The glider and its shadow were the games/mode7_flying example.)

SNES Concepts
  • Mode 7 affine transformation (rotation + scaling via M7A-M7D)
  • Interleaved VRAM format (tilemap in low bytes, pixels in high bytes)
  • 8.8 fixed-point scale representation
  • 8-bit angle for hardware sin/cos lookup
  • The rotation centre on screen: M7X/M7Y is a point of the plane, the scroll says where it lands (here under the glider, the screen centre)
  • Two depth cues on a flat plane: the scale, and a shadow offset that grows with it
What to Observe
  • Press A to rotate the background clockwise
  • Press B to rotate counter-clockwise
  • Press UP to zoom out (background shrinks, more visible)
  • Press DOWN to zoom in (background magnifies)
  • The plane turns under the glider; its shadow drifts away as you zoom out (climb) and closes in as you zoom in (descend)
Modules Used
console, dma, background, sprite, input, mode7
See also
mode7.h, background.h, input.h

Macro Definition Documentation

◆ GLIDER_X

#define GLIDER_X   112

Mode 7 tile pixel data (256 tiles x 64 bytes, 8bpp), converted by the build from res/.

Mode 7 tilemap (128x128 tile indices, one byte each), converted by the build from res/

256-colour palette for the Mode 7 plane (512 bytes), converted by the build from res/

Kit's glider seen from above, 32x32, converted by the build from res/glider.png

Where the glider is drawn: its 32x32 box centred on screen (128, 112).

◆ GLIDER_Y

#define GLIDER_Y   96

Function Documentation

◆ main()

int main ( void )

Entry point – interactive Mode 7 rotation and scaling demo.

Initializes the Mode 7 plane with an image, then allows the user to rotate (A/B buttons) and zoom (D-pad UP/DOWN) in real time. The transformation matrix is updated each frame via mode7SetAngle() and mode7SetScale(), which compute sin/cos values and write to the M7A-M7D hardware registers.

Returns
Does not return (infinite loop).

< Current joypad button state

< Rotation angle (0-255 maps to 0-360 degrees)

< Zoom level in 8.8 fixed-point (1.0 = 1:1, higher = more zoomed out)

◆ place_shadow()

void place_shadow ( u16 zscale)
static

Draw the shadow below the glider, further away the higher it flies.

Variable Documentation

◆ shadow_dy

u16 shadow_dy

Probe oracle: the shadow's offset below the glider, in pixels.