Interface:
Code: Select all
//Internal state of registers is not tracked other than background scroll.
//currently the only direct register access which interferes with the following
//functions is writing to the background scroll registers and rotation registers...
//this will result in the bgRotate() and bgScroll() being invalid (bgSetRotate() and SetScroll()
//will still function as expected )
//Current design calls for the following assertions to enforce background creation rules
// range check layer, mapBase, tileBase
// Verify the type is consistend with the display mode and the layer chosen
// verify height and width are consistent with the type
// passed in ids are checked
// bpp for color mode is checked to ensure color mode is supported by the background
// others as they arise
//current design requires only the following enumeration be exposed to the user
//although they can still set the control bits manually using the libnds defines
typedef enum
{
BgType_Text,
BgType_Rotation,
BgType_ExRotation,
BgType_Bmp8,
BgType_Bmp16
}BgType;
//creates and enables the appropriate background with the supplied attributes
//returns an id which must be supplied to the remainder of the background functions
int bgCreateMain(int layer, BgType type, int width, int height, int mapBase, int tileBase);
int bgCreateSub(int layer, BgType type, int width, int height, int mapBase, int tileBase);
//allows direct access to background control for the chosen layer
void bgSetControlBits(int id, u16 bits);
//inlines for setting attributes
void bgSetPriority(int id, int priority);
void bgSetColorMode(int id, int bpp);
void bgSetMapBase(int id, int base);
void bgSetTileBase(int id, int base);
void bgSetScroll(int id, int x, int y);
//inlines for getting attributes
int bgGetPriority(int id);
int bgGetColorMode(int id);
int bgGetMapBase(int id);
int bgGetTileBase(int id);
//inlines for grabing a pointer to the map and gfx
void* bgGetMapPtr(int id);
void* bgGetGfxPtr(int id);
void* bgGetPalette(int id, int paletteId);
//relative scroll functionality
void bgScroll(int id, int dx, int dy);
//modifies display control registers as necessary
void bgShow(int id);
void bgHide(int id);
//rotates the background to the supplied angle
void bgSetRotate(int id, int angle);
void bgSetCenter(int id, int x, int y);
//rotates the background to: current rotation + supplied angle
void bgRotate(int id, int angle);
//sets the scale for the supplied background
void bgSetScale(int id, fixed_8 sx, fixed_8 sy);
//scales the background by the supplied value
void bgScale(int id, fixed_8 sx, fixed_8 sy);
/////////////////higher (slightly) level functionality/////////////////////
//loads a background palette into the backgrounds palette (Smart enough to use extended palette if selected)
void bgLoadPalette(int id, u16* pal, int paletteId, int colorCount);
//load gfx for a background at the supplied offset
void bgLoadGfx(int id, u16* gfx, int size);
//handles loading of a map into the current background at the supplied offset (relative to the supplied map)
void bgLoadMap(int id, u16* mapData, int width, int height, int offx, int offy));
//this will scroll the loaded map using the allready provided background
void bgScrollMap(int id, int dx, int dy);
//sets the map offset to the absolute coordinates supplied
void bgSetScrollMap(int id, int x, int y);
Code: Select all
//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
int bg0;
int bg1;
int ix, iy;
int scrollx = 0;
int scrolly = 0;
consoleDemoInit();
irqInit();
irqEnable(IRQ_VBLANK);
videoSetMode(MODE_0_2D);
vramSetBankA(VRAM_A_MAIN_BG);
bg0 = bgCreateMain(0, BgType_Text, 64, 32, 2, 1);
bg1 = bgCreateMain(1, BgType_Text, 64, 32, 0, 1);
bgLoadPalette(bg0,Map_1Pal, 256);
bgLoadGfx(bg0, Map_1Tiles, Map1TileLen);
bgLoadMap(bg0, Layer_1Map, 64, 64, 0, 0);
//dont need to load tile graphics or pal for bg1 because these resources are shared with bg0
//we dont need to load the map because we do bg1 manually for illistration
while(1)
{
u16* bg1map = (u16*)bgGetMapPtr(bg1);
scanKeys();
u32 keys = keysHeld();
//use high level to scroll bg0
if(keys & KEY_UP) {bgScrollMap(bg0, 0,-1)}
if(keys & KEY_DOWN) {bgScrollMap(bg0, 0, 1)}
if(keys & KEY_RIGHT) {bgScrollMap(bg0, 1, 0)}
if(keys & KEY_LEFT) {bgScrollMap(bg0,-1, 0)}
//user our own scrolling logic to scroll bg1
if(keys & KEY_UP) {scrolly--;}
if(keys & KEY_DOWN) {scrolly++;}
if(keys & KEY_RIGHT) {scrollx--;}
if(keys & KEY_LEFT) {scrollx++;}
bgSetScroll(bg1, scrollx & 7, scrolly & 7);
//a simple (unhealthy) redraw everything scroll engine:
for(iy = 0; iy < 32; iy++)
{
for(ix = 0; ix < 33; ix++)
{
int mx = (ix + (scrollx / 8)) % (64);
int my = (iy + (scrolly / 8)) % (64);
if(ix != 32)
bg1map[ix + iy * 32] = Layer_2Map[my * width + mx];
else
bg1map[32*32 + iy * 32] = Layer_2Map[my * width + mx];
}
}
}
return 0;
}