Jump to content
  • Advertisement
Sign in to follow this  
DividedByZero

Handling sprite (array or vector)

This topic is 2533 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

Hi Guys,

I am programming a basic 2D game which will have alot of sprites in it.

I have setup a struct to handle the properties of the sprites. Do you think I am better storing these structs as an array or a vector.

I see advantages and disadvantages for both methods. Any advice would be great before I get too deep into the coding.

:)

Share this post


Link to post
Share on other sites
Advertisement
(A standard implementation of) dynamic arrays are usually pretty slow at inserting and/or removing elements from the middle of the list as all data has to be copied. Although they are really fast for iteration and lookup. Depending on your definition of a lot you might want to initialize the dynamic array so you don't end up doubling a dynamic array containing 20M elements in the middle of an update.

(A standard implementation of) lists are pretty fast at inserting and/or removing elements anywhere in the list. Although they are not as fast when it comes to iteration and slow (in comparison) to arrays when it comes to lookup.

Depending on how you're using the container you'll get different results, although it's impossible to tell from the description you gave. Although if you want to analyze the performance of your program based on the type of container used you can find the complexity of each operation at wikipedia, or you could simply change the type of container and time your functions.

Edit: been working too much with python lately, a standard implementation of arrays are of course fixed size. Edited by Deprecated

Share this post


Link to post
Share on other sites
Use linked lists ( a grovable array).
You can set their size, add elements, remove elements, insert an element somewhere, etc-etc dinamically.

Share this post


Link to post
Share on other sites
Don't use linked lists, regardless of how "groovy" they might appear, unless you have proof that they actually are what you want. For common usage patterns, a dynamic array will often have superior performance, even where one might not expect it due to the algorithmic complexity theory.

The algorithmic complexity theory is great, but it only makes sense for large N (typically, millions or more). For small N (like the thousands the OP is handling), the constants that are assumed by the theory to become irrelevant actually become more and more important - in some cases they can dominate. This is where you might often find the cache friendly vector outperforming linked lists for many operations on the kinds of data sets your program can handle.

Don't forget that a linked list implementation requires two algorithms, you have the basic linked list algorithm and the memory management algorithm. The latter is often ignored in highly theoretical discussions, but again it can make a massive difference.

Likewise, the dynamic array complexity ignores techniques like the "remove/erase idiom", which is often suitable for "bag" like data structures (append only, random remove). If the order is irrelevant, you can also use the "swap and pop" trick too.

Most games do not feature millions of sprites, because it would make the game unplayable. As such, you don't have to design the game to work with millions of sprites. If your game is working fine at the expected level of activity on the target hardware, you can stop. However, if you hadn't implemented, for example, collision and you went on to find that you aren't getting the required performance with either linked lists or vectors, you might need to reach for a more complex data structure, such as a spatial based one.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!