displaying sprites on the sub screen

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

displaying sprites on the sub screen

Post by codycox.c » Sun May 06, 2012 6:08 pm

I made a simple ds game and this is my code-

Code: Select all

#include <nds.h> 
 
//include sprites
 
#include "sprite.h" 

void init()
{
	int i;
	
	//point our video buffer to the start of bitmap background video
	u16* video_buffer_main = (u16*)BG_BMP_RAM(0);
 
	//set video mode to mode 5 with background 3 enabled
	//map vram a to start of main background graphics memory
	//initialize the background
	BACKGROUND.control[3] = BG_BMP16_256x256 | BG_BMP_BASE(0);
	
	BACKGROUND.bg3_rotation.hdy = 0;
	BACKGROUND.bg3_rotation.hdx = 1 << 8;
	BACKGROUND.bg3_rotation.vdx = 0;
	BACKGROUND.bg3_rotation.vdy = 1 << 8;
	for(i = 0;i < 256*256;i++)
	video_buffer_main[i] = RGB15(31,31,31) | BIT(15);
}
int main() {
// Setup the video modes.
int x = 0; int y = 0; int held = 0;
videoSetMode( MODE_5_2D | DISPLAY_BG3_ACTIVE);
vramSetPrimaryBanks(VRAM_A_MAIN_SPRITE,VRAM_B_LCD,VRAM_C_MAIN_BG_0x06000000,VRAM_D_LCD);
init();
//setup sprites
oamInit(&oamMain, SpriteMapping_1D_128 , false); //initialize the oam
u16* gfx = oamAllocateGfx(&oamMain, SpriteSize_32x32,SpriteColorFormat_256Color);//make room for the sprite
dmaCopy(spriteTiles,gfx, spriteTilesLen);//copy the sprite
dmaCopy(spritePal, SPRITE_PALETTE, spritePalLen); //copy the sprites palette
 
while (1) //infinite loop
{
scanKeys();
held = keysHeld();
if (held & KEY_UP)
y-=2;
if (held & KEY_DOWN)
y+=2;
if (held & KEY_LEFT)
x-=2;
if (held & KEY_RIGHT)
x+=2;
if (x<0)
x = 0;
else
if (x>224)
x = 224;
if (y<0)
y = 0;
else
if (y>160)
y = 160;
oamSet(&oamMain,0,x,y,0,0,SpriteSize_32x32,SpriteColorFormat_256Color,gfx,0,false,false,false,false,false);
swiWaitForVBlank();
oamUpdate(&oamMain);
}
return 0;
}
It displays a sprite on the top screen but how can I display a sprite on the bottom screen without using anything similar to lcdMainOnBottom?

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: displaying sprites on the sub screen

Post by Discostew » Sun May 06, 2012 7:10 pm

Much like using "oamMain" for the "main screen", use "oamSub" for the "sub screen".

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: displaying sprites on the sub screen

Post by mtheall » Mon May 07, 2012 7:37 pm

Please refactor your code to use the API's. It makes things easier to read and less susceptible to mistakes. I went ahead and did it for you so you can see what API's I'm talking about. I also threw in cloning your sprites and bg from the main engine to the sub engine. With the bgInit() functions, the affine matrix should be initialized to identity, so you won't have to. I will double check.

Edit: yes the affine matrix is initialized for you to identity matrix (scale = 1, rotation = 0) when you use bgInit()/bgInitSub()

Code: Select all

#include <nds.h>
 
//include sprites
 
#include "sprite.h" 

void init()
{
   int i;

   //initialize bg3
   int bgMain = bgInit(3,    BgType_Bmp16, BgSize_B16_256x256, 0, 0);
   int bgSub  = bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);

   u16* video_buffer_main = bgGetGfxPtr(bgMain);
   u16* video_buffer_sub  = bgGetGfxPtr(bgSub);

   for(i = 0;i < 256*256;i++)
      video_buffer_main[i] = ARGB15(1, 31,31,31);
   for(i = 0;i < 256*256;i++)
      video_buffer_sub[i]  = ARGB15(1, 31,31,31);

}

int main() {
   // Setup the video modes.
   int x = 0;
   int y = 0;
   int held = 0;

   videoSetMode(MODE_5_2D);
   vramSetPrimaryBanks(VRAM_A_MAIN_BG,     //need for main engine backgrounds
                       VRAM_B_MAIN_SPRITE, //need for main engine sprites
                       VRAM_C_SUB_BG,      //need for sub engine backgrounds
                       VRAM_D_SUB_SPRITE); //need for sub engine sprites

   init();

   //setup sprites
   oamInit(&oamMain, SpriteMapping_1D_128 , false); //initialize the main engine oam
   oamInit(&oamSub,  SpriteMapping_1D_128 , false); //initialize the sub engine oam

   //copy a sprite into main engine sprite vram
   u16* gfxMain = oamAllocateGfx(&oamMain, SpriteSize_32x32,SpriteColorFormat_256Color);
   dmaCopy(spriteTiles,gfxMain, spriteTilesLen);//copy the sprite
   dmaCopy(spritePal, SPRITE_PALETTE, spritePalLen); //copy the sprites palette

   u16* gfxSub = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_256Color);
   dmaCopy(spriteTiles, gfxSub, spriteTilesLen);//copy the sprite
   dmaCopy(spritePal, SPRITE_PALETTE_SUB, spritePalLen); //copy the sprites palette
 
   while (1) //infinite loop
   {
      scanKeys();
      held = keysHeld();
      if (held & KEY_UP)
         y -= 2;
      if (held & KEY_DOWN)
         y += 2;
      if (held & KEY_LEFT)
         x -= 2;
      if (held & KEY_RIGHT)
         x += 2;

      if (x<0)
         x = 0;
      else if (x>224)
         x = 224;

      if (y<0)
         y = 0;
      else if (y>160)
         y = 160;

      oamSet(&oamMain,0,x,y,0,0,SpriteSize_32x32,SpriteColorFormat_256Color,gfxMain,0,false,false,false,false,false);
      oamSet(&oamSub, 0,x,y,0,0,SpriteSize_32x32,SpriteColorFormat_256Color,gfxSub, 0,false,false,false,false,false);

      swiWaitForVBlank();
      oamUpdate(&oamMain); //flush main engine OAM
      oamUpdate(&oamSub);  //flush sub engine OAM
   }

   return 0;
}

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

