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.

Coding > Optimize OAM for Sprite Animations

#6880 - einhaender - Wed Jun 04, 2003 3:20 am

Hi, I was wondering if the following was possible. I have a bunch of Sprites (30), each with 2 Animation frames.
Animation Frame 1 is unique for each Sprite and is read out of its own Sprite Data Array. Animation Frame 2 is common for each of the 30 Sprites, so I read it out of a common Array for every Sprite.
When copying the OAMData it looks in pseudocode like:

for(every sprite) {
create pointer to the array data for animation frame 1
for(animation frame 2) {
copy common sprite animation data to OAM
}
for(animation frame 1) {
copy from pointer to OAM
}
}

However, now I have copied the exact same data in Animation Frame 2,
which is the same for each sprite, 30 times into OAM. Is that neccessary or can I somehow point to a single location?
The code I have in C now looks like:

#define OAMData ((u16*)0x6010000)
int z = 0;
for(loop = it*256 ; loop < (it+1)*256 ; loop++) {
// Animation Frame 2
if(z < 128){
OAMData[loop] = tile_all_frame2Data[z];
}
// Animation Frame 1
if(z >= 128 && z < 256) {
OAMData[loop] = main_sprite_array[0][z-128];
}
z++;
}

You see that the common Animation 2 Data is written each
n*256 to n*256+128 into OAM

#6895 - Sweex - Wed Jun 04, 2003 10:31 am

Multiple sprite entries can point to the same data in VRAM as far as I know. So I would say you can simply set the sprite entry point to the "second frame" in vram. You'll have 30+1 sprites in vram that way.

(Sorry, it's been a while since I used the gfx terms so replace sprite and vram with the GBA specific terms if you like! Shouldn't matter to the solution offcourse)

#6900 - Touchstone - Wed Jun 04, 2003 11:06 am

Einhander, does it really matter that you have copied 30 identical sprite-frames to VRAM? I suppose you go back and forth between 1'st and 2'nd frame so overall, worst-case scenario, you need all that sprite vram to be able to play your animations. The only time you would benefit from sharing the data for frame 2 is when you know that you will need that extra vram just for the frames when your 30 sprites are displaying frame 2. Are you following me?

Anyways, you might want to write a sprite-data handler / vram memory manager that when you tell to load certain sprite data it keeps track of if the data is already loaded and if it is the manager just return the tile index of where it's loaded. If the data isn't loaded it allocates a couple of tiles, load data and return the base tile index. For unloading you would probably want some sort of reference-counter like smart-pointers. This kind of memory-manager would only bee needed if you have huge environments with no intermission and a large variety of sprites.
_________________
You can't beat our meat

#6903 - einhaender - Wed Jun 04, 2003 11:43 am

Thanks guys for your answers. First of all I tested sweex idea and copied only 30+1 instead of 30+30 Sprites. The second animation frame for each sprite now points to the exact memory location.
Well actually I only changed the pointing for testing sweex idea and left loading of the same Sprite in memory 30 times as it is right now. That brings me Touchstones objection. Does it matter to save VRAM? Well Iam a newbee, I dont know. Doing the same with only 30+1 rather than 30+30 Sprites seemed to be a good idea, i was hoping to gain performance and use less ressources. Maybe it doesnt matter in my scenario.

#6905 - Touchstone - Wed Jun 04, 2003 12:40 pm

einhaender wrote:
Does it matter to save VRAM? .. Doing the same with only 30+1 rather than 30+30 Sprites seemed to be a good idea

Aha, I assumed you unloaded the first frame for the 30 sprites when loading the second frame. I thought you replaced all the first frame data with the second frame data, that way it wouldn't _really_ matter unless you where really in for squeezing out every last byte of memory. It's obviously better to only have 31 frames loaded in VRAM instead of 60. I'm sorry if I misled you.
_________________
You can't beat our meat

#6908 - einhaender - Wed Jun 04, 2003 1:52 pm

Does it increase performance to load 31 instead of 60 sprites?

#6912 - Sweex - Wed Jun 04, 2003 2:27 pm

I'd say less is more. Using less memory, you'll have more to use for different things later on!

And it'll save a few microseconds to copy 31 instead of 60 to vram!:-)

A sprite-handler as Touchstone mentioned is a good idea if you're planning on expanding your game/program