Skip to main content
gamedev.net
🔒 Locked

#define Debug mode

Started by deadlydog 3 replies 650+ views
Original Post
deadlydog
deadlydog
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.
Orbviper
Orbviper
Rather than doing:

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


you should do:

#ifdef DEBUGCrashHard();#elseCrashGracefully();#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.
choffstein
choffstein
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!
doynax
doynax
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.

Topic Locked

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

Sign in to reply to this topic.