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

Both controllers on a terminal's test screen: buttons lit, the edges, a frame counter. More...

#include <snes.h>
#include <snes/text.h>
#include <snes/gameloop.h>
#include "res/screen.inc"
#include "res/cast.inc"

Classes

struct  Lamp
 A lamp: the button it shows, its place on a pad's drawing, its tile. More...

Macros

#define LAMPS   14
 Number of lamps on a pad: one per button, two per shoulder.
#define PAD_Y   32
 Both pads' top edge (the shoulders).
VRAM (word addresses): textModeInit() keeps $0000-$0FFF (font) and $3800 (its map)
#define OBJ_VRAM   0x4000
#define SCREEN_MAP   0x3C00
#define SCREEN_TILES   0x1000
The sheet's tiles (res/cast.png, 128 px wide: one row of the sheet is one row of OBJ tiles)
#define TILE_AVATAR   64
#define TILE_LAMP   128
The bay the avatars walk in: their 32x32 sprites stay inside its outline
#define BAY_X_MAX   206
#define BAY_X_MIN   18
#define BAY_Y_MAX   174
#define BAY_Y_MIN   154

Enumerations

enum  { SOUTH , NORTH , EAST , WEST }
 Facings, in the sheet's order. More...

Functions

static const char * first_name (u16 mask)
 The name of the first button set in a mask, for the edges.
int main (void)
 Entry point — the game loop runs on_init once, then on_update each frame.
static void on_init (void)
 Once, in force blank: the screen, the sheet, the labels.
static void on_update (void)
 Every frame: the lamps, the edges, the counter, the avatars.
static void show_pad (u8 pad)
 Light pad n's lamps for the buttons held, and print its bitmask.
static void walk (u8 pad)
 Walk pad n's avatar with its D-pad, inside the bay.

Variables

u16 avatar_x [2]
 Probe oracle: each avatar's top-left, x (pad 1, pad 2).
u16 avatar_y [2]
 Probe oracle: each avatar's top-left, y (pad 1, pad 2).
static u8 facing [2]
 Each avatar's facing (SOUTH, NORTH, EAST, WEST).
static const u16 key_bit [12]
 The 12 buttons in the order the pad sends them, for the edges.
static const char *const key_name [12]
 Their names, padded to one width so a shorter one covers a longer.
static const Lamp lamps [14]
 The lamps of one pad; the drawing has a button under each.
static const u8 pad_x [2] = { 16, 136 }
 Each pad's top-left on screen.

Detailed Description

Both controllers on a terminal's test screen: buttons lit, the edges, a frame counter.

The pad test of a city terminal. The two controllers are drawn on screen, and each button held lights up on its pad's drawing, several at once; under each pad, the bitmask padHeld() returns, in hex. Under them, the last button pressed and the last button released on pad 1, and the number of VBlanks since boot. In the test bay at the bottom, each pad's D-pad walks its avatar: Kit for pad 1, the raccoon for pad 2. This is the starting point for understanding how SNES games read player input, and the screen the console check uses.

The SNES joypad has 12 buttons: 4 face buttons (A, B, X, Y), 2 shoulder buttons (L, R), a D-pad (4 directions), START, and SELECT. The NMI handler reads both joypads automatically every VBlank. Your code just calls padHeld(pad) for the buttons down, padPressed(pad) for the ones that went down on this frame, and padReleased(pad) for the ones that came up.

Ported from PVSnesLib "controller" example by alekmaul; the second pad was the two_players example, the counter the timer example, both merged here.

SNES Concepts
  • padHeld(n) — 16-bit bitmask of the buttons down on pad n (0 or 1)
  • padPressed(n) / padReleased(n) — the edges: down this frame, up this frame
  • KEY_A, KEY_B, KEY_X, KEY_Y, KEY_L, KEY_R, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_START, KEY_SELECT — the bits, in the order the pad shifts them out (B Y SELECT START UP DOWN LEFT RIGHT A X L R)
  • frame_count — the VBlanks since boot, counted by the NMI handler
  • One lamp sprite per button, shown while its bit is set: the screen is a picture of the bitmask
What to Observe
  • Hold buttons on pad 1 or pad 2: they light up on that pad's drawing (cyan for pad 1, pink for pad 2) while held, several at once, and the HELD value under it shows their bits
  • The last button pressed and released on pad 1 stay on screen
  • Each D-pad walks its own avatar in the test bay
  • The counter goes up by 60 a second (50 on PAL); L+R together resets it
