PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
objects/moveobjects/moveobjects.c
/*---------------------------------------------------------------------------------
snes object engine demo
-- alekmaul
---------------------------------------------------------------------------------*/
#include <snes.h>
extern char snesfont;
extern char gfxpsrite, gfxpsrite_end;
extern char palsprite, palsprite_end;
u16 numspr, i;
u16 pad0;
s16 xp, yp;
t_objs *snesobj;
s16 *snesox, *snesoy; // basics x/y on screen with fixed point
// Definition of object table with values :
// x & y coordinates
// type of objects
// min x & max x coordinate if we want to update objects on regarding these coordinates
const u16 tabobjects[] =
{
15,
5,
0,
0,
0, // no min & max x speified
100,
5,
0,
0,
0, // no min & max x speified
5,
15,
0,
0,
0, // no min & max x speified
150,
150,
0,
0,
0, // no min & max x speified
200,
50,
0,
0,
0, // no min & max x speified
125,
25,
0,
0,
0, // no min & max x speified
200,
25,
0,
0,
0, // no min & max x speified
0xFFFF,
};
//---------------------------------------------------------------------------------
// Init function for each object from type #0
void testInit(u16 xp, u16 yp, u16 type, u16 minx, u16 maxx)
{
// to have a little message regarding init (DO NOT USE FOR REAL SNES GAME, JUST DEBUGGING PURPOSE)
consoleNocashMessage("testInit %d %d\n", (u16)xp, (u16)yp);
// prepare new object
if (objNew(type, xp, yp) == 0)
// no more space, get out
return;
// Init some vars for snes sprite (objgetid is the current object id)
snesobj = &objbuffers[objptr - 1];
// Put sprite at coordinates, with maximum priority 3 from tile entry 0, palette 0
snesobj->sprnum = numspr * 4;
oamSet(snesobj->sprnum, xp, yp, 3, 0, 0, 0, 0);
oamSetEx(snesobj->sprnum, OBJ_SMALL, OBJ_SHOW);
numspr++;
}
// Update function for each object from type #0
void testUpdate(u8 idx)
{
// go to current object
snesobj = &objbuffers[idx];
// grab the coordinate pointers
snesox = (s16 *)&(snesobj->xpos + 1);
snesoy = (s16 *)&(snesobj->ypos + 1);
xp = *snesox;
yp = *snesoy;
// Get current #0 pad
pad0 = padsCurrent(0);
// Kill current object with pad if A pressed (just need to populate objtokill variable)
switch (pad0)
{
case KEY_A:
objtokill = 1;
consoleNocashMessage("killing me %04x\n", (u16)idx);
break;
}
// change sprite coordinates randomly
i = rand() & 0xF;
if (i < 7)
{
xp++;
if (xp > 255)
xp--;
}
else
{
xp--;
if (xp < 1)
xp++;
}
i = rand() & 0xF;
if (i < 7)
{
yp++;
if (yp > 223)
yp--;
}
else
{
yp--;
if (yp < 1)
yp++;
}
// change sprite display
oamSet(snesobj->sprnum, xp, yp, 3, 0, 0, 0, 0);
// update variables for the object
*snesox = xp;
*snesoy = yp;
}
//---------------------------------------------------------------------------------
int main(void)
{
// Initialize SNES
// Init Sprites gfx and palette with default size of 32x32
oamInitGfxSet(&gfxpsrite, (&gfxpsrite_end - &gfxpsrite), &palsprite, (&palsprite_end - &palsprite), 0, 0x0000, OBJ_SIZE32_L64);
// Now Put in 16 color mode
setMode(BG_MODE1, 0);
// Screen activated
// Object engine activate
// Init function for state machine
objInitFunctions(0, &testInit, &testUpdate, NULL);
// Load all objects into memory
numspr = 0;
objLoadObjects((char *)&tabobjects);
// Need to init map , even if not present to allow update functions to work
x_pos=0; y_pos=0;
while (1)
{
}
return 0;
}
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
u16 rand(void)
return a randomized number
void consoleNocashMessage(char *fmt,...)
Send a message to the no$sns debug window.
void consoleInit(void)
Initialize console.
#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
u16 y_pos
Current value of camera in x & y coordinates.
Definition: map.h:57
t_objs objbuffers[OB_MAX]
current object buffer with all objects
u16 objNew(u8 objtype, u16 x, u16 y)
Initialize a new object in game, objgetid will has the id of the object.
void objInitFunctions(u8 objtype, void *initfct, void *updfct, void *reffct)
Initialize the object type functions (initialize, update)
u16 objptr
pointer to current object
void objUpdateAll(void)
call update function for all objects currently active (if they are in "virtual screen" coordinates).
void objGetPointer(u16 objhandle)
get the pointer to an object from its handle (need to do -1 to have offset after),...
void objInitEngine(void)
Initialize object engine, need to be called once.
u16 objgetid
id of current object (useful when creating it)
u8 objtokill
put 1 in variable to kill current object
void objLoadObjects(u8 *sourceO)
Load all objects for a specific table in memory.
the master include file for snes applications.
void oamSetEx(u16 id, u8 size, u8 hide)
Put the correct size and hide or show a sprite.
#define OBJ_SIZE32_L64
default OAM size 32x32 (SM) and 64x64 (LG) pix for OBJSEL register
Definition: sprite.h:44
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
object definition (64 bytes)
Definition: object.h:49
u8 xpos[3]
Definition: object.h:60
u16 sprnum
Definition: object.h:54
u8 ypos[3]
Definition: object.h:61
void setScreenOn(void)
Put screen On.
void setMode(u8 mode, u8 size)
Set the SNES hardware to operate in new display mode.