Loading...
Searching...
No Matches
main.c File Reference

Two-player simultaneous input with independent sprites. More...

#include <snes.h>

Classes

struct  Player
 Per-player position state. More...
 

Functions

int main (void)
 Main entry point – two-player sprite movement demo.
 

Variables

Player p1 = {64, 112}
 Player 1 state, initialized at left-center of screen.
 
Player p2 = {192, 112}
 Player 2 state, initialized at right-center of screen.
 
static const u8 pal_blue []
 Sprite palette 0 (Player 1): Blue.
 
static const u8 pal_red []
 Sprite palette 1 (Player 2): Red.
 
static const u8 sprite_tile [32]
 Inline 8x8 solid square sprite tile in SNES 4bpp format (32 bytes).
 

Detailed Description

Two-player simultaneous input with independent sprites.

Demonstrates reading two SNES controllers simultaneously via the auto-joypad registers. The NMI handler reads both ports every frame, and padHeld(0) / padHeld(1) return the held-button bitmasks for each player. Each player controls an independent 8x8 sprite with the D-pad.

Sprite tiles are defined inline as constant data (a solid 4bpp square), and two palettes (blue for Player 1, red for Player 2) are loaded to separate CGRAM sprite palette slots so each player has a distinct color.

SNES Concepts
  • Simultaneous two-controller reading via auto-joypad
  • Per-player sprite palettes using CGRAM palette indexing
  • Inline tile data (no external assets needed)
  • oamSet() for simple sprite positioning
What to Observe
  • A blue square (Player 1) and a red square (Player 2) on a black screen
  • Each sprite moves independently with its controller's D-pad
  • Both sprites can move at the same time without interference
Modules Used
console, input, sprite, dma, text
See also
input.h, sprite.h, dma.h

Function Documentation

◆ main()

int main ( void  )

Main entry point – two-player sprite movement demo.

Loads a single solid-square tile to sprite VRAM, sets up two palettes (blue for Player 1, red for Player 2), and enters a loop where each player's D-pad input moves their respective sprite independently. Both controllers are read every frame via the NMI auto-joypad mechanism.

Returns
0 (never reached – infinite game loop)

Variable Documentation

◆ p1

Player p1 = {64, 112}

Player 1 state, initialized at left-center of screen.

BG1 palette

◆ p2

Player p2 = {192, 112}

Player 2 state, initialized at right-center of screen.

BG2 palette

◆ pal_blue

const u8 pal_blue[]
static
Initial value:
= {
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0xFF, 0x7C,
}

Sprite palette 0 (Player 1): Blue.

SNES CGRAM stores 15-bit BGR555 colors (2 bytes each, little-endian). Color 0 is transparent for sprites regardless of value. Only color 3 matters here because the tile's bitplanes produce index 3 for every pixel. 0x7CFF = B=31, G=3, R=31... wait – 0xFF,0x7C = $7CFF = blue ($7C00) + some. Actual: low=0xFF high=0x7C => word $7CFF => R=31, G=7, B=15 => bright blue.

◆ pal_red

const u8 pal_red[]
static
Initial value:
= {
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x1F, 0x00,
}

Sprite palette 1 (Player 2): Red.

Loaded to CGRAM 128+16 (sprite palette bank 1). The sprite's attribute byte selects palette 1, so this red color is used for Player 2's square. 0x001F = R=31, G=0, B=0 => pure bright red.

◆ sprite_tile

const u8 sprite_tile[32]
static
Initial value:
= {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}

Inline 8x8 solid square sprite tile in SNES 4bpp format (32 bytes).

SNES 4bpp tiles are stored as 4 interleaved bitplanes. Planes 0-1 (first 16 bytes) are all 0xFF = every pixel has bits 0-1 set. Planes 2-3 (next 16 bytes) are all 0x00 = bits 2-3 clear. Result: every pixel = color index 3 (0b0011), producing a solid square in whichever palette color 3 is assigned. No external asset file needed.