PVSnesLib  4.3.0
Documentation to code in C or ASM for the Nintendo SNES
maps/mapscroll/mapscroll.c
/*---------------------------------------------------------------------------------
Simple map example with object scrolling
-- alekmaul
---------------------------------------------------------------------------------*/
#include <snes.h>
extern char tileset, tilesetend, tilesetpal; // for map & tileset of map
extern char gfxsprite, gfxsprite_end, palsprite;
extern char mapmario, tilesetdef, tilesetatt;
// to update sprite with correct index value
const char sprTiles[4] =
{
0,
2,
4,
6,
};
//---------------------------------------------------------------------------------
unsigned short pad0;
unsigned short xloc, yloc, flipx, frame, frameidx, flip;
//---------------------------------------------------------------------------------
int main(void)
{
// Initialize SNES
// Init layer with tiles and init also map length 0x6800 is mandatory for map engine
bgInitTileSet(0, &tileset, &tilesetpal, 0, (&tilesetend - &tileset), 16 * 2, BG_16COLORS, 0x2000);
bgSetMapPtr(0, 0x6800, SC_64x32);
// Now Put in 16 color mode and disable Bgs except current
setMode(BG_MODE1, 0);
// Init Sprites gfx and palette with default size of 16x16
oamInitGfxSet(&gfxsprite, (&gfxsprite_end - &gfxsprite), &palsprite, 16, 0, 0x0000, OBJ_SIZE16_L32);
// Define sprites parameters
xloc = 0;
yloc = 224 - 32;
flipx = 0;
frame = 0;
frameidx = 0;
flip = 0;
oamSet(0, xloc, yloc, 0, 0, 0, 0, 0);
oamSetEx(0, OBJ_SMALL, OBJ_SHOW);
oamSetVisible(0, OBJ_SHOW);
// Display screen
// Load map in memory and update it regarding current location of the sprite
mapLoad((u8 *)&mapmario, (u8 *)&tilesetdef, (u8 *)&tilesetatt);
mapUpdateCamera(xloc, yloc);
while (1)
{
// Get pad value and change sprite location and camera if need
pad0 = padsCurrent(0);
if (pad0)
{
if (pad0 & KEY_LEFT)
{
if (xloc)
xloc--;
flipx = 1;
mapUpdateCamera(xloc, yloc);
flip++;
if (flip & 1)
{
frameidx++;
frameidx = frameidx & 3;
frame = sprTiles[frameidx];
}
}
if (pad0 & KEY_RIGHT)
{
xloc++;
flipx = 0;
mapUpdateCamera(xloc, yloc);
flip++;
if (flip & 1)
{
frameidx++;
frameidx = frameidx & 3;
frame = sprTiles[frameidx];
}
}
}
// x_pos and y_pos are from map engine and reflect the current map position
oamSet(0, xloc - x_pos, yloc - y_pos, 3, flipx, 0, frame, 0);
}
return 0;
}
void bgSetDisable(u8 bgNumber)
Disable a BG in the actual SNES mode.
void bgSetMapPtr(u8 bgNumber, u16 address, u8 mapSize)
Change Background Map address.
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.
void consoleInit(void)
Initialize console.
#define padsCurrent(value)
Return current value of selected pad.
Definition: input.h:161
@ KEY_LEFT
pad LEFT button.
Definition: input.h:53
@ KEY_RIGHT
pad RIGHT button.
Definition: input.h:52
void WaitForVBlank(void)
Wait for vblank interrupt
void mapVblank(void)
Display map regarding current buffer (must be done once per frame, near Vblank)
void mapUpdate(void)
Update map regarding current camera position (must be done once per frame)
u16 y_pos
Current value of camera in x & y coordinates.
Definition: map.h:57
void mapLoad(u8 *layer1map, u8 *layertiles, u8 *tilesprop)
Load map definition into memory.
void mapUpdateCamera(u16 xpos, u16 ypos)
Update map camera (must be done once per frame)
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.