Sign in to follow this  
coderWalker

Efficent Object management

Recommended Posts

I have a class called Block, which is a template for the object "block" or a cube. This class contains the x and y coordinates for the block as well as the texture data. I want to be able to create the blocks at certain places on the screen. What I want to know is how to keep up with the objects for movement and drawing.

Should I have them in an array? if so how?

Should I have a pointer to the created blocks in an array?

2d or 1d array?

Build on the array as I go or have a array that has a space for every possible block (thinking of making them stick to a 16x16 grid?

Opinions?

I tried to make an array with pointers to the blocks I create but it doesn't work.
block grid[20,20];
block grid[0,0]= new block(0,0);

Am I using classes correctly or should all this be separate arrays? or a Struct array?

Lots of possibilities,
like to hear your opinion,
thanks,
CoderWalker

Share this post


Link to post
Share on other sites
Yes it's c++, my bad for not clearifying.

Thanks.

I have a function block::draw();

can I call that using the pointer in the array?
grid[0][0].draw(); ?

Are classes the best way to do this? or should I use structs?

Share this post


Link to post
Share on other sites
Your knowledge of C++ at this point in time seems inadequate for what your attempting to do. When a pointer to an object is used, the -> operator is used as opposed to the . operator. You should really spend some time working through some basic c++ tutorials before proceeding.

If you know that you have a fixed number of blocks an array is fine, and you do NOT need to use new. If the number of blocks is unknown, then new can be used to create them at runtime, but a better choice of container would be a std::vector, as it can automatically resize itself to hold more instances as needed.If this is all jibberish to you, then I reiterate, practice the fundamentals of c++ (or another, perhaps easier language. C# is often recommended here)

Share this post


Link to post
Share on other sites
Quote:
Are classes the best way to do this? or should I use structs?
Doesn't really matter - classes and structs are exactly the same in C++ except for default privileges.

(People tend to use structs and classes differently in C++ by convention, but it's only by convention - functionality, they're essentially equivalent.)

Share this post


Link to post
Share on other sites
I understand thanks :D It's amazing how much you can forget when your coming back to C++ while taking VB and Python programming classes.

Still, would you store the blocks in a struct or a class?

class block
{
public:
block();
draw();

int x;
int y;
};

----or----

struct block()
{
int x;
int y;
};
void drawBlock();

-->Would there be a speed difference?
-->Would one take more memory than the other?

Share this post


Link to post
Share on other sites
Quote:
-->Would there be a speed difference?
-->Would one take more memory than the other?
I'll just quote myself:
Quote:
classes and structs are exactly the same in C++ except for default privileges.

Share this post


Link to post
Share on other sites
Even though classes and structs are fundamentally the same thing, they are semantically different. In a class, you typically keep all data private and strive to make a meaningful, cohesive object. In a struct, you generally keep everything public and have few to no member functions. A struct generally serves as a way to bundle data. These structs also often end up being private member variables of classes. :P

Yes, it's important to understand how the language perceives the two types, but it's also important to know how they are commonly used. ;)

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