Sign in to follow this  
deadlydog

#define Debug mode

Recommended Posts

In school they say that you should use a sort of Debug mode tag to generate errors so that the program pretty much crashes if there's an error so you know theres a problem, and when you release the game to take the Debug mode option out so if the program crashes, it crashes gracefully. So something like this if (DEBUG) { CrashHard(); }else { CrashGracefully(); } That way when you release the game to the public you can just not define DEBUG and the program won't crash as hard. My question is, is this a good idea for game programming, since that makes that many more if statements it has to evaluate? I know it won't totally slow the game down or anything, but evey little bit helps. Sorry If I wasn't totally clear on this. I'm tired right now.

Share this post


Link to post
Share on other sites
Rather than doing:


if (DEBUG)
{
CrashHard();
}else
{
CrashGracefully();
}



you should do:


#ifdef DEBUG
CrashHard();
#else
CrashGracefully();
#endif



That way it's all handled by the preprocessor before compilation, so your actual program will not be affected by any extra if statements.

Share this post


Link to post
Share on other sites
this code might be helpful...


#ifdef DEBUG
#define DEBUG_ONLY(x) x
#else
#define DEBUG_ONLY(x) 0
#endif



Now, if your project is DEBUG, you could compile it with -DDEBUG in gcc, or your compiler equivalent. Or you could just #define DEBUG in your program somewhere. So later in your code, when you have


DEBUG_ONLY(CrashHard());
CrashGracefully();



Assuming crash hard will exit your program, CrashGracefully() will never be called. But if you dont define DEBUG, CrashHard will not occur.

If your functions return 1 or something, you could do something like


if(!DEBUG_ONLY(SomeFunc()){
SomeOtherFunc();
}



Roight!

Share this post


Link to post
Share on other sites
You probably also want use the macro NDEBUG (not in debug mode), since it's standard and will thus be automatically defined by your compiler.
Oh, and assert() can be a good tool too.

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