gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Background Dissapears when Updated...

#26222 - mr_square - Thu Sep 09, 2004 8:32 pm

Hi all - I currently have a nice finite state machine going to handle my sprite animations, and am now looking at getting some backgrounds working. Currently I have a sprite on the screen, and can happily get a background to display, but upon calling UpdateBackground(), it just dissappears and leaves blackness behind.

This is all my background related code:

Code:

SetMode(OBJ_MAP_1D | MODE_1 | OBJ_ENABLE);

Bg background1;

background1.number = 2;             
background1.charBaseBlock = 0;             
background1.screenBaseBlock = 28;       
background1.colorMode = BG_COLOR_256;           
background1.size = ROTBG_SIZE_256x256;         
background1.mosaic = 0;   
background1.x_scroll = 200;     
background1.y_scroll = 140;

EnableBackground(&background1);

for(loop = 0; loop <256; loop++)
{
BGPaletteMem[loop] = TILESPalette[loop];
}

for(loop = 0; loop < TILES_WIDTH * TILES_HEIGHT /2; loop++)
{
background1.tileData[loop] = TILESData[loop];
}

temp = (u16*)ground;
for(loop = 0; loop < 16*16/2; loop++) 
{
  background1.mapData[loop] = temp[loop];
}




That by itself displays the background, but calling

Code:

UpdateBackground(&background1)


makes it dissapear.

Any ideas?

#26223 - poslundc - Thu Sep 09, 2004 8:42 pm

Hard to tell without seeing the code for EnableBackground and UpdateBackground...

Dan.

#26228 - DiscoStew - Thu Sep 09, 2004 9:16 pm

Those functions came from The Pern Project website of Dovoto's old tutorials, right? I did have those function on my computer, so here they are for those people trying to help mr_square out with this problem
Code:
void EnableBackground(Bg* bg)
{
   u16 temp;

   bg->tileData = (u16*)CharBaseBlock(bg->charBaseBlock);
   bg->mapData = (u16*)ScreenBaseBlock(bg->screenBaseBlock);
   temp = bg->size | (bg->charBaseBlock<<CHAR_SHIFT) | (bg->screenBaseBlock<<SCREEN_SHIFT)
      | bg->colorMode | bg->mosiac;

   switch(bg->number)
   {
   case 0:
      {
         REG_BG0CNT = temp;
         REG_DISPCNT |= BG0_ENABLE;
      }break;
   case 1:
      {
         REG_BG1CNT = temp;
         REG_DISPCNT |= BG1_ENABLE;
      }break;
   case 2:
      {
         REG_BG2CNT = temp;
         REG_DISPCNT |= BG2_ENABLE;
      }break;
   case 3:
      {
         REG_BG3CNT = temp;
         REG_DISPCNT |= BG3_ENABLE;
      }break;
   
   default:break;
   
   }
}