Re: displaying sprites on the sub screen

Post by codycox.c » Wed May 09, 2012 11:38 pm

Thanks

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

Re: displaying sprites on the sub screen

Post by codycox.c » Wed May 09, 2012 11:50 pm

Ok I tried but it did not display the sprite for some reason. Here is my code-

Code: Select all

/****************************************
* 	NDS Sprite Tutorial    	
* 	Author: opearn		
****************************************/
 
/*includes*/
 
//include
 
#include <nds.h> 
 
//include sprites
 
#include "sprite.h" 

void init()
{
	int i;
	
	//point our video buffer to the start of bitmap background video
	u16* video_buffer_main = (u16*)BG_BMP_RAM(0);
 
	//set video mode to mode 5 with background 3 enabled
	//map vram a to start of main background graphics memory
	//initialize the background
	BACKGROUND.control[3] = BG_BMP16_256x256 | BG_BMP_BASE(0);
	
	BACKGROUND.bg3_rotation.hdy = 0;
	BACKGROUND.bg3_rotation.hdx = 1 << 8;
	BACKGROUND.bg3_rotation.vdx = 0;
	BACKGROUND.bg3_rotation.vdy = 1 << 8;
	for(i = 0;i < 256*256;i++)
	video_buffer_main[i] = RGB15(31,31,31) | BIT(15);
}
int main() {
// Setup the video modes.
int x = 0; int y = 0; int held = 0;
videoSetModeSub( MODE_5_2D);
vramSetPrimaryBanks(VRAM_A_MAIN_SPRITE,VRAM_B_MAIN_BG,VRAM_C_LCD,VRAM_D_SUB_SPRITE); //copy the sprites palette
oamInit(&oamSub, SpriteMapping_1D_128, false); //initialize the oam
u16* gfx = oamAllocateGfx(&oamSub, SpriteSize_32x32,SpriteColorFormat_256Color);//make room for the sprite
dmaCopy(spriteTiles,gfx, spriteTilesLen);//copy the sprite
dmaCopy(spritePal, SPRITE_PALETTE, spritePalLen);
while (1) //infinite loop
{
scanKeys();
held = keysHeld();
if (held & KEY_UP)
y-=2;
if (held & KEY_DOWN)
y+=2;
if (held & KEY_LEFT)
x-=2;
if (held & KEY_RIGHT)
x+=2;
if (x<0)
x = 0;
else
if (x>224)
x = 224;
if (y<0)
y = 0;
else
if (y>160)
y = 160;
oamSet(&oamSub,0,x,y,0,0,SpriteSize_32x32,SpriteColorFormat_256Color,gfx,0,false,false,false,false,false);
swiWaitForVBlank();
oamUpdate(&oamSub);
}
return 0;
}

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

Re: displaying sprites on the sub screen

Post by codycox.c » Wed May 09, 2012 11:58 pm

Never mind I figured it out I put SPRITE_PALLETE when I should have put SPRITE_PALLETE_SUB

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: displaying sprites on the sub screen

Post by mtheall » Thu May 10, 2012 12:13 am

You didn't refactor your code at all. Also, please use the code tags for pasting code. It is the button between Quote and List in the formatting toolbar. I went ahead and put the code blocks for you.

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

Re: displaying sprites on the sub screen

Post by codycox.c » Tue May 15, 2012 10:44 pm

Why do you have to set vram a to main background to display the background?

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: displaying sprites on the sub screen

Post by mtheall » Tue May 15, 2012 11:22 pm

It doesn't HAVE to be VRAM A. But I see you used VRAM_B_MAIN_BG. If you want to use VRAM B, then you probably should use VRAM_B_MAIN_BG_0x06000000. VRAM_B_MAIN_BG is actually VRAM_B_MAIN_BG_0x06020000. My site is down for a few hours, but when it comes back up, look here

codycox.c
Posts: 49
Joined: Thu Mar 08, 2012 2:26 am

Re: displaying sprites on the sub screen

Post by codycox.c » Wed May 16, 2012 3:10 am

I do not understand the map base parameter in bgInit function can you explain it to me I believe it has something to do with memory?

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests