0

I have a debug drawing flag in my game project. This flag determines whether Box2d's DebugDraw is drawing before the end of each frame. Is it possible to use something like:

#ifdef 'debug drawing flag'                       
//do debug drawing
#else
//skip

Or would it make more sense to have an if statement to check if that value was assigned?

//With a single command line arg
int main(int argc, char* argv []){
    //game initialization
    .
    .
    .

    //Check if its there and set flag
    uint8_t debugDrawFlag = (argc > 0 && strcmp("debugDraw", argv[0]) == 0) ? 1 : 0
           

    .
    .
    .
    if(debugDrawFlag)
        physicsWorld->DebugDraw();


}

Ian
  • 65
  • 1
  • 8
  • *"Is it possible to use [X]"* -- why not try it and see? – JaMiT Feb 11 '23 at 21:09
  • *"Or would it make more sense to [Y]"* -- First, note that this is not an exclusive "or". It is logically consistent for the first option to be possible, while at the same time the second option makes more sense. Second, making more sense tends to be a matter of opinion and also to be dependent on your desired functionality. – JaMiT Feb 11 '23 at 21:11
  • Definitely the first method since it automatically removes all debug code with only one flag. Also should prevent bugs in your program allowing access to the debug features. – B Remmelzwaal Feb 11 '23 at 21:11
  • @BRem not definitely - you may want to enable debug output at runtime, without an expensive re-compile. – Neil Butterworth Feb 11 '23 at 21:17
  • 1
    You can also combine the two by having a `constexpr bool debugDraw = true` or `false` depending on the build flag. You don't *have to* put `#ifdef` all through the code. – BoP Feb 11 '23 at 21:17
  • @NeilButterworth I suppose a hybrid would be best in that case: enable/disable on command, and exclude all debug code in a release version. – B Remmelzwaal Feb 11 '23 at 21:19

1 Answers1

0

#ifdef only works at compile-time, not at runtime.

Most compilers allow you to define custom symbols on the command-line while compiling, for instance with a -D switch or equivalent.

So, the question comes down to - do you want to specify your drawing flag at compile-time when your app is being built, or do you want to control your drawing mode dynamically when your app is being run? If the former, use #ifdef. If the latter, use argv.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770