PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
graphics/Sprites/DynamicSprite/DynamicSprite.c
/*---------------------------------------------------------------------------------
Dynamic sprite demo in mode 1
-- alekmaul
---------------------------------------------------------------------------------*/
#include <snes.h>
extern char gfxpsrite, gfxpsrite_end;
extern char palsprite, palsprite_end;
unsigned short pad0, padkeya;
unsigned char spr_queue, spr_mutex;
#define ADRGFXSPR 0x0000
typedef struct
{
u8 *gfxoffset;
u16 adrgfxvram;
} spritequeue;
spritequeue sprqueue[16]; // Max 16 entries in queue
//---------------------------------------------------------------------------------
void myconsoleVblank(void)
{
u8 *pgfx;
u16 padrgfx;
// Refresh pad values
// if tile sprite queued
if (spr_queue != 0xff)
{
if (spr_mutex == 0)
{ // if we have finished adding things
// copy memory to vram (2 tiles of the 16x16 sprites)
while (spr_queue != 0xff)
{
pgfx = sprqueue[spr_queue].gfxoffset;
padrgfx = sprqueue[spr_queue].adrgfxvram;
dmaCopyVram(pgfx, padrgfx, 8 * 4 * 2);
dmaCopyVram(pgfx + 8 * 4 * 16, padrgfx + 8 * 4 * 8, 8 * 4 * 2);
spr_queue--;
}
}
}
// Put oam to screen if needed
dmaCopyOAram((unsigned char *)&oamMemory, 0, 0x220);
// count vbls
}
//---------------------------------------------------------------------------------
void addSprite(u8 *pgfx, u16 adrspr)
{
spr_mutex = 1; // to avoid vbl during queue management
spr_queue++;
sprqueue[spr_queue].gfxoffset = pgfx;
sprqueue[spr_queue].adrgfxvram = adrspr;
spr_mutex = 0;
}
//---------------------------------------------------------------------------------
int main(void)
{
// Initialize SNES
// Put current handler to our function
spr_queue = 0xff;
spr_mutex = 0;
nmiSet(myconsoleVblank);
// Init Sprites gfx and palette with default size of 16x16 (and don't load sprite tiles)
oamInitGfxSet(&gfxpsrite, 2, &palsprite, 16 * 2, 0, ADRGFXSPR, OBJ_SIZE16_L32);
// Define sprites parameters
oamSet(0, 100, 100, 3, 0, 0, 0, 0); // Put sprite in 100,100, with maximum priority 3 from tile entry 0, palette 0
oamSetEx(0, OBJ_SMALL, OBJ_SHOW);
// Now Put in 16 color mode and disable all backgrounds
setMode(BG_MODE1, 0);
// add new sprite to queue
addSprite(&gfxpsrite, ADRGFXSPR);
padkeya = 0;
// Wait for nothing :P
while (1)
{
// Refresh pad values in VBL and Get current #0 pad
pad0 = padsCurrent(0);
if (pad0)
{
// Key A pressed
if (pad0 & KEY_A)
{
// if not yet pressed
if (padkeya == 0)
{
padkeya = 1; // avoid adding new sprite continuisly
// add new sprite to queue
addSprite((&gfxpsrite) + 8 * 4 * 2, ADRGFXSPR);
}
}
else
padkeya = 0;
}
// Wait VBL 'and update sprites too ;-) )
}
return 0;
}
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
u16 snes_vblank_count
Number of VBL since consoleInit called (16 bits longs so reset each 18 minutes in NTSC)
void consoleInit(void)
Initialize console.
void dmaCopyOAram(u8 *source, u16 address, u16 size)
copies Sprites from source to destination using channel 0 of DMA available channels in half words
void dmaCopyVram(u8 *source, u16 address, u16 size)
copy data from source to destination using channel 0 of DMA available channels in half words
void scanPads(void)
Wait for pad ready and read pad values in.
#define padsCurrent(value)
Return current value of selected pad.
Definition: input.h:161
@ KEY_A
pad A button.
Definition: input.h:48
void WaitForVBlank(void)
Wait for vblank interrupt
#define nmiSet(handler)
Add a handler for the given interrupt mask.
Definition: interrupt.h:150
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 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.
u8 oamMemory[128 *4+8 *4]
to address oam table low and high
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.