Sign in to follow this  
justin12343

help with sprite class

Recommended Posts

justin12343    217
I'm making a sprite and I want to store data about a sprite in a array, but I would have to set the size of the array when I define it in the class. Is there a way to use as much memory as I need when I add a sprite instead of initially setting the size of the array?

Share this post


Link to post
Share on other sites
justin12343    217
I'll use dynamic memory since I'm using c++.

One last question, for every time I want to add new data, I would do it like this?:

var = new int[new_size];
var[new_size]=value;

Is this correct?

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
I'll use dynamic memory since I'm using c++.
Dynamic memory is just a concept, more or less; it's not a feature that's specific to C++.
Quote:
One last question, for every time I want to add new data, I would do it like this?:

var = new int[new_size];
var[new_size]=value;

Is this correct?
That's probably not correct.

For one thing, an array of size N is indexed from 0 to N - 1, so using an index of N (new_size in your example) is not valid.

Also, more work than what's shown in your example is required to reallocate a dynamic array while preserving existing data. When you invoke new, it returns a completely new block of memory, not an 'expansion' of an existing block of memory, so in your example above, all the existing data is lost (presumably). To reallocate the array, you would need to allocate a new array (assigned to a temporary variable), copy the existing data from the old array to the new array, delete the old array, assign the value of the temporary array pointer to the existing array pointer, and then make assignments for any new elements as necessary.

Now, manual memory management of this sort is rather tedious and error-prone, which is why we have container classes such as std::vector. If you're unfamiliar with memory management in C++, it would probably be a good idea to do it yourself a few times so that you understand what's going on 'under the hood', but as far as real, practical solutions go, a container such as std::vector would probably be the best choice here.

Share this post


Link to post
Share on other sites
Ravyne    14300
I'm a little unclear about what you're actually wanting to do at a high level... what data are you storing in this array?

Typically, if your sprite is defined through a class or struct, then that class's member variables hold that information. You should know what all the data is that describes your sprite ahead of time, and than you define members to hold it.

If you're talking about the image data itself (the bitmap of the sprite, essentially) then its probably unwise to have that be a part of the sprite itself... Actually, now we get into the semantics of what a sprite *is* in your case, so you'll have to define that for us to provide you with meaningful help.

Generally, when people say sprite they mean one of two things:

First, and more commonly, a sprite is a class which holds the state of a visible entity in your game, which may be animated and/or mobile.

Second, the less-common nomenclature of a sprite is the object that stores just image data (and possibly animation data and/or multiple image cells), which is then referenced by an object that represents a visible entity in the game.


The important thing to grasp, however, is that there should be a distinction between a visible entity in the world, and the image data that gives it form, otherwise the image data is duplicated for each entity in the game -- for example, in Super Mario Brothers, each goomba and turtle in each level would have its own local copy of the image data. Clearly this is wasteful.

If you design your system so that a "sprite" or whatever you call your in-game entities has instance-specific data (location in the world, behavior state, animation state) including a reference to another class which holds the image and animation data, then multiple entities can reference and share the same image and animation data.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this