Sign in to follow this  
Beginner_Joe

Multiple Objects

Recommended Posts

Hi, wonder if anyone can help me. I'm learning C++ with the DirectX SDK, and am working on converting a simple Spectrum platform game into 3d. I've set things up so the code reads from an array of ints[30][15] from a level map, where I've set (for instance) 0 to be nothing, 1 to be a wooden platform, 2 to be carpet, 3 for brick etc. etc. At the moment I start by declaring an array of the maximum number of scenery objects, then using an array of pointers to point to the objects that are not 0s (does this make sense?!), what I'm wondering is how would I do this more efficiently, and only declare as many objects as I need - my understanding is I can't say Objects Scenery[number] when number is not a constant as it's not known until the level scenery array is 'scanned'. The only way I can think is to declare a const int linked to each level array with the number of objects that I'd need to use? Sorry if this question is a little muddled! Thanks, Joe

Share this post


Link to post
Share on other sites
You can use std::vector, which is a class that manages a dynamic array that resizes itself automatically when it needs more space. I strongly recommend that you read this, then this to learn more about it.

Although not directly related to your question, you should also take a look at boost's smart pointers as a replacement for raw pointers.

Share this post


Link to post
Share on other sites
Hi Beginner_Joe,

Hopefully this will answer your intended question:

If you do not know the amount of objects you will have at compile-time, you cannot use the static memory model, such as a fixed array. Instead, you have to resort to using dynamic memory.

In C++, this is done through the use of the new operator. However, you will have to do a bit of work and make a few objects to get the most out of managing the memory yourself, including having to delete the memory you initially allocated using 'new'. To talk about efficiency at this point is laughable because people are trying to come up with new ways to be memory efficient every day.

One possible solution that will do is to use the Standard C++ Library (also known as STL), because it contains pre-made classes that deal with dynamic memory and autosize themselves to expand/shorten as objects come and go.

An array-like class that you can use is the std::vector. The function to add an object to the array is push_back. There is a tutorial available here. You can also read all the functions from STL's website.

Another advantage of using vectors is that you can access your objects using standard array notation, so the expression "numVec[3] = 5" is perfectly reasonable.

Hope this helps!

Share this post


Link to post
Share on other sites
Just my two pennies:

One rather useful function belonging to a std::vector is the reserve function. This lets you inform the vector in advance of the amount of memory it is going to need at some point. This is more efficient than letting it figure it out on it's own after loads of push_backs. This might be useful to you if, for example, you can say "I am almost always going to need around 30 x 15 = 450 game objects, for any given level".

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