Jump to content
  • Advertisement
Sign in to follow this  
Boder

Your Opinions, General Stuff

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

return 0; return 1; which one is failure and which one is success? I return 0 from main if nothing goes wrong but a lot of other things use 0 as a fail flag. Calling a function like if(!func() ) { /*error*/ }. And is using a linked list too much for animation purposes with a sprite? Having the last frame loop back to the first frame instead of iterating over an array?

Share this post


Link to post
Share on other sites
Advertisement
First, main should return 0 if there are no errors.

Second, with a linked list, you don't know how the data will end up laid out in memory. However, the overhead of iterating through a linked list is probably not significantly different from the overhead of iterating through an array. Therefore, I would recommend an array because it will guarantee good spatial locality.

Share this post


Link to post
Share on other sites
Yes, I agree !function seems like failure, like testing for NULL, and a lot of code seems to use that.

But returning non-zero for failure opens up the possibility for different errors. Like AUDIO_ERR=1, INIT_ERR=2, SUPER_BAD_ERR=4. Whereas success usually comes in one flavor.

For the animation question, I would use an array, as a linked list seems overkill. Afterall, you know ahead of time how many frames are in the animation sequence. But my friend says there is no performance difference, and doesn't want to keep track of the array and the counter, when you can just deference the next element in the animation list.

Share this post


Link to post
Share on other sites
but in contrast

curr_frame = *(curr_frame.next);
// or if that's a pointer
curr_frame = curr_frame.next;

Share this post


Link to post
Share on other sites
The array will have better caching performance. It might be miniscule, but miniscule savings add up quickly. One drop at a time, the pot is filled.

Share this post


Link to post
Share on other sites
Quote:
Original post by moagstar
*** Source Snippet Removed ***


Agh. Don't go indexing the array before you've corrected for the loop-back condition.

currFrame = currFrame + 1 % frames.length(); // assuming std::vector
setFrame(frames[currFrame]);

Anyway. I'd go with the array (well, std::vector) simply because getting real (idiosyncratic) benefit out of the linked list would require a circular linked list, and the STL list just doesn't do that. (And rolling your own is not terribly much fun, especially the part about making sure all the links are right when you add or remove something.)

Share this post


Link to post
Share on other sites
You could use stl::vector instead of the array. Presumably you'll know how many frames of animation you have, so you can reserve memory before filling the vector:

vector<Sprite> animation;
animation.reserve(numFrames);

//now populate frames

Vectors offer all the efficiency benefits of arrays (the STL *requires* vectors implement their underlying storage as an array) but also offer additional benefits such as automatic growth, helper methods and standard STL algorithms. Assuming you're using C++ of course.

For your own functions, I'd recommend 1 == success, -n = failure. That way, negative numbers can be used to signify different errors. You could even have different (positive) success codes if you required it.

HTH,
Kent

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!