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

Direction-based sprite animation with horizontal flip. More...

#include <snes.h>
#include <snes/anim.h>
#include "res/kit_walk.inc"

Classes

struct  Walker
 Sprite state structure holding position, animation, and direction. More...

Macros

#define ANIM_DELAY   6
 Number of frames to hold each animation frame before advancing.

Enumerations

enum  { SCREEN_TOP = -16 , SCREEN_BOTTOM = 224 , SCREEN_LEFT = -16 , SCREEN_RIGHT = 256 }
 Screen edge boundaries for sprite clamping. More...
enum  SpriteState { W_DOWN = 0 , W_UP = 1 , W_RIGHT = 2 , W_LEFT = 2 }
 Sprite sheet tile data containing all animation frames (converted by the build from res/). More...

Functions

 DECLARE_ANIM_CLIP (walk_down, ANIM_LOOP, 6, 0, 4, 8, 12, 64)
 DECLARE_ANIM_CLIP (walk_side, ANIM_LOOP, 6, 136, 140, 192, 196, 200)
 DECLARE_ANIM_CLIP (walk_up, ANIM_LOOP, 6, 68, 72, 76, 128, 132)
int main (void)
 Entry point: animated character sprite with D-PAD movement.

Variables

Walker kit = {100, 100, 0, W_DOWN, 0}
 Kit, the player-controlled sprite, initialized facing down at (100, 100).
AnimPlayer kit_anim = ANIM_PLAYER_INIT
 Playback head for Kit (zero-init = stopped).

Detailed Description

Direction-based sprite animation with horizontal flip.

Demonstrates a classic sprite animation pattern: Kit, the fox-kit courier seen from above, with a walk cycle per direction (down, up, left, right). Each direction uses a different row of frames in the sprite sheet, and the left-facing animation reuses the right-facing frames with the OAM horizontal flip bit (bit 6 of attribute byte).

Animation is frame-paced with a configurable delay between tile changes. The sprite's OAM tile number is updated each frame via oamSetAttr() to point at the correct frame in the pre-loaded sprite sheet. Movement is controlled with the D-PAD and clamped to screen boundaries.

This began as the PVSnesLib AnimatedSprite example.

SNES Concepts
  • Sprite animation via OAM tile number updates
  • Horizontal flip (OBJ_FLIPX) to mirror sprite frames
  • D-PAD input for 4-directional movement
  • Frame-paced animation with delay counters
What to Observe
  • A 32x32 character sprite that walks in 4 directions with D-PAD
  • Walking left mirrors the right-facing animation
  • The animation cycles through 5 frames per direction
  • The sprite stops animating when no button is pressed
Modules Used
console, sprite, dma, input, anim
See also
sprite.h, input.h, dma.h

Macro Definition Documentation

◆ ANIM_DELAY

#define ANIM_DELAY   6

Number of frames to hold each animation frame before advancing.

At 60fps, a delay of 6 means each walk frame lasts 6/60 = 100ms, producing a 500ms walk cycle (5 frames x 100ms). Adjust this value to make the animation faster (lower) or slower (higher).

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Screen edge boundaries for sprite clamping.

Negative values allow the sprite to partially leave the screen (16px margin), which looks more natural than stopping exactly at the visible edge.

Enumerator
SCREEN_TOP 

Top clamp: 16px above visible area

SCREEN_BOTTOM 

Bottom clamp: bottom of visible area

SCREEN_LEFT 

Left clamp: 16px left of visible area

SCREEN_RIGHT 

Right clamp: right edge of visible area

◆ SpriteState

Sprite sheet tile data containing all animation frames (converted by the build from res/).

Palette for the character sprite (16 colors, 4bpp)

Animation direction states.

Each state maps to a row in the sprite sheet. W_LEFT reuses the W_RIGHT row and sets the OAM horizontal flip bit to mirror the frames, which is a common SNES technique to halve the sprite sheet size for left/right symmetric characters.

Enumerator
W_DOWN 

Walking downward (facing camera)

W_UP 

Walking upward (facing away)

W_RIGHT 

Walking right

W_LEFT 

Walking left — reuses W_RIGHT tiles with horizontal flip

Function Documentation

◆ DECLARE_ANIM_CLIP() [1/3]

DECLARE_ANIM_CLIP ( walk_down ,
ANIM_LOOP ,
6 ,
0 ,
4 ,
8 ,
12 ,
64  )

◆ DECLARE_ANIM_CLIP() [2/3]

DECLARE_ANIM_CLIP ( walk_side ,
ANIM_LOOP ,
6 ,
136 ,
140 ,
192 ,
196 ,
200  )

◆ DECLARE_ANIM_CLIP() [3/3]

DECLARE_ANIM_CLIP ( walk_up ,
ANIM_LOOP ,
6 ,
68 ,
72 ,
76 ,
128 ,
132  )

◆ main()

int main ( void )

Entry point: animated character sprite with D-PAD movement.

Loads a multi-frame sprite sheet, initializes OAM, and runs a game loop that reads D-PAD input, updates the sprite position and animation state, then writes the new tile number and flip flags to OAM each frame.

Returns
Never returns (infinite loop).

Current joypad state — bits set for each held button

Variable Documentation

◆ kit

Walker kit = {100, 100, 0, W_DOWN, 0}

Kit, the player-controlled sprite, initialized facing down at (100, 100).

◆ kit_anim

Playback head for Kit (zero-init = stopped).