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 > How is actual sprite data converted?

#12165 - yaustar - Sat Nov 01, 2003 8:07 pm

at the moment I am trying to write a function to edit the colour an pixel in a particular sprite along the lines of

editSprite(x,y, colour){}

but am unsure of how the data in sprites is stored.

Using 256 colour sprites the data is stored as a 16 bit number
Code:
BBBB BBBB AAAA AAAA


I am guessing that each half of the data represents a pixel with a colour index according to the palette.

My problem is which pixels is it referring to?

imagine an 8x8 sprite in pixels
Code:

01234567
ABCDEFGH
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX


does the data represent the pixels 0 and A as:
Code:
0000 0000 AAAA AAAA

or 0 and 1
Code:
1111 1111 0000 0000

and then what does the next data in the array represent? The X and X below it or 1 and B next to it across or 2 and 3/A and B.

If you are still reading at this point then thanks and any help clarifing this would be appreciated.

Side note. I understand that larger sprites and divided into 8x8 squares left to right then the next row down.
_________________
[Blog] [Portfolio]

#12166 - tepples - Sat Nov 01, 2003 8:48 pm

yaustar wrote:
but am unsure of how the data in sprites is stored.

Using 256 colour sprites the data is stored as a 16 bit number
Code:
BBBB BBBB AAAA AAAA


I am guessing that each half of the data represents a pixel with a colour index according to the palette.

My problem is which pixels is it referring to?

imagine an 8x8 sprite in pixels
Code:

01234567
ABCDEFGH
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX
XXXXXXXX

Pseudocode for how it's stored:
Code:
unsigned short sprite[8][4] =
{
  {0x1100,0x3322,0x5544,0x7766},
  {0xBBAA,0xDDCC,0xFFEE,0xHHGG},  /* before you complain, this is PSEUDO code */
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX},
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX},
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX},
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX},
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX},
  {0xXXXX,0xXXXX,0xXXXX,0xXXXX}
};

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#12167 - yaustar - Sat Nov 01, 2003 8:52 pm

well, that supports my thoery but unfortunely screws my code over.
why couldnt the gba be a nice gba :(

Thanks tepples for the clear up and whenever would i complain :p

might as well post the bad code while I try to fix it :\
Code:

//---Edit Radar sprite 64x64---//
void func_drawBitRadar(u8 var_y, u8 var_x, u16 var_colour)
{
    bool even_odd;//false is even, true is odd
    u8 var_newX, var_newY, var_diffY, var_diffX;//temp variables
   
    if(((var_x >> 1)<< 1) == var_x)
    {even_odd = false;}
    else
    {even_odd = true;}
   
    var_newX = (var_x >> 3);//divide by eight
    var_newY = (var_y >> 3);//divide by eight
    var_diffX = var_x - var_newX;//obtain the reminder
    var_diffY = var_y - var_newY;//obtain the reminder
   
    if(even_odd==false)
    {
        radar[(var_newY << 8)+(var_diffY << 2) +
            (var_newX << 5) + (var_diffX >> 1)] |= var_colour;
    }
    else
    {
        radar[(var_newY << 8)+(var_diffY << 2) +
            (var_newX << 5) + (var_diffX >> 1)] |= var_colour << 8;
    }
}

Basically it is suppose to be able to edit any pixel in a 64x64 sprite but it isn't working during its tests...if anyone can give me a hint what might be wring then I be very grateful.

edit: It's now fixed, minor error in the precalculation
_________________
[Blog] [Portfolio]