Sign in to follow this  

Game Libary Structure Problem

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

Okay so Im making my engine and I have one problem, it's like this(MUCH Simpler version):
class Ram
{
public:
	RDraw					Draw;
	RShader					Shader;
        RMatrix					Matrix;
};


But these classes arent finished yet, see, Matrix needs to use the Draw Object, (to set the matries to the stack), and Shader needs to use both Matrix and Draw. So Im sure many of you have smoked this problem easy. Ive tried making Draw all static vars and static functions but then when I use it I get LNK2001 Error. If this is not normal, please say so and ill provide more code for the problem.

Share this post


Link to post
Share on other sites
Actually, your problem is that you try to do some mutually recursive structures. For example:

class A
{
public:
B b;
};

class B
{
public:
A a;
};

is not implementable. What you can do is make a forward declaration, and use pointers:

class B; // Forward declaration: we will use this object

class A
{
public:
B * b; // The size of class A is 4, because b is a pointer. No compilation problem!
};

class B
{
public:
A a; // A is known, no need to use a pointer
};

You can also use references, which are generally safer, but in this case you will need to set them at the object construction, and that is not always possible.

Share this post


Link to post
Share on other sites
Actually, this cross-access of classes is a royal p.i.t.a. for most people who are designing their own engine. Often it comes down to access levels and variable scope in the end.

Often you have your 'GameEngine' class, which contains a 'Renderer', 'Sound', 'ObjectManager' and 'UserInput' object. The code inside some of the objects will need to call functions of one of the other objects, but can't 'see' it because they are instanced by a parent object.

There are a few ways around this:

* Lots of object references and pointers flying around all over the place.
* Using Singletons
* Putting any cross-object code up a level into the GameEngine class.

Just my 2 cents

Synex
Code Monkey

Share this post


Link to post
Share on other sites

This topic is 4684 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.

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