Skip to main content
GameDev.net gamedev.net
🔒 Locked

Game init error handling

Started by Samurai Jack Aug 17, 2006 at 1:37 PM 1 replies 550+ views
Original Post
Samurai Jack
Samurai Jack
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?
luke2
luke2
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
KulSeran
KulSeran
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;        }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.