PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
graphics/Sprites/AnimatedSprite/AnimatedSprite.c
/*---------------------------------------------------------------------------------
Animated Sprite demo
-- alekmaul
Sprite from Stephen "Redshrike" Challener), http://opengameart.org
---------------------------------------------------------------------------------*/
#include <snes.h>
extern char gfxpsrite, gfxpsrite_end;
extern char palsprite, palsprite_end;
#define FRAMES_PER_ANIMATION 3 // 3 sprites per direction
//---------------------------------------------------------------------
// The Monster sprite
//---------------------------------------------------------------------
typedef struct
{
s16 x, y;
u16 gfx_frame;
u16 anim_frame;
u8 state;
u8 flipx;
} Monster;
//---------------------------------------------------------------------
// The state of the sprite (which way it is walking)
//---------------------------------------------------------------------
enum SpriteState
{
W_DOWN = 0,
W_UP = 1,
W_RIGHT = 2,
W_LEFT = 2
};
//---------------------------------------------------------------------
// Screen dimensions
//---------------------------------------------------------------------
enum
{
SCREEN_TOP = -16,
SCREEN_BOTTOM = 224,
SCREEN_LEFT = -16,
SCREEN_RIGHT = 256
};
const char sprTiles[9] =
{
0, 2, 4, 6, 8, 10, 12, 14, 32}; // Remember that sprites are interleave with 128 pix width,
//---------------------------------------------------------------------------------
int main(void)
{
unsigned short pad0, i;
Monster monster = {100, 100};
// Initialize SNES
// Init Sprites gfx and palette with default size of 16x16
oamInitGfxSet(&gfxpsrite, (&gfxpsrite_end - &gfxpsrite), &palsprite, (&palsprite_end - &palsprite), 0, 0x0000, OBJ_SIZE16_L32);
// Define sprites parameters
oamSet(0, monster.x, monster.y, 0, 0, 0, 0, 0);
oamSetEx(0, OBJ_SMALL, OBJ_SHOW);
oamSetVisible(0, OBJ_SHOW);
// Now Put in 16 color mode and disable all backgrounds
setMode(BG_MODE1, 0);
// Wait for nothing :P
while (1)
{
// Refresh pad values in VBL and Get current #0 pad
pad0 = padsCurrent(0);
if (pad0)
{
// Update sprite with current pad
if (pad0 & KEY_UP)
{
if (monster.y >= SCREEN_TOP)
monster.y--;
monster.state = W_UP;
monster.flipx = 0;
}
if (pad0 & KEY_LEFT)
{
if (monster.x >= SCREEN_LEFT)
monster.x--;
monster.state = W_LEFT;
monster.flipx = 1;
}
if (pad0 & KEY_RIGHT)
{
if (monster.x <= SCREEN_RIGHT)
monster.x++;
monster.state = W_RIGHT;
monster.flipx = 0;
}
if (pad0 & KEY_DOWN)
{
if (monster.y <= SCREEN_BOTTOM)
monster.y++;
monster.state = W_DOWN;
monster.flipx = 0;
}
monster.anim_frame++;
if (monster.anim_frame >= FRAMES_PER_ANIMATION)
monster.anim_frame = 0;
}
// Now, get current sprite in current animation
monster.gfx_frame = sprTiles[monster.anim_frame + monster.state * FRAMES_PER_ANIMATION];
oamSet(0, monster.x, monster.y, 3, monster.flipx, 0, monster.gfx_frame, 0);
// Wait VBL 'and update sprites too ;-) )
}
return 0;
}
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
void consoleInit(void)
Initialize console.
#define padsCurrent(value)
Return current value of selected pad.
Definition: input.h:161
@ KEY_UP
pad UP button.
Definition: input.h:55
@ KEY_LEFT
pad LEFT button.
Definition: input.h:53
@ KEY_RIGHT
pad RIGHT button.
Definition: input.h:52
@ KEY_DOWN
pad DOWN button.
Definition: input.h:54
void WaitForVBlank(void)
Wait for vblank interrupt
the master include file for snes applications.
#define OBJ_SIZE16_L32
default OAM size 16x16 (SM) and 32x32 (LG) pix for OBJSEL register
Definition: sprite.h:42
void oamSetVisible(u16 id, u8 hide)
Hide or show a sprite.
void oamSetEx(u16 id, u8 size, u8 hide)
Put the correct size and hide or show a sprite.
void oamInitGfxSet(u8 *tileSource, u16 tileSize, u8 *tilePalette, u16 paletteSize, u8 tilePaletteNumber, u16 address, u8 oamsize)
Initializes a sprites Gfx and Loads the GFX into VRAM.
void oamSet(u16 id, u16 xspr, u16 yspr, u8 priority, u8 hflip, u8 vflip, u16 gfxoffset, u8 paletteoffset)
sets an oam entry to the supplied values
void setScreenOn(void)
Put screen On.
void setMode(u8 mode, u8 size)
Set the SNES hardware to operate in new display mode.