Sign in to follow this  

Nested Classes ~ I think

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

Edit: Sorry, I should have said, this is c++.

Could someone please help me out with some code.

I have a global class - cGameEngine.

I have two other classes - cRenderEngine and cGameStates.

cGameEngine manages the game, initialises the other two classes and manages the game states.

cRenderEngine handles OpenGL, and cGameStates is an abstract base class from which I derive my game states - InitialState, PlayState and so on.

So far cGameEngine initialises cRenderEngine on creation and has functions to Model 3D, 2D and to render - called through the renderengine object.

cGameEngine also creates a game states stack, and initialises some game states.

So far so good (although I have no idea if this is a wise way of doing things - it's as far as I've been able to work out).

What I want to happen, is for the game states to handle the renderengine though the gamengine object.

I can get the renderengine to function through the gameengine object, because the renderengine object is a member of the game engine object, but I can't get the game state objects to access the gameengine object, because the game states are object members of gameengine.

I understand that I need to pass a pointer from gameengine to the game states, but I can't find how to do this with out upsetting something.

I've managed to find a way to pass a gameengine pointer as far as the gamestates base class, but it isn't inherited by the derived game state objects, and gives me an error regarding explicit and default constructors.

Could some body please provide the code I would need to add to the cGameStates abstract base class, and the derived gamestate classes.

Also, is this a sensible way of doing things? It seems pretty tidy, but I'm a bit worried about memory leaks and so on, not really knowing how to look for them.

Thanks to everyone looks and/or comments. Edited by MrSimon

Share this post


Link to post
Share on other sites
You should consider the [url="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller"]MVC pattern[/url]. In this way you have a model (GameState), a view (RenderEngine) and a contoller (GameEngine).
Both, the controller and view need to [u]see[/u] the model, that is, you need some way to present the gamestate to the rendering/game engine.

One way is to push the information into the view/controller. I.e. when a gamestate change, or a new gamestate is created, then notify the view/controller about this event.

An other way is to pull the information from the model. I.e. the render engines checks, if something has been changed in the gamestate and reacts to this change accordingly.

Back to your issue, first off you should tell the view/controller the existance of the state, here are two simple ways (pseudo code):
[CODE]
Way 1:
class Game
cGameState* mGameState;
cRenderEngine* mRenderEngine;
cGameEngine* mGameEngine;

// constructor
Game()
{
mGameState = new cGameState();
mRenderEngine = new cRenderEngine( mGameState);
mGameEngine = new cGameEngine(mGameState);
}

run()
{
// run game ...
}
}


Way 2:
class Game
cGameState* mGameState;
cRenderEngine* mRenderEngine;
cGameEngine* mGameEngine;

// constructor
Game()
{
mGameState = new cGameState();
mRenderEngine = new cRenderEngine();
mGameEngine = new cGameEngine();
}

// call init, after all engines/states has been created !
init()
{
mRenderEngine->assignGameState( mGameState);
mGameEngine->assignGameState( mGameState);
}

run()
{
// run game ...
}
[/CODE]

Share this post


Link to post
Share on other sites
Hi there. Sorry for the late reply.

I managed to get it working on my own, though essentially I have done what you describe in the second method.

I am having another problem though.

I have my gameengine class create pointers to the renderengine, the gamestates and now the eventengine.

The gameengine constructor creates new instances of the renderengine, the gamestate and the event engine.

The gameengine destructor deletes these instances.

The gamestate class creates a pointer to renderengine, and is now able to access it to do rendering. The gamestate class also creates a pointer to the eventengine class and is able to access that.

However, even though i create pointers in the event engine class (even copying and pasting the same declarations) the event engine is unable to access the functions of the other objects. It compiles, but I am getting bad access errors, which so far have usually been caused by empty pointers.

In pseudo code, if it helps.

[CODE]
class cGameEngine
{
cGameEngine();
void Function();
cEventEngine* EventEngine;
cRenderEngine* RenderEngine;
cGameState* GameState;
};
cGameEngine::cGameEngine()
{
EventEngine = new cEventEngine;
RenderEngine = new cRenderEngine;
GameState = new cGameState;
};
cGameEngine::~cGameEngine()
{
delete EventEngine;
delete RenderEngine;
delete GameState;
};
void cRenderEngine()
{
//Do Something...
};
class cRenderEngine
{
void Function();
};
void cRenderEngine::Function()
{
//Render Something...
};
class cGameState
{
void GetEvents();
void GetRendering();
cRenderEngine* RenderEngine;
cEventEngine* EventEngine;
};
void cGameState::GetEvents()
{
EventEngine->Function();
};
void cGameState::GetRendering()
{
RenderEngine->Function();
};
cGameEngine* Game = new GameEngine;
int main()
{
while(running)
{
Game->GameState->GetRendering();
// ^ This works!
Game->GameState->GetEvent();
// ^ This doesn't work!
};
delete Game;
};
[/CODE]

If I change the event engine function to render (like the render engine function) it fails here too with the same error. I can follow the processes with the debugger, and it is following the references through the gameengine to the gamestate to the render engine, and it is following the process through the gameengine to the gamestate to the eventengine, but the pointers at the eventengine are coming up empty.

Game engine > game state > render engine > to render engine function

Game engine > game state > event engine > empty pointers.

Sorry that I have basically tried to explain the same problem over and over. I want to make it clear.

Thanks in advance for any help offered.

Simon Edited by MrSimon

Share this post


Link to post
Share on other sites
You still need to assign the Render-/EventEngine to your game state like this:
[CODE]
cGameEngine::cGameEngine()
{
EventEngine = new cEventEngine;
RenderEngine = new cRenderEngine;
GameState = new cGameState;

// assign now to game state
GameState->EventEngine = EventEngine;
GameState->RenderEngine = RenderEngine;
};
[/CODE]

Share this post


Link to post
Share on other sites
It all works now.

I needed the correct order of assignments, in the correct place in regards to the object calls.

It works now. Yay!

I've only been using c++ for a few weeks, but I'm getting really really good at diagnosing errors!!!!

It's a start! Edited by MrSimon

Share this post


Link to post
Share on other sites

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