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

Family 4 (Input) · rung 4.2 — drive a sprite with the pad. More...

#include <snes.h>

Macros

#define SPEED   2
 Pixels moved per frame while a direction is held.

Functions

int main (void)
 Entry point — set up one sprite, then steer it with the D-pad.

Variables

u8 palsprite32 []
 16-colour palette for the sprite
u8 sprite32 []
 4bpp 32x32 sprite tile data (data.asm, in ROM)
u8 sprite32_end []

Detailed Description

Family 4 (Input) · rung 4.2 — drive a sprite with the pad.

The rung that turns a demo into a game: read the D-pad every frame and move a sprite with it. It reuses the whole simple_sprite setup — tiles to VRAM, palette to CGRAM 128, OBJSEL — and adds one thing, the input loop. The lesson: padHeld(0) hands you the held-button bitmask; act on it, update the sprite's position with oamSetXY, and the player is in control.

SNES Concepts
  • padHeld(0) returns a 16-bit held-button bitmask (the NMI auto-reads the pad)
  • KEY_UP / KEY_DOWN / KEY_LEFT / KEY_RIGHT are the D-pad bits
  • oamSetXY(id, x, y) updates only a sprite's position (tile and size stay put)
  • Position is 9-bit X / 8-bit Y, so walking off an edge wraps — the OAM coordinate quirk, visible here for free
What to Observe
  • A 32x32 sprite you steer with the D-pad; hold a direction to glide, and walk off an edge to see it wrap around
Modules Used
console, dma, sprite, input
See also
input.h, sprite.h, tutorial_input

Macro Definition Documentation

◆ SPEED

#define SPEED   2

Pixels moved per frame while a direction is held.

Function Documentation

◆ main()

int main ( void )

Entry point — set up one sprite, then steer it with the D-pad.

The setup is identical to simple_sprite. The only new part is the loop: read the pad, nudge (x, y), push the new position to OAM, wait a frame.

Returns
Never returns (infinite loop).

Variable Documentation

◆ palsprite32

u8 palsprite32[]
extern

16-colour palette for the sprite

◆ sprite32

u8 sprite32[]
extern

4bpp 32x32 sprite tile data (data.asm, in ROM)

◆ sprite32_end

u8 sprite32_end[]