//------------------------------------------------------- // set up a 2D layer construced of bitmap sprites // this holds the image when rendering to the top screen //------------------------------------------------------- void initSubSprites(void){ //------------------------------------------------------- oamInit(&oamSub, SpriteMapping_Bmp_2D_256, false); int x = 0; int y = 0; int id = 0; //set up a 4x3 grid of 64x64 sprites to cover the screen for(y = 0; y < 3; y++) for(x = 0; x < 4; x++) { /* u16 *offset = &SPRITE_GFX_SUB[(x * 64) + (y * 64 * 256)]; oamSet(&oamSub, x + y * 4, x * 64, y * 64, 0, 15, SpriteSize_64x64, SpriteColorFormat_Bmp, offset, -1, false,false,false,false,false); */ oamSub.oamMemory[id].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y); oamSub.oamMemory[id].attribute[1] = ATTR1_SIZE_64 | (64 * x); oamSub.oamMemory[id].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x); id++; } swiWaitForVBlank(); oamUpdate(&oamSub); }
sv_sprite *sprite_new(pj_uint16_t *buffer, pj_int32_t width, pj_int32_t height, pj_uint16_t xpos, pj_uint16_t ypos) { static pj_int32_t num = 0; static pj_int32_t nextpos = 0; sv_sprite *spr; SpriteEntry *sprite; pj_uint16_t *icon; PJ_ASSERT_RETURN(buffer, NULL); spr = (sv_sprite*)malloc(sizeof(sv_sprite)); PJ_ASSERT_RETURN(spr, NULL); sprite = ((SpriteEntry *) OAM) + num; num++; sprite->attribute[0] = ATTR0_BMP | ATTR0_COLOR_16 | ypos; sprite->attribute[2] = ATTR2_ALPHA(1) | nextpos / 128; icon = ((pj_uint16_t *) SPRITE_GFX) + nextpos / 2; switch(width) { case 8: sprite->attribute[1] = ATTR1_SIZE_8 | xpos; nextpos += 8*8 * 2; break; case 16: sprite->attribute[1] = ATTR1_SIZE_16 | xpos; nextpos += 16*16 * 2; break; case 32: sprite->attribute[1] = ATTR1_SIZE_32 | xpos; nextpos += 32*32 * 2; break; case 64: sprite->attribute[1] = ATTR1_SIZE_64 | xpos; nextpos += 64*64 * 2; break; default: break; } spr->icons = buffer; spr->bytesperpixel = 2; spr->size = width; spr->nb = height / width; spr->xpos = xpos; spr->ypos = ypos; spr->sprite = sprite; spr->icon = icon; return spr; }
void initD3D() { int x,y,i; videoSetMode(MODE_3_3D); videoSetModeSub(MODE_5_2D | DISPLAY_BG2_ACTIVE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_2D_BMP_256); vramSetPrimaryBanks(VRAM_A_TEXTURE,VRAM_B_TEXTURE,VRAM_C_SUB_BG,VRAM_D_SUB_SPRITE); vramSetBankH(VRAM_H_LCD); vramSetBankI(VRAM_I_LCD); REG_BG0CNT = BG_PRIORITY(1); REG_BG2CNT_SUB = BG_BMP16_256x256 | BG_BMP_BASE(0) | BG_PRIORITY(1); REG_BG2PA_SUB = 1 << 8; REG_BG2PB_SUB = 0; REG_BG2PC_SUB = 0; REG_BG2PD_SUB = 1 << 8; REG_BG2X_SUB = 0; REG_BG2Y_SUB = 0; initSprites(); d3dSpriteRotations[0].hdx=256; d3dSpriteRotations[0].hdy=0; d3dSpriteRotations[0].vdx=0; d3dSpriteRotations[0].vdy=256; i=0; for (y = 0; y < 3; y++) { for (x = 0; x < 4; x++) { d3dSprites[i].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y); d3dSprites[i].attribute[1] = ATTR1_SIZE_64 | (64 * x); d3dSprites[i].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x); i++; } } updateOAM(); d3dScreen=true; }
/* Set up a 2D layer construced of bitmap sprites. This holds the * image when rendering to the top screen. From libnds example. */ static void initSubSprites(void) { oamInit(&oamSub, SpriteMapping_Bmp_2D_256, false); int x = 0; int y = 0; int id = 0; //set up a 4x3 grid of 64x64 sprites to cover the screen for(y = 0; y < 3; y++) for(x = 0; x < 4; x++) { oamSub.oamMemory[id].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y); oamSub.oamMemory[id].attribute[1] = ATTR1_SIZE_64 | (64 * x); oamSub.oamMemory[id].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x); id++; } swiWaitForVBlank(); oamUpdate(&oamSub); }
int main() { //enable vram and map it to the right places vramSetMainBanks( VRAM_A_MAIN_SPRITE, //A and B maped consecutivly as sprite memory VRAM_B_MAIN_SPRITE, //this gives us 256KB which is the max VRAM_C_MAIN_BG_0x06000000, //map C to background memory VRAM_D_LCD //not using D ); //set the video mode videoSetMode( MODE_0_2D | DISPLAY_SPR_ACTIVE | //turn on sprites DISPLAY_BG0_ACTIVE | //turn on background 0 DISPLAY_SPR_1D | //this is used when in tile mode DISPLAY_SPR_1D_BMP //and this in bitmap mode ); consoleInit(0, 0,BgType_Text4bpp, BgSize_T_256x256, 31,0, true, true); //turn off the sprites initSprites(); // direct bitmap sprite // print at using ansi escape sequence \x1b[line;columnH iprintf("\x1b[1;1HDirect Bitmap:"); sprites[0].attribute[0] = ATTR0_BMP | ATTR0_ROTSCALE_DOUBLE | 10; sprites[0].attribute[1] = ATTR1_SIZE_32 | 20; sprites[0].attribute[2] = ATTR2_ALPHA(1)| 0; // red 32*32 square for 1d bitmap mode for(int i=0;i<32*32;i++) SPRITE_GFX[i]=RGB15(31,0,0)|(1<<15); //dont forget alpha bit // 256 color sprite // print at using ansi escape sequence \x1b[line;columnH iprintf("\x1b[9;1H256 color:"); sprites[1].attribute[0] = ATTR0_COLOR_256 | ATTR0_ROTSCALE_DOUBLE | 75; sprites[1].attribute[1] = ATTR1_SIZE_32 | 20; // size 64x64, x 10 sprites[1].attribute[2] = 64; // Blue for 256 color sprite SPRITE_PALETTE[1]=RGB15(0,0,31); // blue 64*64 square for 256 color mode (must write two pixles at time) for(int i=0;i<32*16;i++) SPRITE_GFX[i+64*16]=(1<<8)|1; // 16 color sprite // print at using ansi escape sequence \x1b[line;columnH iprintf("\x1b[16;1H16 color:"); sprites[2].attribute[0] = ATTR0_COLOR_16 | ATTR0_ROTSCALE_DOUBLE | 135; sprites[2].attribute[1] = ATTR1_SIZE_32 | 20; sprites[2].attribute[2] = ATTR2_PALETTE(1) | 96; //yellow for 16 color sprite (it is using palette 1 so colors 16-31) SPRITE_PALETTE[17]=RGB15(31,31,0); // yellow 32*32 square for 16 color mode (must write 4 pixels at a time) for(int i=0;i<32*8;i++) SPRITE_GFX[i+96*16]=(1<<12)|(1<<8)|(1<<4)|1; int angle=0; //we tied all our sprites to the same rotation attributes (0) spriteRotations[0].hdx=256; spriteRotations[0].hdy=0; spriteRotations[0].vdx=0; spriteRotations[0].vdy=256; while(1) { angle+=64; spriteRotations[0].hdx = cosLerp(angle) >> 4; spriteRotations[0].hdy = sinLerp(angle) >> 4; spriteRotations[0].vdx = -spriteRotations[0].hdy; spriteRotations[0].vdy = spriteRotations[0].hdx; swiWaitForVBlank(); u32 display_temp = 0; scanKeys(); if(keysDown()&KEY_A) { screenshotbmp("shot.bmp"); display_temp=REG_DISPCNT; REG_DISPCNT=MODE_FB1; } if(keysUp()&KEY_A) { videoSetMode( MODE_0_2D | DISPLAY_SPR_ACTIVE | //turn on sprites DISPLAY_BG0_ACTIVE | //turn on background 0 DISPLAY_SPR_1D | //this is used when in tile mode DISPLAY_SPR_1D_BMP | //and this in bitmap mode DCAP_OFFSET(1)); } updateOAM(); } }
void gamescreen() { // set the mode for 2 text layers and two extended background layers videoSetMode(MODE_5_2D | //DISPLAY_BG1_ACTIVE | DISPLAY_BG3_ACTIVE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_1D_LAYOUT ); // set the first bank as background memory and the third as sub background memory // B and D are not used (if you want a bitmap greater than 256x256 you will need more // memory so another vram bank must be used and mapped consecutivly vramSetMainBanks(VRAM_A_MAIN_BG_0x6000000, VRAM_B_MAIN_SPRITE, //VRAM_B_MAIN_BG_0x6020000, VRAM_C_SUB_BG, VRAM_D_MAIN_BG_0x6020000); // set up our bitmap background BG3_CR = BG_BMP8_512x256 | BG_WRAP_ON| BG_BMP_BASE(0); //BG2_CR = BG_BMP8_256x256 | BG_BMP_BASE(8); BG1_CR = BG_32x32 | BG_TILE_BASE(0) | BG_MAP_BASE(8) | BG_256_COLOR; // these are rotation backgrounds so you must set the rotation attributes: // these are fixed point numbers with the low 8 bits the fractional part // this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap BG3_XDX = 1 << 8; BG3_XDY = 0; BG3_YDX = 0; BG3_YDY = 1 << 8; BG2_XDX = 1 << 8; BG2_XDY = 0; BG2_YDX = 0; BG2_YDY = 1 << 8; //our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2 BG3_CX = 0; BG3_CY = 0; BG2_CX = 0; BG2_CY = 0; for(int i = 0; i < 512*256; i++) BG_GFX[i] = ((u16*)topscreen_img_bin)[i]; for(int i = 0; i < 256; ++i) BG_PALETTE[i] = ((u16*)topscreen_pal_bin)[i]; //for(int i = 0; i < 256; ++i) //BG_PALETTE[i] = ((u16*)numbers_pal_bin)[i]; x = 0; y = 0; px = 128-32; py = 103; oldx = x; oldy = y; drag = false; touch_down = touchReadXY(); initSprites(); for(int i=0;i<256*256;i++) { SPRITE_GFX[i] = i % 256 | ((i % 256) << 8); } //for(int i = 0; i < 8*8*256/2; ++i) // ((u16*)BG_TILE_RAM(0))[i] = ((u16*)numbers_img_bin)[i]; //for(int i = 0; i < 32*32; ++i) //((u16*)BG_MAP_RAM(8))[i] = i%256; /* for(int y=0;y<64; ++y) for(int x=0;x<32; ++x) { SPRITE_GFX[y*64+x] = ((u16*)sprite_img_bin)[y*32+x]; }*/ const u8* frames[16]; frames[0] = walk_frame00_img_bin; frames[1] = walk_frame01_img_bin; frames[2] = walk_frame02_img_bin; frames[3] = walk_frame03_img_bin; frames[4] = walk_frame04_img_bin; frames[5] = walk_frame05_img_bin; frames[6] = walk_frame06_img_bin; frames[7] = walk_frame07_img_bin; frames[8] = walk_frame08_img_bin; frames[9] = walk_frame09_img_bin; frames[10] = walk_frame10_img_bin; frames[11] = walk_frame11_img_bin; frames[12] = walk_frame12_img_bin; frames[13] = walk_frame13_img_bin; frames[14] = walk_frame14_img_bin; frames[15] = walk_frame15_img_bin; const u8* pal_frames[16]; pal_frames[0] = walk_frame00_pal_bin; pal_frames[1] = walk_frame01_pal_bin; pal_frames[2] = walk_frame02_pal_bin; pal_frames[3] = walk_frame03_pal_bin; pal_frames[4] = walk_frame04_pal_bin; pal_frames[5] = walk_frame05_pal_bin; pal_frames[6] = walk_frame06_pal_bin; pal_frames[7] = walk_frame07_pal_bin; pal_frames[8] = walk_frame08_pal_bin; pal_frames[9] = walk_frame09_pal_bin; pal_frames[10] = walk_frame10_pal_bin; pal_frames[11] = walk_frame11_pal_bin; pal_frames[12] = walk_frame12_pal_bin; pal_frames[13] = walk_frame13_pal_bin; pal_frames[14] = walk_frame14_pal_bin; pal_frames[15] = walk_frame15_pal_bin; for(int i=0;i<64*32; ++i) SPRITE_GFX[i] = ((u16*)sprite_img_bin)[i]; for(int i=0;i<32*32/2; ++i) SPRITE_GFX[i+64*32] = ((u16*)use_icon_img_bin)[i]; for(int i=0;i<64*32; ++i) SPRITE_GFX[i+ (64*32) + (32*16)] = ((u16*)smalldoor_img_bin)[i]; for(int i=0;i<64*32; ++i) SPRITE_GFX[i] = ((u16*)walk_frame00_img_bin)[i]; for(int i = 0; i < 256; ++i) SPRITE_PALETTE[i] = ((u16*)walk_frame00_pal_bin)[i]; sprites[0].attribute[0] = ATTR0_SQUARE | ATTR0_TYPE_BLENDED | ATTR0_BMP | px; sprites[0].attribute[1] = ATTR1_SIZE_64 | py | ATTR1_FLIP_Y; sprites[0].attribute[2] = ATTR2_ALPHA(3); sprites[1].attribute[0] = ATTR0_SQUARE | ATTR0_COLOR_256 | (192-32-8) | ATTR0_DISABLED; sprites[1].attribute[1] = ATTR1_SIZE_32 | (256-32-8); sprites[1].attribute[2] = 64*2;//64*64;//64*32; sprites[2].attribute[0] = ATTR0_SQUARE | ATTR0_COLOR_256 | 90-6; sprites[2].attribute[1] = ATTR1_SIZE_64 | 200+16+2; sprites[2].attribute[2] = 64*2+32; int frame_index = 0; int sx = 0; //int sy = 0; bool left = true;; if (subscreen_mode == PDA) init_pda(); else init_doorminigame(); while(1) { swiWaitForVBlank(); frame_index += 1; frame_index = frame_index % 80; //SPRITE_GFX[i] = ((u16*)numbers_img_bin)[i]; // read the button states scanKeys(); touch = touchReadXY(); pressed = keysDown(); // buttons pressed this loop held = keysHeld(); // buttons currently held if (subscreen_mode != DIALOG) { sprites[0].attribute[0] = ATTR0_SQUARE | ATTR0_TYPE_BLENDED | ATTR0_COLOR_256 | mod(103,512); sprites[0].attribute[1] = ATTR1_SIZE_64 | mod(128-32,512) | (left ? 0: ATTR1_FLIP_X); sprites[0].attribute[2] = ATTR2_ALPHA(3); sprites[2].attribute[0] = ATTR0_SQUARE | ATTR0_TYPE_BLENDED | ATTR0_COLOR_256 | mod(90-6, 512); sprites[2].attribute[1] = ATTR1_SIZE_64 | mod((200+16+2 - px + 128-32 + door_pos/4),512); sprites[2].attribute[2] = ATTR2_ALPHA(1) | 64*2+32; } else { sprites[0].attribute[0] |= ATTR0_DISABLED; sprites[1].attribute[0] |= ATTR0_DISABLED; sprites[2].attribute[0] |= ATTR0_DISABLED; } if ((held & KEY_L) && (held & KEY_R)) swiSoftReset(); if ((pressed & KEY_LEFT) || (pressed & KEY_RIGHT)) { frame_index = 0; } if (held & KEY_LEFT) { left = true; px -= 1; sprites[0].attribute[1] = sprites[0].attribute[1] & ~ATTR1_FLIP_X; for(int i=0;i<64*32; ++i) SPRITE_GFX[i] = ((u16*)frames[frame_index/5])[i]; for(int i = 0; i < 256; ++i) SPRITE_PALETTE[i] = ((u16*)pal_frames[frame_index/5])[i]; } else if (held & KEY_RIGHT) { left = false; px += 1; sprites[0].attribute[1] = sprites[0].attribute[1] | ATTR1_FLIP_X; for(int i=0;i<64*32; ++i) SPRITE_GFX[i] = ((u16*)frames[frame_index/5])[i]; for(int i = 0; i < 256; ++i) SPRITE_PALETTE[i] = ((u16*)pal_frames[frame_index/5])[i]; } else { for(int i=0;i<64*32; ++i) SPRITE_GFX[i] = ((u16*)frames[0])[i]; for(int i = 0; i < 256; ++i) SPRITE_PALETTE[i] = ((u16*)pal_frames[0])[i]; } if (held & KEY_DOWN) py += 1; else if (held & KEY_UP) py -= 1; if ((px + 128) > 328-16 && (px + 128) < (328+35+16)) { sprites[1].attribute[0] &= ~ATTR0_DISABLED; //if (pressed & KEY_X) // { if (subscreen_mode == PDA) { init_doorminigame(); subscreen_mode = DOOR_MINIGAME; } else if (subscreen_mode == DOOR_MINIGAME) { init_pda(); subscreen_mode = PDA; } //} } else if ((px + 128) > 133-16 && (px + 128) < 133-16+32) { init_dialog(); subscreen_mode = DIALOG; } else { sprites[1].attribute[0] |= ATTR0_DISABLED; if (subscreen_mode == DOOR_MINIGAME) { init_pda(); subscreen_mode = PDA; } } sx = px; //sx += 1; if ((held & KEY_TOUCH) && drag) { x = oldx + (touch_down.px - touch.px); y = oldy + (touch_down.py - touch.py); } if (!(held & KEY_TOUCH)) drag = false; if (subscreen_mode != DIALOG) { BG3_CX = x*500 + sx<<8 ; BG3_CY = y*500 + (32<<8); } //2_CX = -x; //2_CY = -y; update_subscreen(); updateOAM(); } }
static inline void SetSprite(u32 objno,u32 Priority,u32 num,u16 alpha) { sprites[objno].attribute[2] = ATTR2_PRIORITY(Priority) | ATTR2_ALPHA(alpha) | num; }
void Main() { extern void Init(); Init(); init(); // Init sprites to display like a big bitmap background //VRAM_D_CR = VRAM_D_SUB_SPRITE | VRAM_ENABLE; int y, x; u16 * ptr = OAM_SUB; for ( y = 0 ; y < 3 ; y++ ) { for ( x = 0 ; x < 4 ; x++ ) { // attribute 0 *ptr++ = ATTR0_BMP | (y*64); // attribute 1 *ptr++ = ATTR1_SIZE_64 | (x*64); // attribute 2 *ptr++ = ATTR2_ALPHA(15) | (x*8+y*256); // pad (matrix) ptr++; } } int t = 0; while ( t < 2576 ) { #if FADES if ( fade_current < fade_target ) fade_current++; else if ( fade_current > fade_target ) fade_current--; int v = 0; if ( fade_current < (16 << 2) ) v = DARK | (16 - (fade_current >> 2)); else if ( fade_current > (16 << 2) ) v = BRIGHT | ((fade_current >> 2) - 16); REG_MASTER_BRIGHT = v; REG_MASTER_BRIGHT_SUB = v; #endif identity(); // Dual screen 3D if ( t&1 ) { REG_POWERCNT &= ~POWER_SWAP_LCDS; // main on bottom VRAM_C_CR = VRAM_ENABLE; VRAM_D_CR = VRAM_D_SUB_SPRITE | VRAM_ENABLE; REG_DISPCAPCNT = DCAP_BANK(2) | DCAP_ENABLE | DCAP_SIZE(3); } else { REG_POWERCNT |= POWER_SWAP_LCDS; // main on top VRAM_C_CR = VRAM_C_SUB_BG | VRAM_ENABLE; VRAM_D_CR = VRAM_ENABLE; REG_DISPCAPCNT = DCAP_BANK(3) | DCAP_ENABLE | DCAP_SIZE(3); translate(0, -5*4096, 0); } // Reset polygon attributes GFX_POLY_FORMAT = LIGHT0|POLYFRONT|SOLID; // Lightweight 3D player -olol int i; for ( i = 0 ; i < sizeof(demo)/sizeof(Drawable) ; i++ ) { if ( t >= demo[i].start && t < demo[i].end ) { push(); translate(0, demo[i].ty*1024, demo[i].tz*1024); scale(demo[i].s*1024, demo[i].s*1024, demo[i].s*1024); demo[i].draw(t-demo[i].start); pop(); } } t++; swap(); }