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.

Coding > Need Help Setting Attribute 2.

#4794 - Saj - Wed Apr 09, 2003 9:44 pm

Greetings,

I'm trying to display several (16-colour) sprites. How do I set up different palettes for them?

I know I have to set attribute 2 but I can't find any source code on this matter( everyone seems to be using 256 colour sprites :( )

Code:

sprites[1].attribute0 = COLOR_16 | SQUARE | ghost.y;   
sprites[1].attribute1 = SIZE_8 | ghost.x;             
sprites[1].attribute2 = ?;                             


Also, I know the first value for attribute 2 is the character name but what do I have to 'OR' with it? btw, Im using Pern's headers.

Some example code would help me a lot.
Thanks.

bonus question for 50 points - what does this do?
Code:
   
pSlot->attribute0 = attribute0;
pSlot->attribute1 = attribute1;
pSlot->attribute2 = attribute2;

_________________
---------------------------------------------------
Click here to give free food to starving animals.
--------------------------------------------------


Last edited by Saj on Wed Apr 09, 2003 9:54 pm; edited 1 time in total

#4795 - Daikath - Wed Apr 09, 2003 9:49 pm

Attribute 2 is the place in object memory where the image of the sprite is stored.

I haven't tried using a 16-colour sprite but I don't think it is much different in bases from 256.

For the first place in memory you simply put 0, then if you have a 64*64 pixel sprite you puy 128 on their for the second place in history and put 64 there for 32 by 32 sprites. However I dont knwo the numbers for rectangle sprites and the sort.
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#4796 - Saj - Wed Apr 09, 2003 10:00 pm

Daikath
Quote:
Attribute 2 is the place in object memory where the image of the sprite is stored.


I know, but It also stores the palette that is being used (when you use 16-colour sprites).

OK, I found this;
Code:
     
      mysprites[0].attribute0 = (COLOR_16 | SQUARE | blowhole.y);
      mysprites[0].attribute1 = (SIZE_16 | blowhole.x);
      mysprites[0].attribute2 = (PRIORITY(2) | PALETTE(0));

and
Code:

//atribute2 stuff

#define PRIORITY(n)         ((n) << 10)
#define PALETTE(n)         ((n) << 12)


But I still don't understand.
Do I just make a (pseudo)256 colour palette and use values (0-15 or 1-16) to
to distinguish between palettes or do I use values (0 - 255)?!?
Please Help!!!!
_________________
---------------------------------------------------
Click here to give free food to starving animals.
--------------------------------------------------

#4797 - DekuTree64 - Wed Apr 09, 2003 11:11 pm

The actual sprite image data is stored as 4 bit values (so you have values 0-15). Each byte represents 2 pixels of the sprite. Check the options for your gfx converter program to see how to set it to generate 16-color data)Then the upper 4 bits of attr2 control the palette it uses. The only difference in how you set up a 16-color sprite is that you don't set the 256 color bit in attr0, and you set the pallete index in attr2.
Does that help?

#4807 - Saj - Thu Apr 10, 2003 10:49 am

Thanks DekuTree, (Gotta love those deku nuts and deku sticks too!)

Quote:
The actual sprite image data is stored as 4 bit values (so you have values 0-15).

So could I fill the palette (as a 256 colour array)with:
Code:

for(loop = 0; loop < 256; loop++)
    {
      OBJPaletteMem[loop] = palette[loop];
    }


and then access the third palette (colours 32-47) using:
Code:
sprites[0].attribute2 = 0 | PRIORITY(3) | PALETTE(2)


What I'm asking is, do I still use OBJPaletteMem[] as a single 256 slot array? (I know each sprite can only use a single 16-colour palette of this).

And in PALETTE(n), n can never be higher than 15?

Quote:
Each byte represents 2 pixels of the sprite. Check the options for your gfx converter program to see how to set it to generate 16-color data)

I've got that part down with gifs2sprites, but I read on a different thread that there may be palette re-ordering issues.
They were talking about 'gif2spr'. Is that the same as gifs2sprites?

Anyways, here's what it made:-
Code:
const u16 palette[16]=
{
RGB( 0, 0, 0),RGB(31,31,31),RGB(15,15,12),RGB(13,13,11),
RGB(11,11,10),RGB( 7, 7, 7),RGB( 0, 0, 2),RGB( 2, 2, 5),
RGB( 0, 0,12),RGB(25,25,10),RGB(16,16,23),RGB( 5, 4,11),
RGB(16,15, 3),RGB(26,26,20),RGB(25,17,12),RGB( 6, 6,15)
};

Quote:
Then the upper 4 bits of attr2 control the palette it uses. The only difference in how you set up a 16-color sprite is that you don't set the 256 color bit in attr0, and you set the pallete index in attr2.

Do you have a code example of this...PLEAAASSSEE!!! Unless it's just:
Code:
sprites[0].attribute0 = COLOR_16 | SQUARE | y;
sprites[0].attribute1 = SIZE_8 | x;
sprites[0].attribute2 = 0 | PRIORITY(3) | PALETTE(15);

Thanks for your help.
_________________
---------------------------------------------------
Click here to give free food to starving animals.
--------------------------------------------------

#4809 - Quirky - Thu Apr 10, 2003 11:50 am

If you look at 16 colour sprite data in VBA palette viewer, you will see how the palette is stored. I use 16 coloured tiles, which are the same idea as sprites and yes, you have a 256 entry PaletteMem[] array.

I keep pointers to my palettes in an array (my generated palette data, that is) and then load each one using a routine like this:

Code:

void load_bg_palette(u8 topal, u8 frompal) {
  u16 palindex = topal*16;
  REG_DMA3SAD = (u32)PALETTES[frompal];
  REG_DMA3DAD = (u32)&BGPaletteMem[palindex];
  REG_DMA3CNT = 16 |DMA_16NOW;
 
}


where PALETTES is an array of pointers to the 16 bit palette data arrays. No checks there, but "topal" should be 0-15 inclusive. frompal depends on how many palettes you use, in my case just 10 of them.

As you can see.. palette 0 will use colours 0-15, palette 1 uses colours 16-31, etc. Colour 0, 16, 32, etc are the transparent colours for the individual 16-colour palettes.