Modules Used
console, sprite, dma, background, text, input, gameloop
See also
input.h

Macro Definition Documentation

◆ BAY_X_MAX

#define BAY_X_MAX   206

◆ BAY_X_MIN

#define BAY_X_MIN   18

◆ BAY_Y_MAX

#define BAY_Y_MAX   174

◆ BAY_Y_MIN

#define BAY_Y_MIN   154

◆ LAMPS

#define LAMPS   14

Number of lamps on a pad: one per button, two per shoulder.

◆ OBJ_VRAM

#define OBJ_VRAM   0x4000

◆ PAD_Y

#define PAD_Y   32

Both pads' top edge (the shoulders).

◆ SCREEN_MAP

#define SCREEN_MAP   0x3C00

◆ SCREEN_TILES

#define SCREEN_TILES   0x1000

◆ TILE_AVATAR

#define TILE_AVATAR   64

pad p's avatar starts at tile 64 p; + 4 per facing

◆ TILE_LAMP

#define TILE_LAMP   128

the four 8x8 lamps: disc, square, pill, half a shoulder

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Facings, in the sheet's order.

Enumerator
SOUTH 
NORTH 
EAST 
WEST 

Function Documentation

◆ first_name()

const char * first_name ( u16 mask)
static

The name of the first button set in a mask, for the edges.

◆ main()

int main ( void )

Entry point — the game loop runs on_init once, then on_update each frame.

Returns
Never returns

◆ on_init()

void on_init ( void )
static

Once, in force blank: the screen, the sheet, the labels.

◆ on_update()

void on_update ( void )
static

Every frame: the lamps, the edges, the counter, the avatars.

◆ show_pad()

void show_pad ( u8 pad)
static

Light pad n's lamps for the buttons held, and print its bitmask.

< the four 8x8 lamps: disc, square, pill, half a shoulder

◆ walk()

void walk ( u8 pad)
static

Walk pad n's avatar with its D-pad, inside the bay.

< pad p's avatar starts at tile 64 p; + 4 per facing

Variable Documentation

◆ avatar_x

u16 avatar_x[2]

Probe oracle: each avatar's top-left, x (pad 1, pad 2).

◆ avatar_y

u16 avatar_y[2]

Probe oracle: each avatar's top-left, y (pad 1, pad 2).

◆ facing

u8 facing[2]
static

Each avatar's facing (SOUTH, NORTH, EAST, WEST).

◆ key_bit

const u16 key_bit[12]
static
Initial value:
= {
}
#define KEY_RIGHT
Definition input.h:81
#define KEY_A
Definition input.h:84
#define KEY_DOWN
Definition input.h:79
#define KEY_L
Definition input.h:86
#define KEY_X
Definition input.h:85
#define KEY_START
Definition input.h:77
#define KEY_R
Definition input.h:87
#define KEY_SELECT
Definition input.h:76
#define KEY_B
Definition input.h:74
#define KEY_Y
Definition input.h:75
#define KEY_LEFT
Definition input.h:80
#define KEY_UP
Definition input.h:78

The 12 buttons in the order the pad sends them, for the edges.

◆ key_name

const char* const key_name[12]
static
Initial value:
= {
"B ", "Y ", "SELECT", "START ", "UP ", "DOWN ",
"LEFT ", "RIGHT ", "A ", "X ", "L ", "R "
}

Their names, padded to one width so a shorter one covers a longer.

◆ lamps

const Lamp lamps[14]
static
Initial value:
= {
{ KEY_B, 74, 31, 0 }, { KEY_Y, 65, 22, 0 }, { KEY_SELECT, 42, 30, 2 }, { KEY_START, 54, 30, 2 },
{ KEY_UP, 22, 14, 1 }, { KEY_DOWN, 22, 30, 1 }, { KEY_LEFT, 14, 22, 1 }, { KEY_RIGHT, 30, 22, 1 },
{ KEY_A, 83, 22, 0 }, { KEY_X, 74, 13, 0 },
{ KEY_L, 10, 0, 3 }, { KEY_L, 18, 0, 3 }, { KEY_R, 78, 0, 3 }, { KEY_R, 86, 0, 3 },
}

The lamps of one pad; the drawing has a button under each.

◆ pad_x

const u8 pad_x[2] = { 16, 136 }
static

Each pad's top-left on screen.