PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
graphics/Sprites/DynamicEngineSprite/DynamicEngineSprite.c
/*---------------------------------------------------------------------------------
Simple Dynamic Sprite Engine demo
-- alekmaul
---------------------------------------------------------------------------------*/
#include <snes.h>
extern char patterns, patterns_end;
extern char palette, palette_end;
extern char map, map_end;
extern char spr32g, spr32g_end, spr32p;
extern char spr16g, spr16g_end, spr16p;
extern char spr8g, spr8g_end, spr8p;
u8 i;
#define SPRNUMBER 64 // 64 sprites on screen
//---------------------------------------------------------------------------------
int main(void)
{
// Initialize SNES
// Copy tiles to VRAM
bgInitTileSet(0, &patterns, &palette, 0, (&patterns_end - &patterns), (&palette_end - &palette), BG_16COLORS, 0x4000);
// Copy Map to VRAM
bgInitMapSet(0, &map, (&map_end - &map), SC_32x32, 0x6800);
// Init Sprites palette
setPalette(&spr16p, 128 + 0 * 16, 16 * 2);
// Now Put in 16 color mode and disable other BGs except 1st one
setMode(BG_MODE1, 0);
// Init sprite engine (0x0000 for large, 0x1000 for small)
oamInitDynamicSprite(0x0000, 0x1000, 0, 0, OBJ_SIZE16_L32);
for (i = 0; i < SPRNUMBER; i++)
{
oambuffer[i].oamx = rand() % 240;
oambuffer[i].oamy = rand() % 208;
oambuffer[i].oamframeid = (i % 24);
if (i < 8)
{
oambuffer[i].oamattribute = 0x20 | (0 << 1); // palette 0 of sprite and sprite 16x16 and priority 2
oambuffer[i].oamgraphics = &spr32g;
}
else
{
oambuffer[i].oamattribute = 0x21 | (0 << 1); // palette 0 of sprite and sprite 16x16 and priority 2
oambuffer[i].oamgraphics = &spr16g;
}
}
// Wait for nothing :P
while (1)
{
// Draw sprite
for (i = 0; i < SPRNUMBER; i++)
{
// change sprites coordinates and frame randomly
if ((rand() & 7) == 7)
{
if (oambuffer[i].oamx < 240)
oambuffer[i].oamx += 2;
}
else if ((rand() & 5) == 5)
{
if (oambuffer[i].oamx > 0)
oambuffer[i].oamx -= 2;
}
else if ((rand() & 8) == 8)
{
if (oambuffer[i].oamy < 208)
oambuffer[i].oamy += 2;
}
else if ((rand() & 3) == 3)
{
if (oambuffer[i].oamy > 0)
oambuffer[i].oamy -= 2;
}
if ((rand() & 15) == 15)
{
if (oambuffer[i].oamframeid > 2)
}
if (i < 8)
{
}
else
{
}
}
// prepare next frame and wait vblank
}
return 0;
}
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
void bgInitMapSet(u8 bgNumber, u8 *mapSource, u16 mapSize, u8 sizeMode, u16 address)
Initializes a Map Set and loads it into SNES VRAM.
void bgInitTileSet(u8 bgNumber, u8 *tileSource, u8 *tilePalette, u8 paletteEntry, u16 tileSize, u16 paletteSize, u16 colorMode, u16 address)
Initializes a Tile Set and Loads the Tile GFX into VRAM.
u16 rand(void)
return a randomized number
void consoleInit(void)
Initialize console.
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
t_sprites oambuffer[128]
Sprite Table (from no$sns help file) Contains data for 128 OBJs. OAM Size is 512+32 Bytes....
void oamVramQueueUpdate(void)
Update VRAM graphics for sprites 32x32, 16x16 and 8x8 (can but call in Vblank if needed).
void oamInitDynamicSpriteEndFrame(void)
Must be call at the end of the frame, initialize the dynamic sprite engine for the next frame.
void oamInitDynamicSprite(u16 gfxsp0adr, u16 gfxsp1adr, u16 oamsp0init, u16 oamsp1init, u8 oamsize)
initialize the dynamic sprite engine with each sprite size entries
void oamDynamic16Draw(u16 id)
Add a 16x16 sprite on screen. oambuffer[id] needs to be populate before.
void oamDynamic32Draw(u16 id)
Add a 32x32 sprite on screen. oambuffer[id] needs to be populate before.
u16 oamframeid
4 frame index in graphic file of the sprite
Definition: sprite.h:62
s16 oamx
0 x position on the screen
Definition: sprite.h:60
u8 * oamgraphics
8..11 pointer to graphic file
Definition: sprite.h:65
s16 oamy
2 y position on the screen
Definition: sprite.h:61
u8 oamattribute
6 sprite attribute value (vhoopppc v : vertical flip h: horizontal flip o: priority bits p: palette n...
Definition: sprite.h:63
u8 oamrefresh
7 =1 if we need to load graphics from graphic file
Definition: sprite.h:64
void setScreenOn(void)
Put screen On.
#define setPalette(palette, paletteEntry, paletteSize)
Change a palette in CGRAM.
Definition: video.h:388
void setMode(u8 mode, u8 size)
Set the SNES hardware to operate in new display mode.