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

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

#include <snes.h>

Classes

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

Macros

#define ANIM_DELAY   6
 Number of frames to hold each animation frame before advancing.
 
#define FRAMES_PER_ANIMATION   3
 Number of distinct frames in each directional walk cycle.
 

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 }
 Animation direction states. More...
 

Functions

int main (void)
 Entry point: animated character sprite with D-PAD movement.
 

Variables

Monster monster = {100, 100, 0, 0, 0, W_DOWN, 0}
 The player-controlled character sprite, initialized facing down at (100, 100)
 
u8 sprite_pal []
 Palette for the character sprite (16 colors, 4bpp)
 
u8 sprite_pal_end []
 
u8 sprite_tiles []
 Sprite sheet tile data containing all animation frames (defined in data.asm)
 
u8 sprite_tiles_end []
 

Detailed Description

Direction-based sprite animation with horizontal flip.

Demonstrates a classic sprite animation pattern: a character sprite with multiple directional walk cycles (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 oamSet() to point at the correct frame in the pre-loaded sprite sheet. Movement is controlled with the D-PAD and clamped to screen boundaries.

Ported from 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 16x16 character sprite that walks in 4 directions with D-PAD
  • Walking left mirrors the right-facing animation
  • The animation cycles through 3 frames per direction
  • The sprite stops animating when no button is pressed
Modules Used
console, sprite, dma, input
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 300ms walk cycle (3 frames x 100ms). Adjust this value to make the animation faster (lower) or slower (higher).

◆ FRAMES_PER_ANIMATION

#define FRAMES_PER_ANIMATION   3

Number of distinct frames in each directional walk cycle.

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

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

◆ 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

◆ monster

Monster monster = {100, 100, 0, 0, 0, W_DOWN, 0}

The player-controlled character sprite, initialized facing down at (100, 100)

◆ sprite_pal

u8 sprite_pal[]
extern

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

◆ sprite_pal_end

u8 sprite_pal_end[]

◆ sprite_tiles

u8 sprite_tiles[]
extern

Sprite sheet tile data containing all animation frames (defined in data.asm)

◆ sprite_tiles_end

u8 sprite_tiles_end[]