OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Super Scope
Screenshot

Light gun detection, calibration via PPU H/V counters, and fire tracking with an impact marker sprite — at the calibration target of the city's security range.

Controls

Input Action
Fire (trigger) Calibrate aim / mark hit position
Pause Return to calibration screen
Cursor Advance to ready state

Build & Run

cd $OPENSNES_HOME
make -C examples/input/superscope

Then open superscope.sfc in an emulator with a Super Scope on port 2 (luna drives one from a test manifest).

What You'll Learn

  • How the SNES Super Scope communicates hit position via PPU H/V counters
  • The detect/calibrate/ready state machine pattern for light guns
  • Using OBJSEL() to configure sprite sizes and VRAM base in one readable macro
  • Direct OAM buffer writes for fast single-sprite updates
  • 2bpp background graphics on Mode 0 with 4-color palettes

Walkthrough

1. Initialization

void consoleInit(void)
Initialize SNES hardware.
#define BG_MODE0
Definition video.h:38
void setMode(u8 mode, u8 flags)
Set background mode.

Mode 0 gives four 2bpp BG layers. We use BG1 for text and BG2 for the calibration target image.

2. Text System (BG1)

textLoadFont(0x0000);
bgSetGfxPtr(0, 0x0000);
void bgSetMapPtr
Definition background.h:140
void bgSetGfxPtr
Definition background.h:169
#define BG_MAP_32x32
Definition background.h:30
void textLoadFont(u16 vram_addr)
Load font tiles to VRAM.
#define TEXT_DEFAULT_FONT_TILE
Default first font tile (zero — font occupies tiles 0-95).
Definition text.h:48
#define TEXT_DEFAULT_PALETTE
Default palette slot (palette 0).
Definition text.h:50
void textInit(u16 tilemap_addr, u16 font_tile, u8 palette)
Initialize the text rendering system.
#define TEXT_DEFAULT_TILEMAP_ADDR
Default tilemap WORD address — 32×32 tilemap at VRAM word $3800.
Definition text.h:46

The built-in 2bpp font is loaded to VRAM $0000. BG1's tilemap sits at $3800 to avoid conflicts with BG2 graphics.

3. Calibration Background (BG2)

dmaCopyVram(aim_target_tiles, 0x1000, ...);
dmaCopyVram(aim_target_map, 0x2000, ...);
bgSetGfxPtr(1, 0x1000);
void dmaCopyVram(const u8 *source, u16 vramAddr, u16 size)
Copy data to VRAM (PVSnesLib compatible).

The crosshair target image is a 256x224 background. Tiles at $1000, tilemap at $2000.

4. Sprites

dmaCopyVram(sprites_tiles, 0x4000, ...);
#define REG_OBJSEL
Object (sprite) size and base (W).
Definition registers.h:52
#define OBJ_SIZE16_L32
Definition sprite.h:55
#define OBJSEL(size, vram_addr)
Build OBJSEL register value from size constant + VRAM base address.
Definition sprite.h:75

The OBJSEL() macro replaces the cryptic 0x62 with a readable size + base address. OBJ_SIZE16_L32 means small=16x16, large=32x32. The sprite sheet is the impact marker alone, a 16x16 sprite: tile 0.

5. Direct OAM Setup

oamMemory[0] = 0;
oamMemory[1] = 0xE0; /* Off-screen */
oamMemory[2] = 0x00; /* Tile 0 (the impact marker) */
oamMemory[3] = 0x30; /* Priority 3, palette 0 */
oamMemory[512] = 0x00; /* Small (16x16), X high = 0 */
u8 oamMemory[]
Hardware OAM buffer (544 bytes at $7E:0300).
volatile u8 oam_update_flag
OAM DMA trigger flag.

Writing directly to oamMemory[] avoids the call and argument pushes of oamSet(). For a single sprite updated occasionally, this is clean and fast.

6. State Machine

The example uses three states:

The fire_armed flag prevents the calibration fire from immediately re-triggering in the next state (debouncing).

Tips & Tricks

  • 2bpp pictures: draw them indexed on their four colours (here devtools/art_assets/build.py writes the target pixel by pixel): an RGB picture quantized down to 4 colours can lose a thin line that covers few pixels.
  • Sprite tile numbering: in a 128px-wide sprite sheet of 16x16 sprites, tile numbers follow row * 32 + col * 2. opensnes-sprite inspect reports the block and tile counts.
  • OBJSEL macro: Instead of memorizing that 0x62 means "16/32 sizes at VRAM $4000", use OBJSEL(OBJ_SIZE16_L32, 0x4000).
  • Trying it without a gun: luna drives the Super Scope from a test manifest (superscope = "frame:x,y,buttons", see testing/manifests/superscope.toml).

Under the Hood: The Build

TARGET := superscope.sfc
USE_LIB := 1
LIB_MODULES := console input sprite dma text background
Module Why it's here
console VBlank/NMI handling
input Super Scope detection and reading
sprite oamInit(), oamClear()
dma DMA transfers for tiles, maps, palettes
text Font loading and text rendering
background bgSetGfxPtr(), bgSetMapPtr(), bgSetScroll()

Modules Used

console, input, sprite, dma, text, background (LIB_MODULES in the Makefile).

Technical Reference

Register Address Role in this example
OBJSEL $2101 Sprite size + tile base address
CGADD $2121 Palette write address
CGDATA $2122 Palette color data (RGB555 LE)
TM $212C Main screen layer enable
OPHCT $213C PPU horizontal counter (Super Scope X)
OPVCT $213D PPU vertical counter (Super Scope Y)

Files

File What's in it
main.c State machine, sprite control, text display
res/*.png.toml the import settings of the target picture (opensnes-tileset, 2bpp, 4 colours) and of the marker (opensnes-sprite, 16-pixel blocks, 16 colours); the build converts and links them
res/aim_adjust_target.png the calibration target, drawn by devtools/art_assets/build.py (scope_target): the rings and the cross must be exact, the aim adjustment reads them
res/marker.png the impact marker, a white ring round a magenta core: assets/neo-lumen/ui/marker.png (PixelLab, CC0), indexed
Makefile LIB_MODULES := console input sprite dma text background