Setting attribute values according to x and y tile positions
Moderator: Moderators
Setting attribute values according to x and y tile positions
Alright, filling the screen with tiles from compressed data is working in my program.
Now I encounter the problem that I was able to circumvent in my previous game by simply hardcoding the upper static background graphics and by only using one and the same palette for the lower, dynamic part of the screen:
How do I set a palette attribute value if I know the x and y position of the corresponding tiles?
Let me elaborate:
I understand that each attribute value refers to a 16 x 16 pixels (2 x 2 tiles) area. Alright, fair enough.
But why did they also have to align each attribute byte in a square format?
Why couldn't one attributes byte, which contains the palette information for four 16 x 16 areas, simply refer to a 64 x 16 area? Why does it have to be a 32 x 32 area?
This makes connecting the PPU address offset of a certain tile with the PPU address offset of the corresponding attribute value a pain in the ass.
So, is there a good function that can change the attributes value according to an x and y position?
What I need is basically a function like this:
void ChangeAttributeBits(byte x, byte y, byte palette, ref byte[64] attributes);
x: Meta tile x position from 0 to 15. (Tile position would be x * 2 and pixel position would be x * 16.)
y: Same as x, only for y.
palette: Palette index value from 0 to 3.
attributes:
An array that is a copy of the 64 attribute bytes from a single name table from the PPU.
This value gets changed:
The palette index value is written to the correct two bits in this array. So, after writing the changed array back into the PPU to $23C0, $27C0, $2BC0 or $2FC0, the 2 * 2 tiles at location x * 16, y * 16 use this new palette value.
Now I encounter the problem that I was able to circumvent in my previous game by simply hardcoding the upper static background graphics and by only using one and the same palette for the lower, dynamic part of the screen:
How do I set a palette attribute value if I know the x and y position of the corresponding tiles?
Let me elaborate:
I understand that each attribute value refers to a 16 x 16 pixels (2 x 2 tiles) area. Alright, fair enough.
But why did they also have to align each attribute byte in a square format?
Why couldn't one attributes byte, which contains the palette information for four 16 x 16 areas, simply refer to a 64 x 16 area? Why does it have to be a 32 x 32 area?
This makes connecting the PPU address offset of a certain tile with the PPU address offset of the corresponding attribute value a pain in the ass.
So, is there a good function that can change the attributes value according to an x and y position?
What I need is basically a function like this:
void ChangeAttributeBits(byte x, byte y, byte palette, ref byte[64] attributes);
x: Meta tile x position from 0 to 15. (Tile position would be x * 2 and pixel position would be x * 16.)
y: Same as x, only for y.
palette: Palette index value from 0 to 3.
attributes:
An array that is a copy of the 64 attribute bytes from a single name table from the PPU.
This value gets changed:
The palette index value is written to the correct two bits in this array. So, after writing the changed array back into the PPU to $23C0, $27C0, $2BC0 or $2FC0, the 2 * 2 tiles at location x * 16, y * 16 use this new palette value.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
- rainwarrior
- Posts: 8735
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Setting attribute values according to x and y tile posit
So if you have X and Y as 16x16 pixel tile locations:
X/2 and Y/2 give you the 32x32 attribute region location.
X mod 2 and Y mod 2 give you the 4 quadrants of that location (i.e. which bits to select)
Since there are 8 attribute regions per row: ((Y/2)*8) + (X/2) = attribute byte offset
Alternatively if you have a PPU tile address and want to convert to its corresponding attribute, you can use the formula here: PPU_scrolling#Tile_and_attribute_fetching
X/2 and Y/2 give you the 32x32 attribute region location.
X mod 2 and Y mod 2 give you the 4 quadrants of that location (i.e. which bits to select)
Since there are 8 attribute regions per row: ((Y/2)*8) + (X/2) = attribute byte offset
Alternatively if you have a PPU tile address and want to convert to its corresponding attribute, you can use the formula here: PPU_scrolling#Tile_and_attribute_fetching
Re: Setting attribute values according to x and y tile posit
An uniform distribution makes more sense, I guess, and several games took advantage of the format by using 32x32-pixel metatiles. There's isn't much you can do with 64x16.DRW wrote:Why couldn't one attributes byte, which contains the palette information for four 16 x 16 areas, simply refer to a 64 x 16 area? Why does it have to be a 32 x 32 area?
It's indeed a pain in the ass, specially if you scroll vertically and have to deal with the bottomost attribute row being only 16 pixels high.This makes connecting the PPU address offset of a certain tile with the PPU address offset of the corresponding attribute value a pain in the ass.
The math I use is the one rainwarrior posted. Divide the coordinates by 2 to find the address of the attribute byte, use mod 2 to index a set of masks that can be used to manipulate the bits.So, is there a good function that can change the attributes value according to an x and y position?
For each possible quadrant of an attribute byte, I have a pair of masks, one to clear the relevant bits in a pre-existing attribute byte, and another (the inverse of the other) to clear the unwanted bits of a byte fetched from the metatile's attributes that contains the palette index repeated 4 times. After clearing the bytes separately, I OR them together to form the final byte.
-
- Posts: 3140
- Joined: Wed May 19, 2010 6:12 pm
Re: Setting attribute values according to x and y tile posit
You can also mask the bits this way.
lda attribute_byte
eor attribute_data
and mask
eor attribute_byte
sta attribute_byte
lda attribute_byte
eor attribute_data
and mask
eor attribute_byte
sta attribute_byte
Re: Setting attribute values according to x and y tile posit
Nice trick! You only need one mask and there's no need for a temporary variable. Here's an example of how this works:psycopathicteen wrote:You can also mask the bits this way.
lda attribute_byte
eor attribute_data
and mask
eor attribute_byte
sta attribute_byte
Code: Select all
;%00111011 (attribute_byte)
;%11111111 (attribute_data) (%11 4 times)
;%00001100 (mask)
lda attribute_byte ;%00111011
eor attribute_data ;%11000100
and mask ;%00000100
eor attribute_byte ;%00111111
sta attribute_byte
-
- Posts: 3140
- Joined: Wed May 19, 2010 6:12 pm
Re: Setting attribute values according to x and y tile posit
I think you can also flag the attribute bytes to update so you don't end up rewriting the same tiles more than once during vblank.
Re: Setting attribute values according to x and y tile posit
With attributes, since you have to read-modify-write each byte, it's often more convenient to keep a copy of the attribute data in RAM for unrestricted manipulation, and then upload rows or columns to VRAM as necessary.
Re: Setting attribute values according to x and y tile posit
Thanks. This was exactly what I was looking for.rainwarrior wrote:X/2 and Y/2 give you the 32x32 attribute region location.
X mod 2 and Y mod 2 give you the 4 quadrants of that location (i.e. which bits to select)
Since there are 8 attribute regions per row: ((Y/2)*8) + (X/2) = attribute byte offset
I also included the EOR hint by psycopathicteen and this is the C function that I came up with:
Code: Select all
byte Attributes[8 * 8];
/* x: 0-15. y: 0-14. paletteIndex: 0-3. */
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
byte attribute = Attributes[attributesIndex];
Attributes[attributesIndex] =
((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;
}
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Re: Setting attribute values according to x and y tile posit
Not in C, but in asm certainly. I think my equivalent function got four times faster and half the space when asm-optimized.
Re: Setting attribute values according to x and y tile posit
Yeah, Assembly is surely better.
But I managed to write a complete scrolling platformer in C with only the low level stuff (everything that is in vblank, access to the PPU etc.) and some general purpose functions (copy array, fill array, randomizer) written in Assembly.
Therefore, unless I really get into trouble, I continue writing the functions in C for now. Makes it 10 times easier for me to write code.
But I managed to write a complete scrolling platformer in C with only the low level stuff (everything that is in vblank, access to the PPU etc.) and some general purpose functions (copy array, fill array, randomizer) written in Assembly.
Therefore, unless I really get into trouble, I continue writing the functions in C for now. Makes it 10 times easier for me to write code.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Re: Setting attribute values according to x and y tile posit
Code: Select all
byte Attributes[8 * 8];
/* x: 0-15. y: 0-14. paletteIndex: 0-3. */
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
byte attribute = Attributes[attributesIndex];
Attributes[attributesIndex] =
((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;
}
So, either I didn't implement psycopathicteen's suggestion correctly or the algorithm with the EOR itself is incorrect.
The following function seems to work correctly:
Code: Select all
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
byte shifts = (((y & 1) << 1) | (x & 1)) << 1;
Attributes[attributesIndex] =
(Attributes[attributesIndex] & (~(0x03 << shifts))) | (paletteIndex << shifts);
}
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
- rainwarrior
- Posts: 8735
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Setting attribute values according to x and y tile posit
Looking at the two ends of your equation, and thinking transitively:
All your mask does here is select which bits to set. It's incapable, however, of clearing any bits. You need to do both to replace the palette index.
i.e. paletteIndex of 3 will work, 1/2 will set one correct bit and fail to clear the other, and 0 will fail to clear either bit.
(By the way, you can use the bitwise complement operator ~a as a shorter form of a ^ 0xFF.)
Code: Select all
// original
((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;
// simplified (mask = the whole & term)
((attribute ^ 0xFF) & mask) ^ attribute;
// some truths about XOR
(attribute ^ attribute) = 0x00;
0x00 ^ 0xFF = 0xFF;
(attribute ^ 0xFF) ^ attribute = 0xFF;
// what the statement really does
((attribute ^ 0xFF) & mask) ^ attribute = mask | attribute;
i.e. paletteIndex of 3 will work, 1/2 will set one correct bit and fail to clear the other, and 0 will fail to clear either bit.
(By the way, you can use the bitwise complement operator ~a as a shorter form of a ^ 0xFF.)
Re: Setting attribute values according to x and y tile posit
O.k., but where exactly is the error? I thought that I simply implemented a C version of the following code:rainwarrior wrote:[...]Code: Select all
// simplified (mask = the whole & term) ((attribute ^ 0xFF) & mask) ^ attribute;
All your mask does here is select which bits to set. It's incapable, however, of clearing any bits.
Isn't this exactly the same?tokumaru wrote:Code: Select all
;%00111011 (attribute_byte) ;%11111111 (attribute_data) (%11 4 times) ;%00001100 (mask) lda attribute_byte ;%00111011 eor attribute_data ;%11000100 and mask ;%00000100 eor attribute_byte ;%00111111 sta attribute_byte
lda attribute_byte
eor attribute_data
-->
attribute = Attributes[attributesIndex]
attribute ^ 0xFF
and mask
-->
& mask
eor attribute_byte
-->
^ attribute;
sta attribute_byte
-->
Attributes[attributesIndex] =
So, is it my code that's still in error because I didn't convert the algorithm correctly from Assembly to C? Or is the algorithm itself as written down by psychopaticteen and tokumaru that's incorrect/insufficient?
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
- rainwarrior
- Posts: 8735
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Setting attribute values according to x and y tile posit
Code: Select all
attribute // lda attribute_byte
^ (palette << shift) // eor attribute_data
& (3 << shift) // and mask
^ attribute // eor attribute_byte
// sta attribute_byte
result = ((attribute ^ (palette << shift)) & (3 << shift)) ^ attribute;
Code: Select all
result = (attribute & (~(3 << shift))) | (palette << shift);
lda mask ;
eor #$FF ; ~ (3 << shift)
and attribute_byte ; & attribute
ora attribute_data ; | (palette << shift)
sta attribute_byte ;
Last edited by rainwarrior on Fri Jan 20, 2017 1:29 am, edited 3 times in total.
Re: Setting attribute values according to x and y tile posit
Now you totally lost me.rainwarrior wrote:Code: Select all
^ (palette << shift) // eor attribute_data
Why does eor attribute_data transform to ^ (palette << shift) instead of ^ 0xFF?
In the example, tokumaru declared attribute_data as %11111111:
So I assumed it's a constant for negation.tokumaru wrote:Code: Select all
;%11111111 (attribute_data) (%11 4 times)
Even if attribute_data was a variable, how could palette << shift have ever been %11111111 in the first place if the palette value is only a value from 0 to 3?
I'm confused.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html