Sign in to follow this  
Samurai Jack

Game init error handling

Recommended Posts

Samurai Jack    455
Greetings! I have some problems with error handling. The problem lies in the code because it looks ungly. Like this: bool InitApplication() { if( InitVideo() == false ) { logWrite("Video init failed.\n"); return false; } else logWrite("Video init successful.\n"); if( AudioInit() == false ) { logWrite("Audio init failed.\n"); return false; } else logWrite("Audio init successful.\n"); return true; } should I use macros like: logAssert( InitVideo(), "Video init" ); logAssert( InitAudio(), "Audio init" ); or should i take exceptions? is there a better way to do it?

Share this post


Link to post
Share on other sites
luke2    100
One way that I like doing initialization( If I understand your 'problem' correctly) is to have a gamestate class like this:

class CGameState
{
public:
virtual bool Init() =0;
virtual bool ShutDown() =0;

virtual bool Update() =0;
virtual bool Render() =0;

virtual bool Minimize() =0;
virtual bool Restore() =0;

virtual bool TakeInput() =0;
};

and all of each states initialization code is in the Init()... but if your just trying to clean up your code, I would make an abstract class with pure virtual methods to represent each system, ie video,sound, input all inherit from it and then all you do is :


ISystem* video = new Graphics(...);
ISystem* sound = new Audio(...);
ISystem* input = new Input(...);

//Now you could've just made it all an array of ISystems and then just for thru
//them, but Im not, that way I know what I've inited etc.
video->Init(...);
sound->Init(...);
input->Init(...);

//And then you just call sound, video, and input's 'special' methods for what //their job is etc. As for error checking, just build in some virtual=0; methods
// into ISystme.

I hope that was enough to help you there =D

Share this post


Link to post
Share on other sites
KulSeran    3267
I'd do something like:

bool LogInit ( bool b, string name )
{
if ( b )
LogSuccess ( "%s Initalized", name.c_str() );
else
LogError ( "%s failed to Initalize", name.c_str() );
return b;
}


and call it like:

bool Init ( )
{
bool rVal = true;
rVal &=LogInit ( GraphicsInit(), "Graphics Engine" );
rVal &=LogInit ( SoundInit(), "Sound Engine" );
return rVal;
}

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