I would have to call the above function each time I wanted to draw a frame. There are no global variables being used here as the database is declared each time the function is called.

The other method was to have a variable type, "Animation," which has a Frame variable in it named "frame." All animations would be declared globally and part of the program would loop through the database and fill them.

Which is more effective? Should I go with using a function each time that returns a frame or should I have all animations and their frames already loaded in memory? Either way, I am going to be using a function that combines different parts of different frames and returns a frame, if that information is useful.

Also, should I have an animation table that points to the start of each animation relative to the start of the animation database so the return_frame function does not have to go through and look for the "End Animation" command however many times in order to get to the animation that it is looking for or what? Should the same be done for frames? Does it matter?

Thank you for your input! That method isn't slower or rougher on the processor, is it? That was actually the one I was leaning towards. Which way would you go about doing this? And should I have an animation and frame table or is it just fine to go through the animation database, counting as it goes until it reaches the animation and frame that it is looking for? Thanks again!

I'd recommend against returning a structure as it would cause a rather unnecessary copy to be made(This can get quite expensive if the structure is large)(Unless C compilers are allowed/able to optimize this (Someone correct me if i'm wrong)), it would probably be better to pass a "out" frame by reference to the function and modify it in place instead and only return a error code in case the function fails for some reason.

Edited by SimonForsman, 04 February 2013 - 10:47 AM.

I don't suffer from insanity, I'm enjoying every minute of it.The voices in my head may not be real, but they have some good ideas!

What he's talking about is the fact that when you return a complex type such as a struct the object has to be copied from its old context into the new one. It's not like a value which can just be set directly. They won't 'stack up' because the struct inside the function will be destroyed when the function exits. The reason you want to avoid it is for performance, but in C++ you'd also want to avoid it because copying objects can trigger complex and possibly destructive behaviors.

If you want to use a function to populate a struct then something like this is common in C:

Thank you for the example code! I should have been a little more specific. I was referring to them stacking up if not called inside a function, like so:

Frame frame=return_frame(0,0);
display(frame);

When called inside a function, it will terminate automatically indeed. What I like about your example code is that the function you declared does not need to return a variable in order to modify the variable. Does that mean it would be better to do this...

Frame frame;
return_frame(&frame,0,0);
display(frame);

Instead of this?

display(return_frame(0,0));

Very helpful advice! I need to go back and check for code that is returning structures!

I'd recommend against returning a structure as it would cause a rather unnecessary copy to be made(This can get quite expensive if the structure is large)(Unless C compilers are allowed/able to optimize this (Someone correct me if i'm wrong)), it would probably be better to pass a "out" frame by reference to the function and modify it in place instead and only return a error code in case the function fails for some reason.

This is called Return Value Optimisation, and C compilers can do it, including gcc. Still, it doesn't hurt to do it explicitly.

I find it useful to use return codes for indicating error states or information about what the function did. Handling objects by reference or pointer not only frees up the return value but allows you a consistent means for when you want to pass more than one object.

Another thing to consider in cases like this - and I'm not implying that you don't already have it right in this case - is whether this function should be a member of the class/struct or remain as an outside function.

Edited by Khatharr, 06 February 2013 - 08:11 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Never mind. For some reason I saw the & and thought reference instead of pointer-to. Possibly an advanced case of herp-derp. I've been spending so much time on schoolwork lately (eng and mat pre-reqs) that I haven't been doing any actual coding. -.-

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.