//Copy Sprites to Shadow void copyShadow() { int i; for(i=0; i<NUMSPRITES; i++) { shadowOAM[i].attr0 = sprites[i].row | ATTR0_8BPP | sprites[i].shape; shadowOAM[i].attr1 = sprites[i].col | sprites[i].size; shadowOAM[i].attr2 = sprites[i].start | ATTR2_PALETTE_BANK(0); } }
//Sets Up Sprites void initializeSprites() { int i; // Load sprite palette for(i=0; i<256; i++) { SPRITEPAL[i] = spritePalette[i]; } // Load sprite images for(i=0; i<64/2; i++) { CHARBLOCKBASE[4].tileimg[i] = spriteTiles[i]; } for(i=0; i<NUMSPRITES; i++) { shadowOAM[i].attr0 = sprites[i].row | ATTR0_8BPP | sprites[i].shape; shadowOAM[i].attr1 = sprites[i].col | sprites[i].size; shadowOAM[i].attr2 = sprites[i].start | ATTR2_PALETTE_BANK(0); } //Hide Sprites for(i=1; i<128; i++) { shadowOAM[i].attr0 = ATTR0_HIDE; } waitForVblank(); for(i=0; i<128; i++) { SPRITEMEM[i] = shadowOAM[i]; } }
int main(void) { //Initializing Program variables int i; int visible = 1; int press = 1; int oldSprite = 0; int currentSprite = 0; ObjAttr shadowOAM[128]; REG_DISPCTL = MODE0 | BG0_ENABLE | OBJ_ENABLE; // Load background palette for(i=0; i<256; i++) { PALETTE[i] = backgroundPal[i]; } // Load background tile images for(i=0; i<backgroundTilesLen/2; i++) { CHARBLOCKBASE[0].tileimg[i] = backgroundTiles[i]; } //Make Map for(i=0; i<1024; i++) { SCREENBLOCKBASE[28].tilemap[i] = 1; } REG_BG0CNT = SBB(28) | CBB(0) | COLOR256 | BG_SIZE0; //Define Sprites Sprite sprites[NUMSPRITES] = {{16,16,SPRITEOFFSET16(0,0),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(0,8),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(0,16),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {5,100,SPRITEOFFSET16(0,24),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(4,0),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(4,8),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(4,16),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}, {0,0,SPRITEOFFSET16(4,16),ATTR0_SQUARE,ATTR1_SIZE32,32,32,1,1}}; // Load sprite palette for(i=0; i<256; i++) { SPRITEPAL[i] = spritePalette[i]; } // Load sprite images for(i=0; i<8192/2; i++) { CHARBLOCKBASE[4].tileimg[i] = spriteTiles[i]; } // Set attributes for all active sprites for(i=0; i<NUMSPRITES; i++) { shadowOAM[i].attr0 = sprites[i].row | ATTR0_8BPP | sprites[i].shape; shadowOAM[i].attr1 = sprites[i].col | sprites[i].size; shadowOAM[i].attr2 = sprites[i].start | ATTR2_PALETTE_BANK(0); } //Hide Sprites for(i=0; i<128; i++) { shadowOAM[i].attr0 = ATTR0_HIDE; } waitForVblank(); //Move Sprites to Mem for(i=0; i<128; i++) { SPRITEMEM[i] = shadowOAM[i]; } while(FOREVER) { //Check for Movement and change direction if(KEY_DOWN_NOW(BUTTON_UP)) { shadowOAM[currentSprite].attr0 = ATTR0_HIDE; sprites[currentSprite].row -= sprites[currentSprite].rdel; oldSprite = currentSprite; currentSprite = 2; sprites[currentSprite].col = sprites[oldSprite].col; sprites[currentSprite].row = sprites[oldSprite].row; } if(KEY_DOWN_NOW(BUTTON_DOWN)) { shadowOAM[currentSprite].attr0 = ATTR0_HIDE; sprites[currentSprite].row += sprites[currentSprite].rdel; oldSprite = currentSprite; currentSprite = 0; sprites[currentSprite].col = sprites[oldSprite].col; sprites[currentSprite].row = sprites[oldSprite].row; } if(KEY_DOWN_NOW(BUTTON_LEFT)) { shadowOAM[currentSprite].attr0 = ATTR0_HIDE; sprites[currentSprite].col -= sprites[currentSprite].cdel; oldSprite = currentSprite; currentSprite = 6; sprites[currentSprite].col = sprites[oldSprite].col; sprites[currentSprite].row = sprites[oldSprite].row; } if(KEY_DOWN_NOW(BUTTON_RIGHT)) { shadowOAM[currentSprite].attr0 = ATTR0_HIDE; sprites[currentSprite].col += sprites[currentSprite].cdel; oldSprite = currentSprite; currentSprite = 5; sprites[currentSprite].col = sprites[oldSprite].col; sprites[currentSprite].row = sprites[oldSprite].row; } //Check for second sprites visibility if(KEY_DOWN_NOW(BUTTON_START) && press) { if(visible) visible = 0; else visible = 1; waitForVblank(); press = 0; } if(!KEY_DOWN_NOW(BUTTON_START)) { press = 1; } //Collision Detection if(sprites[currentSprite].row < HEIGHTMIN) { sprites[currentSprite].row = HEIGHTMIN; } if(sprites[currentSprite].col < WIDTHMIN) { sprites[currentSprite].col = WIDTHMIN; } if(sprites[currentSprite].row + sprites[currentSprite].height > HEIGHTMAX) { sprites[currentSprite].row = HEIGHTMAX - sprites[currentSprite].height; } if(sprites[currentSprite].col + sprites[currentSprite].width > WIDTHMAX) { sprites[currentSprite].col = WIDTHMAX - sprites[currentSprite].width; } //Move Sprites to Memory shadowOAM[currentSprite].attr0 = sprites[currentSprite].row | ATTR0_8BPP | sprites[currentSprite].shape; shadowOAM[currentSprite].attr1 = (MASK&sprites[currentSprite].col) | sprites[currentSprite].size; shadowOAM[currentSprite].attr2 = sprites[currentSprite].start | ATTR2_PALETTE_BANK(0); if(visible) { shadowOAM[3].attr0 = sprites[3].row | ATTR0_8BPP | sprites[3].shape; shadowOAM[3].attr1 = (MASK&sprites[3].col) | sprites[3].size; shadowOAM[3].attr2 = sprites[3].start | ATTR2_PALETTE_BANK(0); } else { shadowOAM[3].attr0 = ATTR0_HIDE; } waitForVblank(); for(i=0; i<NUMSPRITES; i++) { SPRITEMEM[i] = shadowOAM[i]; } } return 0; }
//=====================Main int main(void) { //Set Video Control REG_DISPCNT = BG2_ENABLE | BG1_ENABLE | BG0_ENABLE | MODE_0 | OBJ_ENABLE; //tells the Background 0 control register //where the screen map is, the tiles, the color, and size0=256x256 //background 1 is the collision map REG_BG0CNT= CBB(0) | SBB(8) | COLOR16 | BG_SIZE0; REG_BG1CNT= CBB(2) | SBB(10) | COLOR16 | BG_SIZE2; REG_BG2CNT= CBB(3) | SBB(12) | COLOR16 | BG_SIZE2; //sets the BG's vertical and horz offsets to 0 REG_BG0HOFS=0; REG_BG0VOFS=20; REG_BG1HOFS=0; REG_BG1VOFS=0; REG_BG2HOFS=0; REG_BG2VOFS=0; int i; int speed=3; int hoff=0; int voff=0; //loads the picture pallet //This is the same for all backgrounds for(i=0;i<background0PalLen/2;i++) PALETTE_MEM[i]=background0Pal[i]; //===========================================Sprites ObjAttr shadowOAM[128]; //defining sprites with room for more Sprite spriteArray[NUMSPRITES]={ {WALKINGROW, 16, SPRITEOFFSET16(0,0), ATTR0_TALL, ATTR1_SIZE64, 32, 64, TRUE} } typedef struct { int printRow; int printCol; int arrayLocation; int shape; int size; int height; int width; int active; } Sprite; //Loads sprite tiles for(i=0; i<characterSpritesTilesLen/2; i++) charbase[4].tileimg[i] = characterSpritesTiles[i]; //set attributes of the sprite in the duplicate mem for(i=0; i<NUMSPRITES; i++) { shadowOAM[i].attr0 = spriteArray[i].row | ATTR0_4BPP | spriteArray[i].shape; shadowOAM[i].attr1 = spriteArray[i].col | spriteArray[i].size; shadowOAM[i].attr2 = spriteArray[i].start | ATTR2_PALETTE_BANK(0); } //hides all sprites except for main guy for(i=0; i<128; i++) { if(!spriteArray[i].active) shadowOAM[i].attr0 = ATTR0_HIDE; } waitForVblank(); // Copy shadow into real for(i=0; i<128; i++) { SPRITEMEM[i] = shadowOAM[i]; } while(1) { //LOADS BACKGROUND 0 //loads all the tiles and collision map for(i=0;i<background0TilesLen/2;i++) charbase[3].tileimg[i]=background0Tiles[i]; //loads the map for(i=0;i<background0MapLen/2;i++) screenbase[12].tilemap[i]=background0Map[i]; //LOADS BACKGROUND 1 //loads all the tiles and collision map for(i=0;i<background1TilesLen/2;i++) charbase[2].tileimg[i]=background1Tiles[i]; //loads the map for(i=0;i<background1MapLen/2;i++) screenbase[10].tilemap[i]=background1Map[i]; //LOADS BACKGROUND 2 //loads all the tiles and collision map for(i=0;i<background2TilesLen/2;i++) charbase[0].tileimg[i]=background2Tiles[i]; //loads the map for(i=0;i<background2MapLen/2;i++) screenbase[8].tilemap[i]=background2Map[i]; // these keys move the background around the screen if(KEY_DOWN_NOW(BUTTON_RIGHT))hoff+=speed; if(KEY_DOWN_NOW(BUTTON_LEFT))hoff-=speed; if(KEY_DOWN_NOW(BUTTON_UP))voff-=speed; if(KEY_DOWN_NOW(BUTTON_DOWN))voff+=speed; REG_BG0HOFS=hoff; //REG_BG0VOFS=voff; REG_BG1HOFS=hoff/20; //REG_BG1VOFS=voff/20; waitForVBlank(); } return 0; }