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

Colour — palette cycling: animate without touching a pixel. More...

#include <snes.h>
#include <snes/input.h>
#include "res/sign.inc"

Macros

#define FRAMES_PER_STEP   4
 Frames between rotations — a calm ~15 steps/second.
#define WHEEL_FIRST   8
 The first cycled entry of a palette.
#define WHEEL_LEN   8
 Number of palette entries we cycle in each palette: 8 to 15.

Functions

int main (void)
static void rotate (u16 *w)
 Rotate a wheel down by one, the last entry coming first: each colour moves to the next index, so the light runs the way the indices are laid out (left to right).

Variables

u16 cycle_pos
 Probe oracle: total palette rotations since boot.
u8 cycling
 Probe oracle: 1 while the cycle is running, 0 when frozen.
static u16 flow_wheel [8]
 The coolant: entries 8-15 of BG palette 1, rotated in place (RAM copy).
static u16 sign_wheel [8]
 The sign's letters: entries 8-15 of BG palette 0, rotated in place (RAM copy).

Detailed Description

Colour — palette cycling: animate without touching a pixel.

The oldest trick in the 2D book. The picture on screen never changes: every tile, every tilemap entry, every scroll register is frozen after init. All that moves is CGRAM — every four frames we rotate eight palette entries by one slot, and the light runs along a neon sign while the coolant flows through a pipe. This is how the SNES animates waterfalls, lava, fire, marquee lights and "loading" shimmers for the cost of a 32-byte CGRAM reload, no VRAM traffic at all.

The scene is a noodle bar's sign and a coolant pipe on a brick wall (res/sign.png, drawn by devtools/art_assets/build.py). It was drawn for the cycle: each letter of NOODLES is painted with its own colour index, 8 to 14 of BG palette 0, and the coolant with indices 8 to 15 of BG palette 1, which follow one another along the pipe. The wall, the frame and the pipe's metal use the other indices, and never change. Rotating entries 8-15 of the two palettes sends a wave of light along the letters and makes the coolant run.

ROM mode: LoROM (project default).

SNES Concepts
  • CGRAM is a live lookup table: rewrite an entry and every pixel that indexes it changes colour this frame — no redraw
  • setColor() / dmaCopyCGram() are VBlank-only (CGRAM port is ignored during active display) — the rotation happens right after WaitForVBlank
  • Artwork drawn for cycling: the indices that rotate are kept apart from the ones that do not, and laid out in the order the light runs
  • A seamless colour loop: the 8 entries wrap (entry 15 → entry 8 is continuous) so the cycle never jumps
What to Observe
Boot: the sign's letters light up one after another, and the coolant runs along the pipe. Press START to freeze the cycle — both stop dead, proving nothing on screen actually moved; press again to resume. cycle_pos counts every rotation (probe oracle).
Modules Used
console, dma, background, input
See also
lib/include/snes/video.h — setColor(), RGB()
lib/include/snes/dma.h — dmaCopyCGram()

Macro Definition Documentation

◆ FRAMES_PER_STEP

#define FRAMES_PER_STEP   4

Frames between rotations — a calm ~15 steps/second.

◆ WHEEL_FIRST

#define WHEEL_FIRST   8

The first cycled entry of a palette.

◆ WHEEL_LEN

#define WHEEL_LEN   8

Number of palette entries we cycle in each palette: 8 to 15.

Function Documentation

◆ main()

int main ( void )

◆ rotate()

void rotate ( u16 * w)
static

Rotate a wheel down by one, the last entry coming first: each colour moves to the next index, so the light runs the way the indices are laid out (left to right).

Variable Documentation

◆ cycle_pos

u16 cycle_pos

Probe oracle: total palette rotations since boot.

◆ cycling

u8 cycling

Probe oracle: 1 while the cycle is running, 0 when frozen.

◆ flow_wheel

u16 flow_wheel[8]
static

The coolant: entries 8-15 of BG palette 1, rotated in place (RAM copy).

◆ sign_wheel

u16 sign_wheel[8]
static

The sign's letters: entries 8-15 of BG palette 0, rotated in place (RAM copy).