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++ > Assigning values (Solved)

#176782 - MisterLogan - Tue Oct 11, 2011 5:51 am

I don't know how to easily phrase this so I'll describe the situation. I have a function that gets run up to 32 times when i load a new map section. It loads sprite data into OAM and does some little index things. This function needs 5 variables that are in a struct called CurrentSprite (an x and y, a pointer to where the visual data is, and two pointers used to index OAM and object memory locations), four of which are loaded from another struct, named after whatever sprite is getting loaded, like this:

Code:

CurrentSprite.IOAM = &sadghost.IOAM;
CurrentSprite.IOBJ = &sadghost.IOBJ;
CurrentSprite.X = sadghost.X;
CurrentSprite.Y = sadghost.Y;
CurrentSprite.Tiles = (u16*) &sadghostTiles;
initNPCTiles(OBJMEMCounter, OAMCounter);


initNPCTiles is the function. So my question is, is there any way to give the CurrentSprite struct a sprite structs attributes that doesn't take so much copying and pasting? Some way of giving a function "sadghost" and having it do this for me? I feel like this should be really obvious but my brain is not being very helpful. I guess at this point I would look silly and get past it rather than wait and do nothing.

Thanks


Last edited by MisterLogan on Tue Oct 11, 2011 9:50 pm; edited 1 time in total

#176783 - gauauu - Tue Oct 11, 2011 2:51 pm

Why not use just a simple function for the assignment? I'm not sure what you're types are named, so this is just an example:
Code:


void assignSprite(struct CurrentSprite * currentSprite, struct SpriteAttribs * spriteAttribs, u16* tiles) {

  currentSprite->IOAM = &(spriteAttribs->IOAM);
  currentSprite->IOBJ = &(spriteAttribs->IOBJ);
  currentSprite->X = spriteAttribs->X;
  currentSprite->Y = spriteAttribs->Y
  currentSprite->Tiles = tiles;

}

...

assignSprite(&CurrentSprite, &sadghost, &sadghostTiles);



Warning: I've been doing java recently, haven't touched C in a couple years, so my syntax might be a bit off.

#176786 - MisterLogan - Tue Oct 11, 2011 9:50 pm

Oh cool, I didn't know that there was an operator like ->. And your syntax was right on, just had to change the type names and it worked =).

Thanks a ton, it works great!