#106753 - gs2phateon - Mon Oct 23, 2006 5:36 am
I figured out how to change the priority of backgrounds, but I'm having some trouble changing the order of the sprites within a single background. I know that the default priority is the order in which they are in the RAM, and I probably could do it that way, but is there an easier way?
#106756 - DiscoStew - Mon Oct 23, 2006 5:49 am
As Backgrounds have priority bits, so do Sprites. BIT10 - BIT11 of Attribute 2 holds the Sprite priority. Note that if the sprite and background have the same priority, the sprite will be displayed above that background, as well as any set at a lower priority.
_________________
DS - It's all about DiscoStew
#106757 - gs2phateon - Mon Oct 23, 2006 5:57 am
Thanks, I'm pretty sure I remember reading something like that on a tutorial some time back, but I didn't know BG's and sprites shared the same priority. I guess the big question I have now, is how do you change specific bits. So far, I've gotten along just fine with what I've read in tutorials (or haphazardly typing in numbers), but how do you change bits 10 and 11 as opposed to say 1 through 4?
#106761 - DiscoStew - Mon Oct 23, 2006 6:25 am
Just treat them like how you would when setting bits to an already set area, do an OR on them for the particular bits you want to set on them.
In the gba.h header, there is a macro called PRIORITY that does this for you. When setting the attribute2 section of your sprite, do an OR assignment with that macro, ranging from 0 to 3 for the priority of that sprite.
Don't forget that if you want to change the priority, you'll need to clear those bits, then reset them with the new priority.
Hope that helps, but if any info I have given is wrong, please correct me.
_________________
DS - It's all about DiscoStew
#106833 - Quirky - Mon Oct 23, 2006 9:16 pm
Careful with the priority bits on sprites and in particular trying to short circuit RAM order with BG-Sprite priorities - you can get unexpected results. Strange effects such as sprites that you expect to be "underneath" show up through their neighbouring sprites. Doesn't show up on VBA, but will do on hardware.
#106950 - gs2phateon - Wed Oct 25, 2006 4:11 am
OK, I was able to get most of it to work, but I am experiencing some of those sprite problems, even on VBA. The only thing I don't get is how do you clear bits?
#106956 - tepples - Wed Oct 25, 2006 6:32 am
To clear bits in a register using a read-modify-write sequence, use the following:
Code: |
REG_WHATEVER &= ~(THIS_BIT | THAT_BIT | ANOTHER_BIT); |
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#107578 - gs2phateon - Tue Oct 31, 2006 1:46 am
I tried using the read-modify-write sequence, but I don't think I'm doing it right. I still don't understand what each of the parts are that you have to put in
This is what I have for sprite attribute 2:
Code: |
sprites[spritenum].attribute2 = spriteTile[spritenum] | PRIORITY_0 |
Where spriteTile is the tile number that the sprite is located at, and PRIORITY_(0-3) is the sprite's priority. How should that look using the read-modify-write format?
#107606 - gmiller - Tue Oct 31, 2006 1:24 pm
Depending on your values for the priority constant you need to set the appropriate bits in the attribute2 value. The low order bits are the tile number and the upper bits the 16 color palette selection and then in the middle is the priority bits. Here is a description:
Code: |
F E D C B A 9 8 7 6 5 4 3 2 1 0
L L L L P P T T T T T T T T T T
0-9 (T) = Tile number. This value indexes selects the bitmap of the tile to be displayed by indexing into the tile data area. Each index references 32 bytes, so the memory address of a tile is roughly 0x6010000 + T*32.
A-B (P) = This controls the priority of the sprite. Note that sprites take precedence over backgrounds of the same priority.
C-F (L) = Palette number. If you use 16 color palettes, this tells you which palette number to use.
|
To set the priority in your example the priority value must be something that would set bits 10 (A) or 11 (B). If your value (0 - 3) is just a number between 0 and three this the OR will mess up your tile number. Here are some macro's that could be used to adjust the value to the correct bit positions:
Code: |
//Attribute2 stuff
#define PRIORITY(n) (((n) & 3) << 10)
#define PALETTE(n) (((n) & 15) << 12)
|
The macro force the value to be only the number of bits wide that they can be then shift the value to the correct bit position. This will allow you to set bits. You need to remember that the OR operation can NOT clear bits it can only set them. When you do the read-modify you will need to clear the bits in the field using a AND operation with a NOTed value that is the width of the field. For example to clear the priority field using the macro's:
Code: |
attribute2 &= ~PRIORITY(3); // Clear current priority
attribute2 |= PRIORITY(1); // Set priority to 1
|
Hope this helps.