void UpdateBackground(Bg* bg)
{
   switch(bg->number)
   {
   case 0:
      REG_BG0HOFS = bg->x_scroll;
      REG_BG0VOFS = bg->y_scroll;
      break;
   case 1:
      REG_BG1HOFS = bg->x_scroll;
      REG_BG1VOFS = bg->y_scroll;
      break;
   case 2:
      if(!(REG_DISPCNT & MODE_0))//it is a rot background
      {
         REG_BG2X = bg->DX;
         REG_BG2Y = bg->DY;
         
         REG_BG2PA = bg->PA;
         REG_BG2PB = bg->PB;
         REG_BG2PC = bg->PC;
         REG_BG2PD = bg->PD;
      }
      else  //it is a text background
      {
         REG_BG2HOFS = bg->x_scroll;
         REG_BG2VOFS = bg->y_scroll;
      }
      break;
   case 3:
      if(!(REG_DISPCNT & MODE_0))//it is a rot background
      {
         REG_BG3X = bg->DX;
         REG_BG3Y = bg->DY;
         
         REG_BG3PA = bg->PA;
         REG_BG3PB = bg->PB;
         REG_BG3PC = bg->PC;
         REG_BG3PD = bg->PD;
      }
      else //it is a text background
      {
         REG_BG3HOFS = bg->x_scroll;
         REG_BG3VOFS = bg->y_scroll;
      }
      break;
   default: break;
   }

I'll try to examine it also.

EDIT:
I think I see the problem. In the little code you showed us, you were setting everything correct, except you were putting in data for x_scroll/y_scroll, but you were setting up an affine (rot/scale)_ background, which uses the DX/DY/PA/PB/PC/PD variables of the structure. When initializing something, you pretty much get garbage unless you specify what to put in there. You had not, therefore when you updated the background, it used the garbage in those variables, which could be anything. The reason why it probably worked before calling the update function is because it wasn't using garbage. Doesn't on hardware/emulator the registers used for things like backgrounds and sprites start at a default value of 0? If you haven't touched those position registers, then the 0s in those registers would be used.

Try putting values in DX/DY/PA/PB/PC/PD for affine backgrounds instead of x_scroll/y_scroll of the structure.
_________________
DS - It's all about DiscoStew

#26232 - mr_square - Thu Sep 09, 2004 10:06 pm

Thanks Stew - I think you've got it. I tried inserting some random numbers into those registers and got a display at least (not the right background, but not blackness at least)

I took my code pretty much straight from GBAJunkie's tutorial. He calls
Code:
RotateBackground(&bg2,angle,119,79,zoom);

before first updating the background, which I'm not currently doing, so I presume that is setting up all the required stuff for him and avoiding the problem I'm getting.

Unfortunately I need some predefined sin and cos headers before that function will work - anyone know where I can get some?[/quote]

#26233 - DiscoStew - Thu Sep 09, 2004 10:12 pm

Just copy and paste. These are in 360 degree and 24/8 FIXED
Code:
s32 SIN[360]=
{               0x0000,
                    0x0477, 0x08EF, 0x0D65, 0x11DB, 0x164F, 0x1AC2, 0x1F32, 0x23A0, 0x280C, 0x2C74, 0x30D8, 0x3539, 0x3996, 0x3DEE, 0x4241, 0x4690,
                    0x4AD8, 0x4F1B, 0x5358, 0x578E, 0x5BBD, 0x5FE6, 0x6406, 0x681F, 0x6C30, 0x7039, 0x7438, 0x782F, 0x7C1C, 0x7FFF, 0x83D9, 0x87A8,
                    0x8B6D, 0x8F27, 0x92D5, 0x9679, 0x9A10, 0x9D9B, 0xA11B, 0xA48D, 0xA7F3, 0xAB4C, 0xAE97, 0xB1D5, 0xB504, 0xB826, 0xBB39, 0xBE3E,
                    0xC134, 0xC41B, 0xC6F3, 0xC9BB, 0xCC73, 0xCF1B, 0xD1B3, 0xD43B, 0xD6B3, 0xD919, 0xDB6F, 0xDDB3, 0xDFE7, 0xE208, 0xE418, 0xE617,
                    0xE803, 0xE9DE, 0xEBA6, 0xED5B, 0xEEFF, 0xF08F, 0xF20D, 0xF378, 0xF4D0, 0xF615, 0xF746, 0xF865, 0xF970, 0xFA67, 0xFB4B, 0xFC1C,
                    0xFCD9, 0xFD82, 0xFE17, 0xFE98, 0xFF06, 0xFF60, 0xFFA6, 0xFFD8, 0xFFF6, 0xFFFF, 0xFFF6, 0xFFD8, 0xFFA6, 0xFF60, 0xFF06, 0xFE98,
                    0xFE17, 0xFD82, 0xFCD9, 0xFC1C, 0xFB4B, 0xFA67, 0xF970, 0xF865, 0xF746, 0xF615, 0xF4D0, 0xF378, 0xF20D, 0xF08F, 0xEEFF, 0xED5B,
                    0xEBA6, 0xE9DE, 0xE803, 0xE617, 0xE419, 0xE208, 0xDFE7, 0xDDB3, 0xDB6F, 0xD919, 0xD6B3, 0xD43B, 0xD1B4, 0xCF1B, 0xCC73, 0xC9BB,
                    0xC6F3, 0xC41B, 0xC134, 0xBE3E, 0xBB3A, 0xB826, 0xB505, 0xB1D5, 0xAE97, 0xAB4C, 0xA7F3, 0xA48D, 0xA11B, 0x9D9C, 0x9A10, 0x9679,
                    0x92D6, 0x8F27, 0x8B6D, 0x87A8, 0x83D9, 0x8000, 0x7C1C, 0x782F, 0x7438, 0x7039, 0x6C30, 0x6820, 0x6407, 0x5FE6, 0x5BBE, 0x578E,
                    0x5358, 0x4F1B, 0x4AD9, 0x4690, 0x4242, 0x3DEE, 0x3996, 0x3539, 0x30D9, 0x2C74, 0x280C, 0x23A1, 0x1F32, 0x1AC2, 0x1650, 0x11DB,
                    0x0D66, 0x08EF, 0x0477, 0x0000, 0xFFFFFB89, 0xFFFFF712, 0xFFFFF29B, 0xFFFFEE25, 0xFFFFE9B1, 0xFFFFE53E, 0xFFFFE0CE, 0xFFFFDC60, 0xFFFFD7F5, 0xFFFFD38C, 0xFFFFCF28, 0xFFFFCAC7,
                    0xFFFFC66A, 0xFFFFC212, 0xFFFFBDBF, 0xFFFFB971, 0xFFFFB528, 0xFFFFB0E5, 0xFFFFACA8, 0xFFFFA872, 0xFFFFA443, 0xFFFFA01A, 0xFFFF9BFA, 0xFFFF97E1, 0xFFFF93D0, 0xFFFF8FC8, 0xFFFF8BC8, 0xFFFF87D1,
                    0xFFFF83E4, 0xFFFF8001, 0xFFFF7C27, 0xFFFF7858, 0xFFFF7493, 0xFFFF70D9, 0xFFFF6D2B, 0xFFFF6988, 0xFFFF65F0, 0xFFFF6265, 0xFFFF5EE6, 0xFFFF5B73, 0xFFFF580D, 0xFFFF54B5, 0xFFFF5169, 0xFFFF4E2C,
                    0xFFFF4AFC, 0xFFFF47DA, 0xFFFF44C7, 0xFFFF41C2, 0xFFFF3ECC, 0xFFFF3BE5, 0xFFFF390E, 0xFFFF3646, 0xFFFF338D, 0xFFFF30E5, 0xFFFF2E4D, 0xFFFF2BC5, 0xFFFF294E, 0xFFFF26E7, 0xFFFF2491, 0xFFFF224D,
                    0xFFFF201A, 0xFFFF1DF8, 0xFFFF1BE8, 0xFFFF19E9, 0xFFFF17FD, 0xFFFF1622, 0xFFFF145A, 0xFFFF12A5, 0xFFFF1101, 0xFFFF0F71, 0xFFFF0DF3, 0xFFFF0C88, 0xFFFF0B30, 0xFFFF09EB, 0xFFFF08BA, 0xFFFF079B,
                    0xFFFF0690, 0xFFFF0599, 0xFFFF04B5, 0xFFFF03E4, 0xFFFF0327, 0xFFFF027E, 0xFFFF01E9, 0xFFFF0168, 0xFFFF00FA, 0xFFFF00A0, 0xFFFF005A, 0xFFFF0028, 0xFFFF000A, 0xFFFF0001, 0xFFFF000A, 0xFFFF0028,
                    0xFFFF005A, 0xFFFF00A0, 0xFFFF00FA, 0xFFFF0167, 0xFFFF01E9, 0xFFFF027E, 0xFFFF0327, 0xFFFF03E4, 0xFFFF04B5, 0xFFFF0599, 0xFFFF0690, 0xFFFF079B, 0xFFFF08BA, 0xFFFF09EB, 0xFFFF0B30, 0xFFFF0C88,
                    0xFFFF0DF3, 0xFFFF0F71, 0xFFFF1101, 0xFFFF12A4, 0xFFFF145A, 0xFFFF1622, 0xFFFF17FD, 0xFFFF19E9, 0xFFFF1BE7, 0xFFFF1DF8, 0xFFFF2019, 0xFFFF224D, 0xFFFF2491, 0xFFFF26E7, 0xFFFF294D, 0xFFFF2BC5,
                    0xFFFF2E4C, 0xFFFF30E5, 0xFFFF338D, 0xFFFF3645, 0xFFFF390D, 0xFFFF3BE5, 0xFFFF3ECC, 0xFFFF41C2, 0xFFFF44C6, 0xFFFF47DA, 0xFFFF4AFB, 0xFFFF4E2B, 0xFFFF5169, 0xFFFF54B4, 0xFFFF580D, 0xFFFF5B73,
                    0xFFFF5EE5, 0xFFFF6264, 0xFFFF65F0, 0xFFFF6987, 0xFFFF6D2A, 0xFFFF70D9, 0xFFFF7493, 0xFFFF7857, 0xFFFF7C27, 0xFFFF8000, 0xFFFF83E4, 0xFFFF87D1, 0xFFFF8BC7, 0xFFFF8FC7, 0xFFFF93CF, 0xFFFF97E0,
                    0xFFFF9BF9, 0xFFFFA01A, 0xFFFFA442, 0xFFFFA872, 0xFFFFACA8, 0xFFFFB0E4, 0xFFFFB527, 0xFFFFB970, 0xFFFFBDBE, 0xFFFFC212, 0xFFFFC66A, 0xFFFFCAC6, 0xFFFFCF27, 0xFFFFD38C, 0xFFFFD7F4, 0xFFFFDC5F,
                    0xFFFFE0CD, 0xFFFFE53E, 0xFFFFE9B0, 0xFFFFEE25, 0xFFFFF29A, 0xFFFFF711, 0xFFFFFB88,
};                   
s32 COS[360]=
{               0x10000,
                    0xFFF6, 0xFFD8, 0xFFA6, 0xFF60, 0xFF06, 0xFE98, 0xFE17, 0xFD82, 0xFCD9, 0xFC1C, 0xFB4B, 0xFA67, 0xF970, 0xF865, 0xF746, 0xF615,
                    0xF4D0, 0xF378, 0xF20D, 0xF08F, 0xEEFF, 0xED5B, 0xEBA6, 0xE9DE, 0xE803, 0xE617, 0xE419, 0xE208, 0xDFE7, 0xDDB3, 0xDB6F, 0xD919,
                    0xD6B3, 0xD43B, 0xD1B3, 0xCF1B, 0xCC73, 0xC9BB, 0xC6F3, 0xC41B, 0xC134, 0xBE3E, 0xBB3A, 0xB826, 0xB504, 0xB1D5, 0xAE97, 0xAB4C,
                    0xA7F3, 0xA48D, 0xA11B, 0x9D9C, 0x9A10, 0x9679, 0x92D5, 0x8F27, 0x8B6D, 0x87A8, 0x83D9, 0x8000, 0x7C1C, 0x782F, 0x7438, 0x7039,
                    0x6C30, 0x681F, 0x6407, 0x5FE6, 0x5BBE, 0x578E, 0x5358, 0x4F1B, 0x4AD8, 0x4690, 0x4242, 0x3DEE, 0x3996, 0x3539, 0x30D8, 0x2C74,
                    0x280C, 0x23A0, 0x1F32, 0x1AC2, 0x164F, 0x11DB, 0x0D65, 0x08EF, 0x0477, 0x0000, 0xFFFFFB89, 0xFFFFF711, 0xFFFFF29B, 0xFFFFEE25, 0xFFFFE9B1, 0xFFFFE53E,
                    0xFFFFE0CE, 0xFFFFDC60, 0xFFFFD7F5, 0xFFFFD38C, 0xFFFFCF28, 0xFFFFCAC7, 0xFFFFC66A, 0xFFFFC212, 0xFFFFBDBF, 0xFFFFB970, 0xFFFFB528, 0xFFFFB0E5, 0xFFFFACA8, 0xFFFFA872, 0xFFFFA443, 0xFFFFA01A,
                    0xFFFF9BFA, 0xFFFF97E1, 0xFFFF93D0, 0xFFFF8FC8, 0xFFFF8BC8, 0xFFFF87D1, 0xFFFF83E4, 0xFFFF8001, 0xFFFF7C27, 0xFFFF7858, 0xFFFF7493, 0xFFFF70D9, 0xFFFF6D2B, 0xFFFF6988, 0xFFFF65F0, 0xFFFF6265,
                    0xFFFF5EE5, 0xFFFF5B73, 0xFFFF580D, 0xFFFF54B4, 0xFFFF5169, 0xFFFF4E2B, 0xFFFF4AFC, 0xFFFF47DA, 0xFFFF44C7, 0xFFFF41C2, 0xFFFF3ECC, 0xFFFF3BE5, 0xFFFF390E, 0xFFFF3646, 0xFFFF338D, 0xFFFF30E5,
                    0xFFFF2E4D, 0xFFFF2BC5, 0xFFFF294D, 0xFFFF26E7, 0xFFFF2491, 0xFFFF224D, 0xFFFF2019, 0xFFFF1DF8, 0xFFFF1BE8, 0xFFFF19E9, 0xFFFF17FD, 0xFFFF1622, 0xFFFF145A, 0xFFFF12A5, 0xFFFF1101, 0xFFFF0F71,
                    0xFFFF0DF3, 0xFFFF0C88, 0xFFFF0B30, 0xFFFF09EB, 0xFFFF08BA, 0xFFFF079B, 0xFFFF0690, 0xFFFF0599, 0xFFFF04B5, 0xFFFF03E4, 0xFFFF0327, 0xFFFF027E, 0xFFFF01E9, 0xFFFF0168, 0xFFFF00FA, 0xFFFF00A0,
                    0xFFFF005A, 0xFFFF0028, 0xFFFF000A, 0xFFFF0001, 0xFFFF000A, 0xFFFF0028, 0xFFFF005A, 0xFFFF00A0, 0xFFFF00FA, 0xFFFF0167, 0xFFFF01E9, 0xFFFF027E, 0xFFFF0327, 0xFFFF03E4, 0xFFFF04B5, 0xFFFF0599,
                    0xFFFF0690, 0xFFFF079B, 0xFFFF08BA, 0xFFFF09EB, 0xFFFF0B30, 0xFFFF0C88, 0xFFFF0DF3, 0xFFFF0F71, 0xFFFF1101, 0xFFFF12A5, 0xFFFF145A, 0xFFFF1622, 0xFFFF17FD, 0xFFFF19E9, 0xFFFF1BE7, 0xFFFF1DF8,
                    0xFFFF2019, 0xFFFF224D, 0xFFFF2491, 0xFFFF26E7, 0xFFFF294D, 0xFFFF2BC5, 0xFFFF2E4C, 0xFFFF30E5, 0xFFFF338D, 0xFFFF3645, 0xFFFF390D, 0xFFFF3BE5, 0xFFFF3ECC, 0xFFFF41C2, 0xFFFF44C6, 0xFFFF47DA,
                    0xFFFF4AFB, 0xFFFF4E2B, 0xFFFF5169, 0xFFFF54B4, 0xFFFF580D, 0xFFFF5B73, 0xFFFF5EE5, 0xFFFF6264, 0xFFFF65F0, 0xFFFF6987, 0xFFFF6D2A, 0xFFFF70D9, 0xFFFF7493, 0xFFFF7858, 0xFFFF7C27, 0xFFFF8000,
                    0xFFFF83E4, 0xFFFF87D1, 0xFFFF8BC8, 0xFFFF8FC7, 0xFFFF93D0, 0xFFFF97E0, 0xFFFF9BF9, 0xFFFFA01A, 0xFFFFA442, 0xFFFFA872, 0xFFFFACA8, 0xFFFFB0E5, 0xFFFFB527, 0xFFFFB970, 0xFFFFBDBE, 0xFFFFC212,
                    0xFFFFC66A, 0xFFFFCAC7, 0xFFFFCF27, 0xFFFFD38C, 0xFFFFD7F4, 0xFFFFDC5F, 0xFFFFE0CD, 0xFFFFE53E, 0xFFFFE9B0, 0xFFFFEE25, 0xFFFFF29A, 0xFFFFF711, 0xFFFFFB88, 0x0000, 0x0477, 0x08EE,
                    0x0D65, 0x11DB, 0x164F, 0x1AC2, 0x1F32, 0x23A0, 0x280B, 0x2C73, 0x30D8, 0x3539, 0x3996, 0x3DEE, 0x4241, 0x468F, 0x4AD8, 0x4F1B,
                    0x5358, 0x578E, 0x5BBD, 0x5FE5, 0x6406, 0x681F, 0x6C30, 0x7038, 0x7438, 0x782F, 0x7C1C, 0x7FFF, 0x83D9, 0x87A8, 0x8B6D, 0x8F27,
                    0x92D5, 0x9678, 0x9A10, 0x9D9B, 0xA11A, 0xA48D, 0xA7F3, 0xAB4B, 0xAE97, 0xB1D4, 0xB504, 0xB826, 0xBB39, 0xBE3E, 0xC134, 0xC41B,
                    0xC6F2, 0xC9BA, 0xCC73, 0xCF1B, 0xD1B3, 0xD43B, 0xD6B2, 0xD919, 0xDB6F, 0xDDB3, 0xDFE6, 0xE208, 0xE418, 0xE617, 0xE803, 0xE9DD,
                    0xEBA6, 0xED5B, 0xEEFF, 0xF08F, 0xF20D, 0xF378, 0xF4D0, 0xF615, 0xF746, 0xF865, 0xF970, 0xFA67, 0xFB4B, 0xFC1C, 0xFCD9, 0xFD82,
                    0xFE17, 0xFE98, 0xFF06, 0xFF60, 0xFFA6, 0xFFD8, 0xFFF6,                   
};
This doesn't look pretty, but if you copy it and paste it, it should turn out formatted correctly.
_________________
DS - It's all about DiscoStew

#26384 - Cearn - Tue Sep 14, 2004 11:41 am

Quote:
s32 SIN[360]=
{
0x0000, 0x0477, 0x08EF, 0x0D65, 0x11DB, 0x164F, 0x1AC2, 0x1F32,
0x23A0, 0x280C, 0x2C74, 0x30D8, 0x3539, 0x3996, 0x3DEE, 0x4241,

...
}

err, that's an 16/16 fixed table, not 24/8.

Additionally, you might consider using a power-of-2 sized LUT instead of 360; wrapping becomes a simple AND then. Also, remember that the only difference between cos and sin is a 90 degree offset, so you would only need one table, not two.

#26391 - getch - Tue Sep 14, 2004 3:18 pm

I dont understand how power of 2 table is better?
Surely you would want table to be able to look up all integer degrees?

#26392 - Cearn - Tue Sep 14, 2004 3:43 pm

As said, a power-of-2 is probably easier to work with since you can use shifts and ANDs instead of divisions and if/else blocks. For example
Code:

   // wrapping with 256 lut
   alpha &= 0x00ff;

  // wrapping with 360 lut
  if(alpha<0)
    alpha += 360;
  else if (alpha>=360)
    alpha -= 360; 

It also makes it easier to use a single table for both sine and cosine values, saving space:
Code:

  // sine value (256 lut)
  ss= sin[alpha&0xff];
  // cosine value
  cc= sin[(alpha+0x40)&0xff];


And remember that the division of the circle into 360 parts is arbitrary anyway. You could use any number: 360, 256, 400, 2pi, depending on personal preference and convenience. Whether it's 360 units/circle or 256/circle, you can still use integers for the units (degrees) either way, but the absolute size of the unit changes. The exact unit-size is usually irrelevant anyway, since most of the time you're thinking in terms of circle divisions: full circle, half circle, quarter circle,etc. Just like hexadecimal, power-of-2 circles are just more convenient.

#26396 - DiscoStew - Tue Sep 14, 2004 5:23 pm

cearn wrote:
err, that's an 16/16 fixed table, not 24/8.

My mistake. I found that with the header files that also included Dovoto's background stuff, and I didn't take a good look at it.
_________________
DS - It's all about DiscoStew