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.

C/C++ > Setting sprite image without overwriting the rest of attrs

#80493 - MaxCantor - Sat Apr 22, 2006 9:20 pm

Hey guys. I'm writing a set_sprite_image() function, and I'm trying to figure out how to code it so that it ONLY sets the size, shape, tile and palette bits in the OAM entry. I've been working with the TONC library.

Right now, this code works:

Code:
oe_set_attr( sprite,
   shape,
   fsize,
   OE_A2_16C( palette ) | tile );



However, that's one of the TONC functions, and it overwrites the entire entry (i.e., setting the image also resets the position).

I'm using a couple of macros that come with TONC, but things aren't working out. These are the definitions of the TONC macros:

Code:
// bit field set and get routines
#define BF_MSK(_x, _name)        ( ((_x)<<_name##_SHIFT) & _name##_MASK )
#define BF_UNMSK(_x, _name)      ( ((_x) & _name##_MASK)>>_name##_SHIFT )
#define BF_INS(_dst, _x, _name)  (_dst = ((_dst)&~_name##_MASK) | BF_MSK(_x,_name) )

// Create bitfield:
// attr |= BF_MSK(id, OE_A2_ID);
attr2 |= (id<<OE_A2_ID_SHIFT) & OE_A2_ID_MASK;

// Retrieve bitfield:
// id= BF_UNMSK(attr2, OE_A2_ID);
id= (attr2 & OE_A2_ID_MASK)>>OE_A2_ID_SHIFT;

// Insert bitfield:
// BF_INS(attr2, id, OE_A2_ID);
attr= (attr&~OE_A2_ID_MASK) | ((id<<OE_A2_ID_SHIFT) & OE_A2_ID_MASK);


And finally, here's my code that isn't working:

Code:
   sprite->attr0 |= BF_MSK(shape, OE_A0_SHAPE);
   sprite->attr1 |= BF_MSK(fsize, OE_A1_SIZE);
   sprite->attr2 = OE_A2_BUILD( tile, palette, 0 );


I know that the attr2 build macro works because I've used it in my code before, but something just isn't happening with the bitfield mask macros. I've also tried this:

Code:
   BF_INS( sprite->attr0, shape, OE_A0_SHAPE );
   BF_INS( sprite->attr1, fsize, OE_A1_SIZE );


But it didn't work either. Any hints...? Thanks, all!

EDIT: Nevermind - I e-mailed Cearn (he wrote Tonc) and he figured it out. The existing BF_ macros expect plain numbers, but my "shape" and "fsize" variables were already bitshifted earlier on in my code. These macros will work with pre-shifted values:

#define BF_MSK2(_x, _name) ( (_x) & _name##_MASK )
#define BF_INS2(_y, _x, _name) (_y = ((_y)&~_name##_MASK) |
BF_MSK2(_x,_name) )