Game init error handling
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?
One way that I like doing initialization( If I understand your 'problem' correctly) is to have a gamestate class like this:
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 :
I hope that was enough to help you there =D
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
I'd do something like:
and call it 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; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement